Problem with output redirect
-
by my understanding of what i've found in msdn the folowing code should redirect the output of "ping www.google.com" to a file...and well...it doesn't. Where did i go wrong? STARTUPINFO StartupInfo; PROCESS_INFORMATION ProcessInfo; ULONG rc; HANDLE hFakeStdOut = CreateFile( "C:\\myfile.txt", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); if(hFakeStdOut==INVALID_HANDLE_VALUE) { printf("CreateFile failed with %d\n",GetLastError()); return; } memset(&StartupInfo, 0, sizeof(StartupInfo)); StartupInfo.cb = sizeof(STARTUPINFO); StartupInfo.dwFlags = STARTF_USESTDHANDLES; StartupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE); StartupInfo.hStdOutput = hFakeStdOut;//GetStdHandle(STD_OUTPUT_HANDLE); StartupInfo.hStdError = hFakeStdOut;//GetStdHandle(STD_ERROR_HANDLE); char sCurDir[1023]; GetCurrentDirectory(1023,sCurDir); if (!CreateProcess( NULL, "ping www.google.com", NULL, NULL, TRUE, NULL, NULL, NULL, &StartupInfo, &ProcessInfo)) { printf("Failed with %d\n",GetLastError()); } WaitForSingleObject(ProcessInfo.hProcess, INFINITE); if(!GetExitCodeProcess(ProcessInfo.hProcess, &rc)) rc = 0; CloseHandle(ProcessInfo.hThread); CloseHandle(ProcessInfo.hProcess); CloseHandle(hFakeStdOut); I've been reading about this for about an hour now and i can't get it. Please help
-
by my understanding of what i've found in msdn the folowing code should redirect the output of "ping www.google.com" to a file...and well...it doesn't. Where did i go wrong? STARTUPINFO StartupInfo; PROCESS_INFORMATION ProcessInfo; ULONG rc; HANDLE hFakeStdOut = CreateFile( "C:\\myfile.txt", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); if(hFakeStdOut==INVALID_HANDLE_VALUE) { printf("CreateFile failed with %d\n",GetLastError()); return; } memset(&StartupInfo, 0, sizeof(StartupInfo)); StartupInfo.cb = sizeof(STARTUPINFO); StartupInfo.dwFlags = STARTF_USESTDHANDLES; StartupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE); StartupInfo.hStdOutput = hFakeStdOut;//GetStdHandle(STD_OUTPUT_HANDLE); StartupInfo.hStdError = hFakeStdOut;//GetStdHandle(STD_ERROR_HANDLE); char sCurDir[1023]; GetCurrentDirectory(1023,sCurDir); if (!CreateProcess( NULL, "ping www.google.com", NULL, NULL, TRUE, NULL, NULL, NULL, &StartupInfo, &ProcessInfo)) { printf("Failed with %d\n",GetLastError()); } WaitForSingleObject(ProcessInfo.hProcess, INFINITE); if(!GetExitCodeProcess(ProcessInfo.hProcess, &rc)) rc = 0; CloseHandle(ProcessInfo.hThread); CloseHandle(ProcessInfo.hProcess); CloseHandle(hFakeStdOut); I've been reading about this for about an hour now and i can't get it. Please help
eusto wrote:
if (!CreateProcess( NULL, "ping www.google.com", NULL, NULL, TRUE, NULL, NULL, NULL, &StartupInfo, &ProcessInfo)) { printf("Failed with %d\n",GetLastError()); }
I think that you should specify the full path to
ping.exe
(always a good habit, ask any *nix person, it prevents a command hijack), and put that path into the first parameter. Put the parameters to ping.exe (www.google.com) as the second parameter and see if that works. I have had situations in the past where specifying everything as the command line fails, but breaking them up works. Dunno why, but try it. Also, did you try using pipes as suggested in the MSDN article located at http://msdn.microsoft.com/library/en-us/dllproc/base/creating_a_child_process_with_redirected_input_and_output.asp[^]? Maybe it works with pipes but not with normal files? I would get the pipes version working first. Peace!-=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!) -
eusto wrote:
if (!CreateProcess( NULL, "ping www.google.com", NULL, NULL, TRUE, NULL, NULL, NULL, &StartupInfo, &ProcessInfo)) { printf("Failed with %d\n",GetLastError()); }
I think that you should specify the full path to
ping.exe
(always a good habit, ask any *nix person, it prevents a command hijack), and put that path into the first parameter. Put the parameters to ping.exe (www.google.com) as the second parameter and see if that works. I have had situations in the past where specifying everything as the command line fails, but breaking them up works. Dunno why, but try it. Also, did you try using pipes as suggested in the MSDN article located at http://msdn.microsoft.com/library/en-us/dllproc/base/creating_a_child_process_with_redirected_input_and_output.asp[^]? Maybe it works with pipes but not with normal files? I would get the pipes version working first. Peace!-=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!)Thanks but the full path is not the problem. Braking the args and actual module name did not work either. This sucks :( msdn says that a handle can be anything that supports Read() and Write() so a hadle to a file created by CreateFile shoul work. I realy don't get it
-
by my understanding of what i've found in msdn the folowing code should redirect the output of "ping www.google.com" to a file...and well...it doesn't. Where did i go wrong? STARTUPINFO StartupInfo; PROCESS_INFORMATION ProcessInfo; ULONG rc; HANDLE hFakeStdOut = CreateFile( "C:\\myfile.txt", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); if(hFakeStdOut==INVALID_HANDLE_VALUE) { printf("CreateFile failed with %d\n",GetLastError()); return; } memset(&StartupInfo, 0, sizeof(StartupInfo)); StartupInfo.cb = sizeof(STARTUPINFO); StartupInfo.dwFlags = STARTF_USESTDHANDLES; StartupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE); StartupInfo.hStdOutput = hFakeStdOut;//GetStdHandle(STD_OUTPUT_HANDLE); StartupInfo.hStdError = hFakeStdOut;//GetStdHandle(STD_ERROR_HANDLE); char sCurDir[1023]; GetCurrentDirectory(1023,sCurDir); if (!CreateProcess( NULL, "ping www.google.com", NULL, NULL, TRUE, NULL, NULL, NULL, &StartupInfo, &ProcessInfo)) { printf("Failed with %d\n",GetLastError()); } WaitForSingleObject(ProcessInfo.hProcess, INFINITE); if(!GetExitCodeProcess(ProcessInfo.hProcess, &rc)) rc = 0; CloseHandle(ProcessInfo.hThread); CloseHandle(ProcessInfo.hProcess); CloseHandle(hFakeStdOut); I've been reading about this for about an hour now and i can't get it. Please help
The handle needs to be inheritable: try
SECURITY_ATTRIBUTES SecurityAttributes;
SecurityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
SecurityAttributes.lpSecurityDescriptor = NULL;
SecurityAttributes.bInheritHandle = TRUE;HANDLE hFakeStdOut = CreateFile(
"C:\\myfile.txt",
GENERIC_WRITE,
FILE_SHARE_WRITE,
&SecurityAttributes,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL
); -
by my understanding of what i've found in msdn the folowing code should redirect the output of "ping www.google.com" to a file...and well...it doesn't. Where did i go wrong? STARTUPINFO StartupInfo; PROCESS_INFORMATION ProcessInfo; ULONG rc; HANDLE hFakeStdOut = CreateFile( "C:\\myfile.txt", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); if(hFakeStdOut==INVALID_HANDLE_VALUE) { printf("CreateFile failed with %d\n",GetLastError()); return; } memset(&StartupInfo, 0, sizeof(StartupInfo)); StartupInfo.cb = sizeof(STARTUPINFO); StartupInfo.dwFlags = STARTF_USESTDHANDLES; StartupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE); StartupInfo.hStdOutput = hFakeStdOut;//GetStdHandle(STD_OUTPUT_HANDLE); StartupInfo.hStdError = hFakeStdOut;//GetStdHandle(STD_ERROR_HANDLE); char sCurDir[1023]; GetCurrentDirectory(1023,sCurDir); if (!CreateProcess( NULL, "ping www.google.com", NULL, NULL, TRUE, NULL, NULL, NULL, &StartupInfo, &ProcessInfo)) { printf("Failed with %d\n",GetLastError()); } WaitForSingleObject(ProcessInfo.hProcess, INFINITE); if(!GetExitCodeProcess(ProcessInfo.hProcess, &rc)) rc = 0; CloseHandle(ProcessInfo.hThread); CloseHandle(ProcessInfo.hProcess); CloseHandle(hFakeStdOut); I've been reading about this for about an hour now and i can't get it. Please help
if u want to redirect the out put u can use any of the following ways...
SHELLEXECUTEINFO stInfo = {0}; stInfo.fMask = SEE_MASK_NOCLOSEPROCESS; stInfo.cbSize = sizeof(SHELLEXECUTEINFO); stInfo.lpVerb = _T("open"); stInfo.lpFile = _T("cmd"); stInfo.lpParameters = _T("/c ping www.google.com > c:\\result.txt"); stInfo.nShow = SW_SHOW; ShellExecuteEx( &stInfo ); WaitForSingleObject(stInfo.hProcess, INFINITE);
orSTARTUPINFO StartupInfo; PROCESS_INFORMATION ProcessInfo; ULONG rc; memset(&StartupInfo, 0, sizeof(StartupInfo)); StartupInfo.cb = sizeof(STARTUPINFO); if (!CreateProcess( _T("c:\\windows\\system32\\cmd.exe"), _T("/c ping www.google.com > c:\\result.txt"), NULL, NULL, TRUE, NULL, NULL, NULL, &StartupInfo, &ProcessInfo )) { printf("Failed with %d\n",GetLastError()); } WaitForSingleObject(ProcessInfo.hProcess, INFINITE); CloseHandle(ProcessInfo.hThread); CloseHandle(ProcessInfo.hProcess);
nave
-
The handle needs to be inheritable: try
SECURITY_ATTRIBUTES SecurityAttributes;
SecurityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
SecurityAttributes.lpSecurityDescriptor = NULL;
SecurityAttributes.bInheritHandle = TRUE;HANDLE hFakeStdOut = CreateFile(
"C:\\myfile.txt",
GENERIC_WRITE,
FILE_SHARE_WRITE,
&SecurityAttributes,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL
);