Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem with output redirect

Problem with output redirect

Scheduled Pinned Locked Moved C / C++ / MFC
helpcomquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eusto
    wrote on last edited by
    #1

    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

    J M N 3 Replies Last reply
    0
    • E eusto

      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

      J Offline
      J Offline
      James R Twine
      wrote on last edited by
      #2

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

      E 1 Reply Last reply
      0
      • J James R Twine

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

        E Offline
        E Offline
        eusto
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • E eusto

          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

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

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

          E 1 Reply Last reply
          0
          • E eusto

            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

            N Offline
            N Offline
            Naveen
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • M Mark Salsbery

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

              E Offline
              E Offline
              eusto
              wrote on last edited by
              #6

              Thanks...but it still does not work:(

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups