#include <iostream.h>
#include <string.h>
#include <windows.h>
void KillSelf(const char *sSelfFName)
{
const char *sBatFName = "$tmp$.bat";
const char *sBatContent = "@type %1>nul\n@del %1\n@del %0";
char *sCmdLine = strdup(sBatFName);
strcat(sCmdLine, " ");
strcat(sCmdLine, sSelfFName);
HANDLE hBat = ::CreateFile(sBatFName,
GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_READ,
0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY,
0);
if (hBat == INVALID_HANDLE_VALUE)
return ;
unsigned long cTmp;
::WriteFile(hBat, sBatContent, strlen(sBatContent), &cTmp, NULL);
::CloseHandle(hBat);
STARTUPINFO si;
::ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
PROCESS_INFORMATION pi;
::CreateProcess(NULL, sCmdLine, NULL, NULL, true,
CREATE_NEW_CONSOLE | NORMAL_PRIORITY_CLASS, NULL,
NULL, &si, &pi);
::ExitProcess(0);
}
void RenameSelf(const char *SelfFName, const char *NewName)
{
::CopyFile(SelfFName, NewName, false);
KillSelf(SelfFName);
}
int main(int argc, char *argv[])
{
if (argc < 2)
return 1;
RenameSelf(argv[0], argv[1]);
}
Sergey Shepelev 2:5020/1355.256
Предыдущий вопрос
|
|
Следующий вопрос
источник
by jenyok
|