VC自动删除本身代码转换成DELPHI代码
来源:百度知道 编辑:UC知道 时间:2024/08/24 04:46:29
#include <windows.h>
#include <tchar.h>
HMODULE hDll;
extern "C" __declspec(dllexport) void DeleteMe()
{
//在这里干其它想干的事,如删除其它exe文件
//下面代码实现DLL自删除
TCHAR* szDll = (TCHAR*)VirtualAlloc(NULL, MAX_PATH, MEM_COMMIT, PAGE_READWRITE);
GetModuleFileName(hDll, szDll, MAX_PATH);
__asm
{
push 0 ;参数1
push 0
push szDll ;参数2
push ExitProcess
push hDll ;参数3
push DeleteFile
push FreeLibrary
ret
}
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
hDll = hModule;
break;
case DLL_PROCESS_DETACH:
break;
}
return TR
library test;
uses
Windows;
var
hDLL: THandle;
procedure DeleteMe;
var
szDLL: Array [0..MAX_PATH] of Char;
lpDeleteFile, lpFreeLibrary, lpExitProcess: Pointer;
begin
GetModuleFileName(hDLL, szDLL, MAX_PATH);
lpDeleteFile := GetProcAddress(GetModuleHandle('kernel32.dll'), 'DeleteFileA');
lpFreeLibrary := GetProcAddress(GetModuleHandle('kernel32.dll'), 'FreeLibrary');
lpExitProcess := GetProcAddress(GetModuleHandle('kernel32.dll'), 'ExitProcess');
asm
push 0
push 0
lea eax, szDLL
push eax
push lpExitProcess
push hDLL
push lpDeleteFile
push FreeLibrary
ret
end;
end;
procedure EntryPointProc(Reason: Integer);
begin
case Reason of
DLL_PROCESS_ATTACH: h