Createpipe Hilfe



  • Hi
    Ich habe mir dieses Tutoriel hier durchgelesen http://www.tenouk.com/ModuleU2.html. Und probiere den Code jetzt so umzuschreiben, dass Cmd geöffnet wird und zu Cmd der Befehl dir gesendet wird und in meinen Program die Ausgabe angezeigt wird.
    Ja ich weiß, dass ich mit CreatPipe noch ziemlich am ANfang stehe, aber ich hab einfach kein besseres Tutoriel gefunden. 😞

    Hier mein Qulltext hat jemand eine Idee.

    #define _WIN32_WINNT 0x0501
    
    #include <windows.h>
    
    #include <stdio.h>
    #define BUFSIZE 4096
    // Declaring handles...
    HANDLE hChildStdinRd, hChildStdinWr, hChildStdinWrDup, hChildStdoutRd, hChildStdoutWr, hChildStdoutRdDup, hInputFile, hStdout; 
    
    BOOL CreateChildProcess(VOID);
    
    VOID WriteToPipe(VOID); 
    
    VOID ReadFromPipe(VOID);
    
    VOID MyErrorExit(LPTSTR);
    
    VOID ErrMsg(LPTSTR, BOOL);
    
    // This program takes a single command-line argument, the name of a text file
    
    int main(int argc, char *argv[]) 
    
    {
    
       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;
    
       // Get the handle to the current STDOUT.
    
       hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    
       // Create a pipe for the child process's STDOUT.
    
       if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
    
          MyErrorExit("CreatePipe(), a pipe for the child process's STDOUT creation failed\n");
    
       else
    
          printf("CreatePipe(), a pipe for the child process's STDOUT creation is OK.\n");
    
        // Create noninheritable read handle and close the inheritable read handle. 
    
        fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd, GetCurrentProcess(), &hChildStdoutRdDup , 0, FALSE, DUPLICATE_SAME_ACCESS);
    
        if (!fSuccess)
    
           {
    
                  MyErrorExit("DuplicateHandle() for noninheritable read handle failed");
    
                  CloseHandle(hChildStdoutRd);
    
           }
    
           else
    
                  printf("DuplicateHandle() for noninheritable read handle is OK.\n");
    
       // Create a pipe for the child process's STDIN.
    
       if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
    
          MyErrorExit("A pipe for the child process's STDIN creation failed\n");
    
       else
    
          printf("A pipe for the child process's STDIN creation is OK.\n");
    
       // Duplicate the write handle to the pipe so it is not inherited.
    
       fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr, GetCurrentProcess(), &hChildStdinWrDup, 0, 
    
          FALSE,                  // not inherited 
    
          DUPLICATE_SAME_ACCESS); 
    
       if (!fSuccess) 
    
          MyErrorExit("DuplicateHandle() for the write handle to the pipe failed");
    
       else
    
          printf("DuplicateHandle() for the write handle to the pipe is OK.\n");
    
       CloseHandle(hChildStdinWr); 
    
       // Now create the child process. 
    
       fSuccess = CreateChildProcess();
    
       if (! fSuccess) 
    
          MyErrorExit("CreateChildProcess(), a child process failed");
    
       else
    
          printf("CreateChildProcess(), a child process is OK.\n");
    
       // Get a handle to the parent's input file.
    
       printf("Getting a handle to the parent's input file.\n");
    
       if (argc == 1)
    
              MyErrorExit("Please specify an input file!");
    
       else
    
              printf("An input file is %s\n.", argv[1]);
    
       hInputFile = CreateFile(argv[1], GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
    
       if (hInputFile == INVALID_HANDLE_VALUE)
    
              MyErrorExit("CreateFile(), open existing failed\n");
    
       else
    
              printf("CreateFile(), open existing %s is OK.\n", argv[1]);
    
       // Write to pipe that is the standard input for a child process.
    
       printf("Try writing to pipe...\n");
    
       WriteToPipe();
    
       // Read from pipe that is the standard output for child process.
    
       printf("Try reading from pipe...\n");
    
       ReadFromPipe();
    
       return 0;
    
    }
    
    //================================================
    
    BOOL CreateChildProcess() 
    
    {
    
       PROCESS_INFORMATION piProcInfo; 
    
       STARTUPINFO siStartInfo;
    
       BOOL bFuncRetn = FALSE; 
    
       // Set up members of the PROCESS_INFORMATION structure. 
    
       ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));
    
       // Set up members of the STARTUPINFO structure. 
    
       ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
    
       siStartInfo.cb = sizeof(STARTUPINFO); 
    
       siStartInfo.hStdError = hChildStdoutWr;
    
       siStartInfo.hStdOutput = hChildStdoutWr;
    
       siStartInfo.hStdInput = hChildStdinRd;
    
       siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
    
       // Create the child process.
    
       bFuncRetn = CreateProcess(NULL,                       // Use the following command line
    
             "C:\\Windows\\system32\\cmd.exe",       // 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 
    
       if (bFuncRetn == 0)
    
          MyErrorExit("CreateProcess() for child failed.");
    
       else
    
       {
    
    printf("CreateProcess for child is OK.\n");
    
    CloseHandle(piProcInfo.hProcess);
    
    CloseHandle(piProcInfo.hThread);
    
    return bFuncRetn;
    
       }
    
    }
    
    //================================================================
    
    VOID WriteToPipe(VOID)
    
    {
    
       DWORD dwRead, dwWritten;
    
       CHAR chBuf[BUFSIZE];
    
       // Read from a file and write its contents to a pipe.
    
       printf("In WriteToPipe(), try to read from a file and write its contents to a pipe.\n");
    
       for (;;) // More read control should be implemented here such as EOF etc...
    
       {
    
          if (!(ReadFile(hInputFile, chBuf, BUFSIZE, &dwRead, NULL) || dwRead == 0))
    
             printf("ReadFile() failed.\n");
    
             break;
    
          if (!WriteFile(hChildStdinWrDup, chBuf, dwRead, &dwWritten, NULL))
    
             printf("WriteFile() failed.\n");
    
             break;
    
            }
    
       // Close the pipe handle so the child process stops reading.
    
       if (!CloseHandle(hChildStdinWrDup))
    
          MyErrorExit("Close pipe failed");
    
    } 
    
    //================================================================
    
    VOID ReadFromPipe(VOID) 
    
    {
    
       DWORD dwRead, dwWritten;
    
       CHAR chBuf[BUFSIZE];
    
       // Close the write end of the pipe before reading from the read end of the pipe.
    
       if (!CloseHandle(hChildStdoutWr))
    
          MyErrorExit("CloseHandle() failed");
    
       else
    
          printf("CloseHandle() is OK.\n");
    
       // Read output from the child process, and write to parent's STDOUT.
    
       printf("In ReadFromPipe(), try to read output from the child process, and write to parent's STDOUT.\n");
    
       for (;;) // More read control should be implemented here such as EOF etc...
    
       {
    
          if (!ReadFile(hChildStdoutRdDup, chBuf, BUFSIZE, &dwRead, NULL) || dwRead == 0)
    {
            printf("ReadFile() failed.\n");
    
            break;
    }
      printf(chBuf);
    
    Sleep(3000);
    char s[]="dir";
          if (!WriteFile(hStdout, s, dwRead, &dwWritten, NULL))
    
     {        printf("WriteFile() failed.\n");
    
             break;}
    
            }
    
    }
    
    //================================================================
    
    VOID MyErrorExit(LPTSTR lpszMessage) 
    
    {
    
       fprintf(stderr, "%s\n", lpszMessage);
    
       // Exit peacefully...
    
       ExitProcess(0);
    
    }
    

    Der Code bricht ab, wenn er das 2 Mal Read File gestartet wird.
    Hat jemand eine idee 😕



  • hmm
    evtl. ein besseres beispiel?!

    vieleicht hilft dir ja das weiter...
    http://www.codeproject.com/KB/cpp/9505Yamaha_1.aspx



  • okay thx 😃


Anmelden zum Antworten