AfxGetModuleState not returning DLL's ModuleState
-
I have an MFC executable and an ATL DLL which supports MFC as well. When the DLL calls
AfxGetModuleState
it gets the EXE'sModuleState
class instead of it's own. Why does this happen ? What am I missing ?is it causing a problem ?
I'll write a suicide note on a hundred dollar bill - Dire Straits
-
is it causing a problem ?
I'll write a suicide note on a hundred dollar bill - Dire Straits
Yes because when loading resources (
AfxFindStringResourceHandle
for example),AfxGetModuleState
is called. So, my DLL tries to load a String resource for example, and tries to run through all of the EXE's resource-handles (becauseAfxGetModuleState
returns the EXE's module object) instead of it's own list of resource-handles (stored in it's own module-object). Am I missing something ? Does the DLL really have it's own moduleobject ? I'm pretty sure it does, but why isn't it returned then ? thanks -
Yes because when loading resources (
AfxFindStringResourceHandle
for example),AfxGetModuleState
is called. So, my DLL tries to load a String resource for example, and tries to run through all of the EXE's resource-handles (becauseAfxGetModuleState
returns the EXE's module object) instead of it's own list of resource-handles (stored in it's own module-object). Am I missing something ? Does the DLL really have it's own moduleobject ? I'm pretty sure it does, but why isn't it returned then ? thanksWhen you are loading the resource string, dont you think you need module instance handle of the dll???
I'll write a suicide note on a hundred dollar bill - Dire Straits
-
When you are loading the resource string, dont you think you need module instance handle of the dll???
I'll write a suicide note on a hundred dollar bill - Dire Straits
You can, but the better way is to use
AfxFindStringResourceHandle
which not only looks in your resource-hinstance but also traverses theCDynLinkLibrary
list and any other relevant resource-handles, that's the way to use several resource-dlls at the same time. Only that it doesn't work in this specific scenario because the module returned is the EXE's one and not the DLLs one... -
I have an MFC executable and an ATL DLL which supports MFC as well. When the DLL calls
AfxGetModuleState
it gets the EXE'sModuleState
class instead of it's own. Why does this happen ? What am I missing ?Did you insert
AFX_MANAGE_STATE(AfxGetStaticModuleState());
before any MFC call you do from within a DLL function?
We are men. We are different. We have only one word for soap. We do not own candles. We have never seen anything of any value in a craft shop. We do not own magazines full of photographs of celebrities with their clothes on. - Steve
-
Did you insert
AFX_MANAGE_STATE(AfxGetStaticModuleState());
before any MFC call you do from within a DLL function?
We are men. We are different. We have only one word for soap. We do not own candles. We have never seen anything of any value in a craft shop. We do not own magazines full of photographs of celebrities with their clothes on. - Steve
-
Nope :-) Can you tell me more about this ? What does it do exactly ? Is it possible to do it once per module and not before every call ? thanks
-
Nope :-) Can you tell me more about this ? What does it do exactly ? Is it possible to do it once per module and not before every call ? thanks
try MSDN: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_mfc_afx_manage_state.asp[^]
We are men. We are different. We have only one word for soap. We do not own candles. We have never seen anything of any value in a craft shop. We do not own magazines full of photographs of celebrities with their clothes on. - Steve
-
Did you insert
AFX_MANAGE_STATE(AfxGetStaticModuleState());
before any MFC call you do from within a DLL function?
We are men. We are different. We have only one word for soap. We do not own candles. We have never seen anything of any value in a craft shop. We do not own magazines full of photographs of celebrities with their clothes on. - Steve
-
Ok I'm starting to get it, There is no other solution except for placing this in all my methods that perform MFC calls ? Isn't this abit weird ?
AFAIK this is the only solution. I don't think it's weird cause you only want to change the module state during the function call, and then set it back to the main application. And adding one line of code isn't too bad, is it ? :)
We are men. We are different. We have only one word for soap. We do not own candles. We have never seen anything of any value in a craft shop. We do not own magazines full of photographs of celebrities with their clothes on. - Steve
-
AFAIK this is the only solution. I don't think it's weird cause you only want to change the module state during the function call, and then set it back to the main application. And adding one line of code isn't too bad, is it ? :)
We are men. We are different. We have only one word for soap. We do not own candles. We have never seen anything of any value in a craft shop. We do not own magazines full of photographs of celebrities with their clothes on. - Steve
In the begining of every function that uses an MFC method which in turn uses this DLL's own resources ? I think it sounds terrible :-) Having to remember to do this, and having other people rememebr to do this. Not to mention having my code cluttered by this line all the time... I want my module to always use the same AFX_MODULE_THINGY, why can't the AFX methods do
AfxGetStaticModuleState()
, that is my question actually. Why did they leave it this way ? -
In the begining of every function that uses an MFC method which in turn uses this DLL's own resources ? I think it sounds terrible :-) Having to remember to do this, and having other people rememebr to do this. Not to mention having my code cluttered by this line all the time... I want my module to always use the same AFX_MODULE_THINGY, why can't the AFX methods do
AfxGetStaticModuleState()
, that is my question actually. Why did they leave it this way ?ohadp wrote: I want my module to always use the same AFX_MODULE_THINGY, why can't the AFX methods do AfxGetStaticModuleState(), that is my question actually. Why did they leave it this way ? Unfortunately (?) I was never a part of the development teams that invented MFC and the related techniques. So I fear this is a question I can't answer you - I just take it as is, and every time my DLL does some weird things the 'AFX_MODULE_THINGY' is the first thing that comes to me. Hope you'll reach that state soon, too. ;)
We are men. We are different. We have only one word for soap. We do not own candles. We have never seen anything of any value in a craft shop. We do not own magazines full of photographs of celebrities with their clothes on. - Steve
-
Ok I'm starting to get it, There is no other solution except for placing this in all my methods that perform MFC calls ? Isn't this abit weird ?
ohadp wrote: There is no other solution except for placing this in all my methods that perform MFC calls ? No, it is only needed by those functions that load resources from the DLL. Your DLL can use MFC all day long, but if it ever needs to load one of its own resources, it must use the
AFX_MANAGE_STATE()
macro.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)