Calling C/C + + generated DLL files for the first time is a bit fresher, and it's actually just something that implements "unmanaged code" that executes outside the control of the Common Language execution library (CLR), which is called "managed code" in the control of the Common Language execution library (CLR). How to use the unmanaged under the trusteeship? Now for those who are interested in the beginning of learning simply write an implementation of the whole process it (what questions do not laugh):
1. Use VS2008 to select a different language (C + +) to create a console application named Mydll1, and then select the application type as DLL, OK
Project
Add the following declaration under header file stdafx.h such as:
#definelibexport_api extern "C" __declspec (dllexport)
Libexport_apiintAdd (intA,intb);
in the MyDll.cpp This function is implemented in:
#include"stdafx.h"
intAdd (intA,intb
{
returna+b;
}
Note that the assumption of the implementation of the method is declared in the other header file, be sure to add # include "Xxx.h" to refer to this declaration of the function header file.
build mydll.dll and mydll.lib
2. referencing dll files in Visual C #. NET
The new Visual C # console application is named Testimportdll;
will be MyDll.dll and the MyDll.lib Copy to the executable file folder ():
adding references in Praogram.cs using System.Runtime.InteropServices;
declare one that will be referenced by, for example, the following MyDll.dll class for the function in:
classTest
{
//[DllImport (". \\.. \\lib\\CppDemo.dll ")]
//Public static extern void Function ();
//[DllImport (". \\.. \\lib\\CppDemo.dll ")]
//public static extern int Add (int i, int j);
[DllImport (".. \\.. \\Lib\\Mydll1.dll")]
Public Static extern intAdd (intA,intb);
}
Finally, the output of this class is called in the main function:
Static voidMain (string[] args)
{
Console.WriteLine ("Result:" +test. ADD (2, 3). ToString ());
Console.ReadLine ();
}The following is the code for Program.cs:END ():
Program C + + to C # interaction