Problem with getcommandline and the path to the executable.

Hi Experts,
I ran into problems using the GetCommandLine function.
Here is what I do:
  CString exeLoc = "";
  exeLoc = GetCommandLine();
So far everything is fine.
Now I would like to test for the existance of the file with a very simple function:
   int CFileEx::CheckExistance(const char* filePath) {
      if(strlen(filePath) <= MINFILEPATHLENGTH)
      ifstream is;,ios::in);
      if(!is.is_open()) {
      else {
Calling this function returns NO_FILE which apparently is not true since the executable is currently running.
Putting the path into var and repeating the process works fine, though:

  int ret=fe.CheckExistance("d:\\Programming\\Autherm\\Debug\\Autherm\\Autherm.exe");
This call actually returns FILE_EXISTS as assumed.

I am working with VS .net and am currently in Debug modus.
What am I missing, where could this possibly go wrong?
Thanks, Jens
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You mean if you call the function with exeloc as parameter ie CheckExistance(exeLoc) it returns NO_FILE. In that case it is quite obvious that exeLoc does not contain only the filename or the correct filename. Remember GetCommandLine will return the whole commandline including arguments etc. and ifstream::open() will not be able to open such a file. Try and see in debug mode what exactly exeLoc contains before you use it as parameter to CheckExistance().


in fact, my first suspicions are confirmed. I did a quick check. GetCommandLine returns the string as ""C:\....\...exe"". NOTICE the EXTRA " (double qoutes in the beginning and end) within the string. You will have to remove the " from the string before you can use it as argument to ifstream::open().

In other words, you can't just use the return from GetCommandLine() directly. You will have to parse and format it before you can use it as arguments to other functions.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
In .NET you can use the Environment.GetCommandLineArgs function to return the commandline already parsed as an array of string. This resembles to traditional C++ (char* argv[]). Then you can get the filename of the program from the 0 index in the array.

allmerAuthor Commented:
I was completely blind to that!
Thanks alot for pointing me to the problem.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.