Search code examples
cwindowsappdata

CreateProcessA %appdata%


I am trying to use CreateProcessA to call an application under the %appdata% directory.

For example, using the following:

CreateProcessA(
    NULL, "%appdata%\myfile.exe", NULL, NULL, FALSE,
    CREATE_NO_WINDOW, NULL, NULL, &sI, &pI
);

Do I need to use the complete path when calling myfile.exeor is there someway I can use %appdata% in the call to CreateProcessA?


Solution

  • As stated in MSDN, and since you're passing NULL to CreateProcessA as a first argument, CreateProcessA's 2nd argument : lpApplicationName is playing the role of the command line to be executed. Unless lpApplicationName points to an exe in a directory, the system will look for it in the following order

    1. The directory from which the application loaded.
    2. The current directory for the parent process.
    3. The 32-bit Windows system directory. Use the GetSystemDirectory function to get the path of this directory
    4. The 16-bit Windows system directory. There is no function that obtains the path of this directory, but it is searched. The name of this directory is System. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
    5. The directories that are listed in the PATH environment variable. Note that this function does not search the per-application path specified by the App Paths registry key. To include this per-application path in the search sequence, use the ShellExecute function.

    Therefore, unless the second argument of CreateProcessA is in the form {directory}/{executable_name}.{ext}, you'll have to either :

    1. Place executable_name in the same directory from which the application loaded
    2. Place executable_name in the same directory of the parent process
    3. Place executable_name in the Windows System32 directory : C:\Windows\System32
    4. Place executable_name in the Windows directory : C:\Windows
    5. Include the directory where executable_name is in the PATH

    As stated by Ben, take a look at ExpandEnvironmentStrings to modify PATH env variable.