Запустить консольное приложение из консольных приложений

Я использую этот кусок кода для запуска процесса из приложения с графическим интерфейсом. но, согласно примечаниям этого кода, невозможно запустить процесс из консольного приложения. на самом деле я хочу сделать это, я хочу, чтобы консольное приложение запускало другой процесс консоли, пожалуйста, у вас есть идеи, как это сделать?

// This technique must be used for "console-less" parents such as GUI
//  applications or detached applications.
// Using the STARTUPINFO STARTF_USESTDHANDLES flag, requires that
//  the CreateProcess fInheritHandles parameter be set TRUE so that
//  the file handles specified in the STARTUPINFO structure will be
//  inherited by the child.

// setup the child process's handles for stdin, stdout, & stderr.
STARTUPINFO childProcStartupInfo;
memset( &childProcStartupInfo, 0, sizeof(childProcStartupInfo));
childProcStartupInfo.cb = sizeof(childProcStartupInfo);
childProcStartupInfo.hStdInput = hFromParent;   // stdin
childProcStartupInfo.hStdOutput = hToParent;    //  stdout
childProcStartupInfo.hStdError = hToParentDup;  // stderr
childProcStartupInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
childProcStartupInfo.wShowWindow = SW_HIDE;

// Now create the child process, inheriting handles
PROCESS_INFORMATION childProcInfo;  /* for CreateProcess call */

bOk = CreateProcess(
NULL,           // filename
pCmdLine,   // full command line for child
NULL,           // process security descriptor */
NULL,           // thread security descriptor */
TRUE,           // inherit handles? Also use if STARTF_USESTDHANDLES */
0,              // creation flags */
NULL,           // inherited environment address */
NULL,           // startup dir; NULL = start in current */
&childProcStartupInfo,          // pointer to startup info (input) */
&childProcInfo);            // pointer to process info (output) */

0

Решение

ты пробовал ShellExecute? Я думаю, что это работает ..

1

Другие решения

Ты можешь попробовать:
ShellExecute (), ShellExecuteEx (), CreateProcess (), system (), _wsystem ().

Есть еще несколько, но один из них должен работать на вас!

Лично я бы пошел с CreateProcess, а затем дождался завершения процесса (нашел в Google этот пример: http://www.codeproject.com/Tips/333559/CreateProcess-and-wait-for-result). Обратите внимание, что system () / _ wsystem () проще всего использовать, но если вы не будете осторожны, они могут быть использованы !!!

Надеюсь, поможет! 🙂

0

Попробуйте этот код:

#include <Windows.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char* argv[])
{

char* app_to_launch=new char[80];
strcpy(app_to_launch,"app.exe");

STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );// Start the child process.
if( !CreateProcess( NULL,   // No module name (use command line)
app_to_launch,        // 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
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );

}// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );

// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );

return 0;
}
0
По вопросам рекламы ammmcru@yandex.ru
Adblock
detector