Example: how to use a DLL written in C #

Source: Internet
Author: User

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.

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.