Thread: Redirecting console output to sockets

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    3

    Question Redirecting console output to sockets

    I am developing a program which requires that console output (from a dos prompt) be redirected to a windows socket. The code I figured should work is:

    PROCESS_INFORMATION hProcessInfo;
    STARTUPINFO hStartupInfo;

    memset(&hStartupInfo, 0, sizeof(hStartupInfo));
    hStartupInfo.cb = sizeof(hStartupInfo);
    hStartupInfo.wShowWindow = SW_HIDE;
    hStartupInfo.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;

    hStartupInfo.hStdError = (HANDLE)sock;
    hStartupInfo.hStdInput = (HANDLE)sock;
    hStartupInfo.hStdOutput = (HANDLE)sock;

    if (!CreateProcess(NULL, "cmd.exe", NULL, NULL, true, CREATE_NO_WINDOW|CREATE_SEPARATE_WOW_VDM|REALTIME_ PRIORITY_CLASS|DETACHED_PROCESS, NULL, NULL, &hStartupInfo, &hProcessInfo)) {
    MessageBox(NULL, strerror(GetLastError()), "eRROR", 0);
    }


    But even though CreateProcess created the process, outptu is not redirected to "sock" (the socket handle). Anyone has any ideas?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    There's more to it I'm afraid......you need to make the handles you pass inheritable...then duplicate them with noninheritable handles.....then read the output from the console in a loop (usually better with a thread)...

    Here's a good example http://www.codeproject.com/dialog/quickwin.asp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help editing console output
    By Longbow0705 in forum C++ Programming
    Replies: 6
    Last Post: 03-27-2009, 08:57 AM
  2. Text Justification for console output
    By greywolf0723 in forum C++ Programming
    Replies: 1
    Last Post: 03-10-2009, 12:29 AM
  3. Having trouble making a function do what I want.
    By Shamino in forum C++ Programming
    Replies: 9
    Last Post: 12-07-2007, 11:20 AM
  4. redirecting the output of my profiler...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 12-20-2001, 06:20 PM
  5. Visual C++ printing console output
    By clarinetster in forum C++ Programming
    Replies: 6
    Last Post: 11-08-2001, 01:42 AM