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)
            return(FP_SHORT);
      ifstream is;
      is.open(filePath,ios::in);
      if(!is.is_open()) {
            return(NO_FILE);
      }
      else {
            is.close();
            return(FILE_EXISTS);
      }
   }
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
LVL 5
allmerAsked:
Who is Participating?
 
gerhardschoemanCommented:
allmer,

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.

regards;
0
 
gerhardschoemanCommented:
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().

regards;
0
 
gerhardschoemanCommented:
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.

regards;
0
 
allmerAuthor Commented:
I was completely blind to that!
Thanks alot for pointing me to the problem.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.