CreatePipe is not working properly
-
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); } }
-
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); } }
-
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); } }
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
-
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
-
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
-
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
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
-
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
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
-
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