C ++ call C # DLL

Source: Internet
Author: User
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_^

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.