Calling C + + DLL methods in C # (ii) building a managed DLL

Source: Internet
Author: User
Tags hosting

Before the write operation, or to briefly talk about the difference between managed and unmanaged C + + Well, I actually did not have a deep understanding of the characteristics of managed C + +, its biggest feature is that the system can debug the collection of related code resources, like C # features, just the programming style and C + + Similar , so this determines that C # and managed C + + can be perfectly combined. managed C + + generated DLLs and C # generated DLLs should be said to be no different , the reason for hosting C + + This monster, because Microsoft is strongly advocating C #, must take into account the interaction between different languages.

All right, let's get down to a serious writing process. First of all: I have a C + + write Class (Nativeclass), I want to downgrade this class in C #, but C # is no simple way to get classes in unmanaged C + + DLLs like DllImport. My workaround is to build a DLL that is managed C + + and then reference the DLL under C #. Because managed code and unmanaged code cannot be mixed in one file, I must encapsulate the unmanaged C + + write Nativeclass with managed C + +, and then generate a DLL for C # to invoke.

The following direct method steps:

I. Building a CLR Class library project

Second examples of unmanaged C + +

I have a class Nativeclass, which is written in unmanaged C + +, which belongs to another unmanaged C + + project, and now I copy this class file directly to the directory of this project. if it is a larger class, it is necessary to copy the other files that are contained in NATIVECLASS.H to the new project directory, and then add the files to the VS Explorer, which means the files of the include in unmanaged C + +, It's not about hosting C + +. as shown in the following:

, in addition to the NativeClass.h and NativeClass.cpp files are I added, the other is the project comes with things, wherein ManageClass.h and ManageClass.cpp is to generate DLL to use the things. For the time being, let's take a look at the contents of NativeClass.h and NativeClass.cpp:

NativeClass.h Code:

#pragmaOnceclassNativeclass {Private: intncount; Public: Nativeclass (void); ~nativeclass (void); intGetCount (void); voidIncrease (void); voidClear (void);};

NativeClass.cpp Code:

#include"stdafx.h"#include"NativeClass.h"Nativeclass::nativeclass (void) {      This->ncount =0; } Nativeclass::~nativeclass (void) { } intNativeclass::getcount (void) {     return  This-ncount;} voidNativeclass::increase (void) {      This->ncount++; } voidNativeclass::clear (void) {      This->ncount =0; }

The unmanaged C + + classes are simple.

Iii. encapsulating content as managed C + +

This step is crucial, the reason is that the managed C + + and unmanaged C + + can not be mixed, so I used managed code to encapsulate the above Nativeclass class, originally by specification I should be the function declaration and implementation of separate write, but for the sake of simplicity, Only in the ManageClass.h to make changes, although no use of ManageClass.cpp, but do not delete this file, otherwise it is impossible to generate DLL. My encapsulation code is as follows:

//ManageClass.h#pragmaOnce#include"NativeClass.h" using namespaceSystem;namespaceManageclass { Public ref classNativeclassex {//TODO: Add a method for this class here.     Private: NatiVeclass*M_pnclass;  Public: Nativeclassex (void)         {              This->m_pnclass =NewNativeclass (); }         ~nativeclassex (void) {Delete This-M_pnclass; }         intGetCount (void)         {             return  This->m_pnclass->GetCount (); }         voidIncrease (void)         {              This->m_pnclass->increase (); }         voidClear (void)         {              This->m_pnclass->Clear (); }     protected:         ! Nativeclassex (void) {Delete This-M_pnclass; }     }; }

Iv. generating a managed C + + DLL

In fact, to the end of this step, you directly point to compile, will be in the solution directory of the Debug folder generated ManageClass.dll, be sure to see, after encapsulation, my new class name is called Nativeclassex, please pay attention to when using.

V. Project Test DLLs

calling managed C + + DLLs is no different than calling C # DLLs , creating a new test project (I'm using WinForm's form engineering), called Dlltest, In Solution Explorer, add the ManageClass.dll that you just generated to the reference, use the using Manageclass, and then you can use it, and test the code in a few words:

New Nativeclassex (); Debug.WriteLine ("" + Testcalss.getcount (). ToString ()); Testcalss.increase (); Testcalss.increase (); Testcalss.increase (); Debug.WriteLine ("" + Testcalss.getcount (). ToString ()); Testcalss.clear (); Debug.WriteLine ("" + testcalss.getcount (). ToString ());

Compile, look at the Output window, the class still works perfectly.

VI. matters of caution

1, although C # and managed C + + is largely compatible, but also to pay attention to the basic types of alignment issues, such as structure, String class, the best import parameters in addition to the basic types of other do not use, this point please refer to my previous article;

2, I try to use the managed C + + Package I write OpenCV class, the class calls the OpenCV dll (that is, C # calls the managed DLL, the managed DLL calls the unmanaged DLL), compiled through, but the actual operation does not work, what is the problem is temporarily unclear;

3, suggestions, nothing to do with this method to invoke the class, C # calls the DLL function is the most guaranteed.

4, remember to compile the DLL before use, and make sure to add a reference, there may be some warning on the choice of CPU type, please be self-reliant.

Calling C + + DLL methods in C # (ii) building a managed DLL

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.