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
| // retourne le nom d'une fonction d'une bibliothèque à partir de son adresse.
function GetDllFunctionNameFromAddress(aAddress: Pointer): String;
const
SYMFLAG_FUNCTION = $00000800 or $00000200; // fonction ou export table
DbgHelpDll = 'C:\windows\system32\dbghelp.dll';
MaxSymbolNameLen = 256;
type
TSYMBOL_INFO = record
SizeOfStruct: Cardinal;
TypeIndex: Cardinal;
Reserved_1, Reserved_2: uint64;
Index: Cardinal;
Size: Cardinal;
ModBase: uint64;
Flags: Cardinal; // SYMFLAG_FUNCTION
Value: uint64;
Address: uint64;
Registre: Cardinal;
Scope: Cardinal;
Tag: Cardinal;
NameLen: Cardinal;
MaxNameLen: Cardinal; // MaxSymbolNameLen (modifiable)
Name: array[0..MaxSymbolNameLen-1] of Char; // pas à inclure dans TSYMBOL_INFO.SizeOfStruct
end;
var
ProcessHandle: HMODULE;
i, Deplacement: Cardinal;
SymbolInfo: TSYMBOL_INFO;
function SymInitialize(aHandle: HMODULE;
aUserSearchPath: PChar;
aInvadeProcess: Boolean): Boolean; stdcall; external DbgHelpDll
{$IFDEF UNICODE}
name 'SymInitializeW'
{$ELSE}
name 'SymInitialize'
{$ENDIF};
function SymFromAddr(aHandle: HMODULE;
aAdress: uint64;
var aDisplacement: Cardinal;
aSymbolInfo: Pointer): Boolean; stdcall; external DbgHelpDll
{$IFDEF UNICODE}
name 'SymFromAddrW'
{$ELSE}
name 'SymFromAddr'
{$ENDIF};
function SymCleanup(aHandle: HMODULE): Boolean; stdcall; external DbgHelpDll;
begin
Result := EmptyStr;
ProcessHandle := GetCurrentProcess;
// initialisation de la table des symboles
if not SymInitialize(ProcessHandle, nil, True) then Exit;
// initialisation de la structure pour le retour des infos
i := SizeOf(TSymbol_Info);
ZeroMemory(@SymbolInfo, i);
SymbolInfo.MaxNameLen := MaxSymbolNameLen;
SymbolInfo.SizeOfStruct := i - Length(SymbolInfo.Name) * SizeOf(SymbolInfo.Name[0]);
Deplacement := 0;
// informations sur l'adresse demandée
if SymFromAddr(ProcessHandle, int64(aAddress), Deplacement, @SymbolInfo) then
begin
if (SymbolInfo.Flags or SYMFLAG_FUNCTION) = SYMFLAG_FUNCTION // filtre les fonctions
then Result := SymbolInfo.Name;
end;
// libération de la table des symboles
SymCleanup(ProcessHandle);
end; |