Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problem with getcommandline and the path to the executable.

Posted on 2006-03-28
4
Medium Priority
?
514 Views
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)
            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
0
Comment
Question by:allmer
  • 3
4 Comments
 
LVL 5

Expert Comment

by:gerhardschoeman
ID: 16308640
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
 
LVL 5

Accepted Solution

by:
gerhardschoeman earned 2000 total points
ID: 16308675
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
 
LVL 5

Expert Comment

by:gerhardschoeman
ID: 16308714
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
 
LVL 5

Author Comment

by:allmer
ID: 16309252
I was completely blind to that!
Thanks alot for pointing me to the problem.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

810 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