HowTo hide Console Window with WinAPI?
Asked Answered
R

3

0

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++)

Resurrection answered 23/5, 2011 at 11:23 Comment(1)
If removing the console isn't an option, perhaps you could try EnumWindows() and see if it gives a different result with the antivirus crap running?Chadwick
D
4

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

Deponent answered 23/5, 2011 at 11:49 Comment(2)
Could you submit some code or maybe URL, where I can read about it? Thks.Resurrection
There is nothing to it, the both don't take any parameters and the first hits on google will provide you with enough details.Deponent
I
2

You'd do better to create a new Visual Studio project based on 'Win32 Project' instead of 'Win32 Console Application'. Then a console will not be created automatically. (You can still create one in code if you want to.) This will set the /SUBSYSTEM:WINDOWS compiler setting amongst others.

You don't have to create a GUI in a non-console application, and you don't have to have a WndProc() function.

In response to "@Ian Goldby Could you give me the link to the source code of how to do that?"

There isn't any source code as such. Just create a new Visual C++ Win32 Project (not Win32 Console Application). In the Wizard make sure 'Windows application' is selected. The wizard will generate lots of template code, but you can delete all of this except for the skeleton of the _tWinMain() function. This is the function that will be called when your application starts up. Just paste your own code in here.

Alternatively, check the 'Empty project' box in the last stage of the wizard, and supply your own main.c file and your own _tWinMain() function. You might find this easier.

Either way, your application will run just as before except that, because it is a GUI application rather than a Console application, the OS won't automatically create a Console window for it when it starts up. (So obviously functions like printf/scanf etc won't work.)

Insinuating answered 23/5, 2011 at 11:45 Comment(1)
Could you give me the link to the source code of how to do that?Resurrection
M
0

It is the calling process, which decides, whether you have a console.

The Windows Explorer creates one only for Console Applications, though e.g. planned tasks, which run with the user's account will always have one.

In that special case you need to combine preceding answers:

Create a non-Console Application and in addition your _tWinMain() function shall call FreeConsole() .

Morgenthaler answered 9/1, 2021 at 11:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.