ReadFile fails with error code 998

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:

1.

“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.
 
2.

If I add

BASEDIR  C:\DDK
DDKDRIVE  C:

into environment setting then my systems works fine.

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

3.

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?


Regards,

Suman Kumar
suman_princeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
>>“Invalid access to memory location.”

Post your code.
suman_princeAuthor Commented:
Hi

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
Sleep(200);
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;
      break;
      }
     }
    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 );
                        

Regards,

Suman             
nonubikCommented:
This is not the whole code. You didn't post how is 's_tSendMsg' declared/defined (and other variables).
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

jkrCommented:
Hmmm, is the pipe handle valid at that moment? I am asking because you are mentioning that this happens in a DLL.
suman_princeAuthor Commented:
Yes. I am creating pipe in Dll only just before "pbReceiveBuffer = new BYTE[dwReceiveBuffer];" statment.

mxjijoCommented:

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.