从控制台应用程序启动控制台应用程序
Launch console app from console apps
我正在使用这段代码从GUI应用程序启动进程。但是,根据这段代码的注释,不可能从控制台应用程序启动进程。事实上,我想这样做,我想要一个控制台应用程序来启动另一个控制台进程,你知道怎么做吗?
// 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) */
您尝试执行shell了吗?我认为这行得通。。
您可以尝试:ShellExecute()、ShellExecuteEx()、CreateProcess()、system()、_wsystem()。
还有一些,但其中一个一定对你有用!
就我个人而言,我会选择CreateProcess,然后等待流程退出(在谷歌上找到这个例子:http://www.codeproject.com/Tips/333559/CreateProcess-and-wait-for-result)。请注意,system()/_wsystem()是最容易使用的,但如果你不小心,它们可能会被利用!!!
希望有帮助!:-)
试试这个代码:
#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;
}
相关文章:
- C++控制台应用程序阻止退出
- 如何从 Win32 C++ 应用程序输出到父控制台窗口?
- 如何在 Win32 C++控制台应用程序中调用 UWP 类库
- 是否可以从控制台应用程序使用 C++/WinRT 创建窗口?
- 如何创建一个使用开源c项目的c++控制台应用程序
- c ++控制台应用程序,如何在控制台外部打印字符
- 像 CPP 中的控制台一样的应用程序
- 调试控制台 ChessEngine.exe "used"在另一个应用程序(国际象棋 GUI)中
- 将控制台应用程序直接输出到Visual Studio而不是外部Windows控制台中
- 如何在 Win32 控制台应用程序中注册不可见的窗口类?
- 如何将从控制台应用程序C++回调设置为C++ COM DLL
- 使用代码在 AWS 开发工具包C++控制台应用程序上设置凭证
- 直接输入控制台应用程序
- C++调试控制台应用程序
- C++控制台应用程序调用C++/CLI静态库
- 如何在控制台应用程序中添加滚动功能以显示更大的输入
- C++应用程序控制台未退出
- 如何使用VSCode调试C++应用程序控制台应用程序
- 用于调试消息的跨平台GUI应用程序控制台
- 自定义应用程序控制台和stderr