Home > OS >  What does "access violation " in context of named pipes in windows mean?
What does "access violation " in context of named pipes in windows mean?

Time:06-12

I'm trying to develop a trader bot. The bot itself is written in mql, while it communicates with a c code, to receive trading commands(buy and sell) from it and also, send the latest asset price to it. This communication is being done through named pipes(in the context of windows 10). The problem is that when the mql code tries to write something, I get the following error:

error

What does this error mean and How can I fix it? thanks.

Bu the way, the function that I use for writing is as follows:

int CNamedPipe::WriteUnicode(string message)
  {
   int ushortsToWrite, bytesWritten;
   ushort UNICODEarray[];
   ushortsToWrite = StringToShortArray(message, UNICODEarray);
   WriteFile(hPipe,ushortsToWrite,sizeof(int),bytesWritten,0);
   WriteFile(hPipe,UNICODEarray,ushortsToWrite*sizeof(ushort),bytesWritten,0);
   return bytesWritten;
  }
// -----------

CodePudding user response:

The culprit is almost certainly

ushort UNICODEarray[];
ushortsToWrite = StringToShortArray(message, UNICODEarray);

If this compiles, and its a big 'if', you have a zero size buffer, but presumably StringToShortArray tries to write to it, you dont show that function so its only a guess.

It should not compile since not valid c but maybe your compiler allows it (you could argue that a zero size array is a valid construct and make it a language extension for some reason)

CodePudding user response:

ushort UNICODEarray[]; is not valid C code. An array needs a size specified. If you don't know the size at compile-time, then you have to allocate the array dynamically at runtime, either by using new[] or std::vector.

Also, this:

WriteFile(hPipe,ushortsToWrite,sizeof(int),bytesWritten,0);
WriteFile(hPipe,UNICODEarray,ushortsToWrite*sizeof(ushort),bytesWritten,0);

needs to be like this instead:

WriteFile(hPipe,&ushortsToWrite,sizeof(int),&bytesWritten,NULL);
WriteFile(hPipe,UNICODEarray,ushortsToWrite*sizeof(ushort),&bytesWritten,NULL);
  • Related