|
|
Home » U++ Library support » U++ Library : Other (not classified elsewhere) » Class method called by keyboard hook callback has an empty "this" pointer
Class method called by keyboard hook callback has an empty "this" pointer [message #37334] |
Sun, 23 September 2012 11:31 |
Mircode
Messages: 17 Registered: September 2012
|
Promising Member |
|
|
Hello!
I managed to create a working low level keyboard hook on windows. Now I want to update a label in my GUI everytime something happens.
In order to access my main window class from the hook callback, I created a global variable, which points to this class:
Inside the constructor I define it
KeyBuddy2::KeyBuddy2()
{
MainWindow=this;
CtrlLayout(*this, "Window title");
SetHook();
}
Inside the LowLevelKeyboardProc function, I call
MainWindow->ProcessKbdEvent(...)
but inside the ProcessKbdEvent method, "this" does not point to MainWindow. Instead, it is a nullpointer.
I am happy that I found this problem to be the cause of my program crashing all the time... I wanted to access class members from inside this method.
As a workaround, I can write MainWindow->member instead of just member. But still, this should not be necessary. Does anyone know the cause of this behavior?
Greetings,
Mirko
|
|
|
Re: Class method called by keyboard hook callback has an empty "this" pointer [message #37335 is a reply to message #37334] |
Sun, 23 September 2012 12:51 |
|
Hi Mirko,
Welcome to the forum
Mircode wrote on Sun, 23 September 2012 11:31 | Inside the LowLevelKeyboardProc function, I call
MainWindow->ProcessKbdEvent(...)
but inside the ProcessKbdEvent method, "this" does not point to MainWindow. Instead, it is a nullpointer.
|
This would most probably happen if MainWindow==null at the time of calling MainWindow->ProcessKbdEvent(). You can add a check for this in LowLevelKeyboardProc to confirm this. I don't know how your code is structured, but I believe that the function is called before any instance of KeyBuddy2 is created or something like that.
Best regards,
Honza
[Updated on: Sun, 23 September 2012 12:52] Report message to a moderator
|
|
|
|
Re: Class method called by keyboard hook callback has an empty "this" pointer [message #37337 is a reply to message #37336] |
Sun, 23 September 2012 20:27 |
|
You're welcome. In U++ it is fairly common to use a static variable in global function instead of global variables to prevent all kinds of troubles that global variables cause.
In your case, you can do something like
KeyBuddy2* MainWindow() {
static KeyBuddy2* ptr;
return ptr;
}
...
KeyBuddy2::KeyBuddy2()
{
MainWindow()=this;
CtrlLayout(*this, "Window title");
SetHook();
}
...
MainWindow()->ProcessKbdEvent(...)
There is even a macro for this: GLOBAL_VAR(type, name), for those that are too lazy to write (Such as me )
Honza
|
|
|
|
Re: Class method called by keyboard hook callback has an empty "this" pointer [message #37353 is a reply to message #37343] |
Wed, 26 September 2012 01:16 |
nlneilson
Messages: 644 Registered: January 2010 Location: U.S. California. Mojave &...
|
Contributor |
|
|
Welcome to the forum.
Mircode wrote on Mon, 24 September 2012 09:25 | I should really read through a c++ book someday.
I bet this was invented by people who used global variables all the time but then they were forbidden to do so because its bad style.
|
Global variables are not forbidden. Local variables are safer and takes less memory.
U++ will be good for you to learn C++ but you do need to do some reading to learn the basics and concepts.
|
|
|
Goto Forum:
Current Time: Fri Dec 13 21:45:35 CET 2024
Total time taken to generate the page: 0.01904 seconds
|
|
|