C # instructions for Calling C ++ DLL

Source: Internet
Author: User

(1) create a project solution using vs2008, and add c ++ Project (DLL) and C # project (client) to the solution) [the specific steps for creating the C ++ DLL and C # projects are not described in detail here. Please refer to the related documentation ];

(2) Compile the C ++ DLL project and the C # client project;

(3) After compilation, copy all the DLL-generated extensions (DLL and Lib) and the header files (extension. h) used for compiling the DLL to the C # client.ProgramUnder the debug folder;

(4) Open the C # client program and create a class: mydll (this class name is called by the C # client)

(5) introduce the namespace: Using System in the C # client program. runtime. interopservices; [This space is referenced because some API features in C # are required when calling DLL]

(6) write the required functions in the mydll class. Note that some features should be added to the function before the function, for example:

[Dllimport ("Web. dll", entrypoint = "? Sum @ cspecialthing @ qaehgpadpaunc_pro @ Z ", charset = charset. ANSI, callingconvention = callingconvention. stdcall)]

Public static extern int sum (INT flibhndl, string cnctype, ref nc_pro nc_pro00 );

First, we will introduce the meanings of several parameters in dllimport:

1. entrypoing indicates the name or serial number of the DLL entry point to be called.

If your method name does not want to have the same name as the API function, you must specify some parameters, for example:

[Dllimport ("user32.dll", charset = charset. ANSI, entrypoint = "MessageBox")]

Public static extern int msgbox (intptr hwnd, string txt, string caption, int type );

In the above example, we can see that there seems to be a garbled code in entrypoint :"? Sum @ cspecialthing @ qaehgpadpaunc_pro @ Z ". Why is there such a garbled code? We will continue to introduce it later. Let's first introduce the meanings of several symbols that we can understand:

Sum: This is the name of the function defined in the C ++ DLL;

Cspecialthing: This is the class we defined in DLL;

Nc_pro: This is a parameter in the function. Why is there only one? I personally think that only when your parameter is of a special type (for example, struct, other types may also be the same, but I only use the destruct in my current project ), this call is required.

Some people may think that we have used "Ref" in the parameter, so this parameter will appear in this pile of garbled characters. I can say for sure, it is definitely not .....

I don't know what other symbols mean .....

2. charset controls the name version of the called function and indicates how to mail the string parameter to the method.

This field is set to one of the charset values. If the charset field is set to Unicode, the ne string parameter is converted to Unicode before being passed to an unmanaged implementation. This also causes the name of the dllentrypoint to be appended with the letter "W ". If this field is set to ANSI, the string is converted to an ANSI string, and the letter "A" is appended to the name of the dllentrypoint ". Most Win32 APIs use this APPEND "W" or "A" convention.

3. callingconvention indicates the callconvention value used to pass method parameters to an unmanaged implementation

Callingconvention. cdecl: the caller's cleaning scale. It enables you to call functions with VAR args.

Callingconvention. stdcll: Call method to clear the stack. It is from the managedCodeDefault conventions for calling unmanaged functions.

(7) after completing the above steps, you can call the DLL written in C.

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.