当前位置:知识问问>生活百科>Windows下编写个终止指定进程的程序

Windows下编写个终止指定进程的程序

2023-02-16 14:28:15 编辑:join 浏览量:620

Windows下编写个终止指定进程的程序

在VC++6.0下测试通过 (注:是控制台应用程序) #include #include #include "windows.h" #include "Tlhelp32.h" using namespace std; int FindID(char* name) { HANDLE hProcessSnap = NULL; PROCESSENTRY32 pe32= {0}; hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hProcessSnap == (HANDLE)-1) { printf("\nCreateToolhelp32Snapshot() failed:%d",GetLastError()); return -1; } pe32.dwSize = sizeof(PROCESSENTRY32); if (Process32First(hProcessSnap, &pe32)) { char a[5]; do { itoa(pe32.th32ProcessID,a,10); if (!strcmp(pe32.szExeFile, name)) { CloseHandle (hProcessSnap); return pe32.th32ProcessID; } } while (Process32Next(hProcessSnap, &pe32)); } else { printf("\nProcess32Firstt() failed:%d",GetLastError()); } return -1; } BOOL SetPrivilege()//提升权限 { TOKEN_PRIVILEGES tkp; HANDLE hToken; if (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken)) return FALSE; LookupPrivilegeValue(NULL,SE_DEBUG_NAME,&tkp.Privileges[0].Luid); tkp.PrivilegeCount = 1; tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; AdjustTokenPrivileges(hToken,FALSE,&tkp,0,(PTOKEN_PRIVILEGES)NULL,0); return TRUE; } BOOL killps(DWORD id)//杀进程函数 { HANDLE hProcess=NULL,hProcessToken=NULL; BOOL IsKilled=FALSE,bRet=FALSE; __try { if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ALL_ACCESS,&hProcessToken)) { printf("Open Current Process Token failed:%d\n",GetLastError()); __leave; } printf("\nOpen Current Process Token ok!\n"); if(!SetPrivilege()) { __leave; } printf("SetPrivilege ok!\n"); if((hProcess=OpenProcess(PROCESS_ALL_ACCESS,FALSE,id))==NULL) { printf("Open Process %d failed:%d\n",id,GetLastError()); __leave; } printf("Open Process %d ok!\n",id); if(!TerminateProcess(hProcess,1)) { printf("TerminateProcess failed:%d\n",GetLastError()); __leave; } printf("TerminateProcess ok!\n"); IsKilled=TRUE; } __finally { if(hProcessToken!=NULL) CloseHandle(hProcessToken); if(hProcess!=NULL) CloseHandle(hProcess); } return(IsKilled); } int main() { char strname[256]; int id; cout<<"请输入进程全名:"; cin>>strname; id = FindID(strname); if (id == -1) cout<<"该进程不存在"<

标签:Windows

版权声明:文章由 知识问问 整理收集,来源于互联网或者用户投稿,如有侵权,请联系我们,我们会立即处理。如转载请保留本文链接:https://www.zhshwenwen.com/life/7741.html
热门文章