从 c++ 中调用 exe (视窗)
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您应该尝试使用 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
尝试打开文件进行读取,只是为了检查路径是否正确:
会发生什么?
Try opening the file for reading, just to check that you have the path right:
What happens?
您应该能够像这样使用 ShellExecute:(根据您的情况调整发送到 ShellExecute 的参数) http://msdn.microsoft.com/en-us/library/bb762153(VS.85).aspx?ppud=4
现在假设您正在使用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
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
System() 可能无法找到 cmd.exe 来打开您的环境。
尝试使用 cmd.exe 通过 /C 选项执行您的应用程序。
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.
使用 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
检查您的路径,并确保转义所有字符:
C:\\Users\Li..
Check your path, and make sure you escape all characters:
C:\\Users\Li..
错误是否来自运行主程序,而不是启动 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...
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().
只需首先更改到目录,就像在命令提示符下执行的操作一样:
Just change to the directory first, like you would do from the command prompt: