Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

A Windows Shell for Legacy MS-DOS Applications


June 1997/A Windows Shell for Legacy MS-DOS Applications/Listing 2

Listing 2: PipedProcess.cpp : implementation file

#include "stdafx.h"

BOOL CPipedProcess::CreateProc(char* ExeFileName)
{

    SECURITY_ATTRIBUTES saAttr; 
    BOOL fSuccess; 

    /* Set the bInheritHandle flag so pipe handles are
       inherited. */
 
    saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); 
    saAttr.bInheritHandle = TRUE; 
    saAttr.lpSecurityDescriptor = NULL; 
 
    /* Create a pipe for the child's STDOUT. */ 
 
    if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr,
                     &saAttr, BUFSIZE)) {
        ErrorExit("Stdout pipe creation failed"); 
        return FALSE;
    }
 
    /* Set a write handle to the pipe to be STDOUT. */ 

    if (! SetStdHandle(STD_OUTPUT_HANDLE, hChildStdoutWr)) {
        ErrorExit("Redirecting STDOUT failed"); 
        return FALSE;
    }
 
    /*  Set a write handle to the pipe to be STDERROR */ 

    if (! SetStdHandle(STD_ERROR_HANDLE, hChildStdoutWr)) {
        ErrorExit("Redirecting STDOUT failed"); 
        return FALSE;
    }
 
    /* Create a pipe for the child's STDIN. */ 
 
    if (! CreatePipe(&hChildStdinRd, &hChildStdinWr,
                     &saAttr, BUFSIZE)) {
        ErrorExit("Stdin pipe creation failed"); 
        return FALSE;
    }
 
    /* Set a read handle to the pipe to be STDIN. */ 
 
    if (! SetStdHandle(STD_INPUT_HANDLE, hChildStdinRd)) {
        ErrorExit("Redirecting Stdin failed"); 
        return FALSE;
    }
 
 
    /* Duplicate the write handle to the pipe so it is not
       inherited. */

    fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
                           GetCurrentProcess(), &hChildStdinWrDup,
                           0, FALSE, DUPLICATE_SAME_ACCESS);

    if (! fSuccess)  { 
      ErrorExit("DuplicateHandle failed"); 
      return FALSE;
    }

    CloseHandle(hChildStdinWr); 
 
    /* Now create the child process. */ 

    if (! CreateChildProcess(ExeFileName)) {
        ErrorExit("Create process failed"); 
        return FALSE;
    }

    return TRUE;

}

void CPipedProcess::CloseChildProcess(void)
{
CloseHandle(hChildStdinRd);
CloseHandle(hChildStdinWrDup))
CloseHandle(hChildStdoutRd))
CloseHandle(hChildStdoutWr)) 
}

BOOL CPipedProcess::CreateChildProcess(char *ChildName) 
{ 
    PROCESS_INFORMATION piProcInfo; 
    STARTUPINFO siStartInfo; 
    char TitleProc [280];
    *TitleProc = '\x0';
    strcat(TitleProc, "Pipe \x0"); 
    strcat(TitleProc, ChildName); 
    if(strlen(TitleProc) > 20) TitleProc[18] = '\x0';

    /* Set up members of STARTUPINFO structure. */ 
 
    siStartInfo.cb = sizeof(STARTUPINFO); 
    siStartInfo.lpReserved = NULL; 
    siStartInfo.lpReserved2 = NULL; 
    siStartInfo.cbReserved2 = 0; 
    siStartInfo.lpDesktop = NULL; 
    siStartInfo.dwFlags = STARTF_USESHOWWINDOW;
    siStartInfo.lpTitle = TitleProc;
    siStartInfo.wShowWindow = SW_MINIMIZE;

    /* Create the child process. */ 

    return CreateProcess(NULL, 
        ChildName,     /* command line                       */ 
        NULL,          /* process security attributes        */ 
        NULL,          /* primary thread security attributes */ 
        TRUE,          /* handles are inherited              */ 
        0,             /* creation flags                     */ 
        NULL,          /* use parent's environment           */ 
        NULL,          /* use parent's current directory     */ 
        &siStartInfo,  /* STARTUPINFO pointer                */ 
        &piProcInfo);  /* receives PROCESS_INFORMATION       */ 
 
}

DWORD CPipedProcess::ProcessCommand(CString Command,
                                    CString &ResultBuf)
{
DWORD dwRead;

WriteToPipe((char*) LPCTSTR(Command));
dwRead = ReadFromPipe(ResultBuf);

return dwRead;

}
void CPipedProcess::ErrorExit (char *ErrorMsg) 
{ 
    ::MessageBox(::GetActiveWindow(), ErrorMsg, "Pipe Error", MB_OK);
 CloseChildProcess(); 
} 
//End of File

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.