HowTo hide Console Window with WinAPI?

时光毁灭记忆、已成空白 提交于 2020-01-11 07:55:11

问题


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


回答1:


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




回答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.)



来源:https://stackoverflow.com/questions/6096500/howto-hide-console-window-with-winapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!