gpt4 book ai didi

delphi - 像 PsSuspend 一样暂停/恢复进程

转载 作者:行者123 更新时间:2023-12-03 14:39:42 27 4
gpt4 key购买 nike

我希望这篇文章不是重复的。让我解释一下:

我考虑过类似的帖子 How to pause / resume any external process under Windows?但偏好 C++/Python,但截至发帖时还没有公认的答案。

<小时/>

我的问题:

我对 PsSuspend 提供的功能在 Delphi 中的可能实现感兴趣作者:Windows SysinternalsMark Russinovich

引言:

PsSuspend lets you suspend processes on the local or a remote system, which is desirable in cases where a process is consuming a resource (e.g. network, CPU or disk) that you want to allow different processes to use. Rather than kill the process that's consuming the resource, suspending permits you to let it continue operation at some later point in time.

谢谢。

<小时/>

编辑:

部分实现即可。可以删除远程功能。

最佳答案

您可以尝试使用以下代码。它使用未记录的函数 NtSuspendProcessNtResumeProcess。我已经在 Windows 7 64 位上从 Delphi 2009 中构建的 32 位应用程序尝试过它,它对我有用。请注意,这些函数没有文档记录,因此可以从 Windows 的 future 版本中删除。

更新

以下代码中的 SuspendProcessResumeProcess 包装器现在是函数,如果成功则返回 True,否则返回 False。

type
NTSTATUS = LongInt;
TProcFunction = function(ProcHandle: THandle): NTSTATUS; stdcall;

const
STATUS_SUCCESS = $00000000;
PROCESS_SUSPEND_RESUME = $0800;

function SuspendProcess(const PID: DWORD): Boolean;
var
LibHandle: THandle;
ProcHandle: THandle;
NtSuspendProcess: TProcFunction;
begin
Result := False;
LibHandle := SafeLoadLibrary('ntdll.dll');
if LibHandle <> 0 then
try
@NtSuspendProcess := GetProcAddress(LibHandle, 'NtSuspendProcess');
if @NtSuspendProcess <> nil then
begin
ProcHandle := OpenProcess(PROCESS_SUSPEND_RESUME, False, PID);
if ProcHandle <> 0 then
try
Result := NtSuspendProcess(ProcHandle) = STATUS_SUCCESS;
finally
CloseHandle(ProcHandle);
end;
end;
finally
FreeLibrary(LibHandle);
end;
end;

function ResumeProcess(const PID: DWORD): Boolean;
var
LibHandle: THandle;
ProcHandle: THandle;
NtResumeProcess: TProcFunction;
begin
Result := False;
LibHandle := SafeLoadLibrary('ntdll.dll');
if LibHandle <> 0 then
try
@NtResumeProcess := GetProcAddress(LibHandle, 'NtResumeProcess');
if @NtResumeProcess <> nil then
begin
ProcHandle := OpenProcess(PROCESS_SUSPEND_RESUME, False, PID);
if ProcHandle <> 0 then
try
Result := NtResumeProcess(ProcHandle) = STATUS_SUCCESS;
finally
CloseHandle(ProcHandle);
end;
end;
finally
FreeLibrary(LibHandle);
end;
end;

关于delphi - 像 PsSuspend 一样暂停/恢复进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10153097/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com