[英]How to launch an exe by command prompt and get text from command prompt window
jarsigner.exe
帶有一個參數,並向控制台輸出一些文本:
string command = "jarsigner.exe -verify test.jar";
system(command.c_str());
當我運行此代碼時,將出現命令提示符窗口,並顯示jar is verified
或jar is unsigned
到控制台。
如何從控制台獲取此結果字符串?
您可以嘗試ReadConsoleOutputCharacter 。
您可以將stdout重定向到文件(jarsigner.exe> outfile.txt),然后使用諸如perl或shell腳本之類的實用程序解析文件的內容。
另外,您可以使用dup, _open_osfhandle或freopen函數在應用程序中重定向標准輸出。
我創建了新進程,並將其標准輸出重定向到文本文件,它可以工作。
STARTUPINFO si;
PROCESS_INFORMATION pi;
SECURITY_ATTRIBUTES sa;
HANDLE hOutFile;
ZeroMemory( &si, sizeof(si) );
ZeroMemory( &pi, sizeof(pi) );
ZeroMemory( &sa, sizeof(sa) );
si.cb = sizeof(si);
si.wShowWindow = SW_HIDE;
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES ;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = true;
// Create output file and get file handle
hOutFile = CreateFile ( TEXT(outFilePath.c_str()),
FILE_SHARE_WRITE,
0,
&sa, // provide SECURITY_ATTRIBUTES
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
// Assign StartInfo StdOutput to file handle
si.hStdOutput = hOutFile ;
LPTSTR szCmdline = TEXT( command.c_str() );
if( !CreateProcess( NULL, // No module name (use command line)
szCmdline, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
true, // Set handle inheritance to TRUE
0, // No creation flags
NULL, // Use parent's environment block
TEXT(jarSignerExeDir.c_str()), // Use jarSignerExeDir FOR starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
ShowMessage( "CreateProcess failed (%d).\n" + GetLastError() );
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process,thread and file handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
CloseHandle(hOutFile);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.