我在各种远程服务器上启动进程,有时我需要暂停这些进程。
使用下面的代码,我可以在我的工作站上干净地挂起,然后按.exe名称恢复任何进程。
但是,在远程服务器上尝试查看进程时,出现“访问被拒绝”的信息:
var processes = Process.GetProcesses()
.Where(p => (p.MainWindowHandle.ToInt32() != 0) && p.MainModule.ModuleName.Equals(exeFile))
.ToArray();
GetProcesses()正确返回,但是我无法访问“ MainModule”
电话是
ProcessInfo.SuspendAll("MyAppName.exe");
我已阅读有关32/64版本的问题,但工作站上的设置与服务器上的设置相同。
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Utils.ProcessInfo
{
class ProcessInfo
{
public static void SuspendAll(string exeFile)
{
var processes = Process.GetProcesses()
.Where(p => (p.MainWindowHandle.ToInt32() != 0) && p.MainModule.ModuleName.Equals(exeFile))
.ToArray();
foreach (var process in processes)
{
process.Suspend();
}
}
public static void ResumeAll(string exeFile)
{
var processes = Process.GetProcesses()
.Where(p => (p.MainWindowHandle.ToInt32() != 0) && p.MainModule.ModuleName.Equals(exeFile))
.ToArray();
foreach (var process in processes)
{
if(process.Threads[0].WaitReason.ToString().Equals("Suspended"))
process.Resume();
}
}
}
public static class ProcessExtension
{
[Flags]
public enum ThreadAccess : int
{
TERMINATE = (0x0001),
SUSPEND_RESUME = (0x0002),
GET_CONTEXT = (0x0008),
SET_CONTEXT = (0x0010),
SET_INFORMATION = (0x0020),
QUERY_INFORMATION = (0x0040),
SET_THREAD_TOKEN = (0x0080),
IMPERSONATE = (0x0100),
DIRECT_IMPERSONATION = (0x0200)
}
[DllImport("kernel32.dll")]
static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId);
[DllImport("kernel32.dll")]
static extern uint SuspendThread(IntPtr hThread);
[DllImport("kernel32.dll")]
static extern int ResumeThread(IntPtr hThread);
public static void Suspend(this Process process)
{
foreach (ProcessThread thread in process.Threads)
{
var pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)thread.Id);
if (pOpenThread == IntPtr.Zero)
{
break;
}
SuspendThread(pOpenThread);
}
}
public static void Resume(this Process process)
{
foreach (ProcessThread thread in process.Threads)
{
var pOpenThread = OpenThread(ThreadAccess.SUSPEND_RESUME, false, (uint)thread.Id);
if (pOpenThread == IntPtr.Zero)
{
break;
}
ResumeThread(pOpenThread);
}
}
}
}