Example: how to use a DLL written in C #
How to Use the DLL written in C/C ++ in C. Here is an example:
Create a VC project dlldemo, select MFC Appwizard (DLL) when creating the project, and select regular DLL using shared mfc dll or MFC extension DLL.
Now you can write a function code and add it to the dlldemo. cpp file. You can also use a new file to add code;
Extern "C" _ declspec (dllexport) int add (int A, int B)
{
Afx_manage_state (afxgetstaticmodulestate ());
Trace ("entering dlladd/N ");
Return A + B;
}
Compile the project.
Create a C # winform program to test the dlldemo:
Start vs.net IDE, create a new C # project, and select the winform application.
Add reference in form1.cs: using system. runtime. interopservices;
Add the code at the beginning of the pulic class form1 statement:
[Dllimport ("motorcontroldll. dll", entrypoint = "add", exactspelling = false, callingconvention = callingconvention. cdecl)]
Public static extern int add (int A, int B );
For the usage of the dllimport attribute, you can view msdn and provide detailed descriptions of parameters.
Finally, remember to add the dlldemo. dll file generated by dlldemo In the debug file to the bin directory of the winform program in C.
In this way, you can directly use the add (int A, int B) function.