Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何运行外部程序?_C++_Linux - Fatal编程技术网

C++ 如何运行外部程序?

C++ 如何运行外部程序?,c++,linux,C++,Linux,我在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

我在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; 
} 
size\u t ExecuteProcess(std::wstring FullPathToExe,std::wstring参数,size\u t SecondsToWait)
{ 
size_t iMyCounter=0,iReturnVal=0,IPO=0;
DWORD dwExitCode=0;
std::wstring-sTempStr=L“”;
/*-注意-您应该在此处检查exe是否存在*/
/*在参数的开头添加空格*/
如果(Parameters.size()!=0)
{ 
如果(参数[0]!=L“”)
{ 
参数。插入(0,L“”);
} 
} 
/*第一个参数必须是exe本身*/
sTempStr=FullPathToExe;
iPos=sTempStr.find\u last\u of(L“\\”);
sTempStr.erase(0,iPos+1);
参数=sTempStr.append(参数);
/*CreateProcessW可以修改参数,因此我们可以分配所需的内存*/
wchar_t*pwszParam=新的wchar_t[Parameters.size()+1];
如果(pwszParam==0)
{ 
返回1;
} 
常量wchar_t*pchrTemp=Parameters.c_str();
wcscpy_s(pwszParam,Parameters.size()+1,pchrTemp);
/*CreateProcess API初始化*/
STARTUPINFOW siStartupInfo;
进程信息piProcessInfo;
memset(&siStartupInfo,0,sizeof(siStartupInfo));
memset(&piProcessInfo,0,sizeof(piProcessInfo));
siStartupInfo.cb=sizeof(siStartupInfo);
如果(CreateProcessW(const_cast(FullPathToExe.c_str()),
pwszParam,0,0,false,
创建默认错误模式,0,0,
&siStartupInfo和piProcessInfo)!=false)
{ 
/*观察过程。*/
dwExitCode=WaitForSingleObject(piProcessInfo.hProcess,(SecondsToWait*1000));
} 
其他的
{ 
/*CreateProcess失败*/
iReturnVal=GetLastError();
} 
/*可用内存*/
删除[]pwszParam;
pwszParam=0;
/*释放手柄*/
CloseHandle(piProcessInfo.hProcess);
CloseHandle(piProcessInfo.hThread);
返回i返回值;
} 
<>你可以看到很多理论,第一个答案描述了如何用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;
}
#包括
#包括
int main(int argc,字符**argv)
{
系统(“/usr/share/application/firefox”);
printf(“命令完成!”);
返回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;
}
#包括
int main(int argc,字符**argv))
{
文件*fpipe;
char*command=“/usr/share/application/firefox”;
字符行[256];
if(!(fpipe=(FILE*)popen(命令,“r”))
{//如果fpipe为NULL
perror(“管道问题”);
出口(1);
}
而(fgets(管线、管线尺寸、管道))
{
printf(“%s”,第行);
}
pclose(fpipe);
返回0;
}

我开始编辑这篇文章,但我认为这篇文章有太多的错误需要补救。你能整理一下吗,包括增加基本的大写字母和拼写?你的问题是什么?这不是一个网络论坛,或者是“给我代码”站点。C中的任何事情(几乎总是)也可以用C++来完成,所以在C中找到一个解决方案几乎总是用C++来编译。这里似乎还有不止一个问题。如果您使用的是std::wstring,那么“c”标记有点错误。@LightnessRacesinOrbit我认为ildjarn的答案是我需要的,可能不是因为他删除了他的答案!奇怪的