VB6-与native-dll交互
1. 调用Sleep函数
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Public Console As CConsole Public Sub Main() Set Console = New CConsole ' 睡眠线程1s Sleep 1000 Console.WriteLine 11 End Sub
2. 调用自己写的(add(1, 2))
cpp
// VB6只支持__stdcall调用 extern "C" __declspec(dllexport) int __stdcall add(int a, int b) { return a + b; }
生成的dll需要通过exescope工具查看导出的函数名称。或者可以自己写def文件或者#prgama编译宏
VB6
Private Declare Function addLibCpp Lib _ "C:\Users\Administrator\source\repos\VB6CppInter\Release\VB6CppInter" Alias "_add@8" (ByVal a As Integer, ByVal b As Integer) As Integer
Public Sub Main()
Set Console = New CConsole
Dim a As Integer
Dim b As Integer
a = 1
b = 2
Console.WriteLine (addLibCpp(a, b))
Console.ReadKey
End Sub
3. 指针
cpp
extern "C" __declspec(dllexport) void __stdcall getStrLength(int *strlength, const char *str) { *strlength = strlen(str); }
vb6
Public Console As CConsole Private Declare Sub getStrLength Lib _ "C:\Users\Administrator\source\repos\VB6CppInter\Release\VB6CppInter" Alias "_getStrLength@8" (ByRef StrLength As Long, ByVal b As String) Public Sub Main() Set Console = New CConsole Dim StrLength As Long getStrLength StrLength, "helloworld" Console.WriteLine (StrLength) ' 10 Console.ReadKey End Sub