[C++] 纯文本查看 复制代码
#include <windows.h>
#include <tchar.h>
#pragma comment (linker, "/NODEFAULTLIB")
#ifndef _DEBUG
#pragma comment(linker,"/merge:.rdata=.data")
#pragma comment(linker,"/merge:.text=.data")
#pragma comment(linker,"/merge:.reloc=.data")
#pragma comment(linker,"/FILEALIGN:0x200")
#endif // _DEBUG
TCHAR szAppName[] = _T("delthis");
TCHAR szUsage[] = _T("Usage:\r\n\r\nselfdel [options]\r\n\r\nOptions:\r\n-u (Uninstall)\r\n-pid Pid\r\n-exe Path");
TCHAR szError1[] = _T("Failed to open process [%u]");
TCHAR szDeleting[] = _T("Deleting:\r\n\r\n%s");
int _tatoi(TCHAR *num)
{
int n = 0;
TCHAR *nptr = num;
while(*nptr && IsCharAlphaNumeric(*nptr) && !IsCharAlpha(*nptr))
n = 10 * n + (*nptr++ - '0');
return n;
}
void MyZeroMem(void *mem, DWORD bytes)
{
BYTE *bptr = (BYTE *)mem;
while(bytes--)
*bptr++ = 0;
}
void CommitSuicide(void)
{
HANDLE hTemp;
char szPath[MAX_PATH];
char szTemp[MAX_PATH];
char szBig [MAX_PATH*2 + 100];
STARTUPINFO si;
PROCESS_INFORMATION pi;
UINT ret;
GetTempPath(MAX_PATH, szTemp);
lstrcat(szTemp, "selfdel.exe");
GetModuleFileName(0, szPath, MAX_PATH);
CopyFile(szPath, szTemp, FALSE);
hTemp = CreateFile(szTemp, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_DELETE, 0,
OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, 0);
MyZeroMem(&si, sizeof(STARTUPINFO));
MyZeroMem(&pi, sizeof(PROCESS_INFORMATION));
si.cb = sizeof(STARTUPINFO);
wsprintf(szBig, "\"%s\" -pid %u -exe \"%s\"",
szTemp,
GetCurrentProcessId(),
szPath);
ret = CreateProcess(0, szBig, 0, 0, FALSE, NORMAL_PRIORITY_CLASS, 0, 0, &si, &pi);
Sleep(100);
CloseHandle(hTemp);
}
//
void DeleteExe(DWORD dwPid, TCHAR *szPath)
{
HANDLE hProcess;
TCHAR szErr[MAX_PATH+32];
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | SYNCHRONIZE, FALSE, dwPid);
if(hProcess == 0)
{
wsprintf(szErr, szError1, dwPid);
MessageBox(0, szErr, szAppName, MB_OK|MB_ICONINFORMATION);
return;
}
WaitForSingleObject(hProcess, INFINITE);
CloseHandle(hProcess);
DeleteFile(szPath);
}
TCHAR * GetNextArg(TCHAR *szPtr, TCHAR *szOut)
{
TCHAR *pOut = szOut;
TCHAR ch = *szPtr++;
if(ch == '\0')
return 0;
while(ch == ' ' || ch == '\t')
{
ch = *szPtr++;
}
if(ch == '\"')
{
ch = *szPtr++;
while(ch && ch != '\"')
{
*szOut++ = ch;
ch = *szPtr++;
}
*szOut = '\0';
return szPtr;
}
while(ch && ch != ' ' && ch != '\t')
{
*szOut++ = ch;
ch = *szPtr++;
}
*szOut = '\0';
return szPtr;
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nShowCmd)
{
DWORD dwProcessId;
TCHAR *ptr;
TCHAR buf[MAX_PATH];
ptr = lpCmdLine;
ptr = GetNextArg(ptr, buf);
if(lstrcmpi(buf, "-u") == 0)
{
CommitSuicide();
ExitProcess(0);
return 0;
}
if(lstrcmpi(buf, "-pid") == 0)
{
ptr = GetNextArg(ptr, buf);
dwProcessId = _tatoi(buf);
ptr = GetNextArg(ptr, buf);
if(lstrcmpi(buf, "-exe") == 0)
{
//exe-path specified - get the path
ptr = GetNextArg(ptr, buf);
DeleteExe(dwProcessId, buf);
return 0;
}
else
{
return 1;
}
}
else
{
return 1;
}
return 0;
}
int WINAPI WinMainCRTStartup()
{
UINT ret;
TCHAR *pszCmdLine;
TCHAR temp[MAX_PATH];
HINSTANCE hInst = GetModuleHandle(0);
pszCmdLine = GetCommandLine();
pszCmdLine = GetNextArg(pszCmdLine, temp);
ret = WinMain(hInst, 0, pszCmdLine, SW_SHOWNORMAL);
if(ret != 0)
{
MessageBox(0, szUsage, szAppName, MB_OK|MB_ICONINFORMATION);
}
ExitProcess(ret);
return 0;
}