referencing namespaces using System.Runtime.InteropServices
Call Method:
First, static loading
Load the C++dll in Dllimprot way. The following format:
//corresponding C + + method//void Doginit (Word*,hwnd)//error return value, Window handle[DllImport ("DOG.dll", EntryPoint ="Doginit", CallingConvention =callingconvention.cdecl)] Public Static extern voidDoginit ( out Shortx, IntPtr hwnd); //corresponding C + + method//void Dogreadinfo (word*,int,unsigned char*,unsigned short)//error return value, which item to read, what to return, initialization length[DllImport ("DOG.dll", EntryPoint ="Dogreadinfo", CallingConvention =callingconvention.cdecl)] Public Static extern voidDogreadinfo ( out ShortXintY,[marshalas (unmanagedtype.lparray,sizeconst= +)]byte[] Z, ShortW); /// <summary> ///Initialize dongle/// </summary> Public Static voidDoginit () {IntPtr zero=IntPtr.Zero; Shortx =-1; Doginit ( outX,zero); //at this point, X saves the returned error code, such as 0, 1, 2 } Public Static voidDogreadinfo () { Shortx; inty =0; byte[] Z =New byte[ +]; ShortW = +; Dogreadinfo ( outx,y,z,w); //at this point, X saves the returned error code//Z Saves the returned read content stringContent =System.Text.Encoding.Default.GetString (z); }
Attention:
1, if only one DLL can be in this way, put the DLL in the Bin/debug or Bin folder, both the console form application and Web page application are valid. If there are multiple DLLs, there are dependent calls, such as a, b two dll,a will call the method inside B, because [DllImport ("A.dll")] can only call a DLL, in the console form application, will automatically load B, but the Web page application does not automatically load B , causing the return error code to not find the file. If you write this way [DllImport ("A.dll")], [DllImport ("B.dll")], B is not loaded. With the dynamic loading in the following two, only one DLL can still be loaded. Workaround: Copy A, B to the System folder, 32-bit system copy to C/windows/system32, 64-bit system copy to C/windows/syswow64 folder, write only with [DllImport ("A.dll")], It will automatically load B. This method only applies to you can control the computer, if the use of other people's server space, can not be copied, it is possible to compile a, b into a DLL to call.
2, the data format corresponding problem.
Second, dynamic loading
1, load with assembly mode, this method only applies to managed code. C + + can also be compiled into managed code. Only one DLL can still be loaded.
2, loaded with LoadLibrary and GetProcAddress mode, this way can load unmanaged code. Only one DLL can still be loaded.
The following format:
Public classdllinvoke{[DllImport ("Kernel32.dll")]Private extern StaticIntPtr LoadLibrary (String path); [DllImport ("Kernel32.dll")]Private extern StaticIntPtr GetProcAddress (IntPtr lib, String FuncName); [DllImport ("Kernel32.dll")]Private extern Static BOOLFreeLibrary (IntPtr Lib); PrivateINTPTR hlib; PublicDllinvoke (String dllpath) {hlib=LoadLibrary (DllPath); }~Dllinvoke () {FreeLibrary (hlib); }//Convert the function you want to execute to a delegate PublicDelegate Invoke (String apiname,type t) {INTPTR API=GetProcAddress (Hlib, apiname); return(Delegate) marshal.getdelegateforfunctionpointer (api,t); }}
//Use time Public classClass1 {//Defining delegate Variables Public Delegate intC (); PublicActionResult Index () {//initializing a class in a methodDllinvoke Dllinvoke =NewDllinvoke ("DLL Absolute Path");//You can also save the DLL under the bin and convert it to an absolute path with a relative path//calling the method inside the classc C = (c) Dllinvoke.invoke ("dll inside method name like Doginit",typeof(C)); //Get return value intresult =C.invoke (); } }
Third, Other ways
In the search process, some keywords also have a revelation, but did not look down. such as changing the DLL search directory, adding the bin directory to the DLL search order, dllimport load the dependency DLL.
Iv. other articles, see
Http://www.cnblogs.com/szytwo/archive/2011/12/11/2283780.html
Http://www.cnblogs.com/kvspas/archive/2008/12/15/1355033.html
If there is an incorrect place, please ask.
C # Calling C++dll methods and considerations