Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi~
1. From Cmd.exe mode, if I hit sort
c
b
a
^Z
c
b
a

a
b
c

2. if I hit sort >> test.txt
c
b
a
^Z

3. but then if I hit sort
c
b
a
^Z

doesn't output to crt
c
b
a

a
b
c

why?

C++
else if( !_tcscmp(cmdTokenList[0], _T("sort")) )
{
    if(!_tcscmp(cmdTokenList[1], _T(">>")) )
    {
         SECURITY_ATTRIBUTES fileSec = {
       //    sizeof(SECURITY_ATTRIBUTES), NULL, FALSE
             sizeof(SECURITY_ATTRIBUTES), NULL, TRUE
          };

       // Save the handle to the current STDOUT. 
          HANDLE hSaveStdout = GetStdHandle(STD_OUTPUT_HANDLE); 

          HANDLE hFile = CreateFile ( 
           // cmdTokenList[2], GENERIC_WRITE, 0,
           // cmdTokenList[2], GENERIC_WRITE, FILE_SHARE_READ,
              cmdTokenList[2], GENERIC_WRITE, FILE_SHARE_WRITE,
              &fileSec, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
              NULL);

       // Create noninheritable read handle and close the inheritable read 
       // handle. 
          HANDLE hFileDup;
          if( !DuplicateHandle(GetCurrentProcess(), hFile,
              GetCurrentProcess(), &hFileDup , 0,
              FALSE,
              DUPLICATE_SAME_ACCESS))
              ErrorExit(_T("DuplicateHandle failed"));
          CloseHandle(hFile);
          si.hStdOutput = hFileDup;
     //   si.hStdOutput = hFile;
          si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
          si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
          si.dwFlags |= STARTF_USESTDHANDLES;

     //   isRun = CreateProcess(NULL, cmdTokenList[0], NULL, NULL, FALSE, 0, NULL, NULL,
        //    &si, &pi);
            isRun = CreateProcess(NULL, cmdTokenList[0], NULL, NULL, TRUE, 0, NULL, NULL,
              &si, &pi);

       // After process creation, restore the saved STDIN and STDOUT. 
          if (! SetStdHandle(STD_OUTPUT_HANDLE, hSaveStdout)) 
              ErrorExit(_T("Re-redirecting Stdout failed\n")); 
          WaitForSingleObject(pi.hProcess, INFINITE);
          CloseHandle(hFile);
      }
      else
      {
          si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
     //   isRun = CreateProcess(NULL, cmdTokenList[0], NULL, NULL, TRUE, 0, NULL, NULL,
     //       &si,&pi);
          isRun = CreateProcess(NULL, cmdTokenList[0], NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
          WaitForSingleObject(pi.hProcess, INFINITE);
       }

   //  CloseHandle(si.hStdOutput);
        CloseHandle(pi.hProcess);
       CloseHandle(pi.hThread);
   //  CloseHandle(si.hStdInput);
        SetStdHandle(STD_OUTPUT_HANDLE, hStdout);
       SetStdHandle(STD_INPUT_HANDLE, hStdin);

}


thanks for reading.
Posted

1 solution

Please see how the code sample on that MSDN page redirects: http://msdn.microsoft.com/en-us/library/ms682425%28VS.85%29.aspx[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900