# Retrieve External Process' current/working directory

## Recommended Posts

When writing a script, I can retrieve the script's working directory by using the following macro:

@WorkingDir

When launching an application from within a script, I can set its initial working directory by using the following syntax for the 'Run' function:

Run("x:\path\to\application.exe","x:\working\directory")

The question is:

How do I retrieve the working directory of an application that was already started when my script began execution?

I looked in the PSAPI documentation in MSDN, the Win32_Process WMI class, as well as multiple forum searches for variations on 'current/working directory existing/running process' without positive results... Any help getting this working would be greatly appreciated...

##### Share on other sites

When writing a script, I can retrieve the script's working directory by using the following macro:

@WorkingDir

When launching an application from within a script, I can set its initial working directory by using the following syntax for the 'Run' function:

Run("x:\path\to\application.exe","x:\working\directory")

The question is:

How do I retrieve the working directory of an application that was already started when my script began execution?

I looked in the PSAPI documentation in MSDN, the Win32_Process WMI class, as well as multiple forum searches for variations on 'current/working directory existing/running process' without positive results... Any help getting this working would be greatly appreciated...

This _ProcessListProperties function by PsaltyDS might help.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

##### Share on other sites

That UDF uses the Win32_Process WMI class, which does not provide the working directory.

I know it's possible, since Technet's Process Explorer can do it, but it seems like something that would be documented/known... (i.e. don't feel like setting up a reverse engineering setup to dig up a single API call)...

##### Share on other sites

TMXOD, The only way I know of doing this is to open and read the process memory - there's a pointer in the PEB block to 'Process Parameters' where you can read Current directory, DLL search path pattern, full exe path, Command line, Invokation method, and a number of other interesting strings. Doing this unfortunately means you'll need to have proper privileges to open a handle to read the process memory.

I have code for it, but its not very pretty and has limited error checking, but I just thought I'd try pointing you in the right direction

##### Share on other sites

I'd love to see that code (pretty or not )

##### Share on other sites

I know this is an old thread but nonetheless...

You can use the Sysinternals tool called handle.exe to get the working directory of an external process.

http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx

## Create an account

Register a new account