使用SetPriorityClass让进程退出之后删除自己

来源:互联网 发布:淘宝哪家店铺零食好吃 编辑:程序博客网 时间:2024/04/30 08:16
VOIDDeleteMyself(){TCHARtchCmd[MAX_PATH]= {0};TCHARtchProcPath[MAX_PATH]= {0};STARTUPINFOStartupInfo= {0};PROCESS_INFORMATIONProcInfo= {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 ;}

0 0
原创粉丝点击