WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Windows UDFs

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Get EXE Path of the Active Window


<HR>
#DefineFunction udfGetExePath( prochandle )
   ;Load Dll
   dllname=StrCat(DirWindows(1),"Psapi.dll")

   ;Allocate buffer
   bufsize = 256
   binbuf=BinaryAlloc( bufsize )

   ; Get the Processes filename
   ;DWORD WINAPI GetProcessImageFileName(
   ;  __in   HANDLE hProcess,
   ;  __out  LPTSTR lpImageFileName,
   ;  __in   DWORD nSize
   ;)
   rslt = DllCall( dllname, long:'GetProcessImageFileNameA', long:prochandle,  lpbinary:binbuf, long:256)
   If rslt == 0
      err = DllLastError()
      Message('GetModuleFileNameExA Failed', err )
      Exit
   EndIf

   ; Note DllCalls do not set EOD point in buffer.
   ; EOD Point MUST be set manually with BinaryEODSet
   BinaryEodSet( binbuf, bufsize )  ;
   devicepath = BinaryPeekStr( binbuf, 0, bufsize )

   ;Free buffer
   BinaryFree( binbuf )
   ;Message( 'Window module filename is', filename )
   Return devicepath
#EndFunction


#DefineFunction udfGetDriveLetter(DevicePath)
   ;GetProcessImageFileName, returns  filenames like \Device\HarddiskVolume1\Program Files\Abc.exe
   ;Because these are the real paths as far as the OS is concerned.
   ;You can’t get a DOS drive name directly from this sort of path, instead you’d can use QueryDosDevice
   ;with each drive letter from A to Z and if what it returns matches the path you have, you’ve got your drive letter.
   ;Here’s a little UDF you can use:

   ;Load Dll
   dllname=StrCat(DirWindows(1),"Kernel32.dll")


   ;Loop through the alphabet
   For xx = 1 To 26
         letter = Num2Char(xx+64): ':'

         ;Allocate buffer
         bufsize = 256
         binbuf=BinaryAlloc( bufsize )

         ;DWORD WINAPI QueryDosDevice(
         ;  __in_opt  LPCTSTR lpDeviceName,
         ;  __out     LPTSTR lpTargetPath,
         ;  __in      DWORD ucchMax
         ;);
         rslt = DllCall( dllname, long:'QueryDosDeviceA', lpstr:letter,  lpbinary:binbuf, long:256)
         If rslt == 0
            err = DllLastError()
            ;Message('QueryDosDevice Failed', err )
            Continue
         EndIf
         BinaryEodSet( binbuf, bufsize )  ;
         devicename = BinaryPeekStr( binbuf, 0, bufsize )
         BinaryFree( binbuf )
         ;Compare devive path returned from QueryDosDeviceA to devicepath passed to function
         If StriCmp(devicename, devicepath) == 0
             Return letter
         EndIf
   Next
   Return 0;
#EndFunction




activewin = WinGetactive()


AddExtender("wwprc44I.dll")

;Display a list of processes
proclist=tListProc()

For x = 1 To ItemCount(proclist, @TAB)
   data = ItemExtract( x, proclist, @TAB )
   procid = ItemExtract( 2, data, '|' )
   If procid == 0 Then Continue
   winnames = WinItemProcId(procid, 0 , 1)
   For y = 1 To ItemCount( winnames, @TAB )
      name = ItemExtract( y, winnames, @TAB )
      If name == activewin
           ;Get Process Handle
            ErrorMode(@OFF)
            hProcess=tOpenProc(procid,2)
            ErrorMode(@CANCEL)
            If hProcess==0
               Message("Unable to open process",tGetLastError())
               Exit
            EndIf

            ;Obtain the device path to the process
            devicepath = udfGetExePath( hProcess )

            ;Parse out device data
            devicename = '\' : ItemExtract( 2, devicepath, '\' ) : '\' : ItemExtract( 3, devicepath, '\' )

            ;Obtain the actual drive letter from the device path
            DRIVE = udfGetDriveLetter(devicename)

            ;Get rest of device path
            endpath = ''
            cnt = ItemCount( devicepath, '\' )
            For index = 4 To cnt
               data = ItemExtract( index, devicepath, '\' )
               endpath = endpath : '\': data
            Next

            ;Append drive letter to path
            exepath = DRIVE : endpath
            Message( 'Exe Path of Process', exepath )
      EndIf
   Next
Next


Exit

Article ID:   W17477
File Created: 2013:04:01:09:23:02
Last Updated: 2013:04:01:09:23:02