Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CreatePipe is not working properly

CreatePipe is not working properly

Scheduled Pinned Locked Moved C / C++ / MFC
help
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    singh_nav
    wrote on last edited by
    #1

    Hi All, My exceptions are to write and read data simultaneously.As data is written it should be read by another process.For this I am using the PIPE implementation. I wrote the following code snippet for this but it is not working Please let know where i am wrong Thanks HANDLE rHandle, wHandle ; DWORD dw ; DWORD dw1 ; void WriteData(LPVOID) { char wBuff[512]; strcpy(wBuff, "HELLO world this program for reading the pipe\n ") ; for( ; ; ) { Sleep(1); if(WriteFile(wHandle, wBuff, strlen(wBuff), &dw, NULL)) { printf("Total %d byts written\n", dw); } else { printf("Could not write the buffer Last error:%d\n ", GetLastError()); break; } } } void ReadData(LPVOID) { char wBuff[512]; int i =0 ; strcpy(wBuff, "HELLO world this program for reading the pipe\n ") ; for( ; ; ) { Sleep(1); if(ReadFile(rHandle, wBuff, strlen(wBuff), &dw1, NULL)) { printf("Total %d byts read\n", dw1 ); } else { printf("Could not read the buffer Last error:%d\n ", GetLastError()); break; } } } void main() { HANDLE aThread; DWORD ThreadID; int i; if(CreatePipe( &rHandle, &wHandle, NULL, 0)) { CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) WriteData, NULL, 0, &ThreadID); CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) ReadData, NULL, 0, &ThreadID); } }

    _ S 2 Replies Last reply
    0
    • S singh_nav

      Hi All, My exceptions are to write and read data simultaneously.As data is written it should be read by another process.For this I am using the PIPE implementation. I wrote the following code snippet for this but it is not working Please let know where i am wrong Thanks HANDLE rHandle, wHandle ; DWORD dw ; DWORD dw1 ; void WriteData(LPVOID) { char wBuff[512]; strcpy(wBuff, "HELLO world this program for reading the pipe\n ") ; for( ; ; ) { Sleep(1); if(WriteFile(wHandle, wBuff, strlen(wBuff), &dw, NULL)) { printf("Total %d byts written\n", dw); } else { printf("Could not write the buffer Last error:%d\n ", GetLastError()); break; } } } void ReadData(LPVOID) { char wBuff[512]; int i =0 ; strcpy(wBuff, "HELLO world this program for reading the pipe\n ") ; for( ; ; ) { Sleep(1); if(ReadFile(rHandle, wBuff, strlen(wBuff), &dw1, NULL)) { printf("Total %d byts read\n", dw1 ); } else { printf("Could not read the buffer Last error:%d\n ", GetLastError()); break; } } } void main() { HANDLE aThread; DWORD ThreadID; int i; if(CreatePipe( &rHandle, &wHandle, NULL, 0)) { CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) WriteData, NULL, 0, &ThreadID); CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) ReadData, NULL, 0, &ThreadID); } }

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #2

      See here[^]

      Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

      1 Reply Last reply
      0
      • S singh_nav

        Hi All, My exceptions are to write and read data simultaneously.As data is written it should be read by another process.For this I am using the PIPE implementation. I wrote the following code snippet for this but it is not working Please let know where i am wrong Thanks HANDLE rHandle, wHandle ; DWORD dw ; DWORD dw1 ; void WriteData(LPVOID) { char wBuff[512]; strcpy(wBuff, "HELLO world this program for reading the pipe\n ") ; for( ; ; ) { Sleep(1); if(WriteFile(wHandle, wBuff, strlen(wBuff), &dw, NULL)) { printf("Total %d byts written\n", dw); } else { printf("Could not write the buffer Last error:%d\n ", GetLastError()); break; } } } void ReadData(LPVOID) { char wBuff[512]; int i =0 ; strcpy(wBuff, "HELLO world this program for reading the pipe\n ") ; for( ; ; ) { Sleep(1); if(ReadFile(rHandle, wBuff, strlen(wBuff), &dw1, NULL)) { printf("Total %d byts read\n", dw1 ); } else { printf("Could not read the buffer Last error:%d\n ", GetLastError()); break; } } } void main() { HANDLE aThread; DWORD ThreadID; int i; if(CreatePipe( &rHandle, &wHandle, NULL, 0)) { CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) WriteData, NULL, 0, &ThreadID); CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE) ReadData, NULL, 0, &ThreadID); } }

        S Offline
        S Offline
        sashoalm
        wrote on last edited by
        #3

        singh_nav wrote:

        As data is written it should be read by another process

        You're using the pipe in threads, not processes, may be that's the problem :)

        There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition. Blaise Pascal

        S 1 Reply Last reply
        0
        • S sashoalm

          singh_nav wrote:

          As data is written it should be read by another process

          You're using the pipe in threads, not processes, may be that's the problem :)

          There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition. Blaise Pascal

          S Offline
          S Offline
          singh_nav
          wrote on last edited by
          #4

          Thanks for the input. Can't i use the PIPE in the same process ?

          S 1 Reply Last reply
          0
          • S singh_nav

            Thanks for the input. Can't i use the PIPE in the same process ?

            S Offline
            S Offline
            sashoalm
            wrote on last edited by
            #5

            You certainly can. But if it's in the same process, why use a pipe at all? You can use something much simpler, even a raw array, if you like, so long as you're careful about synchronization[^].

            There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition. Blaise Pascal

            S 1 Reply Last reply
            0
            • S sashoalm

              You certainly can. But if it's in the same process, why use a pipe at all? You can use something much simpler, even a raw array, if you like, so long as you're careful about synchronization[^].

              There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition. Blaise Pascal

              S Offline
              S Offline
              singh_nav
              wrote on last edited by
              #6

              Thanks for the info, Actually , I have used the array in my sample program there i have to synchronize it. Now i want to check that how to use the pipe and will it signal to read Thread automatically as it is written. Please provide any link or sample so i could implement it in the same process. Thanks

              S 1 Reply Last reply
              0
              • S singh_nav

                Thanks for the info, Actually , I have used the array in my sample program there i have to synchronize it. Now i want to check that how to use the pipe and will it signal to read Thread automatically as it is written. Please provide any link or sample so i could implement it in the same process. Thanks

                S Offline
                S Offline
                sashoalm
                wrote on last edited by
                #7

                There is an article about pipes in the Code Project here[^]. Check out the Intra-process Communication part of the article, that's what you need. Hope that helps.

                There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition. Blaise Pascal

                S 1 Reply Last reply
                0
                • S sashoalm

                  There is an article about pipes in the Code Project here[^]. Check out the Intra-process Communication part of the article, that's what you need. Hope that helps.

                  There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition. Blaise Pascal

                  S Offline
                  S Offline
                  singh_nav
                  wrote on last edited by
                  #8

                  Thank you very much This is exactly i wanted for.

                  S 1 Reply Last reply
                  0
                  • S singh_nav

                    Thank you very much This is exactly i wanted for.

                    S Offline
                    S Offline
                    sashoalm
                    wrote on last edited by
                    #9

                    No problem :cool:

                    There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition. Blaise Pascal

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups