DLL with a callback
-
1. That is correct. At that point,
values
is set topublic_func_taking_callback
, which takes the address of a function and immediately calls it. 2.funcA
callscallback_t
, butcallback_t
has never been set to any value. To be honest, I find it difficult to understand your code, so I may have still missed something. However, you should be able to see the exact sequence of events by running the code in the debugger.Hi Richard, thanks for the answers. Yes I can debug it, but it doesn't help me much, since I don't understand why it behaves that way. 1.) OK, got it. Unfortunately then I do not know, how I pass the address of the function of CallB to the dll. I want the dll to call CallB() in the moment callback_t is called. Most likely I confuse here something, but I find it very hard to understand the syntax. It seems even harder since it is wrong :D I wasn't able to find any C++ examples for that. 2.) I thought I set it to public_func_taking_callback, but obviously this is not the case. Maybe I should explain again, what I try to do: In my main application I want to declare a function. This function should be registered in the dll as a callback function. When during run time in the dll some criteria match ( if(a>10) ) the dll should call the function in the main application and execute it.
-
Hi Richard, thanks for the answers. Yes I can debug it, but it doesn't help me much, since I don't understand why it behaves that way. 1.) OK, got it. Unfortunately then I do not know, how I pass the address of the function of CallB to the dll. I want the dll to call CallB() in the moment callback_t is called. Most likely I confuse here something, but I find it very hard to understand the syntax. It seems even harder since it is wrong :D I wasn't able to find any C++ examples for that. 2.) I thought I set it to public_func_taking_callback, but obviously this is not the case. Maybe I should explain again, what I try to do: In my main application I want to declare a function. This function should be registered in the dll as a callback function. When during run time in the dll some criteria match ( if(a>10) ) the dll should call the function in the main application and execute it.
Bodo2407 wrote:
I don't understand why it behaves that way.
It behaves that way because that is the way you have written the code. 1. So you need to change the code in
public_func_taking_callback
as follows:\_\_declspec(dllexport) void public\_func\_taking\_callback(callback\_t evHnd) { // evHnd(333);//value just for testing // this was causing your problem callback\_t = evHnd; // save the callback address for funcA. }
And you need to change funcA to:
if(a>10) //trigger the event in the main application
callback_t(a); // the callback is set to invoke void __stdcall CallB(unsigned int halfbeast)Again, make these changes and then step through the code in the debugger to check that it is correct.
-
Bodo2407 wrote:
I don't understand why it behaves that way.
It behaves that way because that is the way you have written the code. 1. So you need to change the code in
public_func_taking_callback
as follows:\_\_declspec(dllexport) void public\_func\_taking\_callback(callback\_t evHnd) { // evHnd(333);//value just for testing // this was causing your problem callback\_t = evHnd; // save the callback address for funcA. }
And you need to change funcA to:
if(a>10) //trigger the event in the main application
callback_t(a); // the callback is set to invoke void __stdcall CallB(unsigned int halfbeast)Again, make these changes and then step through the code in the debugger to check that it is correct.
Hi Richard, thanks a lot, but unfortunately it cannot compile:
__declspec(dllexport) void public_func_taking_callback(callback_t evHnd)
{
// evHnd(333);//value just for testing // this was causing your problem
callback_t = evHnd; // save the callback address for funcA. //this line doesn't compile
}Failure given: -Error C2513 'void (__stdcall *)(unsigned int)': no variable declared before '=' So the typedef earlier doesn't work or... ?
-
Hi Richard, thanks a lot, but unfortunately it cannot compile:
__declspec(dllexport) void public_func_taking_callback(callback_t evHnd)
{
// evHnd(333);//value just for testing // this was causing your problem
callback_t = evHnd; // save the callback address for funcA. //this line doesn't compile
}Failure given: -Error C2513 'void (__stdcall *)(unsigned int)': no variable declared before '=' So the typedef earlier doesn't work or... ?
callback_t
is a type, not a function. In yourDLL
, you do need a variable of such a type, in order to store the user function address. Something similar to (not tested)typedef void (__stdcall* callback_t) (unsigned int halfbeast);
static callback_t s_user_function = nullptr; // variable used to store the user function addressextern "C" __declspec(dllexport) void funcA(const unsigned long long a, const unsigned long long b);
//following function should be the callback, triggering an assigned function in the main application
extern "C"
{
__declspec(dllexport) void public_func_taking_callback(callback_t evHnd)
{
s_user_function = evHnd; // store the passed user function address
}
}void funcA(
const unsigned long long a,
const unsigned long long b)
{
//some code here... do things with a and b
if(a>10 && s_user_function != nullptr) //trigger the event in the main application
s_user_function(333); // call the user function
}"In testa che avete, Signor di Ceprano?" -- Rigoletto
-
callback_t
is a type, not a function. In yourDLL
, you do need a variable of such a type, in order to store the user function address. Something similar to (not tested)typedef void (__stdcall* callback_t) (unsigned int halfbeast);
static callback_t s_user_function = nullptr; // variable used to store the user function addressextern "C" __declspec(dllexport) void funcA(const unsigned long long a, const unsigned long long b);
//following function should be the callback, triggering an assigned function in the main application
extern "C"
{
__declspec(dllexport) void public_func_taking_callback(callback_t evHnd)
{
s_user_function = evHnd; // store the passed user function address
}
}void funcA(
const unsigned long long a,
const unsigned long long b)
{
//some code here... do things with a and b
if(a>10 && s_user_function != nullptr) //trigger the event in the main application
s_user_function(333); // call the user function
}"In testa che avete, Signor di Ceprano?" -- Rigoletto
-
As you pointed out, the OP code looked a bit confused.
"In testa che avete, Signor di Ceprano?" -- Rigoletto
-
OK, now I experience another issue with it, I'm sorry. Hopefully you can help me here as well. I now want to call the callback function if an timer expires. Therefore I create an timer in a function I call from the main application. I store the handles to the timer and the timer queue globally in static variables.
static HANDLE gDoneEvent;
static HANDLE hTimer = NULL;
static HANDLE hTimerQueue = NULL;
int arg = 123; //will be passed to callback of timerVOID CALLBACK TimerRoutine(PVOID lpParam, BOOLEAN TimerOrWaitFired)
{
if (lpParam == NULL)
{
//do some error handling her
return;
}
else
{
if(s_user_function!=nullptr)
s_user_function(12);
}
//SetEvent(gDoneEvent); //for periodic calls, don't set gDoneEvent
}Now, in some function called from the main application, I allocate the timer and start it:
char timer_init(void)
{
// Use an event object to track the TimerRoutine execution
gDoneEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (NULL == gDoneEvent)
{
return 1;
}
// Create the timer queue.
hTimerQueue = CreateTimerQueue();
if (NULL == hTimerQueue)
{
return 2;
}
CreateTimerQueueTimer(&hTimer, hTimerQueue, (WAITORTIMERCALLBACK)TimerRoutine, &arg, 3000, 10000, 0);WaitForSingleObject(gDoneEvent, INFINITE); //if we don't wait it crashes although handles are stored globally? //the handles should be closed and deleted only after timer had occured. ////CloseHandle(gDoneEvent); ////// Delete all timers in the timer queue. ////if (!DeleteTimerQueue(hTimerQueue)) //// printf("DeleteTimerQueue failed (%d)\\n", GetLastError());
}
As you can see, I don't close the handles and delete the timer queue on purpose. Everything works fine as long as the process waits. The callback in the main application is called. If I comment out the line with WaitForSingleObject I'll get an access exception, although I stored all the handles globally. Teh exception occurs with the timing the timer would have. So most likely it tries to call the callback of the timer function but something is missing. What is it I'm overlooking? I need the dll running, not waiting inside a fucntion for a timer...
-
OK, now I experience another issue with it, I'm sorry. Hopefully you can help me here as well. I now want to call the callback function if an timer expires. Therefore I create an timer in a function I call from the main application. I store the handles to the timer and the timer queue globally in static variables.
static HANDLE gDoneEvent;
static HANDLE hTimer = NULL;
static HANDLE hTimerQueue = NULL;
int arg = 123; //will be passed to callback of timerVOID CALLBACK TimerRoutine(PVOID lpParam, BOOLEAN TimerOrWaitFired)
{
if (lpParam == NULL)
{
//do some error handling her
return;
}
else
{
if(s_user_function!=nullptr)
s_user_function(12);
}
//SetEvent(gDoneEvent); //for periodic calls, don't set gDoneEvent
}Now, in some function called from the main application, I allocate the timer and start it:
char timer_init(void)
{
// Use an event object to track the TimerRoutine execution
gDoneEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (NULL == gDoneEvent)
{
return 1;
}
// Create the timer queue.
hTimerQueue = CreateTimerQueue();
if (NULL == hTimerQueue)
{
return 2;
}
CreateTimerQueueTimer(&hTimer, hTimerQueue, (WAITORTIMERCALLBACK)TimerRoutine, &arg, 3000, 10000, 0);WaitForSingleObject(gDoneEvent, INFINITE); //if we don't wait it crashes although handles are stored globally? //the handles should be closed and deleted only after timer had occured. ////CloseHandle(gDoneEvent); ////// Delete all timers in the timer queue. ////if (!DeleteTimerQueue(hTimerQueue)) //// printf("DeleteTimerQueue failed (%d)\\n", GetLastError());
}
As you can see, I don't close the handles and delete the timer queue on purpose. Everything works fine as long as the process waits. The callback in the main application is called. If I comment out the line with WaitForSingleObject I'll get an access exception, although I stored all the handles globally. Teh exception occurs with the timing the timer would have. So most likely it tries to call the callback of the timer function but something is missing. What is it I'm overlooking? I need the dll running, not waiting inside a fucntion for a timer...
Bodo2407 wrote:
I need the dll running, not waiting inside a fucntion for a timer..
I am not sure what you mean by that. When you create a timer, it is designed to call some function after a specified period of time, either once or repeatedly. The reason for having the call to
WaitForSingleObject
, is in case the main code needs the timer event to complete some action first. But it is not necessary if the remainder of the code does not rely on the actions of the timer event. -
Bodo2407 wrote:
I need the dll running, not waiting inside a fucntion for a timer..
I am not sure what you mean by that. When you create a timer, it is designed to call some function after a specified period of time, either once or repeatedly. The reason for having the call to
WaitForSingleObject
, is in case the main code needs the timer event to complete some action first. But it is not necessary if the remainder of the code does not rely on the actions of the timer event.Hi Richard, yes, I know that. I did put that in, because I got the exception and wanted to try the example code. This worked two times, so I thought it must be caused by leaving the function. Unfortunately thsi is not the case: After some debugging runs it seems that (in rare cases) it works a few times, but most likely it crashes immediately. So the observation I made that it is working if I call WaitForSingleObject isn't correct, it can crash randomly with or without. Unfortunately when it crashs the program pointer isn't on frame any more, so I don't see why it does that. One observation I made is that the handle of gDoneEvent is allocated in an entirely different address room (0x000003b0) than the other two handles hTimer and hTimerQueue(0x008b8928 and 0x008bsomething) which seems strange to me, since I thought allocated event should be also pretty close to the handles, but obviously it is not.
-
Hi Richard, yes, I know that. I did put that in, because I got the exception and wanted to try the example code. This worked two times, so I thought it must be caused by leaving the function. Unfortunately thsi is not the case: After some debugging runs it seems that (in rare cases) it works a few times, but most likely it crashes immediately. So the observation I made that it is working if I call WaitForSingleObject isn't correct, it can crash randomly with or without. Unfortunately when it crashs the program pointer isn't on frame any more, so I don't see why it does that. One observation I made is that the handle of gDoneEvent is allocated in an entirely different address room (0x000003b0) than the other two handles hTimer and hTimerQueue(0x008b8928 and 0x008bsomething) which seems strange to me, since I thought allocated event should be also pretty close to the handles, but obviously it is not.