Jump to content

[c++] redirecting output


Recommended Posts

trying to launch a separate executable and redirect the output to another file for parsing. ive looked in msdn and the sample they give doesn't seem to work with the environment i have. don't know if im missing something.

here is the code i have

int LaunchProcess(LPTSTR programName, LPTSTR commandLine, BOOL wait_flag, const _TCHAR *curDir = NULL, const _TCHAR *redirOut = NULL)
{
PROCESS_INFORMATION pi;
STARTUPINFO si;
DWORD dwExitCode = 0;
DWORD dw;
DWORD last_error;
BOOL fSuccess;
BOOL fExit;
DWORD create_flags;
HANDLE hFileout = NULL;
SECURITY_ATTRIBUTES saAttr;
int return_status;

ZeroMemory(&si, sizeof(STARTUPINFO));

if (redirOut != NULL)
{
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;

hFileout = CreateFile(redirOut
, FILE_WRITE_DATA
, FILE_SHARE_WRITE
, &saAttr //security attributes
, OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL);
if (!hFileout == INVALID_HANDLE_VALUE)
{
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdOutput = hFileout;
si.hStdError = hFileout;
}
}

if( wait_flag )
{
create_flags = 0;
}
else
{
create_flags = CREATE_NEW_CONSOLE;
}

// SETUP STARTUP INFORMATION STRUCTURE:
si.cb = sizeof(STARTUPINFO);
si.lpReserved = NULL;
si.lpDesktop = NULL;
si.lpTitle = programName;
si.cbReserved2 = 0;
si.lpReserved2 = NULL;

GetStartupInfo( &si );

if( ! wait_flag ) // IF WE DO NOT WAIT - RUN MINIMIZED
{
si.dwFlags = si.dwFlags | STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOWMINIMIZED;
}

fSuccess = CreateProcess( programName,
commandLine,
(LPSECURITY_ATTRIBUTES) NULL, /* Process security */
(LPSECURITY_ATTRIBUTES) NULL, /* Thread Security */
(BOOL) TRUE, /* Inherit handles */
(DWORD) create_flags, /* Create Flags */
NULL, /* Environment: */
curDir, /* Current Dir */
(LPSTARTUPINFO) &si, /* Startup info */
(LPPROCESS_INFORMATION) π ); /* Process info */

if( fSuccess != 0 ) // SUCCESS IF != 0
{
dwExitCode = 0;
/* WAIT FOR TASK TO COMPLETE (NO_TIMEOUT): */
if(wait_flag)
{
dw = WaitForSingleObject(pi.hProcess, INFINITE );
// WAS PROCESS RUN SUCCESSFULLY:
fExit = GetExitCodeProcess(pi.hProcess, &dwExitCode );
return_status = dwExitCode;

CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
pi.hThread = 0;
pi.hProcess = 0;
} // END WAIT - SAVE THE EXIT CODE FROM PROGRAM
}
else /* CREATE PROCESS FAILED */
{
last_error = GetLastError();
return_status = -1;
}
if (hFileout) CloseHandle(hFileout);
return( return_status );
}

the function here is compiled and built into a dll. i dont know if it being in a dll is any cause for it to not work or not.

plz help.

thanx

Link to comment
Share on other sites


Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...