C ++ managed C # dynamic library

Source: Internet
Author: User

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 ;}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.