我前段时间也写了一段自删除代码,不过没有那么高深,只是利用了批处理的功能,在程序的最后调用我这个函数,就能够将自己删除。现在把函数代码贴出来。
void selfdelete()
{
static char templ[] =
:repeat\r\n
del \%s\\r\n
if exist \%s\ goto repeat\r\n
rmdir %s \r\n
del \%s\ ;
static const char tempbatname[] = _uninsep.bat ;
char modulename[max_path] ;
char temppath[max_path] ;
char folder[max_path] ;
gettemppath(max_path, temppath) ;
strcat(temppath, tempbatname) ;
getmodulefilename(null, modulename, max_path) ;
strcpy (folder, modulename) ;
char *pb = strrchr(folder, '\\');
if (pb != null)
*pb = 0 ;
handle hf ;
hf = createfile(temppath, generic_write, 0, null,
create_always, file_attribute_normal, null) ;
if (hf != invalid_handle_value)
{
dword len ;
char *bat ;
bat = (char*)alloca(strlen(templ) +
strlen(modulename) * 2 + strlen(temppath) + 20) ;
wsprintf(bat, templ, modulename, modulename, folder, temppath) ;
writefile(hf, bat, strlen(bat), &len, null) ;
closehandle(hf) ;
shellexecute(null, open, temppath, null, null, sw_hide);
}
}
