dynamic_cast and AfxGetMainWnd() failing where it shouldn't fail
-
I got this odd problem today (well, one of my testers got it): I got a function that returns a pointer to my main window, casted to the correct type. It looks something like this: CMyMainWnd* _GetMainWnd() { return dynamic_cast(AfxGetMainWnd()); } The strange thing is that this fails at one point. I first thought that it was due to the function being called from the wrong thread, but that wasn't the case. I took a look at what AfxGetMainWnd() returned, and it was the correct window (at least the title was correct; I never had this problem, so I can't debug it). The window title clearly states that this is "CMyMainWnd", but the dynamic cast still fails and returns 0. What could cause this problem? Maybe I should just use a stupid cast and cross my fingers ;)
-
I got this odd problem today (well, one of my testers got it): I got a function that returns a pointer to my main window, casted to the correct type. It looks something like this: CMyMainWnd* _GetMainWnd() { return dynamic_cast(AfxGetMainWnd()); } The strange thing is that this fails at one point. I first thought that it was due to the function being called from the wrong thread, but that wasn't the case. I took a look at what AfxGetMainWnd() returned, and it was the correct window (at least the title was correct; I never had this problem, so I can't debug it). The window title clearly states that this is "CMyMainWnd", but the dynamic cast still fails and returns 0. What could cause this problem? Maybe I should just use a stupid cast and cross my fingers ;)
It looks like AfxGetMainWnd() will return NULL if it is called from a non-GUI thread. ie. A Worker thread. Or of course if the main window in a GUI thread is not of type CMyMainWnd or derived from it. Simple enough to test what AfxGetMainWnd() returns and ASSERT or log errors. BTW. We can't see you <cast_type> You need to use the < and > and pre and code Formatting Toolbar options when embedding code in posts. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com
-
It looks like AfxGetMainWnd() will return NULL if it is called from a non-GUI thread. ie. A Worker thread. Or of course if the main window in a GUI thread is not of type CMyMainWnd or derived from it. Simple enough to test what AfxGetMainWnd() returns and ASSERT or log errors. BTW. We can't see you <cast_type> You need to use the < and > and pre and code Formatting Toolbar options when embedding code in posts. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com
No, AfxGetMainWnd() does not return NULL. How else could I get the Window caption? :) The thing is that the caption is correct, just the caption of the main window as it should be. So, I assume that AfxGetMainWnd() returns the correct window pointer, but the cast just fails. And thanks for the hint on the < and >... I will keep that in mind next time.
-
It looks like AfxGetMainWnd() will return NULL if it is called from a non-GUI thread. ie. A Worker thread. Or of course if the main window in a GUI thread is not of type CMyMainWnd or derived from it. Simple enough to test what AfxGetMainWnd() returns and ASSERT or log errors. BTW. We can't see you <cast_type> You need to use the < and > and pre and code Formatting Toolbar options when embedding code in posts. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com
Neville Franks wrote: BTW. We can't see you You need to use the < and > and pre and code Formatting Toolbar options when embedding code in posts. It looks like it is:
return dynamic_cast<CMyMainWnd*>(AfxGetMainWnd());
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
Neville Franks wrote: BTW. We can't see you You need to use the < and > and pre and code Formatting Toolbar options when embedding code in posts. It looks like it is:
return dynamic_cast<CMyMainWnd*>(AfxGetMainWnd());
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
You'd have to ask Tak that one. I was simply helping Neville see potentially hidden text.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)