Ver Mensaje Individual
  #9  
Antiguo 02-07-2015
Avatar de nlsgarcia
[nlsgarcia] nlsgarcia is offline
Miembro Premium
 
Registrado: feb 2007
Ubicación: Caracas, Venezuela
Posts: 2.206
Reputación: 21
nlsgarcia Tiene un aura espectacularnlsgarcia Tiene un aura espectacular
Nipx4215,

Cita:
Empezado por nlsgarcia
...¿Revisastes el código sugerido en el Msg #3?...
Cita:
Empezado por Nipx4215
...desafortunadamente solo funciona en Windows 32 bits...


Revisa este código:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ShellAPI, TlHelp32, PsAPI;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  function QueryFullProcessImageName(hProcess : THandle;
                                     dwFlags : DWORD;
                                     lpExeName : Pchar;
                                     lpdwSize : PDWORD) : Bool; stdCall;  external('Kernel32.dll') name 'QueryFullProcessImageNameA';

  function IsWow64Process(hProcess: THandle; Wow64Process : PBool) : Bool; stdCall; external('Kernel32.dll');

var
  Form1: TForm1;

implementation

{$R *.dfm}

function GetParentProcess(PID : DWORD) : String;
var
   SnapShot : THandle;
   ProcessEntry32 : TProcessEntry32;
   PPID : DWORD;
   PPHandle : THandle;
   PPPath : PAnsiChar;
   Size : Integer;
   IsWow64 : BOOL;

begin

   SnapShot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

   if SnapShot <> INVALID_HANDLE_VALUE then
   begin

      GetMem(PPPath, MAX_PATH);
      Size := MAX_PATH;

      ProcessEntry32.dwSize := SizeOf(ProcessEntry32);

      if Process32First(SnapShot, ProcessEntry32) then
      repeat

         if ProcessEntry32.th32ProcessID = PID then
         begin

            PPID := ProcessEntry32.th32ParentProcessID;
            PPHandle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False,PPID);

            if PPHandle <> 0 then
            begin

               isWow64Process(GetCurrentProcess, @IsWow64);

               if IsWow64 then
                  QueryFullProcessImageName(PPHandle, 0, PPPath, @Size)
               else
                  GetModuleFileNameEx(PPHandle, 0, PPPath, Size);

               Result := PPPath;

            end
            else
               Result := EmptyStr;

            CloseHandle(PPHandle);
            Break;

         end;

      until not Process32Next(SnapShot, ProcessEntry32);

      CloseHandle(SnapShot);
      FreeMem(PPPath);

   end;

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
   MessageDlg(GetParentProcess(GetCurrentProcessId),mtInformation,[mbOK],0);
end;

end.
El código anterior en Delphi 7 sobre Windows 7 Professional x64, Obtiene la ruta y nombre del proceso padre de la aplicación lo cual permite determinar desde donde fue ejecutada la misma, como se muestra en la siguiente imagen:



Notas:

1- El código identifica si se esta ejecutando una aplicación de 32 bits en WOW64 para determinar como obtener la ruta y nombre del proceso padre.

2- El código fue probado en Windows 7 Professional x32 y Windows 7 Professional x64, funcionando correctamente según lo esperado.

3- Esta versión es una mejora del código propuesto en el Msg #3.

Revisa esta información:
Espero sea útil

Nelson.
Responder Con Cita