Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# ShowWindow (hWnd, 0) not hiding window

When I compile the below code into an executable and run it, the console windows appears. From the texts that I've read ShowWindow(hWnd,0) should hide the console window, but it doesn't.

This is the below code:

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
using System.Threading;

namespace Foreground {
  class GetForegroundWindowTest {

    /// Foreground dll's
    [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
    public static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
    public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

    /// Console hide dll's
    [DllImport("kernel32.dll")]
    static extern IntPtr GetConsoleWindow();

    [DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    const int SW_HIDE = 0;

    public static void Main(string[] args){
        while (true){
            IntPtr fg = GetForegroundWindow(); //use fg for some purpose

            var bufferSize = 1000;
            var sb = new StringBuilder(bufferSize);

            GetWindowText(fg, sb, bufferSize);

            using (StreamWriter sw = File.AppendText("C:\\Office Viewer\\OV_Log.txt")) 
            {
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss,") + sb.ToString());
            }

            var handle = GetConsoleWindow();
            Console.WriteLine(handle);
            ShowWindow(handle, SW_HIDE);

            Thread.Sleep(5000);
        }
    }
  }
}

Can someone explain to me where the problem in the code is?

Console.WriteLine(handle) was a line put in to show me and you that the program is grabbing the handle, but it just isn't minimising the window to which the handle represents.

Please note: I'd refer a code based answer as opposed to an "alter IDE settings" answer.

like image 899
Phoenix Avatar asked Oct 15 '25 04:10

Phoenix


1 Answers

Took a while to figure it out but here's the working code.

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
using System.Threading;

namespace Foreground {
  class GetForegroundWindowTest {

    /// Foreground dll's
    [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
    public static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
    public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

    [DllImport("kernel32.dll")]
    public static extern bool FreeConsole();

    /// Console hide dll's
    [DllImport("kernel32.dll")]
    static extern IntPtr GetConsoleWindow();

    [DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    const int SW_HIDE = 0;

    public static void Main(string[] args){
        while (true){
            IntPtr fg = GetForegroundWindow(); //use fg for some purpose

            var bufferSize = 1000;
            var sb = new StringBuilder(bufferSize);

            GetWindowText(fg, sb, bufferSize);

            using (StreamWriter sw = File.AppendText("C:\\Office Viewer\\OV_Log.txt")) 
            {
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss,") + sb.ToString());
            }

            var handle = GetConsoleWindow();
            Console.WriteLine(handle);
            ShowWindow(handle, SW_HIDE);

            Thread.Sleep(5000);
        }
    }
  }
}
like image 120
Phoenix Avatar answered Oct 17 '25 16:10

Phoenix