Problem with getcommandline and the path to the executable.

Posted on 2006-03-28
Last Modified: 2008-02-01
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
Question by:allmer
    LVL 5

    Expert Comment

    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().

    LVL 5

    Accepted Solution


    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.

    LVL 5

    Expert Comment

    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.

    LVL 5

    Author Comment

    I was completely blind to that!
    Thanks alot for pointing me to the problem.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
      Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now