moyo
moyo

Reputation: 1402

Get Visual Studio project name (or exe file name ) to use it inside a C++ program

I need to execute this command in my C++ program:

WinExec("program_name.exe", SW_SHOW);

I'm using Visual Studio 2010. Is there any way to use a variable, constant, or something, in order to avoid the string "program_name.exe"? Right now it is working in my computer, but my program will be tested in a different computer where I don't know the name of the project/exe file.

Upvotes: 0

Views: 2546

Answers (4)

Secko
Secko

Reputation: 7716

(To elaborate abdul's answer:)

WinExec function check's the errors for you as you can see in the code below. If you are interested, you can read more about the WinExec function here.

Let's take a look at the function syntax:

UINT WINAPI WinExec(
  _In_  LPCSTR lpCmdLine,
  _In_  UINT uCmdShow
);

Since LPCSTR(Long Pointer to Const String) is actually a const string, you can pass a string type as it's first argument, but you need to convert it to a const char*, which is actually a LPCSTR(Long Pointer to Const String). In the code below it is done by using c_str().

#include<iostream>
#include<string>
#include<Windows.h>
using namespace std;

int main()
{
   string appName="yourappname.exe";
   int ExecOutput = WinExec(appName.c_str(), SW_SHOW);
    /* 
    If the function succeeds, the return value is greater than 31.
    If the function fails, the return value is one of the following error values.
    0 The system is out of memory or resources.
    ERROR_BAD_FORMAT The .exe file is invalid.
    ERROR_FILE_NOT_FOUND The specified file was not found.
    ERROR_PATH_NOT_FOUND The specified path was not found.
    */
    if(ExecOutput > 31){
        cout << appName << " executed successfully!" << endl;
    }else {
        switch(ExecOutput){
        case 0:
            cout << "Error: The system is out of memory or resources." << endl;
            break;
        case ERROR_BAD_FORMAT:
            cout << "Error: The .exe file is invalid." << endl;
            break;
        case ERROR_FILE_NOT_FOUND:
            cout << "Error: The specified file was not found." << endl;
            break;
        case ERROR_PATH_NOT_FOUND:
            cout << "Error: The specified path was not found." << endl;
            break;
        }
    }
   return 0;
}

I purposefully excluded the other function (that abdul created) to not delude from your question and to give you a clearer look at what you could actually do with the WinExec function. You could easily add that application checking function he created and put any other checks you need inside of it.

Upvotes: 1

Joe
Joe

Reputation: 2075

The executable name is passed as a parameter in the main function:

int main(int argc, char **argv)
{
    string exename = argv[0];
}

Upvotes: 2

aah134
aah134

Reputation: 860

Open conf file read app name, test if app do exist save path, then pass to the function

this is just an example, same idea

int main()
{
   string appName;
   if (getAppName(appName)) 
   {
      WinExec(appName.c_str(), SW_SHOW);
     /// WinExec(appName, SW_SHOW);
     /// ^^^ one of these
   }
   return 0;
}

function looks like this

bool getAppName(string& appName)
{
   string str;
   ///file open, and read;
   ///read(str)
   if(fileExist(str))
   {
       appName = str;
       return true;
   }
   return false;
}

Upvotes: -1

SuperSquareBoy
SuperSquareBoy

Reputation: 11

You could store the .exe file you want to run in the same location as the .exe that is running it, that way you could keep the hard coded constant name like it is.

Or a nice way would be to pass in the program name you want to run via the command line arguments, here is a tutorial for parsing the arguments: tutorial

That way you can replace that string with a variable, and pass in the name via command line when you run the .exe.

Upvotes: 0

Related Questions