Writing DLL using C ++ requires many specifications, while implementing DLL through C # On the. NET platform is much more convenient. But pay attention to calling the DLL written by C # In C ++. It cannot be the same as referencing the DLL of C ++ itself. The following details should be noted: (take vs2005 as an example, similar to vs2008)
1. because the default configuration in C ++ is not supported by the public Language Runtime Library, first modify the configuration and click Project> Properties> Configuration Properties> General. select "Public Language Runtime Library support (/CLR)" in the Public Language Runtime Library support )".
2. Reference DLL files and namespaces.
If we want to reference a file named uranusnet and named uranusnet. dll, the reference method is:
# Using "../debug/uranusnet. dll" (using rather than include is used here)
Using namespace uranusnet;
Remember to copy the DLL file to the corresponding directory
3. Call Method
Add the ugsinfo class to call the uranusnet namespace. The Code is as follows: (assume the class has two methods: Login and writeblog)
Ugsinfo ^ UGS = gcnew ugsinfo (); (use ^ instead of * Here, gcnew instead of new)
If (UGS-> login ("ABC", "123 "))
UGS-> writeblog ("good", "very powerful ");
Democode:
1. Create a C # DLL and set the application type to "class library". Code: namespace cslib
{
Public class class1
{
Private string name;
Public string name
{
Get
{
Return name;
}
Set
{
Name =/"Your name:/" + value;
}
}
}
}
2 C ++ client program is a console application. Code: # using/".. // debug // cslib. dll /"
Using namespace cslib;
Int _ tmain (INT argc, _ tchar * argv [])
{
Class1 ^ c = gcnew class1 ();
C-> name =/"zzj /";
Printf (/"% S/", C-> name );
Return 0;
}
3. Remember:
1. Use # using to reference C # DLL, instead of # include. I took it for granted that I used the latter, so I wasted a whole morning;
2 don't forget using namespace cslib;
3. Use the C ++/CLR syntax and use the correct access to the hosted object, that is, use the hat '^' instead of the Start '*'.
The above is the main point of calling the C # DLL in C ++. Because C # is simple and convenient, we can use C # to quickly implement DLL files.
Of course, some underlying functions must be implemented in C ++. Pai_^