A dll is compiled by C #. I need to use the DLL in the C ++ program. I checked the relevant information online and used the C ++ hosting knowledge, that is, the runtime environment in the common CLR language. The procedure is as follows:
1. Copy the DLL to the project running directory.
2. Reference:
# Using \ ".. \ debug \ ctest. dll \"
Using namespace ctest;
If the program references an Assembly created by another. net, reference it as prompted. For example, if the Assembly "system. Windows. Forms" is missing, add # using <system. Windows. Forms. dll>.
3. Modify the project attribute to "/CLR" when the common language runtime is supported ".
4. Use of managed objects:
Classname ^ P = gcnew classname ();
P-> fun (); // call the DLL Function
// Delete P;
Gcnew is used for instantiation. You do not need to use Delete to release the memory.
Virtual Machine hosting, after the program ends, will be automatically destroyed.
5. If the DLL provides an interface, you need to register the callback. The syntax is the same as the C # registration callback syntax.
For example:
The callback function in C # is defined as follows:
Namespace ctest
{
Public class classname: usercontrol
{
Public classname ();
Public event classname. datareceive mydatareceive;
Public Delegate void datareceive (INT idoor, string soptag );
}
}
The callback setting method is as follows:
P-> mydatareceive + = gcnew classname: datareceive (getaccessmsg );
Getaccessmsg is defined as follows:
Void getaccessmsg (INT idoor, system: String ^ soptag) {// convert the system: String ^ type to char * intptr sptr = system: runtime: interopservices :: MARSHAL: stringtohglobalansi (soptag); char * PMSG = (char *) sptr. topointer (); cout <idoor <Endl ;}}