VB调用C++编写的dll(接口约定)

dll示例代码

// 32位编译(VS2017条件编译)
#ifdef _M_IX86
#pragma comment(linker, "/export:Send_Str_To_DLL_TEST=_Send_Str_To_DLL_TEST@4")
#pragma comment(linker, "/export:Start_Server_CallBack=_Start_Server_CallBack@4")
#endif // _M_IX86
// 64位编译(VS2017条件编译)
#ifdef _M_X64
//#pragma comment(lib,"QDPay_mt64.lib")
#endif // _M_X64

typedef void(__stdcall  *RX_Callback)(const char * str);
//回调函数作为参数
int _stdcall Start_Server_CallBack(RX_Callback rx_p) {
	RX_Callback p_fun=rx_p;
	p_fun("123456");
}
//
// 字符串作为参数
extern "C" _declspec(dllexport) int _stdcall  Send_Str_To_DLL_TEST(char* p) {
	//std::string(p).length();
	return std::string(p).length();
}

VB示例代码

模块代码:

'CopyMemory 内存拷贝API
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
'Start_Server_CallBack
Public Declare Sub Start_Server_CallBack Lib "C:\\Users\\Administrator\\source\\repos\\TCP_DataManager_DLL\\Debug\\TCP_DataManager_DLL.dll" (ByVal p_func As Long, ByVal result As Integer)

'Send_Str_To_DLL_TEST
Public Declare Function Send_Str_To_DLL_TEST Lib "TCP_DataManager_DLL.dll" (ByVal file_name As String) As Integer

'Server_Msg_Event
'ByRef c As String
'ByVal c As String
Sub Server_Msg_Event(ByVal c As Long)
  Dim msg As String
  CopyMemory msg, c, 4
  MsgBox msg   '打印服务端消息
  
End Sub
'Start_Server_CallBack 接口调用测试
Public Function test()

   Call Start_Server_CallBack(AddressOf Server_Msg_Event)  '调用回调函数

End Function



窗体代码:

Private Sub Form_Load()
test
End Sub


版权声明:本文为u012577474原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。