CAppModule vs CAtlExeModuleT , getting the application message loop
Asked Answered
W

1

5

I am trying to get the message loop from a ATL::CAppModule in my project, there seems to be none, so:

  • I've tried defining CAppModule, with extern CAppModule _Module; in "stdafx.h" and CAppModule _Module; in my .cpp file, it compiles, linkes and at the perform registration step I get an assertion in atlbase.h here ATLASSERT(_pAtlModule == NULL); which means that the CAppModule has already been declared.

But I can't seem to find another CAppModule instantiation, instead I see a CAtlExeModuleT instantiation (it is not my code..).

now.. from what I've searched I haven t found a way to get the message loop from a CAtlExeModuleT object. Are they different things or am I missing something?

Wesleywesleyan answered 26/7, 2013 at 14:38 Comment(0)
G
10

There is a mix of issues here. CAppModule is a WTL class. _pAtlModule is static/global ATL variable that points to module singleton class.

You cannot fix ATL _pAtlModule problem with WTL CAppModule because the two are unrelated (atlthough have certain similarity between).

To fix the _pAtlModule problem you need an ATL module instance. The simplest is to add CComModule static:

CComModule _Module; // <-- Here you go

int _tmain(int argc, _TCHAR* argv[])
{
  //...

Because CComModule itself is here for backward compatibility only, it would be the better to use CAtlExeModuleT (and friends) instead, however WTL will not work this way because WTL's CAppModule inherits from CComModule. The global instance of CAppModule will also be the instance for ATL CComModule:

CAppModule _Module;

int _tmain(int argc, _TCHAR* argv[])
{
    // ...
    _Module.Init(...
    CMessageLoop MessageLoop;
    _Module.AddMessageLoop(&MessageLoop);
    // ...

and then later on some application object:

CMessageLoop* pMessageLoop = _Module.GetMessageLoop();

the GetMessageLoop call will retrieve the message loop you added earlier.

Having this ATL/WTL issue resolved, you can move on to the WTL message loop thing, where you expect PreTranslateMessage to be called on modal dialog message loop and it won't be called there because it is not expected to work this way (CMessageLoop calls message filter chain, and modal dialog's loop don't).

Garret answered 26/7, 2013 at 14:52 Comment(3)
CModule does not have GetMessageLoop, how I am supposed to get the message loop using CAtlExeModule ? I have already done your "better" aproach, how do I now get the message loopWesleywesleyan
I don't see your real code and I don't understand that is going on. CMessageLoop map is per-thread, and you cannot get an instance you never added earlier on the same thread.Garret
OK, thank you very much and sorry for flooding with comments, neighbour.Wesleywesleyan

© 2022 - 2024 — McMap. All rights reserved.