2014-02-21 135 views
-2

我想制作一个将程序写入文件然后运行的C++程序。我们的目标是制作一些随机数字生成器或类似的东西,测试每一个,做一些类似算法的“自然选择”,以获得最终更好的结果(也许重复这个过程)。运行另一个C++程序的C++程序

我对AI部分不感兴趣。我只想知道是否可以运行另一个C++程序的C++程序,以及如何实现这一点。

感谢您的支持!

+0

http://en.cppreference.com/w/cpp/utility/program/system – willll

回答

1
int result = system("another_program"); 

这里是reference

+0

正常,但有一个窗口的方式这样做? – ioanD

+0

请参阅http://stackoverflow.com/questions/6783256/run-c-program-in-c-program?rq=1 – OMGtechy

0

您可以使用CreateProcess WinAPI的开始,你需要所有其他程序:

STARTUPINFO si; 
PROCESS_INFORMATION pi; 

ZeroMemory(&si, sizeof(si)); 
si.cb = sizeof(si); 
ZeroMemory(&pi, sizeof(pi)); 
wchar_t command[MAX_PATH]; 
swprintf_s(command, L"path to whatever program you need"); 

// Start the child process. 
if(!CreateProcess(NULL, // No module name (use command line) 
    command,  // 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 
) 
{ 
    MessageBox(NULL, L"Could not start program", L"Error", MB_OK | MB_ICONERROR); 
}