日期:2011-12-28 13:28:00 来源:本站整理
vista/win7下一种完毕进程的方法[VC/C++编程]
本文“vista/win7下一种完毕进程的方法[VC/C++编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
//原版C代码貌似是Naylon写的,由于利用了vista/win7下独有的API,所以xp下不能用
program KillProc;
uses
type TZWGETNEXTPROCESS = function(hProcess: THandle; one, two, three: dword; next: pointer): dword; stdcall;
type TGETPROCESSID = function(hProcess: THandle): dword; stdcall;
function MiniFxOpenProcess(dwDesiredAccess: dword; bInheritHandle: dword; dwProcessId: dword): dword;
var
hCurrent, hNext, dwPid, Status: dword;
MyZwGetNextProcess: TZWGETNEXTPROCESS;
MyGetProcessId: TGETPROCESSID;
begin
MyZwGetNextProcess := GetProcAddress(LoadLibrary('ntdll.dll'), 'ZwGetNextProcess');
MyGetProcessId := GetProcAddress(LoadLibrary('kernel32.dll'), 'GetProcessId');
if @MyZwGetNextProcess = nil then messagebox(0, 'ZwGetNextProcess此API仅在Vista及WIN7下导出', 'aa', 0);
if @MyGetProcessId = nil then messagebox(0, 'GetProcessId此API是xp开始才有的', 'aa', 0);
Status := MyZwGetNextProcess(0, dwDesiredAccess, 0, 0, @hNext);
hCurrent := hNext;
if (Status >= 0) then
begin
while (hCurrent <> 0) do
begin
hCurrent := hNext;
dwPid := MyGetProcessId(hCurrent);
if (dwPid = dwProcessId) then
begin
result := hCurrent;
exit;
end;
Status := MyZwGetNextProcess(hCurrent, dwDesiredAccess, 0, 0, @hNext);
CloseHandle(hCurrent);
end;
end else result := 0;
end;
var
hProc: thandle;
pid: dword;
begin
pid := 3228;//要完毕的进程PID
hProc := MiniFxOpenProcess(PROCESS_ALL_ACCESS, 0, pid);
TerminateProcess(hProc, 0);
end.
以下是C++原版代码
//VC-ConsoleWithApi
#include <stdio.h>
typedef long (__stdcall *ZWGETNEXTPROCESS)(HANDLE,long,long,long,PHANDLE);
typedef long (__stdcall *ZWUNMAPVIEWOFSECTION)(HANDLE,PVOID);
typedef ULONG (__stdcall *GETPROCESSID)(HANDLE Process);
HANDLE MiniFxOpenProcess(ULONG dwDesiredAccess, ULONG bInheritHandle, ULONG dwProcessId)
{
ZWGETNEXTPROCESS ZwGetNextProcess=(ZWGETNEXTPROCESS)GetProcAddress(GetModuleHandleW(L"ntdll.dll"),"ZwGetNextProcess");
GETPROCESSID GetProcessId=(GETPROCESSID)GetProcAddress(GetModuleHandleW(L"kernel32.dll"),"GetProcessId");
HANDLE hCurrent=0, hNext=0;ULONG dwPid=0;long Status=0;
if((PVOID)ZwGetNextProcess == NULL || (PVOID)GetProcessId == NULL) return (HANDLE)0;
Status = ZwGetNextProcess(0, dwDesiredAccess, 0, 0, &hNext);
if (Status >= 0)
{
do{
hCurrent = hNext;
dwPid = GetProcessId(hCurrent);
if (dwPid==dwProcessId) return hCurrent;
Status = ZwGetNextProcess(hCurrent, dwDesiredAccess, 0, 0, &hNext);
CloseHandle(hCurrent);
}while(hCurrent != 0);
}
return (HANDLE)0;
}
int main()
{
ULONG pid=0;HANDLE hProc=0;
printf("Input PID: ");scanf("%ld",&pid);
hProc=MiniFxOpenProcess(PROCESS_ALL_ACCESS,0,pid);printf("ProcessHandle: %ld\n",hProc);
MessageBox(0,"aa","ff",0);
HMODULE hNtdll=GetModuleHandleW(L"ntdll.dll");
ZWUNMAPVIEWOFSECTION ZwUnmapViewOfSection=(ZWUNMAPVIEWOFSECTION)GetProcAddress(hNtdll,"ZwUnmapViewOfSection");
if(hProc!=0)
ZwUnmapViewOfSection(hProc,(PVOID)hNtdll);
return 0;
}
以上是“vista/win7下一种完毕进程的方法[VC/C++编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |
评论内容只代表网友观点,与本站立场无关!
评论摘要(共 0 条,得分 0 分,平均 0 分)
查看完整评论