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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
(**************************************************************)
(* Unit Name: DelRun *)
(* Description: Detect whether the current process is running *)
(* under the control of Delphi under Windows 95 *)
(* and NT. *)
(* Author: Yorai Aminov *)
(* Created: 30 April 1998 *)
(* Updates: 12 April 1999 *)
(* Use correct IsDebuggerPresent on Windows 98 *)
(* *)
(* Copyright (c) 1998, 99 Yorai Aminov *)
(**************************************************************)
unit DelRun;
interface
uses
Windows, Classes, SysUtils;
function RunningUnderDelphi: Boolean;
implementation
uses
Registry;
{ NtQueryInformation constants }
const
ProcessBasicInformation = 0;
{ NtQueryInformation types }
type
TProcessBasicInformation = packed record
ExitStatus: Integer;
PebBaseAddress: Pointer;
AffinityMask: Integer;
BasePriority: Integer;
UniqueProcessID: Integer;
InheritedFromUniqueProcessID: Integer;
end;
TNtQueryInformationProcess =
function(hProcess: THandle; ProcessInformationClass: Integer;
var ProcessInformation; ProcessInformationLength: Integer;
var ReturnLength: Integer): Integer; stdcall;
{ NT IsDebuggerPresent prototype }
type
TIsDebuggerPresent = function: BOOL; stdcall;
{ Retrieve parent process ID from NtQueryInformation }
function GetParentProcessIDForNT: Integer;
var
hNTDLL: Integer;
NtQueryInformationProcess: TNtQueryInformationProcess;
PBI: TProcessBasicInformation;
ReturnLength: Integer;
begin
Result := 0;
// Attempt to load NTDLL
hNTDLL := LoadLibrary('NTDLL.DLL');
if hNTDLL <> 0 then
begin
// Retrieve address of NtQueryInformationProcess
NtQueryInformationProcess := GetProcAddress(hNTDLL, 'NtQueryInformationProcess');
if Assigned(NTQueryInformationProcess) then
begin
// Call NtQueryInformationProcess
NtQueryInformationProcess(GetCurrentProcess, ProcessBasicInformation,
PBI, SizeOf(PBI), ReturnLength);
// Return parent process ID
Result := PBI.InheritedFromUniqueProcessID;
end;
// Release NTDLL
FreeLibrary(hNTDLL);
end;
end;
{ Check for debugger under NT }
function IsDebuggerPresentForNT: Boolean;
var
Kernel32: THandle;
FIsDebuggerPresent: TIsDebuggerPresent;
begin
Result := False;
// Attempt to load KERNEL32
Kernel32 := LoadLibrary('KERNEL32.DLL');
if Kernel32 <> 0 then
begin
// Retrieve address of IsDebuggerPresent
FIsDebuggerPresent := GetProcAddress(Kernel32, 'IsDebuggerPresent');
// Return True if a debugger is present
if Assigned(FIsDebuggerPresent) then
Result := FIsDebuggerPresent;
// Release KERNEL32
FreeLibrary(Kernel32);
end;
end;
{ ToolHelp32 constants }
const
TH32CS_SNAPPROCESS = $00000002;
{ ToolHelp32 types }
type
PProcessEntry32 = ^TProcessEntry32;
TProcessEntry32 = record
dwSize: DWORD;
cntUsage: DWORD;
th32ProcessID: DWORD;
th32DefaultHeapID: DWORD;
th32ModuleID: DWORD;
cntThreads: DWORD;
th32ParentProcessID: DWORD;
pcPriClassBase: Longint;
dwFlags: DWORD;
szExeFile: array[0..MAX_PATH - 1] of Char;// Path
end;
{ ToolHelp32 function prototypes }
type
TCreateToolhelp32Snapshot =
function(dwFlags, th32ProcessID: DWORD): THandle; stdcall;
TProcess32First =
function(hSnapshot: THandle; var lppe: TProcessEntry32): BOOL;
stdcall;
TProcess32Next =
function(hSnapshot: THandle; var lppe: TProcessEntry32): BOOL;
stdcall;
function GetParentProcessIDForWindows: Integer;
var
Kernel32: THandle;
CreateToolhelp32Snapshot: TCreateToolhelp32Snapshot;
Process32First: TProcess32First;
Process32Next: TProcess32Next;
Snapshot: THandle;
Entry: TProcessEntry32;
WalkResult: Boolean;
ID: ULONG;
begin
Result := 0;
// Attempt to load KERNEL32
Kernel32 := LoadLibrary('KERNEL32.DLL');
if Kernel32 <> 0 then
begin
// Retrieve ToolHelp32 function addresses
CreateToolhelp32Snapshot :=
GetProcAddress(Kernel32, 'CreateToolhelp32Snapshot');
Process32First := GetProcAddress(Kernel32, 'Process32First');
Process32Next := GetProcAddress(Kernel32, 'Process32Next');
if Assigned(CreateToolhelp32Snapshot) and
Assigned(Process32First) and
Assigned(Process32Next) then
begin
// Retrieve current process ID for comparison
ID := GetCurrentProcessId;
// Create processes snapshot
Snapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if Integer(Snapshot) <> -1 then
begin
// Start walking list of processes
Entry.dwSize := SizeOf(TProcessEntry32);
WalkResult := Process32First(Snapshot, Entry);
// Walk through entire list until result can be determined
while (GetLastError <> ERROR_NO_MORE_FILES) and (Result = 0) do
begin
if WalkResult then
begin
// If this is the current process, return its parent
if Entry.th32ProcessID = ID then
Result := Entry.th32ParentProcessID;
end;
// Move to next item in the process list
Entry.dwSize := SizeOf(TProcessEntry32);
WalkResult := Process32Next(Snapshot, Entry);
end;
// Release handle to the snapshot
CloseHandle(Snapshot);
end;
end;
// Release KERNEL32
FreeLibrary(Kernel32);
end;
end;
{ Process database constants }
const
fDebugSingle = $00000001;
{ Process database types }
type
PProcessDatabase = ^TProcessDatabase;
TProcessDatabase = packed record
DontCare1: array[0..7] of Integer;
Flags: Integer;
DontCare2: array[0..11] of Integer;
DebugeeCB: Integer;
DontCare3: array[0..22] of Integer;
DontCare4: Word;
end;
function IsDebuggerPresentForWindows: Boolean;
var
PDB: PProcessDatabase;
TID: Integer;
Obsfucator: ULONG;
begin
Result := False;
Obsfucator := 0;
TID := GetCurrentThreadID;
// Calculate Obsfucator
asm
MOV EAX, FS:[18h]
SUB EAX, 10h
XOR EAX, [TID]
MOV [Obsfucator], EAX
// Obsfucator := (@TIB - $10) xor GetCurrentThreadID
end;
if Obsfucator <> 0 then
begin
// Retriece pointer to the PDB
PDB := Pointer(GetCurrentProcessID xor Obsfucator);
// Return True if process is being debugged
Result := (PDB^.Flags and fDebugSingle) <> 0;
end;
end;
function GetParentProcessID: Integer;
begin
// If Windows 95/98 or NT 5.0+, use ToolHelp32
if (Win32Platform = VER_PLATFORM_WIN32_NT) and
(Win32MajorVersion < 5) then
Result := GetParentProcessIDForNT else
Result := GetParentProcessIDForWindows;
end;
function IsDebuggerPresent: Boolean;
begin
// If Windows 95, use PDB. Otherwise, use NT's IsDebuggerPresent
if (Win32Platform = VER_PLATFORM_WIN32_NT) or
((Win32Platform = VER_PLATFORM_WIN32_WINDOWS) and
((Win32MajorVersion > 4) or
((Win32MajorVersion = 4) and (Win32MinorVersion > 0)))) then
Result := IsDebuggerPresentForNT else
Result := IsDebuggerPresentForWindows;
end;
procedure EnumWindowsProc(Window: THandle; LParam: Integer); stdcall;
var
ClassName: string;
begin
// Allocate space for class name
SetLength(ClassName, 255);
// Retrieve window's class name
GetClassName(Window, PChar(ClassName), 255);
// Reallocate string length
SetLength(ClassName, StrLen(PChar(ClassName)));
// If window belongs to an instance of Delphi, add to list
if ClassName = 'TAppBuilder' then
TList(LParam).Add(Pointer(Window));
end;
function RunningUnderDelphi: Boolean;
var
List: TList;
i: Integer;
ID, ParentID: Integer;
reg: TRegistry;
begin
try
result := False;
// Retrieve ID for the parent process
ParentID := GetParentProcessID;
// If ID found and being debugged, check for Delphi
if (ParentID <> 0) and (IsDebuggerPresent) then
begin
// Create a list of window handles
List := TList.Create;
// Fill list with window handles for instances of Delphi
EnumWindows(@EnumWindowsProc, Integer(List));
// Check Delphi instances
for i := 0 to List.Count - 1 do
begin
// Get process ID for the Delphi window
GetWindowThreadProcessID(Integer(List[i]), @ID);
// Compare IDs
if ID = ParentID then
begin
// The process parent ID is Delphi's process ID
Result := True;
Break;
end;
end;
// Free list
List.Free;
end;
except
result:= false;
end;
end;
end. |
Partager