1. First write the C + + dynamic Library
extern "C" __declspec (dllexport) int __stdcall Add (int x, int y) {return x + y;}
extern "C" __declspec (dllexport)
extern "C" makes it possible to use C-compile in C + +. The "C" function is defined under C + + and requires the addition of the extern "C" keyword. Use extern "C" to indicate that the function uses C compile mode. The output "C" function can be called from the "C" code.
Using Microsoft-specific _declspec (dllexport)
CPP file when compiling to the obj file to rename the function, C will rename the function name to _name, and C + + will be renamed to [email protected] @decoration,
extern "C" means renaming a function in the C language format
To output the entire class, use _declspec (_dllexpot) for the class, and to output the member function of the class, use _declspec (_dllexport) for the function.
The __stdcall qualifier modifier needs to be added. Do not add this qualifier, can not be successfully called, do not know why??
2.c# calling the encapsulated interface function in the C++dll library
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Using System.runtime.interopservices;//public class dll{ [DllImport ("DLLCpp.dll", EntryPoint = "add")] public extern static int add (int x, int y);//consistent with DLL}namespace dlltest{ class program { static void Main (Stri Ng[] args) { int ret = Dll.add (2, 3); Console.WriteLine (ret); Console.readkey ();}}}
Using System.Runtime.InteropServices;
Commonly used in DllImport, which is used to invoke functions (Windows API) of some DLLs in Windows, or to invoke functions in DLLs written in C + +
public class DLL
{
[DllImport ("DLLCpp.dll", EntryPoint = "add")]
public extern static int add (int x, int y);//Consistent with DLL
}
Note: C # if it is a 64-bit environment, C + + encapsulates the dynamic library when it is set to x64. Why can't 32 bits be called, not yet clear?
C # invoke dynamic Library DLL demo routines for C + +