以下程序将exe文件和它所在的目录都删除掉
主要是在程序运行当中生成一个批处理文件(bat),通过批处理将exe删除,
删除后再将bat也删除.
涉及线程操作
#define DELUNSETUPBAT "temp.bat"STARTUPINFO si;
PROCESS_INFORMATION pi;char szSourcePathName[MAX_PATH] = "";
char szBatFile[MAX_PATH];
DWORD dwNumByte; //number of byte // get module file name
GetModuleFileName(NULL, szSourcePathName, _MAX_PATH);
*(strrchr(szSourcePathName, '\\')) = '\0';
HANDLE hFile = CreateFile(DELUNSETUPBAT,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL,
NULL);
// Construct the lines for the batch file.
wsprintf(szBatFile,
__TEXT(":Repeat\r\n")
__TEXT("rmdir /Q /S \"%s\"\r\n")
__TEXT("if exist \"%s\" goto Repeat\r\n")
__TEXT("del \"%s\"\r\n"),
szSourcePathName,
szSourcePathName,
DELUNSETUPBAT);
// Write the batch file and close it.
WriteFile(hFile, szBatFile, lstrlen(szBatFile) * sizeof(char),
&dwNumByte, NULL);
CloseHandle(hFile); // Get ready to spawn the batch file we just created.
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si); // We want its console window to be invisible to the user.
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE; // Spawn the batch file with low-priority and suspended.
if (CreateProcess(NULL, DELUNSETUPBAT, NULL, NULL, FALSE,
CREATE_SUSPENDED | IDLE_PRIORITY_CLASS, NULL,
__TEXT("\\"), &si, &pi)) { // Lower the batch file's priority even more.
SetThreadPriority(pi.hThread, THREAD_PRIORITY_IDLE); // Raise our priority so that we terminate as quickly as possible.
SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_TIME_CRITICAL);
SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); // Allow the batch file to run and clean-up our handles.
CloseHandle(pi.hProcess);
ResumeThread(pi.hThread);
// We want to terminate right away now so that we can be deleted
CloseHandle(pi.hThread);}
主要是在程序运行当中生成一个批处理文件(bat),通过批处理将exe删除,
删除后再将bat也删除.
涉及线程操作
#define DELUNSETUPBAT "temp.bat"STARTUPINFO si;
PROCESS_INFORMATION pi;char szSourcePathName[MAX_PATH] = "";
char szBatFile[MAX_PATH];
DWORD dwNumByte; //number of byte // get module file name
GetModuleFileName(NULL, szSourcePathName, _MAX_PATH);
*(strrchr(szSourcePathName, '\\')) = '\0';
HANDLE hFile = CreateFile(DELUNSETUPBAT,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL,
NULL);
// Construct the lines for the batch file.
wsprintf(szBatFile,
__TEXT(":Repeat\r\n")
__TEXT("rmdir /Q /S \"%s\"\r\n")
__TEXT("if exist \"%s\" goto Repeat\r\n")
__TEXT("del \"%s\"\r\n"),
szSourcePathName,
szSourcePathName,
DELUNSETUPBAT);
// Write the batch file and close it.
WriteFile(hFile, szBatFile, lstrlen(szBatFile) * sizeof(char),
&dwNumByte, NULL);
CloseHandle(hFile); // Get ready to spawn the batch file we just created.
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si); // We want its console window to be invisible to the user.
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE; // Spawn the batch file with low-priority and suspended.
if (CreateProcess(NULL, DELUNSETUPBAT, NULL, NULL, FALSE,
CREATE_SUSPENDED | IDLE_PRIORITY_CLASS, NULL,
__TEXT("\\"), &si, &pi)) { // Lower the batch file's priority even more.
SetThreadPriority(pi.hThread, THREAD_PRIORITY_IDLE); // Raise our priority so that we terminate as quickly as possible.
SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_TIME_CRITICAL);
SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); // Allow the batch file to run and clean-up our handles.
CloseHandle(pi.hProcess);
ResumeThread(pi.hThread);
// We want to terminate right away now so that we can be deleted
CloseHandle(pi.hThread);}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货