BOOL SelfDelete()

...{
TCHAR szModule [MAX_PATH],
szComspec[MAX_PATH],
szParams [MAX_PATH];

// get file path names:
if((GetModuleFileName(0,szModule,MAX_PATH)!=0) &&
(GetShortPathName(szModule,szModule,MAX_PATH)!=0) &&
(GetEnvironmentVariable("COMSPEC",szComspec,MAX_PATH)!=0))

...{
// set command shell parameters
lstrcpy(szParams," /c del ");
lstrcat(szParams, szModule);
lstrcat(szParams, " > nul");
lstrcat(szComspec, szParams);


// set struct members

STARTUPINFO si=...{0};

PROCESS_INFORMATION pi=...{0};
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;

// increase resource allocation to program
SetPriorityClass(GetCurrentProcess(),
REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(),
THREAD_PRIORITY_TIME_CRITICAL);

// invoke command shell
if(CreateProcess(0, szComspec, 0, 0, 0,CREATE_SUSPENDED|
DETACHED_PROCESS, 0, 0, &si, &pi))

...{
// suppress command shell process until program exits
SetPriorityClass(pi.hProcess,IDLE_PRIORITY_CLASS);
SetThreadPriority(pi.hThread,THREAD_PRIORITY_IDLE);

// resume shell process with new low priority
ResumeThread(pi.hThread);

// everything seemed to work
return TRUE;
}
else // if error, normalize allocation

...{
SetPriorityClass(GetCurrentProcess(),
NORMAL_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(),
THREAD_PRIORITY_NORMAL);
}
}
return FALSE;
}

...{
TCHAR szModule [MAX_PATH],
szComspec[MAX_PATH],
szParams [MAX_PATH];
// get file path names:
if((GetModuleFileName(0,szModule,MAX_PATH)!=0) &&
(GetShortPathName(szModule,szModule,MAX_PATH)!=0) &&
(GetEnvironmentVariable("COMSPEC",szComspec,MAX_PATH)!=0))
...{
// set command shell parameters
lstrcpy(szParams," /c del ");
lstrcat(szParams, szModule);
lstrcat(szParams, " > nul");
lstrcat(szComspec, szParams);

// set struct members
STARTUPINFO si=...{0};
PROCESS_INFORMATION pi=...{0};
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
// increase resource allocation to program
SetPriorityClass(GetCurrentProcess(),
REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(),
THREAD_PRIORITY_TIME_CRITICAL);
// invoke command shell
if(CreateProcess(0, szComspec, 0, 0, 0,CREATE_SUSPENDED|
DETACHED_PROCESS, 0, 0, &si, &pi))
...{
// suppress command shell process until program exits
SetPriorityClass(pi.hProcess,IDLE_PRIORITY_CLASS);
SetThreadPriority(pi.hThread,THREAD_PRIORITY_IDLE); 
// resume shell process with new low priority
ResumeThread(pi.hThread);
// everything seemed to work
return TRUE;
}
else // if error, normalize allocation
...{
SetPriorityClass(GetCurrentProcess(),
NORMAL_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(),
THREAD_PRIORITY_NORMAL);
}
}
return FALSE;
}
本文介绍了一个使用Windows API实现的程序自删除方法。该方法通过获取当前程序路径,并利用CMD命令行来删除自身进程。文中详细展示了如何设置命令行参数、调整资源分配优先级以及创建并挂起新的命令行进程来实现程序的自我删除。
1131

被折叠的 条评论
为什么被折叠?



