Lm th? no ? ? tr?ng qu tr?nh giao di?n i?u khi?n v?i chuy?n h?ng x? l? tiu chu?n

D?ch tiu ? D?ch tiu ?
ID c?a bi: 190351 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?

? Trang ny


Bi vi?t ny m t? cch ? chuy?n h?ng cc ?u vo v ?u ra c?a m?t qu tr?nh con m nh?n ?c ?u vo t? cc x? l? ?u vo tiu chu?n ho?c g?i ra ? x? l? ?u ra tiu chu?n. Win32 API cho php cc ?ng d?ng ? ? tr?ng m?t ?a tr? bn giao ti?p v?i qu tr?nh chuy?n ch?t tiu chu?n. Tnh nng ny cho php m?t qu tr?nh ph? huynh ? g?i v nh?n cc ?u vo v ?u ra c?a qu tr?nh con.

Chu y M?t s? ?ng d?ng d?a trn giao di?n i?u khi?n khng s? d?ng x? l? tiu chu?n cho ?u vo/?u ra (IO) ho?t ?ng c?a h?. Win32 API h? tr? chuy?n h?ng cc qu tr?nh.


CreateProcess() thng qua c?u trc STARTUPINFO API cho php b?n ? chuy?n h?ng cc ch?t tiu chu?n c?a m?t qu tr?nh giao di?n i?u khi?n d?a trn con. N?u ti kho?n c?a dwFlags ?c thi?t l?p ? STARTF_USESTDHANDLES, sau thnh vin STARTUPINFO sau y ch? ra cc ch?t tiu chu?n c?a qu tr?nh giao di?n i?u khi?n d?a trn con:
   HANDLE hStdInput - Standard input handle of the child process.
   HANDLE hStdOutput - Standard output handle of the child process.
   HANDLE hStdError - Standard error handle of the child process.
B?n c th? thi?t l?p cc ch?t cho m?t trong hai m?t ?ng x? l?, x? l? t?p tin, ho?c b?t k? x? l? m c th? lm ?ng b? l?n ?c v vi?t thng qua readfile () v WriteFile() API. Cc ch?t ph?i ?c th?a k? v CreateProcess() API ph?i ch? ra r?ng c th? th?a k? x? l? ang ?c th?a k? b?i con x? l? b?ng cch xc ?nh ng trong tham s? bInheritHandles. N?u ph? huynh qu tr?nh ch? mu?n chuy?n h?ng m?t ho?c hai tiu chu?n ch?t, ch? ?nh GetStdHandle() cho x? l? c? th? gy ra cc con ? t?o ra cc tiu chu?n x? l? v? n th?ng s? khng chuy?n h?ng. V d?, n?u qu tr?nh ph? huynh ch? c?n ? chuy?n h?ng ?u ra tiu chu?n v l?i c?a qu tr?nh tr? em, sau cc thnh vin hStdInput c?a c?u trc STARTUPINFO ?c lm ?y nh sau:
   hStdInput = GetStdHandle(STD_INPUT_HANDLE);
Chu y Cc qu tr?nh con qu? v? s? d?ng cc ch?c nng th?i gian ch?y c nh printf() v fprintf() c th? hnh x? km khi ?i h?ng. Cc ch?c nng th?i gian ch?y c duy tr? ring bi?t IO b? ?m. Khi chuy?n h?ng, cc b? ?m c th? khng ? ?ng ngay l?p t?c sau m?i cu?c g?i IO. K?t qu? l, ?u ra cho cc ?ng ?ng chuy?n h?ng cu?c g?i printf() ho?c ?u vo t? m?t cu?c g?i getch() khng ph?i l ? ?ng ngay l?p t?c v ch?m tr?, tr? ho?n v h?n i khi x?y ra. V?n ? ny trnh n?u tr?nh con flushes b? ?m IO sau m?i cu?c g?i ?n m?t ch?c nng IO l?n ch?y C. Ch? l qu tr?nh tr? em c th? tun ra c?a n th?i gian ch?y C IO b? ?m. M?t qu tr?nh c th? tun ra c?a n th?i gian ch?y C IO b? ?m b?ng cch g?i cc ch?c nng fflush().

Chu y Windows 95 v Windows 98 ?i h?i m?t b?c thm khi b?n chuy?n h?ng cc ch?t tiu chu?n c?a m?t s? cc qu tr?nh con.

M?u sau chuy?n h?ng tiu chu?n ?u vo, ?u ra v l?i c?a cc qu tr?nh tr? em ?c ch? r? trong cc cu?c g?i CreateProcess. M?u ny chuy?n h?ng giao di?n i?u khi?n ?c cung c?p tr?nh (Child.c).

