"Go" writing C++dll for C # calls

Source: Internet
Author: User

in recent times, often encountered these problems, a while ago, did not remember, did not think of recent research and some do not remember, today write it down for forgetting. The DLLs we provide to other languages are usually called C or C.+ + is written and then encapsulated. My side is also used in C + +. First, there are a few points to note:1, if the functionality is simple, or if you do not use a third-party library (such as an MFC-brought library), create a Win32 console program, and then change the project generation to a DLL. It is worth mentioning that the code generation inside the runtime library is divided into four kinds: (1Multi-threaded MTD (Static library, after compiling, your lib with debug function)--Debug with (2) Multithreaded MT (Static library, no debug function)--release time with (3) Multithreaded DLL MTD (dynamic library with debug function)--Debug with (4) Multithreaded DLL MT (dynamic library, no debug function). -release with S since encapsulation DLL, that debugging time with (3), when published (4). 2, set as export function, and adopt C-style. Add extern before function"C"__declspec (dllexport).      The definition function empties the stack itself before exiting, adding __stdcall before the function. such as extern"C"__declspec (dllexport)int__stdcall Add (intXinty); With the above conditions, the generated DLL contains the function of the exported function, but at this time the function name in the DLL is not a rule, using the compiler to customize, it may be a name [email protected] -, you can use the VS depends tool to see it. 3, change the name of the exported function to a standard name, add a module definition file, or a. def file. The contents are as follows: (Requires comments, preceded by semicolons, comments need to be separate lines) LIBRARY"TEST"exports; Add function adds library name exports after the name of each function that needs to be exported is recompiled, and then with the depends tool, the function has changed into standard add, not [email protected] -. This is useful when loading dynamically, especially when the GetProcAddress function looks for the inbound function. 4, C # calls C + +DLL, which describes two methods (1) static load [DllImport ("TEST.dll", EntryPoint ="Add")]              Public intAddintXinty);//consistent with the DLLNote that if you need to return a string, C++inintGetString (Const Char* Source,Char*dest); In C #intGetString (stringSource,stringbuilder SBR);             Remember to call the time to allocate space for StringBuilder, otherwise it will error.             If the dest length is 10, you can do so. StringBuilder SBR=NewStringBuilder (Ten); GetString ("Hello", SBR); If you want C++DLL can also be called by VB and other languages, it is recommended to write a string into COM form, such as C++inintGetString (BSTR source,bstr dest);//A BSTR is an array of characters in COM form, equivalent to a stringin C #intGetString (stringSource,stringbuilder SBR); Declare Function getString Lib in VB"TEST.dll"(ByVal Source As String, ByVal dest As String) as Integer; (2) Dynamically load [DllImport ("Kernel32.dll")]             Private extern StaticIntPtr LoadLibrary (String path);//path is the DLL path that returns a result of 0 for failure. [DllImport ("Kernel32.dll")]             Private extern StaticIntPtr GetProcAddress (IntPtr lib, String FuncName);//Lib is the handle returned by LoadLibrary, and FuncName is a function name that returns a result of 0 identity failure. [DllImport ("Kernel32.dll")]             Private extern Static BOOLFreeLibrary (IntPtr Lib); //declaring a delegate             Delegate intADD (intXinty); //using dynamic loadingIntPtr hlib= LoadLibrary (DllPath);//Load FunctionIntPtr apifunction= GetProcAddress (hlib, apiname);//Get function Address             inti =Marshal.GetLastWin32Error (); if(Apifunction.toint32 () = =0)//0 indicates that the function was not found                 return NULL; //Gets the function interface, which is equivalent to the function pointerAdd add = (Delegate) marshal.getdelegateforfunctionpointer (Apifunction,typeof(ADD)) asADD; //calling FunctionsAdd (1,2); //release handleFreeLibrary (hlib); Finally,1C + + When returning a string, remember last Add/0, otherwise in C # and so on call, will show some garbled. 2C + + Dynamic application of memory, need to be released before the function, otherwise it will report unexpected errors. such as memory write errors and so on.

"Go" writing C++dll for C # calls

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.