TerminateProcess结束进程(包括进程信息遍历)

#include<stdio.h>
#include <WINDOWS.H>
#include<Tlhelp32.h>
#include <string>      
#include <map>

using namespace std;

// 进程信息
map<DWORD, string> mapPrsInfo;

BOOL KillProcess(DWORD dwProcessId)  
{ 
    HANDLE hProcess= OpenProcess(PROCESS_ALL_ACCESS,FALSE,dwProcessId);  
    
    BOOL bKill= TerminateProcess(hProcess,0);  
    if(!bKill)  
    {  
        return FALSE; 
    } 

    return TRUE;
}

bool EnumProcess()  
{   
    //CreateToolhelp32Snapshot 
    //Process32Next 
    //Process32First  
    PROCESSENTRY32 pe32;  
    pe32.dwSize=sizeof(PROCESSENTRY32);  
    HANDLE hSnapshot= CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);  
    if(INVALID_HANDLE_VALUE==hSnapshot)  
    {
        return false;  
    }
    
    if(Process32First(hSnapshot,&pe32))  
    {  
        do   
        {  
            mapPrsInfo[pe32.th32ProcessID] = string(pe32.szExeFile);
            printf("ID:%d,Name:%s\n",pe32.th32ProcessID,pe32.szExeFile);  
        } while (Process32Next(hSnapshot,&pe32)); 
    }  

    return false;  
}  

bool EnablePri()  
{  
    //OpenProcessToken 
    //LookupPrivilegeValue  
    //AdjustTokenPrivileges()  
    HANDLE TokenHandle;  
    TOKEN_PRIVILEGES tkp;  
    tkp.PrivilegeCount=1;  
    
    BOOL bOpen= OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&TokenHandle);  
    if(!bOpen) 
    {
        return false; 
    }
    
    BOOL bLook= LookupPrivilegeValue(NULL,SE_SECURITY_NAME,&tkp.Privileges[0].Luid);  
    
    if(!bLook) 
    {
        return false; 
    }

    BOOL bAdjust= AdjustTokenPrivileges(TokenHandle,false,&tkp,sizeof(tkp),NULL,NULL);  
    
    return(GetLastError()==ERROR_SUCCESS);  
} 

int _tmain(int argc, _TCHAR* argv[])
{
    if(!EnablePri()) 
    {
        printf("EnablePri fail\n");  
        return 0;  
    }  

    if(EnumProcess())  
    {  
        printf("EnumProcess fail\n");  
        getchar();  
        return 0;  
    }  

    printf("InPut Process PID:"); 
    DWORD dwProcessId;  
    scanf("%d",&dwProcessId);  

    system("pause");

     return 0;
}

转自:http://blog.csdn.net/apxar/article/details/9898649

其他:http://blog.csdn.net/yangluoning/article/details/14647969

posted @ 2013-11-27 11:15  jdfemqi  阅读(1015)  评论(0编辑  收藏  举报