Hi All,
I have a C++ program( using VC++6.0 with MFC) which calls another C++ .exe and performs some actions on it. The problem I am facing is that I get the file name of the .exe by using a GetDirectory function. Thus I am able to get the full file name such as "C:\projectname\software\debug\something.exe". Next I am trying to use the ShellExecute function to use this file name in the parameters such as ShellExecute(NULL,"open" ,"PatternName", NULL, NULL, SW_SHOWNORMAL); where PatternName is "C:....." . This doesn't give any errors but just that the .exe doesn't open! I realised that if I use double slash in the file name it works fine. But since I get the file in the above format(with single slash) I am unable to do that. Is there any workaround?As far as the filename is concerned I am only able to break it into drive,dir,filename(debug), ext using splitpath.
I cannot give the file name directly because diff user save the file in diff locations and we need to change the path in each of their pgms which is not viable option.
Any help is appreciated..
Thanks in advance

It sounds to me Krisny that you've got to parse the file name apart returned from your directory obtaining function and insert the "\\" characters the compiler wants to see in the string you feed into ShellExecute(). Afterall, you said it works if you feed in a correctly specified path, i/e., "C:\\Dir1\\Dir2\\SomeProgram.exe". You are using MFC and have got a usable String class, No?

Thanks for your reply. I thought about it and I used the CString Replace option and converted all the single slashes to double!! it looks fine but still doesn't work correctly. May be some stupid mistake..
Thanks again

What about replacing the backslashes with forward slashes?
So wherever there was a single backslash, replace it with a forward slash. That should also fix your problem!

I know typically in Windows, filepaths look like this:

But in C++ forward slashes are also acceptable for file paths..

The obvious reason for using forward slashes is to avoid the problems with backslashes in C/C++ strings.

A backslash in a C/C++ string is an escape sequence for special characters like '\n'. Double backslashes '\\' is the escape sequence used to put a single backslash into a string!

So instead of replacing the single backslashes with doubles; replace them with forward slashes and you can circumvent the entire problem.. I use forward slashes all the time in file paths and have never experienced any problems!

Cheers for now,

Hi All,
Thanks for all your help. It works fine now.