Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi TShellExecuteInfo.hProcess<>TProcessInformation.dw ProcessId (https://www.delphipraxis.net/57103-tshellexecuteinfo-hprocess-tprocessinformation-dwprocessid.html)

Meta777 15. Nov 2005 22:06


TShellExecuteInfo.hProcess<>TProcessInformation.dw Proc
 
Huhu DPler,

Bin grad dabei eine bestehende ExecAndWait Function um den Parameter "MoniIdx" zu erweitern.
Die ExecAndWait benutzt ShellExecuteEx(SEInfo) zum ausführen.
Ich will nun also das Haupt-Fenster des gestarteten proggis auf einen anderen Monitor setzen (moniIdx)!
Nun hab ich aber das Problem das ich mit SEInfo.hProcess nicht auf das Haupt-Formular zugreifen kann - hab versucht mit EnumWindows die Prozess-Id mit GetWindowThreadProcessId(hWnd, @PId) herauszufinden und zu vergleichen, aber das Fenster hat eine andere Prozeß-Id als SEInfo.hProcess :?

Allerdings, wie der Titel erahnen läßt, funktioniert das ermitteln des Haupt-Fensters mit TProcessInformation.dwProcessId! TProcessInformation wird jedoch mit CreateProcess() verwendet.
Wo ist also der Unterschied :?:
Und wie kann ich das Haupt-Fenster des gestarteten proggis mittels SEInfo ermitteln :?:

Danke & Gottes Segen euch

Flocke 15. Nov 2005 23:44

Re: TShellExecuteInfo.hProcess<>TProcessInformation.dw
 
Das eine ist ein Prozesshandle (HPROCESS) und das andere eine Prozess-Id (DWORD).

Die Funktion MSDN-Library durchsuchenGetProcessId liefert dir die Id zu einem Handle. Ansonsten liefert dir CreateProcess in der Struktur TProcessInformation aber auch alle Infos, die du benötigst, nämlich sowohl das Handle als auch die Id.

Meta777 16. Nov 2005 14:22

Re: TShellExecuteInfo.hProcess<>TProcessInformation.dw
 
Hi Flocke,

hab grad im MSDN nachgeschaut und gelesen: http://msdn.microsoft.com/library/de...tprocessid.asp
Zitat:

Requirements
Client Requires Windows "Longhorn" or Windows XP SP1.
Server Requires Windows Server "Longhorn" or Windows Server 2003.
Das würde sicher erklären das GetProcessId nicht in D7 unter W2k verfügbar ist, oder?
Welche anderen möglichkeiten gibt es die processId anhand des process handles herzubekommen, was dann auch - wenn möglich - unter Win9x läuft?

Danke für deine Hilfe

NicoDE 16. Nov 2005 14:56

Re: TShellExecuteInfo.hProcess<>TProcessInformation.dw
 
Zitat:

Zitat von Meta777
Welche anderen möglichkeiten gibt es die processId anhand des process handles herzubekommen, was dann auch - wenn möglich - unter Win9x läuft?

Soweit ich mich erinnere, mit der offiziellen Win32-API, keine.

Wobei das bei Win9x noch relativ einfach ist (da Process-IDs xor-verschlüsselte Process-Handles sind, siehe: http://www.delphipraxis.net/internal...=212248#212248 ).

NicoDE 16. Nov 2005 15:53

Re: TShellExecuteInfo.hProcess<>TProcessInformation.dw
 
Hier eine Version die mit allen Windows-Versionen (32-Bit, inklusive WoW64) funktionieren sollte...
Delphi-Quellcode:
unit ProcessUtils {platform};

interface

uses
  Windows;

function GetProcessId(Process: THandle): DWORD stdcall;

implementation

{$ALIGN 8}
{$MINENUMSIZE 4}
{$WRITEABLECONST ON}

//
// Windows 9x
//

function GetObsfucator(): DWORD;
asm
        call   GetCurrentThreadId
        push   eax
        call   GetCurrentProcessId
        xor    edx, edx
        xor    eax, fs:[edx + 30h]
        pop    ecx
        xor    ecx, eax
        sub    ecx, fs:[edx + 18h]
        add    ecx, 08h
        jecxz  @@done
        add    ecx, 08h
        jecxz  @@done
        xor    eax, eax
@@done:
end;

//
// Windows NT
//

type
  PProcessInfoClass = ^TProcessInfoClass;
  TProcessInfoClass = (
    ProcessBasicInformation,
    ProcessQuotaLimits,
    ProcessIoCounters,
    ProcessVmCounters,
    ProcessTimes,
    ProcessBasePriority,
    ProcessRaisePriority,
    ProcessDebugPort,
    ProcessExceptionPort,
    ProcessAccessToken,
    ProcessLdtInformation,
    ProcessLdtSize,
    ProcessDefaultHardErrorMode,
    ProcessIoPortHandlers,
    ProcessPooledUsageAndLimits,
    ProcessWorkingSetWatch,
    ProcessUserModeIOPL,
    ProcessEnableAlignmentFaultFixup,
    ProcessPriorityClass,
    ProcessWx86Information,
    ProcessHandleCount,
    ProcessAffinityMask,
    ProcessPriorityBoost,
    ProcessDeviceMap,
    ProcessSessionInformation,
    ProcessForegroundInformation,
    ProcessWow64Information,
    ProcessImageFileName,
    ProcessLUIDDeviceMapsEnabled,
    ProcessBreakOnTermination,
    ProcessDebugObjectHandle,
    ProcessDebugFlags,
    ProcessHandleTracing,
    ProcessIoPriority,
    ProcessExecuteFlags,
    ProcessResourceManagement,
    ProcessCookie,
    ProcessImageInformation,
    MaxProcessInfoClass
  );

type
  PProcessBasicInformation = ^TProcessBasicInformation;
  TProcessBasicInformation = record
    ExitStatus                 : LongInt;
    PebBaseAddress             : Pointer;
    AffinityMask               : Cardinal;
    BasePriority               : LongInt;
    UniqueProcessId            : Cardinal;
    InheritedFromUniqueProcessId: Cardinal;
  end;

function NtQueryInformationProcess(ProcessHandle: THandle;
  ProcessInformationClass: TProcessInfoClass; ProcessInformation: Pointer;
  ProcessInformationLength: ULONG; ReturnLength: PULONG): LongInt stdcall;
type
  TFNNtQueryInformationProcess = function(ProcessHandle: THandle;
    ProcessInformationClass: TProcessInfoClass; ProcessInformation: Pointer;
    ProcessInformationLength: ULONG; ReturnLength: PULONG): LongInt stdcall;
const
  FNNtQueryInformationProcess: TFNNtQueryInformationProcess = nil;
begin
  if not Assigned(FNNtQueryInformationProcess) then
    FNNtQueryInformationProcess := TFNNtQueryInformationProcess(
      GetProcAddress(GetModuleHandle('ntdll.dll'), 'NtQueryInformationProcess'));
  if not Assigned(FNNtQueryInformationProcess) then
    Result := LongInt($C0000002) // STATUS_NOT_IMPLEMENTED
  else
    Result := FNNtQueryInformationProcess(ProcessHandle,
      ProcessBasicInformation, ProcessInformation, ProcessInformationLength,
      ReturnLength);
end;

//
// Wrapper
//

function GetProcessId(Process: THandle): DWORD stdcall;
type
  TFNGetProcessId = function(Process: THandle): DWORD stdcall;
const
  FNGetProcessId: TFNGetProcessId = nil;
var
  ExitCode: DWORD;
  BasicInformation: TProcessBasicInformation;
begin
  // Check for 'CurrentProcess' handle
  if Process = GetCurrentProcess() then
  begin
    Result := GetCurrentProcessId();
    Exit;
  end;
  // Check for exported Win32 API...
  if not Assigned(FNGetProcessId) then
    FNGetProcessId := TFNGetProcessId(
      GetProcAddress(GetModuleHandle(kernel32), 'GetProcessId'));
  if Assigned(FNGetProcessId) then
    Result := FNGetProcessId(Process)
  else
    // Try native versions
    if (DWORD(GetVersion()) > DWORD($80000000)) then
    begin
      // Win9x
      if GetExitCodeProcess(Process, ExitCode) then // validate handle
        Result := Process xor GetObsfucator()
      else
        Result := 0;
    end
    else
      // WinNT
      if NtQueryInformationProcess(Process, ProcessBasicInformation,
        @BasicInformation, SizeOf(TProcessBasicInformation), nil) >= 0 then
        Result := BasicInformation.UniqueProcessId
      else
        Result := 0;
end;

end.
[edit] Kleine Änderung: das Prozess-Handle wird nun unter Win9x mittels GetExitCodeProcess() auf Gültigkeit überprüft [/edit]

Meta777 16. Nov 2005 15:58

Re: TShellExecuteInfo.hProcess<>TProcessInformation.dw
 
Kuhl, Danke Nico!!!!!!

Gottes Segen


Edit: Diese GetObsfucator ruft ja GetCurrentProcessId auf, ich brauche aber die ProcessId des mit ShellExecuteEx() gestarteten Program? Ich hab den code nur überflogen, vielleicht muss das ja so sein?

Luckie 16. Nov 2005 16:09

Re: TShellExecuteInfo.hProcess<>TProcessInformation.dw
 
Na, programmiert da wieder jemand zu viel in C:
Code:
GetObsfucator[b]()[/b]
. :mrgreen:

aber,. Nico, dürfte ich dich mal auf mein Problem hinweisen: http://www.delphipraxis.net/internal...ct.php?t=68126 :angel2:

NicoDE 16. Nov 2005 16:18

Re: TShellExecuteInfo.hProcess<>TProcessInformation.dw
 
Zitat:

Zitat von Meta777
Diese GetObsfucator ruft ja GetCurrentProcessId auf, ich brauche aber die ProcessId des mit ShellExecuteEx() gestarteten Program? Ich hab den code nur überflogen, vielleicht muss das ja so sein?

Der Quellcode oben ist eine komplette Unit die nur eine Funktion zur Verfügung stellt (GetProcessId) - der Rest passiert automatisch.
Es fehlt mir die Zeit um auf Details einzugehen. Deshalb nur kurz: der sogenannte 'Obsfucator' ist ein Xor-Schlüssel mit dem unter Win9x Process-Handles in Process-IDs 'übersetzt' werden können...

edit@luckie: a) das ist gültiges Pascal (Delphi Language) b) wenn ich zuviel Zeit habe ;)

Luckie 16. Nov 2005 16:20

Re: TShellExecuteInfo.hProcess<>TProcessInformation.dw
 
Nur mal so aus Neugier: hast du das eben selber alles geschrieben :shock: oder hattest du das so rumliegen gehabt?

NicoDE 16. Nov 2005 16:22

Re: TShellExecuteInfo.hProcess<>TProcessInformation.dw
 
Zitat:

Zitat von Luckie
Nur mal so aus Neugier: hast du das eben selber alles geschrieben :shock: oder hattest du das so rumliegen gehabt?

Typen aus dem DDK kopiert, nach Pascal übersetzt, Logik implementiert und hier gepostet...


Alle Zeitangaben in WEZ +1. Es ist jetzt 19:16 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz