ReadFile fails with error code 998

suman_prince used Ask the Experts™
Dear All

My project is divided into two parts one is client and other is server (which is Language Monitor DLL).
On server side creates NamedPipe using “CreateNamedPipe” and waiting for Client connection using “ConnectNamedPipe” Win32 API’s.

Once the connection is established ReadFile and WriteFile follows.

After three times successfully ReadFile and WriteFile operation server side application terminates when it try to read from Pipe(using ReadFile), with error code 998 which is “Invalid access to memory location.”

I am passing a buffer (which is a structure) to ReadFile, which I have allocated through “new”.

My Observations:


“Spoolsv.exe” calls my Server DLL (which is a Language Monitor DLL) on every system reboot.
If I try to stop “Spoolsv.exe” through Task Manager and again start “Spoolsv.exe” by double clicking, every thing works fine. Problem occurs only when “Spoolsv.exe” calls my dll just after reboot.

If I add


into environment setting then my systems works fine.

I have not installed DDK on my system; I am just adding these two entries.    


If I am creating my own heap using “HeapCreate” API function and allocating memory through “HeapAlloc” API then every thing working fine.

Problem History:

Same problem we had faced some time back, that time we were passing static buffer to ReadFile then we changed to dynamic buffer and system started working, if today I pass static buffer then systems work fine.

Development and Test environment:

Visual studio 5.0
Service pack 1.
Window XP.
Could you tell where I would be going wrong?


Suman Kumar
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2012

>>“Invalid access to memory location.”

Post your code.



Here is the code:

pbReceiveBuffer = new BYTE[dwReceiveBuffer];
ZeroMemory(pbReceiveBuffer, dwReceiveBuffer);
pbSendBuffer = (PBYTE) &s_tSendMsg;
dwCommandState = 0;
// cmck start 24/02/03
// delay added to allow time for pipe to come available
while ( bOk )
  bOk = ReadFile( s_hApiPipe,                  // Handle of file to read
        pbReceiveBuffer,      // Buffer that receives data
      dwReceiveBuffer,      // Number of bytes to read
      &dwBytesRead,            // Number of bytes read
      NULL );                        // Structure needed for overlapped I/O
      if ( !bOk )
                                                dwLastError = GetLastError();
      if ( dwLastError == ERROR_BROKEN_PIPE )
      DBG_WARN((MSG_INFO, L"Pipe broken" ));
      bOk = TRUE;
    bOk = WriteFile( s_hApiPipe,            // Handle to pipe
                 pbSendBuffer,            // Data to write to pipe
                 dwBytesToSend,            // Number of bytes to write
       &dwBytesWritten,      // Number of bytes written
       NULL );                  // Structure needed for overlapped I/O
  if ( !bOk )
      dwLastError = GetLastError();
      DBG_ERROR((MSG_ERROR,  L"WriteFile failed, LastError %s (0x%X = %d)",            EvtMapStatusToName( dwLastError ),   dwLastError,  dwLastError ));
delete (pbReceiveBuffer);
DisconnectNamedPipe( s_hApiPipe );



This is not the whole code. You didn't post how is 's_tSendMsg' declared/defined (and other variables).
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Top Expert 2012

Hmmm, is the pipe handle valid at that moment? I am asking because you are mentioning that this happens in a DLL.


Yes. I am creating pipe in Dll only just before "pbReceiveBuffer = new BYTE[dwReceiveBuffer];" statment.


I dont know much about lang monitors so.. my advance pardon !!

Did you check the size of s_tSendMsg and dwBytesToSend are matching all the time  ?
s_tSendMsg looks like a global variable to me.

If dwBytesToSend is bigger than the size of s_tSendMsg you might get this error.

I guess the spool mgr sends a set of commands and expects a set of results from the lang monitor DLLs.
May be one of those command structures are not matching with what your DLL had compiled with.

Did you write the DLL from the scratch or took it from the DDK ? (just wondering about the DDK dependency)
If you have taken it from the DDK, make sure the version matches too (WinXP home v/s XP pro etc)

What I am trying to say is there might be difference in the size of the structures being passed around.

home that helped.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial