Solved

Named Pipe Server returning NULL on read file

Posted on 2010-09-21
4
335 Views
Last Modified: 2012-05-10
Hi Guys,

I have a named pipe server in c++ and a named pipe client in c++, but whenever the client sends something to the server with the WriteFile funcion, the server doesent detect what it received. It just returns a NULL like:

Client sent the following message: (NULL)

Here is the Named Pipe SERVER code if you can spot something wrong with it:

#include "stdafx.h"
#include <stdio.h>
#include "windows.h"

#define BUFSIZE 1024 //1k

int main(){

    BOOL fConnected;
    LPTSTR lpszPipename = "\\\\.\\pipe\\MainStream";
    CHAR chRequest[BUFSIZE];
    DWORD cbBytesRead;
    BOOL fSuccess;
    HANDLE hPipe;

     hPipe = CreateNamedPipe( 
          lpszPipename,             // pipe name 
          PIPE_ACCESS_DUPLEX,       // read/write access 
          PIPE_TYPE_MESSAGE |       // message type pipe 
          PIPE_READMODE_MESSAGE |   // message-read mode 
          PIPE_WAIT,                // blocking mode 
          PIPE_UNLIMITED_INSTANCES, // max. instances  
          BUFSIZE,              // output buffer size 
          BUFSIZE,              // input buffer size 
          NMPWAIT_USE_DEFAULT_WAIT, // client time-out 
          NULL);                    // default security attribute 

     if (hPipe == INVALID_HANDLE_VALUE){
         return true;
     }

    for (;;)
     {

          fConnected = ConnectNamedPipe(hPipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);

          if (fConnected)
          {

              LPVOID  szBuffer;
              DWORD cbBytes;

                fSuccess = ReadFile( 
                    hPipe,                // handle to pipe 
                    szBuffer,           // buffer to receive data 
                    sizeof(szBuffer),   // size of buffer 
                    &cbBytes,           // number of bytes read 
                    NULL);              // not overlapped I/O 

                
                printf("\nClient sent the following message: %s", szBuffer);

                
                if (! fSuccess || cbBytesRead == 0)
                //break;

                FlushFileBuffers(hPipe);                  
                DisconnectNamedPipe(hPipe);    

          }else{
              CloseHandle(hPipe); 
          }

    }
    
}

Open in new window


I can post the client code aswell, but i think that the issue is located on the server side. Thank you for all your help.


0
Comment
Question by:xNejX
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 33729711
What does 'GetLastError()' report in that case?
0
 

Author Comment

by:xNejX
ID: 33729872
its:

Invalid access to memory location.
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 33729921
You are using a locally declared and uninitialized

LPVOID  szBuffer;

in your loop. Either make that

LPVOID  szBuffer = chRequest;

or allocate memory for it.
0
 

Author Closing Comment

by:xNejX
ID: 33729976
It solved the problem. Thank you.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now