简体   繁体   中英

How to launch an exe by command prompt and get text from command prompt window

jarsigner.exe takes a parameter and prints some text to console:

string command = "jarsigner.exe -verify test.jar";
system(command.c_str());

when I run this code, command prompt window appears and it prints jar is verified or jar is unsigned to console.

How can I get this result string from the console?

您可以尝试ReadConsoleOutputCharacter

I used Google to find this .

EDIT: Does jarsigner not return error codes? Like 0 on success and 1 on failure? You could use CreateProcess and trap the return code.

You could redirect stdout to to a file (jarsigner.exe > outfile.txt) and then parse the contents of the file using a utility like a perl or shell script.

Alternatively, you can redirect stdout in your application using the dup, _open_osfhandle , or freopen functions.

i created new process and redirect its stdout to text file, it works.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM