none
SPAWN , POPEN usarli per comandare un programma esterno dal codice C++ RRS feed

  • Domanda

  • Io dovrei usare un programma esterno nel mio codice. Mi è stato consigliato dall'autore del programma di usare degli script. Essendo un principiante, dovrebbe riuscirmi più facile che modificare il suo codice (secondo lui). Chiarimenti: per quanto riguarda _POPEN, l'esempio da msdn di VC++ 2010, usa _popen e _pclose per ricevere una stream di testo da un processo di sistema. Bene non capisco la riga del codice:

    if( (pPipe = _popen( "dir *.c /on /p", "rt" )) == NULL )
          exit( 1 );

    e ancora prima, cosa si dichiara con la riga:

    char   psBuffer[128]; ?  http://msdn.microsoft.com/en-us/library/96ayss4b(VS.80).aspx

    Quest'altro esempio trovato su yahoo mi appare più semplice, per spiegarmelo:

    "Volendo invece leggere l'output di un programma, allora ti consiglio di usare popen (man 3 popen). Popen accetta 2 parametri, uno e` il comando da eseguire e l'altro e` la direzione della pipe (se in lettura o scrittura).

    FILE elenco = popen("ls /path/mio/dir","r") ;

    ti lancia ls (sulla directory richiesta) ed apre un file (di quelli di stdio.h) da cui pui leggere l'output del comando ls. Peraltro, non occorre fare il cd verso una nuova directory, basta passarne il path come argomento a ls. "

    Allora mettiamo che io ho un eseguibile, che accetta come parametri una stringa lunga 3 caratteri, preceduta dal comando "set" e mi restituisce una stringa lunga 3 caratteri. [Ovvero spiegazione del funzionamento del programma child: io lancio il file esempio.exe (modalità console), e digito "set a1b" invio, poi "st" invio. Il programma mi restituisce una stringa "xx1". Se invece digito "set a2b" invio, poi "st" invio, il programma mi restituisce una stringa "xx2"]. 

     Allora io voglio chiamare dal mio codice C++ il programma esempio.exe, inviare una stringa di 3 caratteri preceduta dalla parola chiave "set" e seguita dal comando st e leggere la stringa restituita. Credo che con SPAWN e POPEN si possa fare. (in pratica la stringa in ingresso è "set,spazio più 3 caratter"i, inviata al programma child esempio.exe e seguita dal comando "st" che il programma child interpreta come deve).




    beginner of planning

    domenica 17 aprile 2011 23:27

Risposte

  • Mi rispondo da solo. Girovagando in rete ho trovato questa pagina inerente al discorso sopra. Se qualcuno ha  qualche cosa d'aggiungere al riguardo (consigli/commenti/aggiornamenti), sarà gradito.

    : "

    Using EXEC and SPAWN to Link to Another Program

    If you are writing a program in C or C++ there are occasions when you want another program to execute. Sometimes you need your program to continue running while this other program runs and other times you just want to start the other program and terminate your program.

    The commands to start other programs from within a C or C++ program are called EXEC and SPAWN and they come in a number of different versions depending on how you want to pass parameters to the other program.

    Spawn

    The Spawn command is used to start execution of another program without terminating the existing program. The first parameter with the Spawn command tells the system whether the current program should wait for the new program to finish before continuing (P_WAIT) or to execute in parallel (P_NOWAIT). The other parameters identify the program to be executed, any arguments to be passed to the program to be executed, and any environment variables that need to be set before the program is executed.

    The program to be executed needs to have a path specified for it unless the program can be found in the current working directory. If no file type is specified then a program of file type .com will be executed (if one exists) otherwise one of file type .exe will be executed (if that exists). If no path is specified in the program argument and the program doesn't exist in the current directory then the directories referenced in the path statement will be searched if you use the spawnlp, spawnlpe, spawnvp, or spawnvpe command. If you use spawnl, spawnle, spawnv, or spawnve and the specified program is not found in the current working directory (or specified path if one was specified) then the command will fail.

    Arguments can be passed to the new program in a number of different ways. They can each be passed separately (using spawnl, spawnle, spawnlp, or spawnlpe) or they can be passed as a vector of pointers (using spawnv, spawnve, spawnvp, or spawnvpe). When the arguments are passed separately, you need to follow the arguments with an additional NULL argument to terminate the list.

    The environment variables to be used by the new program can be inherited from your calling program (using spawnl, spawnlp, spawnv, or spawnvp) or can be defined using an environment argument passed in the span command following the program parameters (using spawnle, spawnlpe, spawnve, or spawnvpe).

    The following are the different versions of the Spawn command as described above:

    int spawnl( mode, path, arg0, arg1, ..., argn, NULL);

    int spawnle( mode, path, arg0, arg1, ..., argn, NULL, envp);

    int spawnlp( mode, file, arg0, arg1, ..., argn, NULL);

    int spawnlpe( mode, file, arg0, arg1, ..., argn, NULL, envp);

    int spawnv( mode, path, argv);

    int spawnve( mode, path, argv, envp);

    int spawnvp( mode, file, argv);

    int spawnvpe( mode, file, argv, envp);

     

    Exec

    The Exec command is used to start execution of another program and end the execution of the existing program. The parameters identify the program to be executed, any arguments to be passed to the program to be executed, and any environment variables that need to be set before the program is executed.

    The program to be executed needs to have a path specified for it unless the program can be found in the current working directory. If no file type is specified then a program of file type .com will be executed (if one exists) otherwise one of file type .exe will be executed (if that exists). If no path is specified in the program argument and the program doesn't exist in the current directory then the directories referenced in the path statement will be searched if you use the execlp, execlpe, execvp, or execvpe command. If you use execl, execle, execv, or execve and the specified program is not found in the current working directory (or specified path if one was specified) then the command will fail.

    Arguments can be passed to the new program in a number of different ways. They can each be passed separately (using execl, execle, execlp, or execlpe) or they can be passed as a vector of pointers (using execv, execve, execvp, or execvpe). When the arguments are passed separately, you need to follow the arguments with an additional NULL argument to terminate the list.

    The environment variables to be used by the new program can be inherited from your calling program (using execl, execlp, execv, or execvp) or can be defined using an environment argument passed in the span command following the program parameters (using execle, execlpe, execve, or execvpe).

    The following are the different versions of the Exec command as described above:

    int execl( path, arg0, arg1, ..., argn, NULL);

    int execle( path, arg0, arg1, ..., argn, NULL, envp);

    int execlp( file, arg0, arg1, ..., argn, NULL);

    int execlpe( file, arg0, arg1, ..., argn, NULL, envp);

    int execv( path, argv);

    int execve( path, argv, envp);

    int execvp( file, argv);

    int execvpe( file, argv, envp);

    ".


    beginner of planning


    • Contrassegnato come risposta Albitex lunedì 25 aprile 2011 02:54
    lunedì 25 aprile 2011 02:48