C # how the program calls an unmanaged C ++ DLL file

Source: Internet
Author: User

Function declaration in C ++1 extern "C" __declspec(dllexport) int __stdcall testfunc(char* astr,int* a);

Extern "C"

Generally, the C ++ compiler may change the names of functions and variables, resulting in serious link program problems. For example, if you use C ++ to compile a DLL, the Microsoft compiler changes the function name when creating the DLL. The function name is prefixed with a forward underscore (_), followed by a @ symbol. A number is used to indicate the number of bytes passed to the function as a parameter. For example, the following function is output as _ MyFunc @ 8 in the DLL output section:

1 __declspec ( dllexport ) LONG __stdcall MyFunc( int a, int b);

If an executable module is created using a tool from another vendor, it tries to link to a function named MyFunc which does not exist in the existing DLL of the Microsoft compiler, so the link will fail.

The extern "C" keyword allows the compiler to compile the DLL file in C language, that is, the function name is not changed during compilation.

 

_ Declspec (dllexport)

In the 32-bit compiler version, you can use the _ declspec (dllexport) keyword to export data, functions, classes, or class member functions from the DLL. _ Declspec (dllexport) will add the Export command to the object file, so the. def file is not required.

To export a function, the __declspec (dllexport) keyword must appear on the left side of the call Convention keyword (if a keyword is specified ). For example:

1 __declspec ( dllexport ) void __cdecl Function1( void );

 

_ Stdcall

Indicates that the called party clears the stack.

 

Function declaration in C # 1 using System.Runtime.InteropServices; 2        3   4 public class Program 5 { 6 [DllImport( @"E:Projectsestdlldebugestdll.dll" )] 7 public static extern int testfunc(StringBuilder abuf, ref int a); 8 }

 

Using System. Runtime. InteropServices;

The System. Runtime. InteropServices namespace provides a variety of members that support the COM interop and platform call services, allowing the program to interact with non-hosted code.

 

[DllImport ("dllfile path")]

In the code, the DllImport keyword is used to tell the compiler entry point and bind the packaging function to this class. You can add several attributes when declaring them:

1 [DllImport( "MyDLL.dll" , 2 EntryPoint= "mySum" , 3 CharSet=CharSet.Auto, 4 CallingConvention=CallingConvention.StdCall)]

EntryPoint: Specifies the DLL entry point to be called. The default entry name is the name of the hosting method.
CharSet: controls the method for name Rename and sending String parameters (UNICODE by default)
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.