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 102 103 104 105 106
|
unit SBTools;
interface
uses
Windows,Messages,SysUtils,Variants,Classes,StrUtils,TlHelp32,PSAPI;
function GetModuleBaseAddress(const PHandle: Thandle; MName: String):Pointer;
function GetPIDFromName(appname: string): cardinal;
function GetBasePointerOfModule(const ProcessId: cardinal; ModuleName: string): Pointer;
var
MyHandle : THandle;
BaseAdr : pointer;
PID : cardinal;
implementation
function GetPIDFromName(appname: string): cardinal;
Var
processEntry : TProcessEntry32;
Begin
Result := 0; appName := UpperCase(appname);
MyHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
If MyHandle <> 0 Then
try
processEntry.dwSize := Sizeof(processEntry);
If Process32First(MyHandle, processEntry) Then
Repeat
If Pos(appname,UpperCase(ExtractFilename(StrPas(processEntry.szExeFile)))) > 0
Then Begin
Result:= processEntry.th32ProcessID;
Break;
End; { If }
Until not Process32Next(MyHandle, processEntry);
finally
CloseHandle(MyHandle);
End; { try }
End;
function GetModuleBaseAddress(const PHandle: Thandle; MName: String):Pointer;
var
Modules : Array of HMODULE;
cbNeeded, i : Cardinal;
ModuleInfo : TModuleInfo;
ModuleName : Array[0..MAX_PATH] of Char;
begin
Result := nil;
SetLength(Modules, 1024);
if (PHandle <> 0) then
begin
try
EnumProcessModules(PHandle, @Modules[0], 1024 * SizeOf(HMODULE),cbNeeded);
except
Exit;
end;
SetLength(Modules, cbNeeded div SizeOf(HMODULE)); //Setting the number of modules
if (Length(Modules) > 0) then
begin
for i := 0 to Length(Modules) - 1 do //Start the loop
begin
try
GetModuleBaseName(PHandle, Modules[i], ModuleName, SizeOf(ModuleName));
if (Pos(MName, ModuleName) > 0) then
begin
GetModuleInformation(PHandle, Modules[i], @MoDuleInfo, SizeOf(ModuleInfo));
Result := ModuleInfo.lpBaseOfDll;
Exit;
end;
Except
end;
end;
end;
end;
end;
function GetBasePointerOfModule(const ProcessId: cardinal; ModuleName: string): Pointer;
var
FSnapshotHandle: THandle;
FModulEntry32: MODULEENTRY32;
s: string;
begin
Result := nil;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE (* {$IFDEF WIN64}or
TH32CS_SNAPMODULE32{$ENDIF} *), ProcessId);
if FSnapshotHandle = INVALID_HANDLE_VALUE then RaiseLastOSError;
try
FModulEntry32.dwSize := SizeOf(FModulEntry32);
if not Module32First(FSnapshotHandle, FModulEntry32) then
begin
if GetLastError() <> ERROR_NO_MORE_FILES then RaiseLastOSError;
Exit;
end;
repeat
s := FModulEntry32.szModule;
if s = ModuleName then
begin
Result := FModulEntry32.modBaseAddr;
Exit;
end;
until not Module32Next(FSnapshotHandle, FModulEntry32);
if GetLastError() <> ERROR_NO_MORE_FILES then RaiseLastOSError;
finally
CloseHandle(FSnapshotHandle);
end;
end;
end. |
Partager