Visual C++ FAQ




Как удалить .exe-файл из самого себя?


#include <windows.h> #include <stdio.h>

void DelSelf(void) { char modulename[MAX_PATH]; char batfile[MAX_PATH]; char batlines[MAX_PATH*4]; LPSTR tempdir; char Buf[MAX_PATH];

GetModuleFileName(NULL,modulename,MAX_PATH);

tempdir = ((GetEnvironmentVariable(TEXT("TEMP"), Buf, MAX_PATH) > 0) ? Buf : NULL);

strcpy(batfile,tempdir); strcat(batfile,"\\"); strcat(batfile,"delself.bat"); strcpy(batlines,"@echo off\n:try\ndel "); strcat(batlines,modulename); strcat(batlines,"\nif exist "); strcat(batlines,modulename); strcat(batlines," goto try\n"); strcat(batlines,"del "); strcat(batlines,batfile);

DWORD NOfBytes; HANDLE hbf= CreateFile(batfile, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); WriteFile(hbf,batlines,strlen(batlines),&NOfBytes, NULL); CloseHandle(hbf);

STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory( &si, sizeof(si) ); si.cb = sizeof(si); si.wShowWindow = SW_HIDE; si.dwFlags = STARTF_USESHOWWINDOW;

CreateProcess( NULL, batfile, NULL, NULL, FALSE, IDLE_PRIORITY_CLASS|DETACHED_PROCESS, NULL, NULL, &si, &pi);

} void main() { DelSelf(); }




Содержание  Назад  Вперед