让:
百du文心助手 - 办公学习一站解决
用精易模块的 程序_删除自身1 改的:
#include <windows.h>
#include <shlobj.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
puts("结束后会删除自身");
system("PAUSE");
// 获取当前程序完整路径
WCHAR szFileName[MAX_PATH];
GetModuleFileNameW(NULL, szFileName, MAX_PATH);
// 获取短路径名(避免空格问题)
WCHAR szShortPath[MAX_PATH];
DWORD len = GetShortPathNameW(szFileName, szShortPath, MAX_PATH);
if (len == 0 || len > MAX_PATH) {
return 1;
}
// 获取命令解释器路径
WCHAR szComspec[MAX_PATH];
DWORD comspecLen = GetEnvironmentVariableW(L"COMSPEC", szComspec, MAX_PATH);
if (comspecLen == 0) {
return 1;
}
// 构建批处理命令:/c del 文件名 > nul
WCHAR szBat[MAX_PATH * 2];
swprintf(szBat, MAX_PATH * 2, L"/c del \"%s\" > nul", szShortPath);
// 设置Shell执行信息结构
SHELLEXECUTEINFOW stShellDel;
ZeroMemory(&stShellDel, sizeof(stShellDel));
stShellDel.cbSize = sizeof(stShellDel);
stShellDel.hwnd = NULL;
stShellDel.lpVerb = L"open";
stShellDel.lpFile = szComspec;
stShellDel.lpParameters = szBat;
stShellDel.lpDirectory = NULL;
stShellDel.nShow = SW_HIDE;
stShellDel.fMask = SEE_MASK_NOCLOSEPROCESS;
// 执行删除命令
if (ShellExecuteExW(&stShellDel)) {
// 设置批处理进程优先级为空闲
SetPriorityClass(stShellDel.hProcess, IDLE_PRIORITY_CLASS);
// 设置当前进程为实时优先级,确保快速退出
SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
// 通知资源管理器文件已被删除
SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, szFileName, NULL);
// 立即退出进程
ExitProcess(0);
}
return 0;
}
|