当前位置:七道奇文章资讯编程技术VC/C++编程
日期:2011-12-28 13:28:00  来源:本站整理

vista/win7下一种完毕进程的方法[VC/C++编程]

赞助商链接



  本文“vista/win7下一种完毕进程的方法[VC/C++编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
//原版C代码貌似是Naylon写的,由于利用了vista/win7下独有的API,所以xp下不能用
 
program KillProc;
 
uses
  Windows,Sysutils;
 
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>
#include <windows.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++编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • win7和vista操作系统关闭用户账户掌握的办法
  • 在Win7/Vista下因为Flash 11.3招致Firefox崩溃问题办理办法
  • Windows Vista系统下更改"我的文档"的位置办法
  • Win Vista系统启动时间设置问题的最佳办理筹划
  • vista系统开机后直接进安全情势的办理办法
  • 把Windows Server 2008降级成Vista
  • Windows 7硬盘读写速度完胜Vista系统
  • Win7里Vista没有的十八个特点功效
  • Windows7和Vista多余更新提醒排除
  • Windows 7 RC、Vista基准测试比较
  • 强化Vista和Windows 7系统安全性
  • Windows7/XP/Vista局域网的驱动器同享
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .