如何运行外部程序
How do I run an external program?
我使用的是Linux mint 12。
我想运行一个程序usr/share/application/firefox
,然后在任何地方传递一个字符串。我还没有找到Linux的解决方案,但从我目前所看到的情况来看,有很多关于Windows的理论。
size_t ExecuteProcess(std::wstring FullPathToExe, std::wstring Parameters, size_t SecondsToWait)
{
size_t iMyCounter = 0, iReturnVal = 0, iPos = 0;
DWORD dwExitCode = 0;
std::wstring sTempStr = L"";
/* - NOTE - You should check here to see if the exe even exists */
/* Add a space to the beginning of the Parameters */
if (Parameters.size() != 0)
{
if (Parameters[0] != L' ')
{
Parameters.insert(0,L" ");
}
}
/* The first parameter needs to be the exe itself */
sTempStr = FullPathToExe;
iPos = sTempStr.find_last_of(L"\");
sTempStr.erase(0, iPos +1);
Parameters = sTempStr.append(Parameters);
/* CreateProcessW can modify Parameters thus we allocate needed memory */
wchar_t * pwszParam = new wchar_t[Parameters.size() + 1];
if (pwszParam == 0)
{
return 1;
}
const wchar_t* pchrTemp = Parameters.c_str();
wcscpy_s(pwszParam, Parameters.size() + 1, pchrTemp);
/* CreateProcess API initialization */
STARTUPINFOW siStartupInfo;
PROCESS_INFORMATION piProcessInfo;
memset(&siStartupInfo, 0, sizeof(siStartupInfo));
memset(&piProcessInfo, 0, sizeof(piProcessInfo));
siStartupInfo.cb = sizeof(siStartupInfo);
if (CreateProcessW(const_cast<LPCWSTR>(FullPathToExe.c_str()),
pwszParam, 0, 0, false,
CREATE_DEFAULT_ERROR_MODE, 0, 0,
&siStartupInfo, &piProcessInfo) != false)
{
/* Watch the process. */
dwExitCode = WaitForSingleObject(piProcessInfo.hProcess, (SecondsToWait * 1000));
}
else
{
/* CreateProcess failed */
iReturnVal = GetLastError();
}
/* Free memory */
delete[]pwszParam;
pwszParam = 0;
/* Release handles */
CloseHandle(piProcessInfo.hProcess);
CloseHandle(piProcessInfo.hThread);
return iReturnVal;
}
你可以在这里看到很多理论,第一个答案描述了如何用C在Linux上完成它,我想用C++来完成它。我已经在谷歌上搜索了几个小时,我看到了很多理论。这门学科似乎比量子物理学:)
有更多的理论
我是一个Python爱好者,因为我喜欢简单,所以如果可能的话,请给出一个可以在32位和64位上工作的简单代码。
我想做一些事情,比如如果usr/share/application/firefox
可用,运行它,否则运行usr/share/application/googlechrome
你能告诉我为什么同样的代码不能在Mac和Windows上运行吗?
这可以使用system
(与Python中调用os.system
相同)或fork
和execl
或popen
(与Python中调用subprocess.Popen
类似)来完成。
下面显示了一些示例。它们应该在Linux或Mac上运行。
对于Windows,请使用_system和_popen,使用C预处理器的if defined函数。
IFDEF示例
#ifdef __unix__ /* __unix__ is usually defined by compilers targeting Unix systems */
# callto system goes here
#elif defined _WIN32 /* _Win32 is usually defined by compilers targeting 32 or 64 bit Windows systems */
# callto _system goes here
#endif
它们依赖于体系结构,尽管firefox二进制文件的位置可能不在各种系统上。
系统
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
system("/usr/share/application/firefox");
printf("Command done!");
return 0;
}
popen
#include <stdio.h>
int main(int argc, char **argv))
{
FILE *fpipe;
char *command="/usr/share/application/firefox";
char line[256];
if ( !(fpipe = (FILE*)popen(command,"r")) )
{ // If fpipe is NULL
perror("Problems with pipe");
exit(1);
}
while ( fgets( line, sizeof line, fpipe))
{
printf("%s", line);
}
pclose(fpipe);
return 0;
}
相关文章:
- 如何运行外部程序,向其传递内存地址以读取/写入?
- C++外部程序的输入和输出管道
- 如何调用外部程序并在 C/C++ 中获取多个返回值
- GetAsyncKeyState() - 外部程序会干扰它
- 运行外部程序并获得返回的整数
- 在 c++ 中捕获外部程序的退出代码
- 从外部程序中捕获标准和标准输出 C++.
- 如何通过C 在Mac OS中运行外部程序
- 一个唱片播放器如何使用外部程序在CS:GO中杀人
- Qt:如何通过外部程序打开文件,"open with..."对话框
- C++外部程序集:我的代码中的错误在哪里
- 通过Qt崩溃运行外部程序
- Cpp 中的插件 - 执行外部程序函数
- Qt 执行外部程序
- 停止来自 C++ 代码的外部程序
- 视觉 从 C++ 应用程序执行的外部程序中的 CPU 负载有限
- 如何从服务启动外部程序
- C++暂停外部程序
- 几次调用后,外部程序的执行失败
- 如何在Windows中不显示cmd窗口的情况下运行带参数的外部程序