FpDebugger (pure): Retrieve the file-names of loaded processes and modules.

git-svn-id: trunk@45400 -
This commit is contained in:
joost 2014-06-08 13:18:23 +00:00
parent a8fb1a0c44
commit 24a6aab526

View File

@ -45,6 +45,7 @@ uses
FpDbgClasses, FpDbgClasses,
process, process,
FpDbgWinExtra, FpDbgWinExtra,
strutils,
FpDbgInfo, FpDbgInfo,
FpDbgLoader, FpdMemoryTools, FpDbgLoader, FpdMemoryTools,
DbgIntfBaseTypes, DbgIntfBaseTypes,
@ -152,20 +153,59 @@ begin
DebugLn('FpDbg-ERROR: ', GetLastErrorText); DebugLn('FpDbg-ERROR: ', GetLastErrorText);
end; end;
function QueryFullProcessImageName(hProcess:HANDLE; dwFlags: DWord; lpExeName:LPTSTR; var lpdwSize:DWORD):BOOL; stdcall; external 'kernel32' name 'QueryFullProcessImageNameA';
function GetFullProcessImageName(AProcessHandle: THandle): string;
var
s: string;
len: DWORD;
begin
len := MAX_PATH;
SetLength(S, len);
if QueryFullProcessImageName(AProcessHandle, 0, @S[1], len)
then SetLength(S, len)
else begin
S := '';
LogLastError;
end;
result := s;
end;
function GetModuleFileName(AModuleHandle: THandle): string; function GetModuleFileName(AModuleHandle: THandle): string;
var var
s: string; s: string;
len: Integer; len: Integer;
hMod: THandle;
_GetFinalPathNameByHandle: function(hFile: HANDLE; lpFilename:LPTSTR; cchFilePath, dwFlags: DWORD):DWORD; stdcall;
begin begin
result := '';
// GetFinalPathNameByHandle is only available on Windows Vista / Server 2008
_GetFinalPathNameByHandle := nil;
// normally you would load a lib, but since kernel32 is
// always loaded we can use this (and we don't have to free it
hMod := GetModuleHandle(kernel32);
if hMod = 0 then Exit; //????
pointer(_GetFinalPathNameByHandle) := GetProcAddress(hMod, 'GetFinalPathNameByHandleA');
if assigned(_GetFinalPathNameByHandle) then begin
SetLength(S, MAX_PATH); SetLength(S, MAX_PATH);
len := windows.GetModuleFileName(AModuleHandle, @S[1], MAX_PATH);
len := _GetFinalPathNameByHandle(AModuleHandle, @S[1], MAX_PATH, 0);
if len > 0 if len > 0
then SetLength(S, len - 1) then SetLength(S, len - 1)
else begin else begin
S := ''; S := '';
LogLastError; LogLastError;
end; end;
result := s; // On some older Windows versions there's a bug in GetFinalPathNameByHandleA,
// which leads to a trailing #0.
if strutils.RightStr(S,1) =#0 then
SetLength(S,length(S)-1);
// Remove the \\?\ prefix
Delete(S,1,4);
result := S;
end;
end; end;
function GetProcFilename(AProcess: TDbgProcess; lpImageName: LPVOID; fUnicode: word; hFile: handle): string; function GetProcFilename(AProcess: TDbgProcess; lpImageName: LPVOID; fUnicode: word; hFile: handle): string;
@ -174,24 +214,27 @@ var
S: String; S: String;
W: WideString; W: WideString;
begin begin
W := ''; S := '';
if (lpImageName<>nil) and AProcess.ReadOrdinal(TDbgPtr(lpImageName), NamePtr) if (lpImageName<>nil) and AProcess.ReadOrdinal(TDbgPtr(lpImageName), NamePtr)
then begin then begin
if fUnicode <> 0 if fUnicode <> 0
then begin then begin
AProcess.ReadWString(NamePtr, MAX_PATH, W); if AProcess.ReadWString(NamePtr, MAX_PATH, W)
then S := W;
end end
else begin else begin
if AProcess.ReadString(NamePtr, MAX_PATH, S) AProcess.ReadString(NamePtr, MAX_PATH, S);
then W := S;
end; end;
end; end;
if W = '' if S = ''
then begin then begin
W := GetModuleFileName(hFile); if hFile=0 then
S := GetFullProcessImageName(AProcess.Handle)
else
S := GetModuleFileName(hFile);
end; end;
result := W; result := S;
end; end;
{ tDbgWinLibrary } { tDbgWinLibrary }
@ -266,6 +309,7 @@ var
BytesRead: PtrUInt; BytesRead: PtrUInt;
buf: array of Char; buf: array of Char;
begin begin
AData := '';
SetLength(buf, AMaxSize + 1); SetLength(buf, AMaxSize + 1);
Result := ReadProcessMemory(Handle, Pointer(PtrUInt(AAdress)), @Buf[0], AMaxSize, BytesRead); Result := ReadProcessMemory(Handle, Pointer(PtrUInt(AAdress)), @Buf[0], AMaxSize, BytesRead);
if not Result then Exit; if not Result then Exit;
@ -280,6 +324,7 @@ var
BytesRead: PtrUInt; BytesRead: PtrUInt;
buf: array of WChar; buf: array of WChar;
begin begin
AData := '';
SetLength(buf, AMaxSize + 1); SetLength(buf, AMaxSize + 1);
Result := ReadProcessMemory(Handle, Pointer(PtrUInt(AAdress)), @Buf[0], SizeOf(WChar) * AMaxSize, BytesRead); Result := ReadProcessMemory(Handle, Pointer(PtrUInt(AAdress)), @Buf[0], SizeOf(WChar) * AMaxSize, BytesRead);
if not Result then Exit; if not Result then Exit;
@ -839,7 +884,7 @@ var
begin begin
FInfo := AInfo; FInfo := AInfo;
s := GetProcFilename(Self, AInfo.lpImageName, AInfo.fUnicode, AInfo.hFile); s := GetProcFilename(Self, AInfo.lpImageName, AInfo.fUnicode, 0);
if s <> '' if s <> ''
then SetName(s); then SetName(s);