首页 > 代码库 > C++结束进程 并能显示其父进程
C++结束进程 并能显示其父进程
声明:有些网友有可能在CSDN博客上看到过相同的文章,因为本人有两个账号。。。请不要误会,均为原创
这个程序功能强大哦~~
#include <cstdio> #include <windows.h> #include <winbase.h> #include <wtypes.h> #include <tlhelp32.h> #include <psapi.h> #include <tchar.h> #include <unistd.h> #include <sys/types.h> #define illegal_parameter_value 100000000 #define illegal_parameter_value2 100000001 #pragma comment(lib,"kernel32.lib") #pragma comment(lib,"advapi32.lib") using namespace std; bool compare(char a[],char b[]) { if (strlen(a)!=strlen(b)) return false; for (int i=0;i<strlen(a);i++) if (a[i]!=b[i] && (a[i]-‘a‘+‘A‘)!=b[i] && a[i]!=(b[i]-‘a‘+‘A‘)) return false; return true; } BOOL SetRealTimePriority() { if(!SetPriorityClass( GetCurrentProcess(), REALTIME_PRIORITY_CLASS )) return false; return true; } BOOL EnableShutdownPriv() { HANDLE hToken; LUID sedebugnameValue; TOKEN_PRIVILEGES tkp; if (!OpenProcessToken( GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) ) return false; if (!LookupPrivilegeValue( NULL, SE_SHUTDOWN_NAME, &sedebugnameValue ) ) { CloseHandle( hToken ); return false; } tkp.PrivilegeCount = 1; tkp.Privileges[0].Luid = sedebugnameValue; tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; if (!AdjustTokenPrivileges( hToken, FALSE, &tkp, sizeof tkp, NULL, NULL ) ) CloseHandle( hToken ); return true; } BOOL EnableDebugPriv() { HANDLE hToken; LUID sedebugnameValue; TOKEN_PRIVILEGES tkp; if ( ! OpenProcessToken( GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) ) return false; if ( ! LookupPrivilegeValue( NULL, SE_DEBUG_NAME, &sedebugnameValue ) ) { CloseHandle( hToken ); return false; } tkp.PrivilegeCount = 1; tkp.Privileges[0].Luid = sedebugnameValue; tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; if ( ! AdjustTokenPrivileges( hToken, FALSE, &tkp, sizeof tkp, NULL, NULL ) ) CloseHandle( hToken ); return true; } BOOL EnableBackupPriv() { HANDLE hToken; LUID sedebugnameValue; TOKEN_PRIVILEGES tkp; if ( ! OpenProcessToken( GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) ) return false; if ( ! LookupPrivilegeValue( NULL, SE_BACKUP_NAME, &sedebugnameValue ) ) { CloseHandle( hToken ); return false; } tkp.PrivilegeCount = 1; tkp.Privileges[0].Luid = sedebugnameValue; tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; if ( ! AdjustTokenPrivileges( hToken, FALSE, &tkp, sizeof tkp, NULL, NULL ) ) CloseHandle( hToken ); return true; } BOOL EnableRestorePriv() { HANDLE hToken; LUID sedebugnameValue; TOKEN_PRIVILEGES tkp; if ( ! OpenProcessToken( GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) ) return false; if ( ! LookupPrivilegeValue( NULL, SE_RESTORE_NAME, &sedebugnameValue ) ) { CloseHandle( hToken ); return false; } tkp.PrivilegeCount = 1; tkp.Privileges[0].Luid = sedebugnameValue; tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; if ( ! AdjustTokenPrivileges( hToken, FALSE, &tkp, sizeof tkp, NULL, NULL ) ) CloseHandle( hToken ); return true; } //------------------------------------------------get_Privileges------------------------------------------------------ DWORD GetProcessID(char *FileName) { HANDLE myhProcess; PROCESSENTRY32 mype; mype.dwSize = sizeof(PROCESSENTRY32); BOOL mybRet; //进行进程快照 myhProcess=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); //TH32CS_SNAPPROCESS快照所有进程 //开始进程查找 mybRet=Process32First(myhProcess,&mype); //循环比较,得出ProcessID while(mybRet) { if(compare(FileName,mype.szExeFile)) return mype.th32ProcessID; else mybRet=Process32Next(myhProcess,&mype); } return illegal_parameter_value; } DWORD GetP(DWORD id ) { HANDLE hProcessSnap; HANDLE hProcess; PROCESSENTRY32 pe32; DWORD dwPriorityClass; hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ); pe32.dwSize = sizeof( PROCESSENTRY32 ); do { dwPriorityClass = 0; hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID ); dwPriorityClass = GetPriorityClass( hProcess ); CloseHandle( hProcess ); if (pe32.th32ProcessID==id) return pe32.th32ParentProcessID; } while( Process32Next( hProcessSnap, &pe32 ) ); CloseHandle( hProcessSnap ); return 0; } int pskill(int id) //根据进程ID杀进程 { HANDLE hProcess=NULL; //打开目标进程 hProcess=OpenProcess(PROCESS_TERMINATE,FALSE,id); if (hProcess==NULL) { if (GetLastError()==5) printf("错误:无法终止PID %d (属于 PID %d 子进程的进程)。\n原因:拒绝访问 :Access is denied\n",id,GetP(id)); else if (GetLastError()==87) printf("错误:未找到进程 :Error to find process %d\n",id); else printf("错误:未知错误 :Unknown Error %d\n",GetLastError()); return illegal_parameter_value; } //结束目标进程 DWORD ret=TerminateProcess(hProcess,0); Sleep(100); if(!ret) { if (GetLastError()==5) printf("错误:无法终止PID %d (属于 PID %d 子进程的进程)。\n原因:拒绝访问 :Access is denied\n",id,GetP(id)); else if (GetLastError()==87) printf("错误:未找到进程 :Error to find process %d\n",id); else printf("错误:未知错误 :Unknown Error %d\n",GetLastError()); return illegal_parameter_value; } return 0; } bool check(char x[]) { for (int i=0;i<strlen(x);i++) if (x[i]<‘0‘ || x[i]>‘9‘) return true; return false; } int main() { system("Title ProcessKiller"); system("Mode con cols=87"); system("color 3F"); next: printf("Loading... Please wait....."); system("cls"); system("tasklist"); //杀进程 printf("the process‘s name/id which you want to kill:(直接输入,不区分大小写),键入‘re‘来刷新进程列表:\n"); char a[1000]; scanf("%s",a); if (strlen(a)==4 && a[0]==‘e‘ && a[1]==‘x‘ && a[2]==‘i‘ && a[3]==‘t‘) exit(0); if (compare(a,"re")) goto next; bool symbol = 1; if (check(a)) symbol=0; int id = 0; if (symbol) { int i=0; while (a[i]!=‘\0‘) { id=id*10+(a[i]-‘0‘); i++; } bool s1=SetRealTimePriority(); bool s2=EnableShutdownPriv(); bool s3=EnableDebugPriv(); bool s4=EnableBackupPriv(); bool s5=EnableRestorePriv(); if (s1==0 || s2==0 || s3==0 || s4==0 || s5==0) { printf("错误:ProcessKiller无法终止这个进程\n"); printf("原因:权限不足 Haven‘t got enough privilliges\n"); printf("请在UAC用户账户控制中更改权限,或与计算机管理员取得联系\n"); getchar();getchar(); exit(5); } int ppid=GetP(id); int f=pskill(id); if (f==illegal_parameter_value2 || f==illegal_parameter_value) {getchar();getchar();goto next;} else printf("成功:已终止 PID %d (属于 PID %d 子进程)的进程;\nSuccessful to terminate PID %d(the child process of PID %d)\n",id,ppid,id,ppid); getchar();getchar();goto next; } else { id=GetProcessID(a); if (id==illegal_parameter_value) { printf("错误:未找到进程 :Error to find proccess ‘%s‘\n",a); getchar();getchar();goto next; } while (id!=illegal_parameter_value) { bool s1=SetRealTimePriority(); bool s2=EnableShutdownPriv(); bool s3=EnableDebugPriv(); bool s4=EnableBackupPriv(); bool s5=EnableRestorePriv(); if (s1==0 || s2==0 || s3==0 || s4==0 || s5==0) { printf("错误:ProcessKiller无法终止这个进程\n"); printf("原因:权限不足 Haven‘t got enough privilliges\n"); printf("请在UAC用户账户控制中更改权限,或与计算机管理员取得联系\n"); getchar();getchar(); exit(5); } int ppid=GetP(id); int f=pskill(id); if (f==illegal_parameter_value2 || f==illegal_parameter_value) {getchar();getchar();goto next;} else printf("成功:已终止PID %d (属于 PID %d 子进程)的进程;\nSuccessful to terminate PID %d(the child process of PID %d)\n",id,ppid,id,ppid); id=GetProcessID(a); } getchar();getchar();goto next; } return 0; }
#include <bits/stdc++.h>#include <windows.h>#include <winbase.h>#include <wtypes.h>#include <tlhelp32.h>#include <psapi.h>#include <tchar.h>#include <unistd.h>#include <sys/types.h>#define illegal_parameter_value 100000000#define illegal_parameter_value2 100000001#pragma comment(lib,"kernel32.lib")#pragma comment(lib,"advapi32.lib") using namespace std; bool compare(char a[],char b[]) { if (strlen(a)!=strlen(b)) returnfalse; for (int i=0;i<strlen(a);i++) if (a[i]!=b[i] && (a[i]-‘a‘+‘A‘)!=b[i] && a[i]!=(b[i]-‘a‘+‘A‘)) returnfalse; returntrue; } BOOL SetRealTimePriority() { if(!SetPriorityClass( GetCurrentProcess(), REALTIME_PRIORITY_CLASS )) returnfalse; returntrue; } BOOL EnableShutdownPriv() { HANDLE hToken; LUID sedebugnameValue; TOKEN_PRIVILEGES tkp; if (!OpenProcessToken( GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) ) returnfalse; if (!LookupPrivilegeValue( NULL, SE_SHUTDOWN_NAME, &sedebugnameValue ) ) { CloseHandle( hToken ); returnfalse; } tkp.PrivilegeCount = 1; tkp.Privileges[0].Luid = sedebugnameValue; tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; if (!AdjustTokenPrivileges( hToken, FALSE, &tkp, sizeof tkp, NULL, NULL ) ) CloseHandle( hToken ); returntrue; } BOOL EnableDebugPriv() { HANDLE hToken; LUID sedebugnameValue; TOKEN_PRIVILEGES tkp; if ( ! OpenProcessToken( GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) ) returnfalse; if ( ! LookupPrivilegeValue( NULL, SE_DEBUG_NAME, &sedebugnameValue ) ) { CloseHandle( hToken ); returnfalse; } tkp.PrivilegeCount = 1; tkp.Privileges[0].Luid = sedebugnameValue; tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; if ( ! AdjustTokenPrivileges( hToken, FALSE, &tkp, sizeof tkp, NULL, NULL ) ) CloseHandle( hToken ); returntrue; } BOOL EnableBackupPriv() { HANDLE hToken; LUID sedebugnameValue; TOKEN_PRIVILEGES tkp; if ( ! OpenProcessToken( GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) ) returnfalse; if ( ! LookupPrivilegeValue( NULL, SE_BACKUP_NAME, &sedebugnameValue ) ) { CloseHandle( hToken ); returnfalse; } tkp.PrivilegeCount = 1; tkp.Privileges[0].Luid = sedebugnameValue; tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; if ( ! AdjustTokenPrivileges( hToken, FALSE, &tkp, sizeof tkp, NULL, NULL ) ) CloseHandle( hToken ); returntrue; } BOOL EnableRestorePriv() { HANDLE hToken; LUID sedebugnameValue; TOKEN_PRIVILEGES tkp; if ( ! OpenProcessToken( GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) ) returnfalse; if ( ! LookupPrivilegeValue( NULL, SE_RESTORE_NAME, &sedebugnameValue ) ) { CloseHandle( hToken ); returnfalse; } tkp.PrivilegeCount = 1; tkp.Privileges[0].Luid = sedebugnameValue; tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; if ( ! AdjustTokenPrivileges( hToken, FALSE, &tkp, sizeof tkp, NULL, NULL ) ) CloseHandle( hToken ); returntrue; } //------------------------------------------------get_Privileges------------------------------------------------------ DWORD GetProcessID(char *FileName) { HANDLE myhProcess; PROCESSENTRY32 mype; mype.dwSize = sizeof(PROCESSENTRY32); BOOL mybRet; //进行进程快照 myhProcess=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); //TH32CS_SNAPPROCESS快照所有进程//开始进程查找 mybRet=Process32First(myhProcess,&mype); //循环比较,得出ProcessIDwhile(mybRet) { if(compare(FileName,mype.szExeFile)) return mype.th32ProcessID; else mybRet=Process32Next(myhProcess,&mype); } return illegal_parameter_value; } DWORD GetP(DWORD id ) { HANDLE hProcessSnap; HANDLE hProcess; PROCESSENTRY32 pe32; DWORD dwPriorityClass; hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ); pe32.dwSize = sizeof( PROCESSENTRY32 ); do { dwPriorityClass = 0; hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID ); dwPriorityClass = GetPriorityClass( hProcess ); CloseHandle( hProcess ); if (pe32.th32ProcessID==id) return pe32.th32ParentProcessID; } while( Process32Next( hProcessSnap, &pe32 ) ); CloseHandle( hProcessSnap ); return0; } int pskill(intid) //根据进程ID杀进程 { HANDLE hProcess=NULL; //打开目标进程 hProcess=OpenProcess(PROCESS_TERMINATE,FALSE,id); if (hProcess==NULL) { if (GetLastError()==5) printf("错误:无法终止PID %d (属于 PID %d 子进程的进程)。\n原因:拒绝访问 :Access is denied\n",id,GetP(id)); elseif (GetLastError()==87) printf("错误:未找到进程 :Error to find process %d\n",id); else printf("错误:未知错误 :Unknown Error %d\n",GetLastError()); return illegal_parameter_value; } //结束目标进程 DWORD ret=TerminateProcess(hProcess,0); Sleep(100); if(!ret) { if (GetLastError()==5) printf("错误:无法终止PID %d (属于 PID %d 子进程的进程)。\n原因:拒绝访问 :Access is denied\n",id,GetP(id)); elseif (GetLastError()==87) printf("错误:未找到进程 :Error to find process %d\n",id); else printf("错误:未知错误 :Unknown Error %d\n",GetLastError()); return illegal_parameter_value; } return0; } bool check(char x[]) { for (int i=0;i<strlen(x);i++) if (x[i]<‘0‘ || x[i]>‘9‘) returntrue; returnfalse; } int main() { system("Title ProcessKiller"); system("Mode con cols=87"); system("color 3F"); next: printf("Loading... Please wait....."); system("cls"); system("tasklist"); //杀进程 printf("the process‘s name/id which you want to kill:(直接输入,不区分大小写),键入‘re‘来刷新进程列表:\n"); char a[1000]; scanf("%s",a); if (strlen(a)==4 && a[0]==‘e‘ && a[1]==‘x‘ && a[2]==‘i‘ && a[3]==‘t‘) exit(0); if (compare(a,"re")) goto next; bool symbol = 1; if (check(a)) symbol=0; intid = 0; if (symbol) { int i=0; while (a[i]!=‘\0‘) { id=id*10+(a[i]-‘0‘); i++; } bool s1=SetRealTimePriority(); bool s2=EnableShutdownPriv(); bool s3=EnableDebugPriv(); bool s4=EnableBackupPriv(); bool s5=EnableRestorePriv(); if (s1==0 || s2==0 || s3==0 || s4==0 || s5==0) { printf("错误:ProcessKiller无法终止这个进程\n"); printf("原因:权限不足 Haven‘t got enough privilliges\n"); printf("请在UAC用户账户控制中更改权限,或与计算机管理员取得联系\n"); getchar();getchar(); exit(5); } int ppid=GetP(id); int f=pskill(id); if (f==illegal_parameter_value2 || f==illegal_parameter_value) {getchar();getchar();goto next;} else printf("成功:已终止 PID %d (属于 PID %d 子进程)的进程;\nSuccessful to terminate PID %d(the child process of PID %d)\n",id,ppid,id,ppid); getchar();getchar();goto next; } else { id=GetProcessID(a); if (id==illegal_parameter_value) { printf("错误:未找到进程 :Error to find proccess ‘%s‘\n",a); getchar();getchar();goto next; } while (id!=illegal_parameter_value) { bool s1=SetRealTimePriority(); bool s2=EnableShutdownPriv(); bool s3=EnableDebugPriv(); bool s4=EnableBackupPriv(); bool s5=EnableRestorePriv(); if (s1==0 || s2==0 || s3==0 || s4==0 || s5==0) { printf("错误:ProcessKiller无法终止这个进程\n"); printf("原因:权限不足 Haven‘t got enough privilliges\n"); printf("请在UAC用户账户控制中更改权限,或与计算机管理员取得联系\n"); getchar();getchar(); exit(5); } int ppid=GetP(id); int f=pskill(id); if (f==illegal_parameter_value2 || f==illegal_parameter_value) {getchar();getchar();goto next;} else printf("成功:已终止PID %d (属于 PID %d 子进程)的进程;\nSuccessful to terminate PID %d(the child process of PID %d)\n",id,ppid,id,ppid); id=GetProcessID(a); } getchar();getchar();goto next; } return0; }
C++结束进程 并能显示其父进程