简体   繁体   English

从c ++(windows)中调用exe

[英]call an exe from within c++ (windows)

I'm using VS2010 and I would like to call an exe file which I've created in another directory. 我正在使用VS2010,我想调用我在另一个目录中创建的exe文件。 I've tried the following: 我尝试过以下方法:

int main(){

 system("C:\\Users\\Li\\Desktop\\Debug\\modelExample_4pcs.exe");     
     return 0;
};

but I get "The system could not find the file specified" error. 但我得到“系统找不到指定的文件”错误。

I've tried to run the exe file directly from the command line, and it only works when I'm inside its directory. 我试图直接从命令行运行exe文件,它只在我在其目录中时才有效。 Could you please tell me how can I run it from a different directory? 你能告诉我如何从不同的目录运行它?

(I'm using win7) (我正在使用win7)

Thanks, Li. 谢谢,李。

您应该尝试使用CreateProcess Windows API功能: http//msdn.microsoft.com/en-us/library/ms682425%28VS.85%29.aspx

Try opening the file for reading, just to check that you have the path right: 尝试打开文件进行阅读,只是为了检查路径是否正确:

char* filename = "C:\\Users\\Li\\Desktop\\Debug\\modelExample_4pcs.exe" ;
FILE* fp = fopen (filename, "rb") ; // Open for reading, binayr mode
if (fp == 0) {
  printf ("Duh! File not found\n") ;
  exit (0) ;
  }
printf ("File found\n") ;
fclose (fp) ;

// Now try the system call, as before:
system(filename);

What happens? 怎么了?

You should be able to use ShellExecute like so: (adjusting the params sent to ShellExecute for your situation) http://msdn.microsoft.com/en-us/library/bb762153(VS.85).aspx?ppud=4 您应该能够像这样使用ShellExecute :(根据您的情况调整发送到ShellExecute的params) http://msdn.microsoft.com/en-us/library/bb762153( VS.85) .aspx?ppud=4

HINSTANCE hinst = ShellExecute( NULL, _T("open"), commandLine.c_str(), additionalParams.c_str(), NULL, SW_RESTORE );

if(hinst <= (HINSTANCE)SHELLEXERROR)// see: http://msdn2.microsoft.com/en-us/library/bb762153.aspx for further info on the return values

Now given that you are using Win7, you may be having a privilege issue and you need to run at an elevated level (ie administrator) you can test this by opening cmd as admin and running your exe from another directory 现在假设你正在使用Win7,你可能有一个特权问题而且你需要在更高级别(即管理员)运行你可以通过打开cmd作为管理员并从另一个目录运行你的exe来测试这个

and as Steve mentioned above you can certainly use CreateProcess. 正如史蒂夫上面提到的,你当然可以使用CreateProcess。

HTH, HTH,

EB EB

System() may not be able to find cmd.exe to open your environment. System()可能无法找到cmd.exe来打开您的环境。 Try using cmd.exe to execute your app via the /C option. 尝试使用cmd.exe通过/ C选项执行您的应用程序。

System("C:\\WINDOWS\\system32\cmd.exe /C \"C:\\Users\\Li\\Desktop\\Debug\\modelExample_4pcs.exe\"");

Try this using CreateProcess. 使用CreateProcess尝试此操作。 Less (or at least different) environmental dependencies than using system(). 比使用system()更少(或至少不同)环境依赖性。 At least you will get a nice Win32 error code if this still fails. 如果仍然失败,至少你会得到一个不错的Win32错误代码。

http://msdn.microsoft.com/en-us/library/ms682425(VS.85).aspx http://msdn.microsoft.com/en-us/library/ms682425(VS.85).aspx

检查你的路径,并确保你逃脱所有字符: C:\\\\Users\\Li..

Is the error from running the main program, not from launching modelExample_4pcs.exe? 运行主程序时出错,而不是启动modelExample_4pcs.exe吗? Try commenting out the system() call and see if you get the same error. 尝试注释掉system()调用,看看是否得到了同样的错误。

Your main program is not on the path when you're outside its folder... 当你在文件夹之外时,你的主程序不在路径上......

Is modelExample_4pcs.exe trying to load another file from the current working folder, and THAT's what's generating the error? 是modelExample_4pcs.exe试图从当前工作文件夹加载另一个文件,那是什么产生错误?

Maybe try chdir() before the call to system(). 也许在调用system()之前尝试chdir()。

只需先更改到目录,就像从命令提示符那样:

system("C: && CD \\Users\\Li\\Desktop\\Debug\\ && modelExample_4pcs.exe"); 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM