A detailed introduction to a small program for deleting processes

高洛峰
Release: 2017-03-28 11:58:41
Original
1989 people have browsed it

When I looked at the windows api, as an exercise, I wrote a program to delete the process. I think there are quite a lot of knowledge points in it, so I posted it as a note. Of course, there are bound to be errors, and any advice is welcome ^^
The function for adjusting permissions mentioned in the article was also used, so I copied it directly from the shutdown program. The following is the program, with comments in key places.

看windows api的时候,作为练习,编写了个删除进程的程序。觉得里面知识点挺多的,所以贴上来当个笔记用。当然难免有错误,欢迎指教 ^^    
其中也用到了上篇文章里面说到的调整权限的函数,于是就直接从关机程序里面copy过来了。下面是程序,关键地方给了注释。
#include<stdio.h>
#include<windows.h>
#include<Tlhelp32.h>

void ListProcess()                        //列出进程名称及ID
{
    HANDLE hProcessSnap=NULL;
    PROCESSENTRY32 pe32={0};            //存放进程信息的结构体
    hProcessSnap=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);        //创建系统所有进程的快照
    if(hProcessSnap==INVALID_HANDLE_VALUE)
    {
        printf("CreateToolhelp32Snapshot failed: %d\n",GetLastError());
        return;
    }
    pe32.dwSize=sizeof(PROCESSENTRY32);
    printf("ProcessName            ProcessID\n");
    if(Process32First(hProcessSnap,&pe32))                //指向第一个进程,并将其放入PROCESSENTRY32结构体中
    {
        char c[5];
        do
        {
            itoa(pe32.th32ProcessID,c,10);
            printf("%-30s%d\n",pe32.szExeFile,pe32.th32ProcessID);        //szExeFile为进程的可执行文件名称
        }
        while(Process32Next(hProcessSnap,&pe32));
    }
    else
    {
        printf("Process32First() failed:%d\n",GetLastError());
    }
    CloseHandle(hProcessSnap);
    return;
}

BOOL EnablePrivilege()                //获取系统权限
{
    HANDLE hProcess = NULL;
    HANDLE hToken = NULL;
    LUID uID = {0};
    TOKEN_PRIVILEGES stToken_Privileges = {0};
    hProcess = GetCurrentProcess();                                    //获取当前应用程序进程句柄
    if(!OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES,&hToken))    //打开当前进程的访问令牌句柄
                                                                    //(OpenProcessToken函数调用失败返回值为零)
        return FALSE;
    if(!LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&uID))        //获取权限名称为"SeShutdownPrivilege"的LUID
                                                                //LookupPrivilegeValue函数调用失败返回值为零
        return FALSE;
    stToken_Privileges.PrivilegeCount = 1;                            //欲调整的权限个数
    stToken_Privileges.Privileges[0].Luid = uID;                    //权限的LUID
    stToken_Privileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;        //权限的属性,
                                                                            //SE_PRIVILEGE_ENABLED为使能该权限
    //调整访问令牌里的指定权限(AdjustTokenPrivileges函数调用失败返回值为零)
    if(!AdjustTokenPrivileges(hToken,FALSE,&stToken_Privileges,sizeof stToken_Privileges,NULL,NULL))
        return FALSE;
    if(GetLastError() != ERROR_SUCCESS)                                //查看权限是否调整成功
        return FALSE;
    CloseHandle(hToken);
    return TRUE;
}

bool KillProcess(DWORD id)
{
    HANDLE hProcess=NULL,hProcessToken=NULL;
    bool isKilled=false,bRet=false;

    EnablePrivilege();                    //调整权限
    printf("Enable Privilege OK!\n");
    if((hProcess=OpenProcess(PROCESS_ALL_ACCESS,FALSE,id))==NULL)    //打开进程获得句柄
    {
        printf("Open process %d failed: %d\n",id,GetLastError());
        return false;
    }
    if(!TerminateProcess(hProcess,1))                                //终结进程
    {
        printf("TerminateProcess failed: %d\n",GetLastError());
        return false;
    }
    isKilled=true;
    if(hProcessToken!=NULL)
        CloseHandle(hProcessToken);
    if(hProcess!=NULL)
        CloseHandle(hProcess);
    return isKilled;
}

void main()
{
    int id=0;
    ListProcess();
    while(1)
    {
        printf("选择要删除的进程ID:");
        scanf("%d",&id);
        if(KillProcess(id)==true)
        {
            system("cls");
            ListProcess();
        }
        else
            printf("Failed!!");
    }
}
Copy after login

The above is the detailed content of A detailed introduction to a small program for deleting processes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!