简体   繁体   English

获取Visual Studio项目名称(或exe文件名)以在C ++程序中使用它

[英]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: 我需要在我的C ++程序中执行此命令:

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"? 我正在使用Visual Studio 2010.有没有办法使用变量,常量或其他东西,以避免字符串“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. 现在它正在我的电脑上工作,但我的程序将在不同的计算机上进行测试,我不知道项目/ exe文件的名称。

The executable name is passed as a parameter in the main function: 可执行文件名作为main函数中的参数传递:

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

(To elaborate abdul's answer:) (详细说明阿卜杜勒的答案:)

WinExec function check's the errors for you as you can see in the code below. WinExec函数检查您的错误,如下面的代码所示。 If you are interested, you can read more about the WinExec function here . 如果您有兴趣,可以在这里阅读有关WinExec功能的更多信息。

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). 由于LPCSTR (长指向const字符串)实际上是一个const string ,你可以传递一个字符串类型作为它的第一个参数,但你需要将它转换为一个const char *,它实际上是一个LPCSTR (指向常量字符串的长指针) 。 In the code below it is done by using c_str() . 在下面的代码中,它使用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. 我有目的地排除了另一个功能(abdul创建的)不要从你的问题中脱离出来,并让你更清楚地了解你可以用WinExec函数实际做些什么。 You could easily add that application checking function he created and put any other checks you need inside of it. 您可以轻松添加他创建的应用程序检查功能,并在其中放置您需要的任何其他检查。

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. 您可以将要运行的.exe文件存储在与运行它的.exe相同的位置,这样就可以保持硬编码的常量名称。

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. 这样你可以用变量替换该字符串,并在运行.exe时通过命令行传入名称。

Open conf file read app name, test if app do exist save path, then pass to the function 打开conf文件读取应用程序名称,测试app是否存在保存路径,然后传递给该函数

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio Addin - 获取名称\ c ++项目的二进制路径? - Visual Studio Addin - Get Name\Path of binary of a c++ project? C++ 带空格的文件名导致 Visual Studio Code 出错 - C++ file name with space causes error in Visual Studio Code 在 C++ 项目的 Visual Studio 安装项目结束时启动 exe - Launching an exe at the end of a Visual Studio Setup Project of a c++ Project 按名称链接导入 (C++/Visual Studio) - Link imports by name (C++/Visual Studio) Visual Studio C++:带有谷歌测试的单元测试 exe 项目? - Visual Studio C++: Unit test exe project with google test? 将Visual Studio C ++ dll项目转换为exe - convert visual studio C++ dll project to exe Visual Studio C ++测试“名称必须是名称空间名称”错误 - Visual Studio C++ testing “name must be a namespace name” error Visual Studio生成调试目标.exe文件名不匹配 - Visual Studio build debug target .exe file-name mismatch 如果我使用 ./program_name 执行我的程序,如何在 C++ 中获取 file_name - How to get the file_name in C++ if I executed my program with ./program_name<file_name c ++ visual studio 2010-如何在其他文件夹名称下创建项目? - c++ visual studio 2010 - How to create a project under a different folder name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM