Scenario: I have a c++ DLL. In this DLL, I created a worker thread. In the worker thread, I have a loop that waits for user input via a USB hardware device. The loop ends only when the user input on USB device meets some criteria. Additionally, I need to feedback the user usage feedback of the USB device real-time to display on screen. It is using a Delphi GUI for feedback.
When user uses the USB device, a callback function will be made by Windows system. This callback function is written in the same C++ DLL file and passed in as a parameter in an Initialization function of the USB device.
I used a global variable in the DLL as a flag to determine when this loop must exit.
I am also loading this C++ DLL from a Delphi DLL. Delphi DLL -> C++ DLL The feedback display is from the Delphi DLL.
Basically, the problem I face now is that the function ptr, funcptr, cannot be called at all. There is no real-time feedback on screen. This is a function in Delphi DLL. This is the line of code:
(*(reinterprete_cast<FUNCPTR>(funcPtr)))("this is the feedback msg displayed on Delphi GUI");
Does anyone has a solution for this?
I'm a novice and appreciate any answers at all. Thanks for the help.
//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");
}