Hide console window of Win32 console application
-
Dear All, Code project people has helped me a lot during my initial career by flooding me with useful solutions & valuable comments to my doubts. Thanks a lot for that. I have another small question. I am developing an application in which I am running an executable (the job of this exe is to move some files from one location to the other, etc.). This exe is built using "Win32 Console application" provided in Visual C++ Version 6.0 When I run the application, it calls my exe, and this running exe shows its console window and performs its required operation successfully and close downs decently. BUT I DONOT WANT THAT CONSOLE WINDOW TO BE DISPLAYED DURING MY APPLICATION. I cannot change the application since I don’t have the code for that, but I can easily do the same for my exe. So can any body please tell me how to hide the console window or console should not be displayed. Regards, Rohit Dhamija
-
Dear All, Code project people has helped me a lot during my initial career by flooding me with useful solutions & valuable comments to my doubts. Thanks a lot for that. I have another small question. I am developing an application in which I am running an executable (the job of this exe is to move some files from one location to the other, etc.). This exe is built using "Win32 Console application" provided in Visual C++ Version 6.0 When I run the application, it calls my exe, and this running exe shows its console window and performs its required operation successfully and close downs decently. BUT I DONOT WANT THAT CONSOLE WINDOW TO BE DISPLAYED DURING MY APPLICATION. I cannot change the application since I don’t have the code for that, but I can easily do the same for my exe. So can any body please tell me how to hide the console window or console should not be displayed. Regards, Rohit Dhamija
In VB, it would be simple:
Shell "foobar.exe", vbHide
modified on Monday, August 30, 2010 6:46 AM
-
In VB, it would be simple:
Shell "foobar.exe", vbHide
modified on Monday, August 30, 2010 6:46 AM
See the Console class on my website www.xfcpro.com, under the freestuff section for the full code, but here is a sample of what you need to do.
// Obtain the Handle to this applications Console Window // As we don't have the lovely GetConsoleWindow() function under Win9X // We make a copy of the Console Windows title, then change it to something unique, ( a GUID value ) // We then use this GUID to perform a FindWindow() ensuring we only locate the console for 'this' application HWND Console::GetConsoleWindow9X() { HWND hWndConsole = NULL; // The console window handle TCHAR szTempTitle[_MAX_PATH]; TCHAR szTempOldTitle[_MAX_PATH]; // This is used to store the Console Window's title temporarily // Get the title of the console window - this may not be unique if(GetConsoleTitle(szTempOldTitle, _MAX_PATH) > 0) { // Lets make the title 'unique', so that we can perform FindWindow()... // Created as a UNICODE string WCHAR szBuff[_MAX_PATH]; memset(&szBuff, 0, _MAX_PATH); GUID obGuid; CoCreateGuid(&obGuid); StringFromGUID2(obGuid, szBuff, _MAX_PATH); wsprintf(szTempTitle, "%ws", szBuff); SetConsoleTitle(szTempTitle); Sleep(50); // This gives enough time to ensure the console title is updated, else the FindWindow may fail // Ok, try to Find the window using a variety of class names as we don't really know what OS we are running on if((hWndConsole = FindWindow(_T("tty"), szTempTitle)) == NULL) if((hWndConsole = FindWindow(_T("ConsoleWindowClass"), szTempTitle)) == NULL) // Windows 2000 hWndConsole = FindWindow(NULL, szTempTitle); // Catch all SetConsoleTitle(szTempOldTitle); // Set the title back to it's original value } return hWndConsole; } // Show/Hide the console applications console window void Console::ShowConsoleWindow(bool bHide /* = false */) { HWND hWndConsole = NULL; #if(_WIN32_WINNT >= 0x0500) // Windows 2000 and later only hWndConsole = GetConsoleWindow(); #else hWndConsole = GetConsoleWindow9X(); #endif ShowWindow(hWndConsole, (bHide) ? SW_SHOW : SW_HIDE); iLastErrorCode_ = XFC_SUCCESS; }
regards, Dark Angel
-
See the Console class on my website www.xfcpro.com, under the freestuff section for the full code, but here is a sample of what you need to do.
// Obtain the Handle to this applications Console Window // As we don't have the lovely GetConsoleWindow() function under Win9X // We make a copy of the Console Windows title, then change it to something unique, ( a GUID value ) // We then use this GUID to perform a FindWindow() ensuring we only locate the console for 'this' application HWND Console::GetConsoleWindow9X() { HWND hWndConsole = NULL; // The console window handle TCHAR szTempTitle[_MAX_PATH]; TCHAR szTempOldTitle[_MAX_PATH]; // This is used to store the Console Window's title temporarily // Get the title of the console window - this may not be unique if(GetConsoleTitle(szTempOldTitle, _MAX_PATH) > 0) { // Lets make the title 'unique', so that we can perform FindWindow()... // Created as a UNICODE string WCHAR szBuff[_MAX_PATH]; memset(&szBuff, 0, _MAX_PATH); GUID obGuid; CoCreateGuid(&obGuid); StringFromGUID2(obGuid, szBuff, _MAX_PATH); wsprintf(szTempTitle, "%ws", szBuff); SetConsoleTitle(szTempTitle); Sleep(50); // This gives enough time to ensure the console title is updated, else the FindWindow may fail // Ok, try to Find the window using a variety of class names as we don't really know what OS we are running on if((hWndConsole = FindWindow(_T("tty"), szTempTitle)) == NULL) if((hWndConsole = FindWindow(_T("ConsoleWindowClass"), szTempTitle)) == NULL) // Windows 2000 hWndConsole = FindWindow(NULL, szTempTitle); // Catch all SetConsoleTitle(szTempOldTitle); // Set the title back to it's original value } return hWndConsole; } // Show/Hide the console applications console window void Console::ShowConsoleWindow(bool bHide /* = false */) { HWND hWndConsole = NULL; #if(_WIN32_WINNT >= 0x0500) // Windows 2000 and later only hWndConsole = GetConsoleWindow(); #else hWndConsole = GetConsoleWindow9X(); #endif ShowWindow(hWndConsole, (bHide) ? SW_SHOW : SW_HIDE); iLastErrorCode_ = XFC_SUCCESS; }
regards, Dark Angel
Thanks Dear, I am developing my application only for Windows2000 (no Win9x systems) So I there any simpler way out for my doubt ?? Thanks a lot. Regards, Rohit Dhamija
-
Thanks Dear, I am developing my application only for Windows2000 (no Win9x systems) So I there any simpler way out for my doubt ?? Thanks a lot. Regards, Rohit Dhamija
Hi Rohit, For Windows 2000 just add the following function to your code and call it at the start of your application.
// Show/Hide the console applications console window void ShowConsoleWindow(bool bHide /* = false */) { ShowWindow(GetConsoleWindow(), (bHide) ? SW_SHOW : SW_HIDE); }
Then to hide the console window, call:
ShowConsoleWindow(true);
and to show it again (if necessary):
ShowConsoleWindow(false);
best wishes, Dark Angel