Hello.
He broke his head thinking that he was wrong and came to the conclusion that in general everything is wrong, there are suspicions that it is impossible to do this.
The essence of the program is that one stream performs calculations, creates a channel and gives the result to another stream, which, after transformations, displays the data on the screen.
Here is the main function, everything is clear. We create two streams, nothing goes to their input.
int _tmain(int argc, _TCHAR* argv[]) { DWORD dw; HANDLE IDThread[2]; IDThread[0]=CreateThread(NULL,0,MainTread,0, 0, &dw); IDThread[1]=CreateThread(NULL,0,MinorThread,0, 0, &dw); WaitForMultipleObjects(ThrCount,IDThread,TRUE,INFINITE); system("pause>>void"); return 0; }
Next, the 1st thread. (I simplified the program to horror, leaving the most basic).
DWORD WINAPI MainTread(LPVOID Insert) { wchar_t *PName = L"\\\\.\\pipe\\TransmitPipe"; //ΠΠΌΡ ΠΊΠ°Π½Π°Π»Π° HANDLE hPipe; //ΠΠΊΠ·Π΅ΠΌΠΏΠ»ΡΡ ΡΠ°ΠΌΠΎΠ³ΠΎ ΠΊΠ°Π½Π°Π»Π° BOOL Success; //ΠΡΠΈΠ½ΠΈΠΌΠ°Π΅Ρ Π·Π½Π°ΡΠ΅Π½ΠΈΡ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΠΌΡΠ΅ ΡΡΠ½ΠΊΡΠΈΡΠΌΠΈ DWORD byteCnt, byteWrt; //Π§ΠΈΡΠ»ΠΎ Π±Π°ΠΉΡ ΠΊΠΎΡΠΎΡΡΠ΅ Π½Π°Π΄ΠΎ Π·Π°ΠΏΠΈΡΠ°ΡΡ Π² ΠΏΠΎΡΠΎΠΊ ΠΈ ΡΡΠ΅ΡΡΠΈΠΊ Π±Π°ΠΉΡ ΡΠ΅Π°Π»ΡΠ½ΠΎ Π·Π°ΠΏΠΈΡΠ°Π½Π½ΡΡ
Π² ΠΊΠ°Π½Π°Π». char X[10]; //ΠΡΠ΄Π΅ΠΌ ΠΏΠ΅ΡΠ΅Π΄Π°Π²Π°ΡΡ Π²ΡΠΎΡΠΎΠΌΡ ΠΏΠΎΡΠΎΠΊΡ ΡΡΠΎΡ ΡΠ°Ρ for(int i=0; i<10; i++) {X[i]='9';} //ΠΠ°ΠΏΠΎΠ»Π½ΠΈΠΌ Π΅Π³ΠΎ Π΄Π΅Π²ΡΡΠΊΠ°ΠΌΠΈ X[i]='\0' //ΠΠ° Π²ΡΡΠΊΠΈΠΉ ΡΠ»ΡΡΠ°ΠΉ //Π‘Π°ΠΌΠΎΠ΅ ΠΈΠ½ΡΠ΅ΡΠ΅ΡΠ½ΠΎΠ΅ hPipe = CreateNamedPipe(PName, PIPE_ACCESS_DUPLEX,PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,PIPE_UNLIMITED_INSTANCES,4096,4096,0,NULL); if(hPipe!=INVALID_HANDLE_VALUE) {std::cout<<"1) Pipe create is OK \n";} //ΠΡΠΎΠ²Π΅ΡΠΊΠ°. ΠΡΠ»ΠΈ ΡΠΊΠ·Π΅ΠΌΠΏΠ»ΡΡ Π½Π΅ Π²Π΅ΡΠ½ΡΠ» Π½Π΅ΠΏΡΠ°Π²ΠΈΠ»ΡΠ½ΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ Π·Π½Π°ΡΠΈΡ ΠΊΠ°Π½Π°Π» ΡΠΎΠ·Π΄Π°Π½. 1) - ΠΎΠ·Π½Π°ΡΠ°Π΅Ρ ΡΡΠΎ ΠΏΠΎΡΠΎΠΊ 1ΡΠΉ. Success = ConnectNamedPipe(hPipe, NULL); //ΠΠΆΠΈΠ΄Π°Π΅ΠΌ Π·Π°ΠΏΡΠΎΡΠ° Π½Π° ΡΠΎΠ΅Π΄ΠΈΠ½Π΅Π½ΠΈΠ΅ Ρ ΡΡΠΈΠΌ ΠΊΠ°Π½Π°Π»ΠΎΠΌ if(Success){std::cout<<"1) Connect is OK \n";} //ΠΡΠ»ΠΈ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ 1, Π·Π½Π°ΡΠΈΡ ΡΠΎΠ΅Π΄ΠΈΠ½Π΅Π½ΠΈΠ΅ ΠΏΡΠΎΡΠ»ΠΎ Success = WriteFile(hPipe, X, byteCnt, &byteWrt, NULL); //ΠΠΈΡΠ΅ΠΌ Π½Π°Ρ Π₯ Π² ΠΊΠ°Π½Π°Π» hPipe if(Success){std::cout<<"1) Write is OK \n";} //Π€ΡΠ½ΠΊΡΠΈΡ Π²Π΅ΡΠ½ΡΠ»Π° 1 - Π²ΡΠ΅ ΠΠ }
Next, the second stream (There in the output messages on the screen will be "2)").
DWORD WINAPI MinorThread(LPVOID Insert) { wchar_t *PName = L"\\\\.\\pipe\\TransmitPipe"; //ΠΡΠ΅ ΡΠ°ΠΊ ΠΆΠ΅, ΡΡΠΎ ΠΈΠΌΡ ΠΊΠ°Π½Π°Π»Π° HANDLE hPipe; //ΠΠΊΠ·Π΅ΠΌΠΏΠ»ΡΡ BOOL Success; DWORD pMode; //ΠΡΠΎ ΡΠΊΠ°Π·Π°ΡΠ΅Π»Ρ Π½Π° ΡΠ΅ΠΆΠΈΠΌ ΡΠ°Π±ΠΎΡΡ, ΠΏΠΎΠ΄ΡΠΎΠ±Π½Π΅Π΅ Π½ΠΈΠΆΠ΅. char X[10]; //Π‘ΡΠ΄Π° ΠΏΠΈΡΠ΅ΠΌ, ΡΠΎ ΡΡΠΎ ΠΏΡΠΈΡΠ»Π΅Ρ ΠΊΠ°Π½Π°Π» hPipe = CreateFile(PName,GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL); //ΠΠΎΠ΄ΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ ΠΊ ΡΡΡΠ΅ΡΡΠ²ΡΡΡΠ΅ΠΌΡ ΠΊΠ°Π½Π°Π»Ρ (ΡΠΎΠ·Π΄Π°Π½Π½ΠΎΠΌΡ Π² 1ΠΎΠΌ ΠΏΠΎΡΠΎΠΊΠ΅) if(hPipe!=INVALID_HANDLE_VALUE) {std::cout<<"2) File create is OK \n";} //ΠΡΠΎΠ²Π΅ΡΠΊΠ° (ΡΠΎΡ ΠΆΠ΅ ΡΠΌΡΡΠ» ΡΡΠΎ ΠΈ Π² 1Π) Success = WaitNamedPipe(PName, NMPWAIT_WAIT_FOREVER); //ΠΠΆΠΈΠ΄Π°Π½ΠΈΠ΅ ΠΏΠΎΠ΄ΠΊΠ»ΡΡΠ΅Π½ΠΈΡ ΠΊ ΡΠΊΠ·Π΅ΠΌΠΏΠ»ΡΡΡ ΠΊΠ°Π½Π°Π»Π° if(Success){std::cout<<"2) Pipe avelible OK \n";} //ΠΠ°Π½Π°Π» Π΄ΠΎΡΡΡΠΏΠ΅Π½ Π΅ΡΠ»ΠΈ Π²ΡΠ΅ Π½ΠΎΡΠΌΠ°Π»ΡΠ½ΠΎ pMode = PIPE_READMODE_MESSAGE; //Π Π΅ΠΆΠΈΠΌ ΡΡΠ΅Π½ΠΈΡ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠΉ Success = SetNamedPipeHandleState(hPipe, &pMode, NULL, NULL); //ΠΠ΅ΡΠ΅ΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ ΠΊΠ°Π½Π°Π»Π° Π² ΡΠ΅ΠΆΠΈΠΌ ΡΡΠ΅Π½ΠΈΡ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠΉ if(Success){std::cout<<"2) Readmode is OK \n";} Success = ReadFile(hPipe, Π₯, 4096, &byteWrt, NULL); //Π§ΡΠ΅Π½ΠΈΠ΅ ΠΈΠ· ΡΠΊΠ·Π΅ΠΌΠΏΠ»ΡΡΠ° Π² Π₯. 4096 - ΡΠ°Π·ΠΌΠ΅Ρ Π±ΡΡΠ΅ΡΠ° ΠΏΠΎΠ΄ ΡΠΈΡΠ°Π΅ΠΌΠΎΠ΅ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ if(Success){std::cout<<"2) Read is OK \n";} std::cout<<X; }
Displays 0.
But he writes:
That is, the channel is created and data is written there, and even the second stream is connected to it, in general, everything stops working.
So the question: Was it a good idea to throw a channel from stream to stream? Maybe you know where I nakosyachil code?
Put semaphores. The second stream is trying to read from the channel:
The essence of the error: MSDN
ERROR_NOACCESS 998 (0x3E6) Invalid access to memory location.
Question: Why? Writing to the channel is normal, why is there an error when reading, and yes even with a complaint about a section of memory.