スレッド内でサードパーティ プログラムを起動する必要があります。C++ を使用して stdout/stderr の両方から結果を取得するのを待ちます。
- どのような方法がありますか?
- それらはクロスプラットフォームですか?つまり、両方を cl/gcc に使用できますか?
スレッド内でサードパーティ プログラムを起動する必要があります。C++ を使用して stdout/stderr の両方から結果を取得するのを待ちます。
Unix の場合:
http://linux.die.net/man/3/execl
#include <sys/types.h>
#include <unistd.h>
void run_process (const char* path){
pid_t child_pid;
/* Duplicate this process. */
child_pid = fork ();
if (child_pid != 0){
/* This is the parent process. */
int ret = waitpid(child_pid, NULL, 0);
if (ret == -1){
printf ("an error occurred in waitpid\n");
abort ();
}
}
else {
execl (path, path);
/* The execvp function returns only if an error occurs. */
printf ("an error occurred in execl\n");
abort ();
}
}
Windows の場合:
http://msdn.microsoft.com/en-us/library/ms682425%28v=vs.85%29.aspx
# include <windows.h>
void run_process (const char* path){
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
bool ret = = CreateProcess(
NULL, // No module name (use command line)
path, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
false, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi // Pointer to PROCESS_INFORMATION structure
)
if (!ret){
printf("Error");
abort();
}
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
システムはプラットフォームに依存しない必要がありますが、同じセキュリティ特権でプログラムを実行することに懸念がある場合は、createprocess(win)/ exec(others)を使用することをお勧めします。
外部実行可能ファイルを起動する一連の posix 関数があります ( execを参照)。これらはクロスプラットフォームです。Windows で特定のタスクを実行するには、Windows 固有のcreateprocessを使用する必要がある場合があります。
これらは通常ブロックされるため、新しいスレッドで開始する必要があります。Windows では posix (pthreads) を使用できますが、通常、スレッド化はクロスプラットフォームではありません。
別の方法は、 Qtや wxWidgets クロス プラットフォーム ライブラリなどを使用することです。