1

I am trying to code an executable that will call an exe file when run.

The windows location of the file is:

"C:\\Users\\User1\\AppData\\Local\\Apptest\\Application\\apptest.exe"

Because it is located inside Local App Data folder for the user profile, it needs to be called with %LOCALAPPDATA% short-cut because I will share the execurable with others.

The below code calls above path via a short-cut:

"%LOCALAPPDATA%\\Apptest\\Application\\apptest.exe"

but it does not work.

#include "stdafx.h"
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <errno.h>
#include <ctype.h>
#include <shellapi.h>
#include <tchar.h>
#include <time.h>
#include <iostream>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){

    STARTUPINFO siStartupInfo;
    PROCESS_INFORMATION piProcessInfo;
    memset(&siStartupInfo, 0, sizeof(siStartupInfo));
    memset(&piProcessInfo, 0, sizeof(piProcessInfo));
    siStartupInfo.cb = sizeof(siStartupInfo);


    if (CreateProcess(_T("%LOCALAPPDATA%\\Apptest\\Application\\apptest.exe") ,NULL, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &siStartupInfo, &piProcessInfo)){
        WaitForSingleObject(piProcessInfo.hProcess, INFINITE);
        CloseHandle(piProcessInfo.hProcess);
        CloseHandle(piProcessInfo.hThread);
    }
    else {
        MessageBox(0, TEXT("Can not start program"), TEXT("AppTest"), MB_OK);
        exit(0);// Shutdown on fail
    }

    return 0;
}

How is %LOCALAPPDATA% short-cut is used in CreateProcess?

crayzeewulf
  • 5,840
  • 1
  • 27
  • 30
Nico Neill
  • 113
  • 2
  • 8
  • 2
    Possible duplicate of [expand file names that have environment variables in their path](http://stackoverflow.com/questions/1902681/expand-file-names-that-have-environment-variables-in-their-path) – MrEricSir Nov 25 '15 at 21:31
  • 1
    You should probably use [`SHGetKnownFolderPath`](https://msdn.microsoft.com/en-us/library/windows/desktop/bb762188%28v=vs.85%29.aspx) to retrieve the path to AppData and then append the rest of your path to that. – Praetorian Nov 25 '15 at 21:36
  • Alternatively, you can use the `ExpandEnvironmentStrings()` function. – Jonathan Potter Nov 25 '15 at 21:54
  • @Praetorian, SHGetKnownFolderPath seems fine. If you can post the code, I can accept your answer. – Nico Neill Nov 25 '15 at 21:57
  • Nah, feeling too lazy to write and verify the code :) Feel free to post your own answer. – Praetorian Nov 25 '15 at 22:10
  • Thank anyway. Just figured out `SHGetKnownFolderPath` is not compatible with XP. I think I will have a look for `ExpandEnvironmentStrings` – Nico Neill Nov 25 '15 at 22:14
  • 2
    On XP, you can use the deprecated [SHGetFolderPath](https://msdn.microsoft.com/en-us/library/windows/desktop/bb762181(v=vs.85).aspx). The call would look something like this: `SHGetFolderPath(0, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, my_dest_tchar_buffer);` – Christopher Oicles Nov 26 '15 at 00:18

0 Answers0