Ma mu


      Copyright (c) 1998  Microsoft Corporation

      Module Name:


          This sample illustrates how to spawn a child console based
          application with redirected standard handles.

          The following import libraries are required:

      Dave McPherson (davemm)   11-March-98


   #pragma comment(lib, "User32.lib")
   void DisplayError(char *pszAPI);
   void ReadAndHandleOutput(HANDLE hPipeRead);
   void PrepAndLaunchRedirectedChild(HANDLE hChildStdOut,
                                     HANDLE hChildStdIn,
                                     HANDLE hChildStdErr);
   DWORD WINAPI GetAndSendInputThread(LPVOID lpvThreadParam);

   HANDLE hChildProcess = NULL;
   HANDLE hStdIn = NULL; // Handle to parents std input.
   BOOL bRunThread = TRUE;

   void main ()
      HANDLE hOutputReadTmp,hOutputRead,hOutputWrite;
      HANDLE hInputWriteTmp,hInputRead,hInputWrite;
      HANDLE hErrorWrite;
      HANDLE hThread;
      DWORD ThreadId;

      // Set up the security attributes struct.
      sa.nLength= sizeof(SECURITY_ATTRIBUTES);
      sa.lpSecurityDescriptor = NULL;
      sa.bInheritHandle = TRUE;

      // Create the child output pipe.
      if (!CreatePipe(&hOutputReadTmp,&hOutputWrite,&sa,0))

      // Create a duplicate of the output write handle for the std error
      // write handle. This is necessary in case the child application
      // closes one of its std output handles.
      if (!DuplicateHandle(GetCurrentProcess(),hOutputWrite,

      // Create the child input pipe.
      if (!CreatePipe(&hInputRead,&hInputWriteTmp,&sa,0))

      // Create new output read handle and the input write handles. Set
      // the Properties to FALSE. Otherwise, the child inherits the
      // properties and, as a result, non-closeable handles to the pipes
      // are created.
      if (!DuplicateHandle(GetCurrentProcess(),hOutputReadTmp,
                           &hOutputRead, // Address of new handle.
                           0,FALSE, // Make it uninheritable.

      if (!DuplicateHandle(GetCurrentProcess(),hInputWriteTmp,
                           &hInputWrite, // Address of new handle.
                           0,FALSE, // Make it uninheritable.

      // Close inheritable copies of the handles you do not want to be
      // inherited.
      if (!CloseHandle(hOutputReadTmp)) DisplayError("CloseHandle");
      if (!CloseHandle(hInputWriteTmp)) DisplayError("CloseHandle");

      // Get std input handle so you can close it and force the ReadFile to
      // fail when you want the input thread to exit.
      if ( (hStdIn = GetStdHandle(STD_INPUT_HANDLE)) ==
                                                INVALID_HANDLE_VALUE )


      // Close pipe handles (do not continue to modify the parent).
      // You need to make sure that no handles to the write end of the
      // output pipe are maintained in this process or else the pipe will
      // not close when the child process exits and the ReadFile will hang.
      if (!CloseHandle(hOutputWrite)) DisplayError("CloseHandle");
      if (!CloseHandle(hInputRead )) DisplayError("CloseHandle");
      if (!CloseHandle(hErrorWrite)) DisplayError("CloseHandle");

      // Launch the thread that gets the input and sends it to the child.
      hThread = CreateThread(NULL,0,GetAndSendInputThread,
      if (hThread == NULL) DisplayError("CreateThread");

      // Read the child's output.
      // Redirection is complete

      // Force the read on the input to return by closing the stdin handle.
      if (!CloseHandle(hStdIn)) DisplayError("CloseHandle");

      // Tell the thread to exit and wait for thread to die.
      bRunThread = FALSE;

      if (WaitForSingleObject(hThread,INFINITE) == WAIT_FAILED)

      if (!CloseHandle(hOutputRead)) DisplayError("CloseHandle");
      if (!CloseHandle(hInputWrite)) DisplayError("CloseHandle");

   // PrepAndLaunchRedirectedChild
   // Sets up STARTUPINFO structure, and launches redirected child.
   void PrepAndLaunchRedirectedChild(HANDLE hChildStdOut,
                                     HANDLE hChildStdIn,
                                     HANDLE hChildStdErr)

      // Set up the start up info struct.
      si.cb = sizeof(STARTUPINFO);
      si.dwFlags = STARTF_USESTDHANDLES;
      si.hStdOutput = hChildStdOut;
      si.hStdInput  = hChildStdIn;
      si.hStdError  = hChildStdErr;
      // Use this if you want to hide the child:
      //     si.wShowWindow = SW_HIDE;
      // Note that dwFlags must include STARTF_USESHOWWINDOW if you want to
      // use the wShowWindow flags.

      // Launch the process that you want to redirect (in this case,
      // Child.exe). Make sure Child.exe is in the same directory as
      // redirect.c launch redirect from a command line to prevent location
      // confusion.
      if (!CreateProcess(NULL,"Child.EXE",NULL,NULL,TRUE,

      // Set global child process handle to cause threads to exit.
      hChildProcess = pi.hProcess;

      // Close any unnecessary handles.
      if (!CloseHandle(pi.hThread)) DisplayError("CloseHandle");

   // ReadAndHandleOutput
   // Monitors handle for input. Exits when child exits or pipe breaks.
   void ReadAndHandleOutput(HANDLE hPipeRead)
      CHAR lpBuffer[256];
      DWORD nBytesRead;
      DWORD nCharsWritten;

         if (!ReadFile(hPipeRead,lpBuffer,sizeof(lpBuffer),
                                          &nBytesRead,NULL) || !nBytesRead)
            if (GetLastError() == ERROR_BROKEN_PIPE)
               break; // pipe done - normal exit path.
               DisplayError("ReadFile"); // Something bad happened.

         // Display the character read on the screen.
         if (!WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE),lpBuffer,

   // GetAndSendInputThread
   // Thread procedure that monitors the console for input and sends input
   // to the child process through the input pipe.
   // This thread ends when the child application exits.
   DWORD WINAPI GetAndSendInputThread(LPVOID lpvThreadParam)
      CHAR read_buff[256];
      DWORD nBytesRead,nBytesWrote;
      HANDLE hPipeWrite = (HANDLE)lpvThreadParam;

      // Get input from our console and send it to child through the pipe.
      while (bRunThread)

         read_buff[nBytesRead] = '\0'; // Follow input with a NULL.

         if (!WriteFile(hPipeWrite,read_buff,nBytesRead,&nBytesWrote,NULL))
            if (GetLastError() == ERROR_NO_DATA)
               break; // Pipe was closed (normal exit path).

      return 1;

   // DisplayError
   // Displays the error number and corresponding message.
   void DisplayError(char *pszAPI)
       LPVOID lpvMessageBuffer;
       CHAR szPrintBuffer[512];
       DWORD nCharsWritten;

                NULL, GetLastError(),
                (LPTSTR)&lpvMessageBuffer, 0, NULL);

         "ERROR: API    = %s.\n   error code = %d.\n   message    = %s.\n",
                pszAPI, GetLastError(), (char *)lpvMessageBuffer);



   // child.c
   // Echoes all input to stdout. This will be redirected by the redirect
   // sample. Compile and build child.c as a Win32 Console application and
   // put it in the same directory as the redirect sample.

   void main ()
      FILE*    fp;
      CHAR     szInput[1024];

      // Open the console. By doing this, you can send output directly to
      // the console that will not be redirected.

      fp = fopen("CON", "w");
      if (!fp) {
         printf("Error opening child console - perhaps there is none.\n");

      // Write a message direct to the console (will not be redirected).

         fprintf(fp,"This data is being printed directly to the\n");
         fprintf(fp,"console and will not be redirected.\n\n");
         fprintf(fp,"Since the standard input and output have been\n");
         fprintf(fp,"redirected data sent to and from those handles\n");
         fprintf(fp,"will be redirected.\n\n");
         fprintf(fp,"To send data to the std input of this process.\n");
         fprintf(fp,"Click on the console window of the parent process\n");
         fprintf(fp,"(redirect), and enter data from it's console\n\n");
         fprintf(fp,"To exit this process send the string 'exit' to\n");
         fprintf(fp,"it's standard input\n");

      while (TRUE)
         printf("Child echoing [%s]\n",szInput);
         fflush(NULL);  // Must flush output buffers or else redirection
                        // will be problematic.
         if (!_stricmp(szInput,"Exit") )

         ZeroMemory(szInput,strlen(szInput) );



MSDN Library SDK ti li?u: CreateProcess(); C?u trc STARTUPINFO

Th?a k? m?u trong SDK Win32 n?n t?ng d?i:

Thu?c tnh

ID c?a bi: 190351 - L?n xem xt sau cng: 20 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Win32 Application Programming Interface
T? kha:
kbapi kbconsole kbFAQ kbhowto kbipc kbkernbase kbmt KB190351 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:190351

Cung cp Phan hi


Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com