1
A
回答
1
在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);
}
1
有一組posix函數來啓動外部可執行文件 - 請參閱exec - 它們是跨平臺的。要在Windows上執行一些特定的任務,您可能需要使用特定的窗口createprocess。
這些通常會阻止,因此您必須在新線程中啓動它們。線程通常不是跨平臺的,儘管你可以在windows上使用posix(pthreads)。
另一種方法是使用諸如Qt或wxWidgets跨平臺庫之類的東西。
1
系統應該是獨立的平臺,但如果擔心運行具有相同安全權限的程序,您可能希望堅持使用createprocess(win)/ exec(others)。
相關問題
- 1. 從C++啓動C#.Net應用程序
- 2. 如何從C#應用程序啓動應用程序?
- 3. 從C#啓動ClickOnce應用程序#
- 4. 從應用程序啓動「啓動器應用程序抽屜」?
- 5. 自動阻止應用程序從啓動程序啓動
- 6. C#服務應用程序啓動無盡線程
- 7. 從Web應用程序啓動進程
- 8. 從java程序啓動C++程序
- 9. 從ASP.NET C#啓動程序#
- 10. 從C++程序啓動IE
- 11. C#應用程序線程
- 12. 用c#console應用程序啓動wpf.exe應用程序?
- 13. 從我的應用程序啓動外部應用程序
- 14. 從我的應用程序啓動Android視頻應用程序
- 15. 從我的應用程序內啓動本機應用程序?
- 16. 從我的Android應用程序啓動Adobe Air應用程序
- 17. 在應用程序啓動時啓動應用程序類
- 18. 如何從32位C++應用程序啓動64位Java應用程序?
- 19. 如何從x86應用程序啓動x64位應用程序c#
- 20. 從另一個應用程序(C#)啓動WCF服務應用程序
- 21. 如何從其他應用程序啓動ClickOnce應用程序?
- 22. 如何從Java Web應用程序啓動Android應用程序?
- 23. 從另一個Java應用程序啓動Java應用程序
- 24. 從另一個應用程序啓動應用程序
- 25. 從iphone應用程序啓動照片庫應用程序
- 26. 從java應用程序啓動java webstart應用程序
- 27. 從iPhone應用程序啓動Google地圖應用程序。
- 28. 從另一個rcp應用程序啓動rcp應用程序
- 29. 從應用程序包中啓動應用程序
- 30. Nativescript - 從Nativescript應用程序啓動另一個應用程序
http://stackoverflow.com/questions/43116/how-can-i從c-and-parse-its-output運行外部程序看起來像是在談論同樣的事情...... – Nim