Search code examples
cwinapishowwindow

HowTo hide Console Window with WinAPI?


I'm trying to hide console window when my C application lauching with this simple WinAPI code:

#define CONSOLE_NAME "6FD66E14-FF0F-4B94-B8AF-AFE3D42DC399"

void hide_window(void)
{
    // Ставим заголовок для консольного окна / Setting window caption
    SetConsoleTitle(TEXT(CONSOLE_NAME));

    // Находим по заголовку Handle для окна / Searching Handle of the window
    HWND hWnd = FindWindow(NULL, TEXT(CONSOLE_NAME));
    if (hWnd == 0)
    {
        ErrorExit(TEXT("FindWindow"));
    }

    // Скрываем консоль / Hidding console
    ShowWindow(hWnd, SW_HIDE);
}

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

    /* other code */
}

Everything works fine, if no antiviruses activated, but when Kaspersky is running and monitors the system, I can't get work the code above, because hWnd == 0 is true and GetLastError() = 183 error ("Cannot create a file when that file already exists.") lauched!

Question: What can I do? All that I need is to hide that console window.

Please, help me with this stuff.

Great thanks!

PS. Using Visual Studio 2010 (Visual C++)


Solution

  • Just call FreeConsole() get rid of it and AllocConsole() to create a new one.