Problem Source: http://www.cnblogs.com/del/archive/2008/07/30/1255869.html#1272342
In this example:
Code File:
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; button2: tbutton; procedure button1click (Sender: tobject); Procedure button2click (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} uses psapi; {required by the getmodulefilenameex function} {obtain the Location Based on the window handle Program Path function} function getprocessexepath (H: hwnd): string; var PID: Cardinal; phandle: thandle; Buf: array [0 .. max_path] of char; begin {obtain process ID} getwindowthreadprocessid (H, @ PID); {obtain Process Handle} phandle: = OpenProcess (process_all_access, false, pid ); {obtain process path} getmodulefilenameex (phandle, 0, Buf, length (BUF); closehandle (phandle); Result: = Buf; end; {test current program} procedure tform1.button1click (Sender: tobject); var path: string; begin path: = getprocessexepath (handle); showmessage (PATH); end; {test notepad-open a notepad} procedure tform1.button2click (Sender: tobject); var wh: hwnd; Path: string; begin wh: = findwindow ('notepad ', nil ); path: = getprocessexepath (Wh); showmessage (PATH); end.
Form file:
Object form1: tform1 left = 0 Top = 0 caption = 'form1 'clientheight = 116 clientwidth = 471 color = clbtnface font. charset = default_charset font. color = clwindowtext font. height =-11 font. name = 'tahoma 'font. style = [] oldcreateorder = false position = podesktopcenter pixelsperinch = 96 textheight = 13 object button1: tbutton left = 112 Top = 33 width = 233 Height = 25 caption = 'button1' taborder = 0 onclick = button1click end object button2: tbutton left = 112 Top = 64 width = 233 Height = 25 caption = 'button2' taborder = 1 onclick = button2click endend