[英]How do I run an external program?
我在Linux薄荷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語言完成它,我想用C ++做,我已經谷歌搜索了幾個小時,我看到了很多理論 。 這個主題似乎比量子物理學有更多的理論:)
我是一個Python人,因為我喜歡簡單,所以請提供一個簡單的代碼,如果可能的話,可以在32位和64位上運行。
我想做的事情,如果usr/share/application/firefox
可用,運行它,否則運行usr/share/application/googlechrome
你能告訴我為什么不能在Mac和Windows上運行相同的代碼?
這可以通過使用任一來實現system
,其是作為調用同os.system
在Python或fork
和execl
或popen
,其類似於調用subprocess.Popen
在Python。
一些例子如下所示。 它們應該在Linux或Mac上運行。
對於Windows,使用_system和_popen代替,使用if預定義函數的if定義函數。
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.