从 c++ 中调用 exe (视窗)

发布于 2024-09-16 17:39:11 字数 305 浏览 5 评论 0原文

我正在使用 VS2010,我想调用我在另一个目录中创建的 exe 文件。 我已尝试以下操作:

int main(){

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

但出现“系统找不到指定的文件”错误。

我尝试直接从命令行运行 exe 文件,它仅在我位于其目录中时才有效。 您能告诉我如何从不同的目录运行它吗?

(我用的是win7)

谢谢, 李。

I'm using VS2010 and I would like to call an exe file which I've created in another directory.
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.
Could you please tell me how can I run it from a different directory?

(I'm using win7)

Thanks,
Li.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

轻许诺言 2024-09-23 17:39:11

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

You should try using CreateProcess Windows API funcion: http://msdn.microsoft.com/en-us/library/ms682425%28VS.85%29.aspx

七秒鱼° 2024-09-23 17:39:11

尝试打开文件进行读取,只是为了检查路径是否正确:

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

会发生什么?

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?

故人的歌 2024-09-23 17:39:11

您应该能够像这样使用 ShellExecute:(根据您的情况调整发送到 ShellExecute 的参数) 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

现在假设您正在使用Win7,您可能遇到权限问题,您需要以更高级别运行(即管理员),您可以通过以管理员身份打开 cmd 并从另一个目录运行您的 exe 来测试这一点

,正如 Steve 上面提到的,您当然可以使用 CreateProcess。

HTH、

EB

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

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 (i.e. administrator) you can test this by opening cmd as admin and running your exe from another directory

and as Steve mentioned above you can certainly use CreateProcess.

HTH,

EB

灼痛 2024-09-23 17:39:11

System() 可能无法找到 cmd.exe 来打开您的环境。
尝试使用 cmd.exe 通过 /C 选项执行您的应用程序。

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

System() may not be able to find cmd.exe to open your environment.
Try using cmd.exe to execute your app via the /C option.

System("C:\\WINDOWS\\system32\cmd.exe /C \"C:\\Users\\Li\\Desktop\\Debug\\modelExample_4pcs.exe\"");
关于从前 2024-09-23 17:39:11

使用 CreateProcess 尝试此操作。比使用 system() 更少(或至少不同)的环境依赖性。如果仍然失败,至少您会得到一个不错的 Win32 错误代码。

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

Try this using CreateProcess. Less (or at least different) environmental dependencies than using system(). At least you will get a nice Win32 error code if this still fails.

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

删除会话 2024-09-23 17:39:11

检查您的路径,并确保转义所有字符:C:\\Users\Li..

Check your path, and make sure you escape all characters: C:\\Users\Li..

感性 2024-09-23 17:39:11

错误是否来自运行主程序,而不是启动 modelExample_4pcs.exe?尝试注释掉 system() 调用,看看是否出现相同的错误。

当您位于其文件夹之外时,您的主程序不在路径上......

Is the error from running the main program, not from launching modelExample_4pcs.exe? Try commenting out the system() call and see if you get the same error.

Your main program is not on the path when you're outside its folder...

温柔戏命师 2024-09-23 17:39:11

modelExample_4pcs.exe 是否尝试从当前工作文件夹加载另一个文件,这就是生成错误的原因?

也许在调用 system() 之前尝试 chdir() 。

Is modelExample_4pcs.exe trying to load another file from the current working folder, and THAT's what's generating the error?

Maybe try chdir() before the call to system().

想挽留 2024-09-23 17:39:11

只需首先更改到目录,就像在命令提示符下执行的操作一样:

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

Just change to the directory first, like you would do from the command prompt:

system("C: && CD \\Users\\Li\\Desktop\\Debug\\ && modelExample_4pcs.exe"); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文