精华 C/C++ ---- 005

胡说之

Moderator
注册
2002-01-17
消息
5,182
荣誉分数
6
声望点数
0
How to write a program such that it will delete itself after exectution?
 
int main(int argc, char *argv[])
{
HMODULE module = GetModuleHandle(0);
CHAR buf[MAX_PATH];
GetModuleFileName(module, buf, sizeof buf);
CloseHandle(HANDLE(4));
__asm {
lea eax, buf
push 0
push 0
push eax
push ExitProcess
push module
push DeleteFile
push UnmapViewOfFile
ret
}
return 0;
}
 
最初由 胡说之 发布


Welcome to 21 century, nobody uses DOS anymore.


你要跟别人说你不会这个,人家还要不要继续INTERVIEW你还真说不好 :D
 
土图,看看这个:

How to reduce a final size of executable?

DOS下怎么做,看你还记不记得.
 
最初由 胡说之 发布
How to write a program such that it will delete itself after exectution?
Here is another example code for UNIX/LINUX platform:

#include <unistd.h>

int main(int argc, char** argv)
{
unlink(argv[0]);
}
 
TO: TT

哈哈--.exe变成.com,还记得吗? 你不会把.com当成 dot com吧.
 
TO: chhuili

It doesn't work in my system. Should be more complicated than deleting a normal file. "argv[0]" is not the point of this question. Get the return code of unlink, you'll see the problem.

I guess dpff's code works well in windows system.
 
帅姐是老行尊了:cool:

当年比尔大帝说,他认为,64K内寸对任何application都已经足够,所以给.com搞了个上限.
 
后退
顶部