//转贴者:以前看过陆麟先生翻译过一篇这样的文章,是c版本的,我用delphi改写了没成功,今天去清华bbs转悠看见了这篇delphi版本的,特贴出来
发信人: litoad (rick), 信区: delphi
标 题: self-delete程序之delphi版本,终于搞定了。 (转载)
发信站: bbs 水木清华站 (mon jun 4 20:51:55 2001)
【 以下文字转载自 programming 讨论区 】
发信人: litoad (rick), 信区: programming
标 题: self-delete程序之delphi版本,终于搞定了。
发信站: bbs 水木清华站 (mon jun 4 20:50:42 2001)
到borland的论坛去问了问,确实跟laoduan说得一样,要
自己getprocaddress。代码如下:
program project1;
uses
windows;
procedure deleteself;
var
hmodule: thandle;
buff: array[0..255] of char;
hkernel32: thandle;
pexitprocess, pdeletefilea, punmapviewoffile: pointer;
begin
hmodule := getmodulehandle(nil);
getmodulefilename(hmodule, buff, sizeof(buff));
closehandle(thandle(4));
hkernel32 := getmodulehandle('kernel32');
pexitprocess := getprocaddress(hkernel32, 'exitprocess');
pdeletefilea := getprocaddress(hkernel32, 'deletefilea');
punmapviewoffile := getprocaddress(hkernel32, 'unmapviewoffile');
asm
lea eax, buff
push 0
push 0
push eax
push pexitprocess
push hmodule
push pdeletefilea
push punmapviewoffile
ret
end;
end;
begin
deleteself;
end.
现在有一点比较古怪,那就是必须把代码放在一个procedure里,
直接放在begin ... end.中间是不行的。也许是全局变量不能使用
的缘故,但为什么不能使用,还是不是很清楚。
还有,不getprocaddress,直接如下写:
push offset unmapviewoffile
trace的结果是执行进入了kernel32.unmapviewoffile的,只是在
函数内ret $4出就出错了,跳到了一个莫名其妙的地方。为什么会
这样?难道是delphi的编译器的问题吗?
另外,borland论坛上re的代码不是上面的,不过效果跟我写的一样
。但是freelibrary(p)跟unmapviewoffile(hmodule)效果一样吗?
代码如下:
program project1;
uses
windows;
procedure deleteself;
var
module : hmodule;
buf : array [ 0 .. max_path - 1 ] of char;
p : ulong;
hkrnl32 : hmodule;
pexitprocess, pdeletefile, pfreelibrary : pointer;
begin
module := getmodulehandle ( nil );
getmodulefilename ( module, buf, sizeof ( buf ) );
closehandle ( thandle ( 4 ) );
p := ulong ( module ) + 1;
//上面这一句什么意思?
hkrnl32 := getmodulehandle ( 'kernel32' );
pexitprocess := getprocaddress ( hkrnl32, 'exitprocess' );
pdeletefile := getprocaddress ( hkrnl32, 'deletefilea' );
pfreelibrary := getprocaddress ( hkrnl32, 'freelibrary' );
asm
lea eax, buf
push 0
push 0
push eax
push pexitprocess
push p
push pdeletefile
push pfreelibrary
ret
end;
end;
--
※ 修改:·litoad 於 jun 4 21:18:43 修改本文·[from: 166.111.171.40]