12

I am using MFC in my C++ program (using Visual Studio 2008). I have to call AfxGetInstanceHandle() at the begining of my program.

This function triggers a break point:

AFXWIN_INLINE HINSTANCE AFXAPI AfxGetInstanceHandle()
{ ASSERT(afxCurrentInstanceHandle != NULL);
return afxCurrentInstanceHandle; }

The ASSERT statement fails. Is there something special that needs to be done in order to initialize the afxCurrentInstanceHandle before we try to access it?

PS: I am using MFC in a shared dll.

EDIT

My code is like that:

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

  CoInitialize(NULL);
  AfxGetInstanceHandle();
  return 0;
}

I would like to use the InstanceHandle in order to initialize a CComModule and then use it to manipulate com object.

0x26res
  • 11,925
  • 11
  • 54
  • 108

4 Answers4

18

I made a Console App with MFC and got the Message too. I found the solution, that you need a "prologue" at the beginning of your main (_tmain, etc).

int main(int args, char* argv[]) //, char *envp[])
{
    // initialize MFC and print and error on failure
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))  
    {       
        // TODO: change error code to suit your needs       
        cerr << _T("Fatal Error: MFC initialization failed") << endl;       
        return 1;   
    }   
    AfxGetInstanceHandle();
    // TODO: code your application's behavior here.
    ...
user2528794
  • 181
  • 1
  • 3
  • I have the same problem and I was wondering: shouldn't call to `AfxInitExtensionModule(...)` initialize MFC as well? I still get error when calling `AfxGetInstanceHandle()`. If I add `˙AfxWinInit(...)` error is gone. – Nikola Sep 15 '20 at 11:55
9

Use:

AFX_MANAGE_STATE(AfxGetStaticModuleState());

Before you call:

AfxGetInstanceHandle();
Vin.X
  • 4,759
  • 3
  • 28
  • 35
5

This can happen if you mix unicode/mbcs or debug/release build modes for DLL/application.

Erik
  • 88,732
  • 13
  • 198
  • 189
  • My project and all it's dependencies are being complied using MFC in a shared DLL, with MultiThreaded Debug DLL, and using Multibytes characters. It may be due to something else – 0x26res Mar 02 '11 at 09:03
2

If you are using MFC you shouldn't be providing a main, wmain, _tmain, or WinMain -- MFC provides its own entry point. Put initialisation code in InitInstance of your CWinApp derived class. If you don't have a CWinApp derived class you haven't created the project correctly -- use the Visual Studio Wizards for creating an MFC application.

ymett
  • 2,425
  • 14
  • 22
  • So there is no way to access AfxGetInstanceHandle() without having the all CWinApp object ? – 0x26res Mar 02 '11 at 12:52
  • I think I was doing something wrong, and I did not really needed this to use COM object. Thanks. – 0x26res Mar 03 '11 at 15:56
  • 2
    Downvote because this is not true. For Window/GUI applications your statement is correct, the project wizzard will create a CWinApp derived class. But when you create a console application with MFC support, then source code with a main (_tmain) function is created and AfxWinInit is called first to set up the MFC. – Werner Henze Jun 28 '13 at 11:48