Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Hide console window of Win32 console application

Hide console window of Win32 console application

Scheduled Pinned Locked Moved C / C++ / MFC
c++careertutorialquestionannouncement
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rohit dhamija 0
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • R rohit dhamija 0

      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

      M Offline
      M Offline
      Marek Grzenkowicz
      wrote on last edited by
      #2

      In VB, it would be simple: Shell "foobar.exe", vbHide

      modified on Monday, August 30, 2010 6:46 AM

      J 1 Reply Last reply
      0
      • M Marek Grzenkowicz

        In VB, it would be simple: Shell "foobar.exe", vbHide

        modified on Monday, August 30, 2010 6:46 AM

        J Offline
        J Offline
        Jonathan Darka
        wrote on last edited by
        #3

        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

        R 1 Reply Last reply
        0
        • J Jonathan Darka

          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

          R Offline
          R Offline
          rohit dhamija 0
          wrote on last edited by
          #4

          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

          J 1 Reply Last reply
          0
          • R rohit dhamija 0

            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

            J Offline
            J Offline
            Jonathan Darka
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups