검색을 통하여 나온 코드들을 재 나름대로 짜집기(??) 한 코드 입니다.
함수 두개로 만들어져 있습니다.
사용법은
KillExeProcess("aaa.exe")
이런식으로 하시면 됩니다. 그럼 만수 무강 하시길....
#include
#include
#include
bool __fastcall KillExeProcess(AnsiString ExeName ){
DWORD dwPID;
HANDLE hSnapShot;
PROCESSENTRY32 pEntry;
HANDLE hToken = NULL;
HANDLE hProcess = NULL;
hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL,NULL);
pEntry.dwSize =sizeof(pEntry);
Process32First (hSnapShot,&pEntry);
ExeName = ExeName.LowerCase();
while(true){
BOOL hRes = Process32Next (hSnapShot,&pEntry);
if(hRes != TRUE)break;
if (ExeName == AnsiString(pEntry.szExeFile).LowerCase() ){
OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
SetPrivilege(hToken,"SeDebugPrivilege" /*SE_DEBUG_NAME*/, TRUE);
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | SYNCHRONIZE | PROCESS_TERMINATE,FALSE, pEntry.th32ProcessID);
if (hProcess != NULL)TerminateProcess( hProcess, 0 );
/* break; */ // 하나만 종료 할 경우 주석 삭제
}
}
if (hProcess != NULL) {
CloseHandle(hProcess);
hProcess = NULL;
}
if (hToken != NULL) {
SetPrivilege(hToken,"SeDebugPrivilege"/*SE_DEBUG_NAME*/, FALSE);
CloseHandle(hToken);
hToken = NULL;
}
if (hSnapShot != NULL) {
CloseHandle(hSnapShot);
hSnapShot = NULL;
}
return true;
}
bool __fastcall SetPrivilege(HANDLE hToken, const char* lpszPrivilege, int bEnablePrivilege){
TOKEN_PRIVILEGES tp = {0};
LUID luid = {0};
TOKEN_PRIVILEGES tpPrevious = {0};
unsigned long cbPrevious=sizeof(TOKEN_PRIVILEGES);
if (!LookupPrivilegeValueA( NULL, lpszPrivilege, &luid ))return false;
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, 0, &tp, sizeof(TOKEN_PRIVILEGES), &tpPrevious, &cbPrevious);
if (GetLastError() != ERROR_SUCCESS)return false;
tpPrevious.PrivilegeCount = 1;
tpPrevious.Privileges[0].Luid = luid;
if (bEnablePrivilege)tpPrevious.Privileges[0].Attributes |= (SE_PRIVILEGE_ENABLED);
else tpPrevious.Privileges[0].Attributes ^= (SE_PRIVILEGE_ENABLED &tpPrevious.Privileges[0].Attributes);
AdjustTokenPrivileges(hToken, FALSE, &tpPrevious, cbPrevious, NULL, NULL);
if (GetLastError() != ERROR_SUCCESS)return false;
return true;
}
|