C # invoke dynamic Library DLL demo routines for C + +

Source: Internet
Author: User

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 + +

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.