Question

I have an application, in which I use a console to see some of the values being output. Now some of the requirements have changed, and I do not need the console during runtime anymore.

I tried to change that, by toggling the /SUBSYSTEM parameter, found under Project Properties->Linker->System->Subsystem from Console to Windows, as I'd done the same for an earlier thing, and it had worked.

On this occasion, it gives me an Unhandled Exception, in mfc110u.dll, as the object cannot be instantiated.

Why does this exception occur, and how else can I turn off the console with the running program? I'm using VS2012 as the dev environment.

Was it helpful?

Solution

If you don't want a console, declare a winmain. This is the non-unicode version

#include <windows.h>
#include <iostream>
#include <cstdio>

int main (int, char**);

// If we just start with main, we will always get a console window
int WINAPI WinMain (
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow
) 
{
    int argc = __argc;
    char** argv = __argv;
#ifdef DEBUG
  // If we are running in debug mode, open a console window
    AllocConsole();
    freopen("conin$", "r", stdin);
    freopen("conout$", "w", stdout);
    freopen("conout$", "w", stderr);
#endif

    return main (argc, argv);
}

int main (
    int argc,
    char** argv
)
{
    MessageBox(NULL, "Whoo hoo", "It Works!!!", MB_OK);
    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top