[C#] 寫一個隱藏其它程序的小程式

C#程式語言

前言

上篇為了挖礦而介紹了如何寫出一個阻擋使用者關機的程式

今天,打算把如何寫一個隱藏其它程序的程式也補上

我原本在挖礦的時候其實是用HiddeX去把挖礦程式的視窗隱藏啦

但想說既然上一篇的阻擋關機是用C#寫的,不然我也把HiddeX的功能加進去,就可以省一個exe比較方便

但這樣文章寫起來會很亂,所以在文章內我還是分開來寫:))

完成後的功能示範:

 

程式實作

首先,開好一個新的視窗程式專案,並且新增好

一個Textbox,取名為textBox_windowName
一個Button,取名為button_hide
一個Button,取名為button_show

接著切換到程式碼頁

因為要抓取其它程序的名稱,會調用user32.dll裡面的FindWindow,所以我們要先在程式碼裡面告訴電腦FindWindow在那裡被定義實作過

[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

然後等等也會用到ShowWindow,加上去

[DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);

由於剛剛我們用到動態呼叫dll的功能,會用到System.Runtime.InteropServices,因此要在最上面補上

using System.Runtime.InteropServices;

當按下hide後,再按下show,這時由於程式已經隱藏了,所以如果再去FindWindow會抓不到

解決的方法就是在class內設一個IntPtr的參考,當按下hide時有抓到視窗就將參考指到它,這樣要show時就不會找不到視窗

IntPtr targetWindow = IntPtr.Zero;

到這邊,事前的準備大致完成了,現在我們要來寫按下按鈕觸發的事件

先是hide

private void button_hide_Click(object sender, EventArgs e)
{
	targetWindow = FindWindow(null, textBox_windowName.Text);
	if (targetWindow != IntPtr.Zero)
	{
		ShowWindow(targetWindow, 0);
		textBox_windowName.Enabled = false;
	}
	else
		MessageBox.Show("Window Not Found");
}

上面的textBox_windowName.Text就只是從textBox抓視窗名稱,為了求方便我自己在用時是寫成hard code

再來是show

private void button_show_Click(object sender, EventArgs e)
{
	if (targetWindow != IntPtr.Zero)
	{
		ShowWindow(targetWindow, 1);
		textBox_windowName.Enabled = true;
	}
	else
		MessageBox.Show("Window Not Found");
}

然後編譯執行就可以看到結果了。
 

附上完整程式碼

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace HideWindow
{
	public partial class HideWindow : Form
	{
		[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
		public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
		[DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
		static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);

		IntPtr targetWindow = IntPtr.Zero;

		public HideWindow()
		{
			InitializeComponent();
		}

		private void button_hide_Click(object sender, EventArgs e)
		{
			targetWindow = FindWindow(null, textBox_windowName.Text);
			if (targetWindow != IntPtr.Zero)
			{
				ShowWindow(targetWindow, 0);
				textBox_windowName.Enabled = false;
			}
			else
				MessageBox.Show("Window Not Found");
		}

		private void button_show_Click(object sender, EventArgs e)
		{
			if (targetWindow != IntPtr.Zero)
			{
				ShowWindow(targetWindow, 1);
				textBox_windowName.Enabled = true;
			}
			else
				MessageBox.Show("Window Not Found");
		}
	}
}

 

附註

FindWindow有兩個參數

第一個是要隱藏的程序的名稱,例如記事本就是”notepad”(省略.exe)

第二個是要隱藏的程序的視窗名稱,例如新開的一個notepad在未存檔之前是”未命名 – 記事本”

視要隱藏的程式哪個比較比較好給就餵那個引數,另一個引數給null即可。
 

ShowWindow的第二個參數可以接收的值:

值 功能
00 HIDE
01 SHOW NORMAL
02 SHOW MINIMIZED
03 SHOW MAXIMIZED
04 SHOW NOACTIVATE
05 SHOW
06 MINIMIZE
07 SHOW MINNOACTIVE
08 SHOW NA
09 RESTORE
10 SHOW DEFAULT
11 FORCE MINIMIZE

8 Comments

      1. 可以讓程式在工作管理員的applications內隱藏
        但processes還是找得到

  1. 網路上搜尋很多其他的方法都介紹的很複雜,您的範例簡單明瞭,感謝分享。

發佈回覆給「Yz」的留言 取消回覆

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *