-
Notifications
You must be signed in to change notification settings - Fork 5
/
GetProcAddress_ALT_Example.pas
101 lines (77 loc) · 2.29 KB
/
GetProcAddress_ALT_Example.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Jean-Pierre LESUEUR (@DarkCoderSc)
// ...
uses tlhelp32, Windows, SysUtils, UntEnumDLLExport;
// ...
{
Retrieve module full path from it handle (Returned by LoadLibrary()), we need
that information to parse it PE Header and retrieve function address.
}
function GetModuleImagePath(hModule : HMODULE) : String;
var ASnap : THandle;
AModuleEntry : TModuleEntry32;
const TH32CS_SNAPMODULE32 = $00000010;
begin
result := '';
///
ASnap := CreateToolHelp32Snapshot(TH32CS_SNAPMODULE or TH32CS_SNAPMODULE32, GetCurrentProcessId());
if ASnap = INVALID_HANDLE_VALUE then
Exit();
try
ZeroMemory(@AModuleEntry, SizeOf(TModuleEntry32));
AModuleEntry.dwSize := SizeOf(TModuleEntry32);
///
if NOT Module32First(ASnap, AModuleEntry) then
Exit();
if (AModuleEntry.hModule = hModule) then begin
result := AModuleEntry.szExePath;
Exit();
end;
while True do begin
ZeroMemory(@AModuleEntry, SizeOf(TModuleEntry32));
AModuleEntry.dwSize := SizeOf(TModuleEntry32);
///
if NOT Module32Next(ASnap, AModuleEntry) then
Break;
if (AModuleEntry.hModule = hModule) then begin
result := AModuleEntry.szExePath;
break;
end;
end;
finally
CloseHandle(ASnap);
end;
end;
{
Retrieve function address from DLL PE Header Export Function Table.
}
function GetProcAddress_ALT(hModule : HMODULE; lpProcName : LPCSTR) : Pointer;
var ADLLExport : TEnumDLLExport;
I : Integer;
begin
result := nil;
///
ADLLExport := TEnumDLLExport.Create(GetModuleImagePath(hModule));
if (ADLLExport.Enum > 0) then begin
for I := 0 to ADLLExport.Items.Count -1 do begin
if (ADLLExport.Items[i].Name.ToLower = String(lpProcName).ToLower) then begin
result := Pointer(hModule + ADLLExport.Items[i].RelativeAddr);
break;
end;
end;
end;
end;
// ...
procedure LoadAndTriggerMessageBox();
var _MessageBoxW : function(hWnd: HWND; lpText, lpCaption: LPCWSTR; uType: UINT): Integer; stdcall;
hModule : HMODULE;
begin
_MessageBoxW := nil;
hModule := LoadLibrary('user32.dll');
@_MessageBoxW := GetProcAddress_ALT(hModule, 'MessageBoxW');
if Assigned(_MessageBoxW) then
_MessageBoxW(0, 'Hello World', 'Hey', 0);
end;
begin
LoadAndTriggerMessageBox();
end.
/// ...