Author: William Gerbert
Retrieve list of exported functions from a DLL
Answer:
To retrieve the list of exported functions from a DLL, pass the DLL name and a
TStrings object to the function ListDLLFunctions() shown below.
This does not show the parameters for each export, which you can only get from the
author of the DLL.
1 program Project1;
2
3 uses
4 Forms,
5 Classes,
6 SysUtils,
7 Dialogs,
8 ImageHlp, // routines to access debug information
9 Windows;
10
11
12 procedure ListDLLFunctions(DLLName: string; List: TStrings);
13 type
14 chararr = array[0..$FFFFFF] of Char;
15 var
16 H: THandle;
17 I, fc: integer;
18 st: string;
19 arr: Pointer;
20 ImageDebugInformation: PImageDebugInformation;
21 begin
22 List.Clear;
23 DLLName := ExpandFileName(DLLName);
24 if FileExists(DLLName) then
25 begin
26 H := CreateFile(PChar(DLLName), GENERIC_READ, FILE_SHARE_READ or
27 FILE_SHARE_WRITE, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
28 if H <> INVALID_HANDLE_VALUE then
29 try
30 ImageDebugInformation := MapDebugInformation(H, PChar(DLLName), nil, 0);
31 if ImageDebugInformation <> nil then
32 try
33 arr := ImageDebugInformation^.ExportedNames;
34 fc := 0;
35 for I := 0 to ImageDebugInformation^.ExportedNamesSize - 1 do
36 if chararr(arr^)[I] = #0 then
37 begin
38 st := PChar(@chararr(arr^)[fc]);
39 if Length(st) > 0 then
40 List.Add(st);
41 if (I > 0) and (chararr(arr^)[I - 1] = #0) then
42 Break;
43 fc := I + 1
44 end
45 finally
46 UnmapDebugInformation(ImageDebugInformation)
47 end
48 finally
49 CloseHandle(H)
50 end
51 end
52 end;
53
54 // the following is an example how to use the procedure
55 var
56 List: TStrings;
57 I: integer;
58 S: string;
59
60 begin
61 List := TStringList.Create;
62
63 ListDLLFunctions('c:\winnt\system32\mfc42.dll', List);
64
65 S := 'List of functions';
66 for I := 0 to List.Count - 1 do
67 S := S + #13#10 + List[I];
68 ShowMessage(S);
69 List.Free
70 end.
|