场景: 我有一个C++ DLL。 在这个DLL中,我创建了一个工作线程。 在工作线程中,我有一个等待用户通过USB硬件设备输入的循环。 仅当USB设备上的用户输入符合某些标准时,循环才会结束。 此外,我需要实时反馈USB设备的用户使用情况反馈以显示在屏幕上。 它使用Delphi GUI进行反馈。从工作线程调用主线程回调函数
当用户使用USB设备时,Windows系统会生成回调函数。此回调函数被写入相同的C++ DLL文件,并作为USB设备的初始化函数中的参数传入。
我在DLL中使用了一个全局变量作为标志来确定何时该循环必须退出。
我也从Delphi DLL加载这个C++ DLL。 Delphi DLL - > C++ DLL 反馈显示来自Delphi DLL。
基本上,我现在面临的问题是函数ptr,funcptr根本无法调用。屏幕上没有实时反馈。这是Delphi DLL中的一个函数。 这是代码行:
(*(reinterprete_cast<FUNCPTR>(funcPtr)))("this is the feedback msg displayed on Delphi GUI");
有没有人有一个解决方案?
我是新手,很欣赏任何答案。谢谢您的帮助。
//Global variable
BOOL flag = TRUE;
//A function type in Delphi calling app
typedef void (__stdcall *FUNCPTR)(PCHAR);
//Functions start here.....
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
do {} while (flag);
}
function_1st_CalledFromDelphiDLL(FUNCPTR funcPtr)
{
Initialize_USBDevice(handleUSBDeviceEvent_callback, funcPtr);
}
function_2nd_CalledFromDelphiDLL()
{
DWORD threadID;
HANDLE hWorkerThread;
hWorkerThread = CreateThread(NULL,0,ThreadProc, 0, 0 , &threadID);
if (hWorkerThread!=NULL)
{
WaitForSingleObject(hWorkerThread, 30000);
}
}
//This is the callback function, called by Windows system when user meddles with the USB device
handleUSBDeviceEvent_callback(void *funcPtr)
{
flag = FALSE; //so loop in ThreadProc can exit
//The following code cannot be executed at all. Even when i Try MessageBox(NULL,L"msg",NULL,NULL), the message box doesn't popup too. But, I can write something to a filestream here.
(*(reinterprete_cast<FUNCPTR>(funcPtr)))("this is the feedback msg displayed on Delphi GUI");
}
我已添加代码。用户输入是一个USB传感器设备。 – Robogal 2010-07-28 07:43:45