Da się uzyskać PID. Tylko że nie z ShellExecute, a z ShellExecuteEx. Ta druga funkcja w określonych warunkach zwraca uchwyt procesu, który uruchomiła. Potem pozostaje tylko pobrać PID procesu o danym uchwycie:
uses
Windows, SysUtils, ShellApi;
function NtQueryInformationProcess(hProcess: DWORD; InfoClass: Integer; ProcessInfo: Pointer; ProcessInfoLen: Integer; ReturnLen: Pointer): Integer; stdcall; external 'ntdll.dll';
type
PROCESS_BASIC_INFORMATION = packed record
Reserved1: Pointer;
PebBaseAddress: Pointer;
Reserved2: array[0..1] of Pointer;
UniqueProcessId: DWORD;
Reserved3: Pointer;
end;
var
shi : TShellExecuteInfo;
pbi : PROCESS_BASIC_INFORMATION;
begin
FillChar(shi, SizeOf(shi), 0);
shi.cbSize := SizeOf(shi);
shi.fMask := SEE_MASK_NOCLOSEPROCESS;
shi.lpVerb := 'open';
shi.lpFile := 'DSC01917.JPG';
shi.nShow := SW_SHOWNORMAL;
ShellExecuteEx(@shi);
NtQueryInformationProcess(shi.hProcess, 0, @pbi, SizeOf(pbi), nil);
MessageBox(0, PChar(Format('%d', [pbi.UniqueProcessId])), 'ProcessId', MB_ICONINFORMATION);
end.
Ten kawałek kodu zadziała tylko w Windows NT4/2000/XP/2003.