Windows anonymous pipe ReadFile not reading

318 views Asked by At

I have just started doing some pipes in Windows. So i wrote a little sample code for basic READ/WRITE COMMUNICATION. But ReadFile is non-stop blocking and never reads data.

HANDLE Oprimek_Read;
HANDLE Oprimek_Write;
SECURITY_ATTRIBUTES varnostniAtributi;
varnostniAtributi.nLength = sizeof(SECURITY_ATTRIBUTES);
varnostniAtributi.bInheritHandle = TRUE;
varnostniAtributi.lpSecurityDescriptor = NULL;
thread prvi (pisanje);
thread drugi (branje);
BOOL ustvarjenaCev = FALSE;
ustvarjenaCev = CreatePipe(&Oprimek_Read,&Oprimek_Write,&varnostniAtributi,150);
if(ustvarjenaCev == TRUE)
    printf("Anon pipe created\n");
else
    printf("Error creating pipe\n");

    DWORD dwWritten;
    DWORD dwBytesToWrite = (DWORD)strlen(buffer);
    cin>>beseda;
    strcpy(buffer,beseda.c_str());
    printf("Buffer to send is: %s\n", buffer);

    ustvarjenaCev =     WriteFile(Oprimek_Write,buffer,dwBytesToWrite,&dwWritten,NULL);
    FlushFileBuffers(Oprimek_Write);
    FlushFileBuffers(Oprimek_Read);

    if( ! ustvarjenaCev){
        ErrorExit(TEXT("Write to Pipe")); 
    }
    //-----------------HERE IS THE PROBLEM--------------
    cout<<"Waiting\n";
    ustvarjenaCev = ReadFile(Oprimek_Read,buffer1,sizeof(buffer),0,NULL);
    cout<<"ReadPipe\n";

    if( ! ustvarjenaCev){
        ErrorExit(TEXT("Read to pipe")); 
    }
    else
        cout<<buffer1<<endl;

I think I have done everything that was demanded via MSDN documentation. Any ideas?

0

There are 0 answers