VOID
DeleteMyself()
{
TCHAR tchCmd[MAX_PATH] = {0};
TCHAR tchProcPath[MAX_PATH] = {0};
STARTUPINFO StartupInfo = {0};
PROCESS_INFORMATION ProcInfo = {0};
__try
{
if (!GetEnvironmentVariable(_T("COMSPEC"), tchCmd, MAX_PATH))
{
printf("[DeleteMyself] : GetEnvironmentVariable failed. (%d) \n", GetLastError());
__leave;
}
if (!GetModuleFileName(NULL, tchProcPath, MAX_PATH))
{
printf("[DeleteMyself] : GetModuleFileName failed. (%d) \n", GetLastError());
__leave;
}
_tcscat_s(tchCmd, MAX_PATH, _T(" /c del \""));
_tcscat_s(tchCmd, MAX_PATH, tchProcPath);
_tcscat_s(tchCmd, MAX_PATH, _T("\""));
// 设置本程序进程的执行级别为实时执行,这本程序马上获取CPU执行权,快速退出。
if (!SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS))
{
printf("[DeleteMyself] : SetPriorityClass failed. (%d) \n", GetLastError());
__leave;
}
// if (!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL))
// {
// printf("[DeleteMyself] : SetThreadPriority failed. (%d) \n", GetLastError());
// __leave;
// }
StartupInfo.cb = sizeof(STARTUPINFO);
StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow = SW_HIDE;
if (!CreateProcess(NULL, tchCmd, NULL, NULL, FALSE, CREATE_SUSPENDED | CREATE_NO_WINDOW | DETACHED_PROCESS, NULL, NULL, &StartupInfo, &ProcInfo))
{
printf("[DeleteMyself] : CreateProcess failed. (%d) \n", GetLastError());
__leave;
}
// 设置命令行进程的执行级别为空闲执行,这使本程序有足够的时间从内存中退出。
if (!SetPriorityClass(ProcInfo.hProcess, IDLE_PRIORITY_CLASS))
{
printf("[DeleteMyself] : SetPriorityClass failed. (%d) \n", GetLastError());
__leave;
}
// if (!SetThreadPriority(ProcInfo.hThread, THREAD_PRIORITY_IDLE))
// {
// printf("[DeleteMyself] : SetThreadPriority failed. (%d) \n", GetLastError());
// __leave;
// }
if (ResumeThread(ProcInfo.hThread) == -1)
{
printf("[DeleteMyself] : ResumeThread failed. (%d) \n", GetLastError());
__leave;
}
}
__finally
{
if (ProcInfo.hProcess)
CloseHandle(ProcInfo.hProcess);
if (ProcInfo.hThread)
CloseHandle(ProcInfo.hThread);
}
ExitProcess(ERROR_SUCCESS);
return ;
}
使用SetPriorityClass让进程退出之后删除自己
最新推荐文章于 2024-06-23 09:12:28 发布