按进程名称取消隐藏进程? [英] Unhide process by its process name?
问题描述
前段时间我写了一段代码来隐藏/恢复进程窗口,我做的是这样的:
Time ago I written a code to hide/restore a process window, what I did is this:
隐藏进程:
1) 在正在运行的进程中查找进程名称.
1) Find the process name in the running processes.
2) 将 MainWindowHandle 添加到容器(在本例中为字典),这对于稍后取消隐藏该进程是必要的.
2) Add the MainWindowHandle to a Container (a Dictionary in this case), this would be necessary to unhide that process later.
3) 使用 ShowWindow API 函数隐藏进程.
3) Hide the process using ShowWindow API function.
取消隐藏进程:
1) 在正在运行的进程中查找进程名称.
1) Find the process name in the running processes.
2) 从容器中检索保存的指定进程的 MainWindowHandle.
2) Retrieve the saved MainWindowHandle of the specified process from the container.
3) 使用 ShowWindow API 函数取消隐藏进程.
3) Unhide the process using ShowWindow API function.
为什么我使用字典来取消隐藏进程?嗯,因为隐藏进程的 MainWindowHandle
值为零 0
,所以这是我唯一的方法发现检索正确的句柄以在 ShowWindow
函数中使用以恢复进程.
Why I use a dictionary to unhide the process?, well, because a Hidden process have a MainWindowHandle
value of Zero 0
, so that is the only way that I found to retrieve the proper handle to use in the ShowWindow
function to restore the process.
但我真的不想依赖于在隐藏进程之前保存所需 HWND 的 Hide
方法,我想通过知道如何改进这一切在 VB.NET 或 C# 中执行取消隐藏操作仅通过指定进程名称(例如:cmd.exe)而不在 <之前保存code>MainWindowHandle,这有可能吗?
But I really don't want to depend on the Hide
method that saves the required HWND before hidding the process, I would like to improve all this by knowing how to perform an unhide operation in VB.NET or C# only by specifying the process name (eg: cmd.exe) without saving before the MainWindowHandle
, this is possible to do?
我展示了代码(在 VB.NET 中),让您了解我为 HideProcess 方法所做的工作:
I show the code (in VB.NET) to give you an idea of what I did for the HideProcess method:
但请注意,此代码与问题并不完全相关,我的问题是如何仅通过指定进程名称来取消隐藏隐藏进程,以避免下面编写的代码需要检索保存句柄以取消隐藏进程.
But please note that this code is not fully relevant in the question, my question is how to unhide a hidden process only by specifying the process name to avoid the code written below that needs to retrieve a saved handle to unhide a process.
' Hide-Unhide Process
'
' Usage Examples :
'
' HideProcess(Process.GetCurrentProcess().MainModule.ModuleName)
' HideProcess("notepad.exe", Recursivity:=False)
' HideProcess("notepad", Recursivity:=True)
'
' UnhideProcess(Process.GetCurrentProcess().MainModule.ModuleName)
' UnhideProcess("notepad.exe", Recursivity:=False)
' UnhideProcess("notepad", Recursivity:=True)
Private ProcessHandles As New Dictionary(Of String, IntPtr)
<System.Runtime.InteropServices.DllImport("User32")>
Private Shared Function ShowWindow(ByVal hwnd As IntPtr, ByVal nCmdShow As Integer) As Integer
End Function
Private Sub HideProcess(ByVal ProcessName As String, Optional ByVal Recursivity As Boolean = False)
If ProcessName.EndsWith(".exe", StringComparison.OrdinalIgnoreCase) Then
ProcessName = ProcessName.Remove(ProcessName.Length - ".exe".Length)
End If
Dim Processes() As Process = Process.GetProcessesByName(ProcessName)
Select Case Recursivity
Case True
For Each p As Process In Processes
ProcessHandles.Add(String.Format("{0};{1}", ProcessName, CStr(p.Handle)), p.MainWindowHandle)
ShowWindow(p.MainWindowHandle, 0)
Next p
Case Else
If Not (Processes.Count = 0) AndAlso Not (Processes(0).MainWindowHandle = 0) Then
Dim p As Process = Processes(0)
ProcessHandles.Add(String.Format("{0};{1}", ProcessName, CStr(p.Handle)), p.MainWindowHandle)
ShowWindow(p.MainWindowHandle, 0)
End If
End Select
End Sub
Private Sub UnhideProcess(ByVal ProcessName As String, Optional ByVal Recursivity As Boolean = False)
If ProcessName.EndsWith(".exe", StringComparison.OrdinalIgnoreCase) Then
ProcessName = ProcessName.Remove(ProcessName.Length - ".exe".Length)
End If
Dim TempHandles As New Dictionary(Of String, IntPtr)
For Each Handle As KeyValuePair(Of String, IntPtr) In ProcessHandles
TempHandles.Add(Handle.Key, Handle.Value)
Next Handle
For Each Handle As KeyValuePair(Of String, IntPtr) In TempHandles
If Handle.Key.ToLower.Contains(ProcessName.ToLower) Then
ShowWindow(Handle.Value, 9)
ProcessHandles.Remove(Handle.Key)
If Recursivity Then
Exit For
End If
End If
Next Handle
End Sub
推荐答案
代码:
using System.Diagnostics;
using System.Runtime.InteropServices;
[DllImport("User32")]
private static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("User32.dll")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string strClassName, string strWindowName);
[DllImport("user32.dll")]
private static extern int GetWindowThreadProcessId(IntPtr hWnd, out int ProcessId);
private const int SW_RESTORE = 9;
private void UnhideProcess(string processName) //Unhide Process
{
IntPtr handle = IntPtr.Zero;
int prcsId = 0;
//an array of all processes with name "processName"
Process[] localAll = Process.GetProcessesByName(processName);
//check all open windows (not only the process we are looking) begining from the
//child of the desktop, handle = IntPtr.Zero initialy.
do
{
//get child handle of window who's handle is "handle".
handle = FindWindowEx(IntPtr.Zero, handle, null, null);
GetWindowThreadProcessId(handle, out prcsId); //get ProcessId from "handle"
//if it matches what we are looking
if (prcsId == localAll[0].Id)
{
ShowWindow(handle, SW_RESTORE); //Show Window
return;
}
} while (handle != IntPtr.Zero);
}
如果有更多同名的实例,你可以使用一个变量,例如 count 并递增它在 if 语句
If there are more instances with the same name you can use a variable eg count and increment it in the if statement
int count = 0;
if (prcsId == localAll[count].Id)
{
ShowWindow(handle, SW_RESTORE);
count++;
}
FindWindowEx() 和 Process.MainWindowHandle() 之间的区别可能在于每个函数的位置正在寻找把手.FindWindowEx() 与 MainWindowHandle 不同,它到处寻找.此外,进程句柄被称为HANDLE,窗口被称为HWND
The difference between FindWindowEx() and Process.MainWindowHandle() maybe is where each function is looking for handles. FindWindowEx() is looking everywhere unlike MainWindowHandle. Also a process handle is reffered as HANDLE and a window one as HWND
这篇关于按进程名称取消隐藏进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!