Reputation: 285
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?
Upvotes: 0
Views: 1729
Reputation: 285
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);
Upvotes: 0
Reputation: 27214
EDIT: Does jarsigner
not return error codes? Like 0
on success and 1
on failure? You could use CreateProcess
and trap the return code.
Upvotes: 2
Reputation: 52679
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.
Upvotes: 1