AfxGetInstanceHandle() triggers an assertion failure

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.
    ...

Use:

AFX_MANAGE_STATE(AfxGetStaticModuleState());

Before you call:

AfxGetInstanceHandle();

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