3

I'm on Linux mint 12.

I want to run a program usr/share/application/firefox and then pass a string anywhere. I haven't found a solution for Linux but from what I've seen so far, there are many theories for 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; 
} 

You can see many theories here the first answer describes how to get it done for Linux with C, I want to do it with C++, I've been googling for hours and i saw many theories. This subject appears to have more theories than quantum physics :)

I am a Python guy, because I like simplicity, so please give a simple code that would work on 32 and 64 bit if possible.

I would like to do something like if usr/share/application/firefox is available, run it, else run usr/share/application/googlechrome

And would you please tell me why can't the same code run on Mac and Windows?

qdii
  • 12,505
  • 10
  • 59
  • 116
user
  • 973
  • 4
  • 14
  • 29
  • 2
    I started editing this, but I think there's just too much wrong with it to salvage. Can you clean it up, including adding basic capitalisation and spelling? What is your question? This is not a webforum or a "give me the code" site. – Lightness Races in Orbit Mar 03 '12 at 22:41
  • Anything done in C can (almost always) also be done in C++, so finding a solution in C will still almost always compile with C++ as well. There also appears to be *more than one question here*. – Andrew Marshall Mar 03 '12 at 22:43
  • 4
    If you're using std::wstring then the "c" tag is a bit wrong. – Omri Barel Mar 03 '12 at 22:44
  • @LightnessRacesinOrbit I think the answer of ildjarn is what i need – user Mar 03 '12 at 22:46
  • maybe not coz he deleted his answer! strange – user Mar 03 '12 at 22:47
  • There is no answer to this "question", deleted or otherwise. – Lightness Races in Orbit Mar 03 '12 at 22:51
  • @LightnessRacesinOrbit a user called **ildjarn** left a comment about [this link](http://www.boost.org/doc/libs/1_49_0/libs/filesystem/v3/doc/index.htm) then he deleted his comment – user Mar 04 '12 at 00:16
  • @AndrewMarshall if you can just answer **how to open an external program** and ignore the other questions, I'll be grateful, i told you about what i have to do but that does not mean that you have to answer everything – user Mar 04 '12 at 00:20
  • @user: OK, so a comment not an answer. – Lightness Races in Orbit Mar 04 '12 at 00:35
  • From what you ask it seems more like you want to open a web site: in that case you should call `xdg-open` instead of a particular browser. – Matteo Italia Mar 04 '12 at 01:05
  • @MatteoItalia no i want to open an application, firefox was just an example – user Mar 04 '12 at 10:36

1 Answers1

8

This can be done using either system which is the same as calling os.system in Python or fork and execl or popen which is similar to calling subprocess.Popen in Python.

Some examples are shown below. They should work on Linux or Mac.

For Windows use _system and _popen instead, using if defined function of the C preprocessor.

IFDEF Example

#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

They are architecture dependent, though the location of the firefox binary may not be on various systems.

system

#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;
}
Appleman1234
  • 15,946
  • 45
  • 67