How can i open a file in Program Files folder

1
2
3
4
5
6
7
8
9
10
#include <windows.h>
char buf[255]={0};
int main(){
  DWORD dwLength = ExpandEnvironmentStrings("C:\\%ProgramFiles%\\Exam\\Exam.exe", buf, sizeof(buf));
if(dwLength>0)
MessageBox(NULL,"Success","",MB_OK);
else
MessageBox(NULL,"Not Success","",MB_OK);
return 0;
}

But it doesn't open.
You don't need the C:\\ in your string

ExpandEnvironmentStrings("%ProgramFiles%\\Exam\\Exam.exe", ...

But the code you pasted is just expanding an envrionment string. It's not trying to run an app.

See MSDN entry for CreateProcess()

Also, the use of the environment variables is deprecated; see SHGetFolderPath() instead (but note that the exact function you need depends on the versions of Windows you want to support)
Last edited on
I tried without C:\\:.But it doesn't work.I used CreateProcess()
1
2
3
4
5
6
7
8
9
10
11
#include <windows.h>
STARTUPINFO si;
    PROCESS_INFORMATION pi;
    LPTSTR szCmdline[] = {"C:\\ProgramFiles\\Exam\\Exam.exe -L -S"};
int main(){
if(CreateProcess(NULL, szCmdline,NULL, NULL,FALSE,0,NULL,NULL,&si,&pi ))
MessageBox(NULL,"Success","",MB_OK);
else
MessageBox(NULL,"Not Success","",MB_OK);
return 0;
}

But i take this error:6 C:\Exam.cpp cannot convert `TCHAR**' to `CHAR*' for argument `2' to `BOOL CreateProcessA(const CHAR*, CHAR*, _SECURITY_ATTRIBUTES*, _SECURITY_ATTRIBUTES*, BOOL, DWORD, void*, const CHAR*, _STARTUPINFOA*, _PROCESS_INFORMATION*)'

How can i use this CreateProcess() func.
What doesn't work without C:\ ?? You weren't expecting that function to run the exe, were you?

Regarding CreateProcess...

This line

LPTSTR szCmdline[] = {"C:\\ProgramFiles\\Exam\\Exam.exe -L -S"};

should be

TCHAR szCmdline[MAX_PATH] = "\"C:\\ProgramFiles\\Exam\\Exam.exe\" -L -S";

You were declaring an array of LPTSTRs with a single element. If declare just a char buffer containing you string.

Note that CreateProcess takes a char* not a const char*, which wy I have declared the saize of the array. So it's a buffer and not a const string.

Also, I have quote your path. When file paths have spaces in they need to be quoted

"C:\ProgramFiles\Exam\Exam.exe"

which becomes (in C++)

"\"C:\\ProgramFiles\\Exam\\Exam.exe\""

And you should zero the memory of si and pi (using memset)

And set the cb member of the STARTUPINFO info

si.cb = sizeof(STARTUPINFO);

Andy

If MAX_PATH isn't defined, try _MAX_PATH instead
Last edited on
Thank u andywestken.I tried this all.But it doesn't work.I am using Dev-C++
What 'does not work' ? Do you check GetLastError() after CreateProcess call ? Probably no, do you ?
Yep -- given all the above, it should be working by now.

Aa modoran said, when CreatreProcess fails you should call GetLastError to get the error code.

This is mentioned in the MSDN entry I previously mentioned! See that for further info.
Well if you copy pasted from andywestken's post it seems to me that a space got filtered out somehow. It's "Program Files" not "ProgramFiles". Then of course you need to make sure that you actually have access to that file with the account you are running this on, and that the path is correct.

Also, STOP USING BLOODSHED DEV-C++!!!! Switch to wxDev-C++: http://wxdsgn.sourceforge.net/ Trust me, you'll barely see a difference.
I know this.But i am developing myself.I am learning this.And i solved my problem.Ok?
Last edited on
@helegurbann -- Cool! So what was your solution? It's always nice to end a thread with the solution!

@Computergeek01 -- the space should have appeared when %ProgramFile% was replasced by a literal path. :-(

Andy

P.S. For anyone reading this thread in the future; first bear in mind Computergeek01's comments about rights. Secondly, you should not use either a literal path or the environment path in this way. The required folder path should be obtained using either SHGetKnownFolderPath or SHGetFolderPath (which one is required is dependent on which versions of Windows you want to support: see MSDN for details)
Topic archived. No new replies allowed.