Get ready:
First Open VC + + 6.0 new project, select Win32 Dynamic link-library, named Stdlibrary
Create a new Library.cpp file with the following content
#include <stdio.h><windows.h>BOOL apientry DllMain (hmodule hmodule, DWORD ul_ Reason_for_call, lpvoid lpreserved) { return TRUE;} HRESULT __stdcall testadd (intintreturn i + j;}
The new text file is then named Export.def, which reads as follows
LIBRARY "Stdlibrary"
Exports
Testadd @1
Compile project Get StdLibrary.dll
1 VB Call StdLibrary.dll
New Standard EXE project, drag interface,
Right-click on the code and write the following code
Private Declare FunctionTestaddLib "StdLibrary.dll"(ByValInX as Long,ByValIny as Long) as LongPrivate SubCmdcaclute_click ()DimX as LongDimY as LongDimResult as Longx=txtx.texty=Txty.textresult=testadd (x, y) txtresult.text=resultEnd Sub
Then use VB to generate EXE, drag the StdLibrary.dll to the project in the compiled directory, run can, call success.
Note: VB calls the third-party DLL, the debugging process Lib "StdLibrary.dll" if not the absolute path will be error, do not know whether VB Bug, as long as the generation of EXE after the run will not error
2 C # Call StdLibrary.dll
Write the following code and drag the StdLibrary.dll to the Debug/release directory
usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Runtime.InteropServices;namespacecsharpcallclibrary{classProgram {[DllImport ("StdLibrary.dll", CallingConvention =Callingconvention.stdcall)] Public Static extern intTestadd (intXinty); Static voidMain (string[] args) { intx =1, y =2, result=0; Result=testadd (x, y); Console.WriteLine ("{0} + {1} = {2}", X,y,result); Console.read (); } }}
Run compile-generated EXE, this call succeeds
C#/vb calling C-written standard DLL