C言語がプロセスを殺す
1771 ワード
注:他人のコードを転載して、とても使いやすくて、とても感謝します!!
#include
#include
#pragma comment(lib,"Psapi.lib")
#include
#include
DWORD FindProcess(char *strProcessName)
{
DWORD aProcesses[1024], cbNeeded, cbMNeeded;
HMODULE hMods[1024];
HANDLE hProcess;
char szProcessName[MAX_PATH];
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ) return 0;
for(int i=0; i< (int) (cbNeeded / sizeof(DWORD)); i++)
{
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, aProcesses[i]);
EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbMNeeded);
GetModuleFileNameEx( hProcess, hMods[0], szProcessName,sizeof(szProcessName));
if(strstr(szProcessName, strProcessName))
{
return(aProcesses[i]);
}
}
return 0;
}
DWORD KillProcess(char *strProcessName)
{
HANDLE TargetProcess = OpenProcess(PROCESS_TERMINATE, FALSE, FindProcess(strProcessName));
if(TargetProcess == NULL)
{
return 0;
}
TerminateProcess(TargetProcess, 0);
CloseHandle(TargetProcess);
return 1;
}
int main(int argc, char *argv[])
{
if (argc > 2)
{
if (strcmp(argv[1], "getpid") == 0)
{
DWORD dwRet = FindProcess(argv[2]);
if (dwRet != 0)
{
printf("find success
");
return 0;
}
else
{
printf("find failed
");
return 0;
}
}
else if (strcmp(argv[1], "kill") == 0)
{
DWORD dwRet = KillProcess(argv[2]);
if (dwRet != 0)
{
printf("kill success
");
return 0;
}
else
{
printf("kill faild
");
return 0;
}
}
}
else
{
printf("
");
return 0;
}
}
// : Psapi.lib