C # mixed programming with C ++/CLI

Source: Internet
Author: User
Tags hosting

Recently, I used a QR code recognition SDK for my project. I want to call the library to generate and parse the QR code on the Interface C #. I have previously done research on calling DLL in C, I didn't expect this attempt to be very difficult. Let me talk about it slowly.

I used Google to search for several methods to Call DLL in C:

1. invoke under C. Use dllimport to dynamically import the functions in the DLL and then call it directly. This method is suitable for simple functions of winapi and parameters (it is best to use the pure C method, that is, the function exported using the extern "C" method ), otherwise, it is very troublesome and difficult to construct various custom struct and pointers in C;

2. Create a com-Based DLL warpper. Unless you understand COM, use method 1;

3. Use C ++/CLI for warpper. I used to ignore the existence of C ++/CLI and thought it was a freak and the code looked ugly. However, through this project, I found that I was too superficial. This is a method that I strongly recommend. If you have a lot of original modules, you want to switch. net can be used later, so listen to me and take some time to learn C ++/CLI!

Since C ++/CLI can write code for both hosted and unmanaged platforms, it is no longer appropriate to use it as a warpper. I will talk about the content of C ++/CLI in another forum. This article focuses on the application in the project.

1. To identify the classes and methods you write in C #, the parameters must be the types that can be recognized by the hosting platform. For example:

System::Int32 QRCodeWapperCLI::QRCodeWapper::DeCodeQRFromFile(String^ filename,[System::Runtime::InteropServices::OutAttribute] String^% outQRInfo)

The hosted type in this declaration has a ^ symbol, which can be distinguished by the value type. The % character indicates ref, and the attribute [system: runtime: interopservices :: outattribute] indicates an out parameter.

2. The following code converts a hosted string to a non-hosted string char:

Intptr ifilename = MARSHAL: stringtohglobalansi (filename );

Char * strfile = reinterpret_cast <char *> (static_cast <void *> (ifilename ));

The marshal class is a great class between hosting and non-hosting platforms. There are many ways for you to look at msdn. The above information is very good.

In turn, you can do this:

Char * S1 = "Native string1 ";

Wchar_t * S2 = l "Native string2 ";

String ^ str1 = gcnew string (S1 );

String ^ str2 = gcnew string (S2 );

3. Copy data between hosted and unmanaged memory buffers by referring to the following code:

CLI: array <byte> ^ BMP bytes2 = gcnew array <byte> (bfsize); // managed memory

Pin_ptr <byte> P2 = & BMP bytes2 [0]; // unmanaged memory

: Memcpy (P2, pmybmp, length * sizeof (byte ));

 
 
 
 
 
  

You did not see it wrong, it is memcpy!

4. In addition to the system namespace, other namespaces must be declared using the using namespace statement, but also the DLL file containing using! To use the drawing namespace, not only using namespace system: drawing;, but also # using <system. Drawing. dll>. Otherwise, an error will be reported during compilation.

5. When debugging, enable the mixed debugging of C # And the mixed debugging options of C ++ to debug the code of the hosted and unmanaged platforms at the same time.

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.