Skip to content

How to get the caption of Console window..(Hiding console window)

Basu Biswas

Asked Apr 8, 2007, 11:51 PM EDT1 answer
Share
LinkedInX

Hi,

I want to hide the console window as i m running a windows service in background.

after searching a lot in google i found this code....

 

using System.Runtime.InteropServices;

 

...

      [DllImport("user32.dll")]

      public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);

 

      [DllImport("user32.dll")]

      static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

 

...

 

Java
         //Sometimes System.Windows.Forms.Application.ExecutablePath works for the caption depending on the system you are running under.
         IntPtr hWnd = FindWindow(null, "Your console windows caption"); //put your console window caption here
         if(hWnd != IntPtr.Zero)
         {
            //Hide the window

            ShowWindow(hWnd, 0); // 0 = SW_HIDE

Code
         }


         if(hWnd != IntPtr.Zero)
         {
            //Show window again
            ShowWindow(hWnd, 1); //1 = SW_SHOWNORMA
         }

In this code the line

  IntPtr hWnd = FindWindow(null, "Your console windows caption");

wants "Console window caption" ..from where i can get console window caption...

if possible do help...or else is there any other way to hide the console window as I am not sure where the above code gonna work ??

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    A cleaner solution to this is to avoid using Native Code FindWindow. Here is a C# Managed code alternative to FindWindow:

    http://www.mycsharpcorner.com/Post.aspx?postID=32

    You can also use it to close another window, and even get the window text.

    Hope this helps.

    0