Search code examples
c++nul

C++ String reaches over several NUL Bytes


I hate to ask this because I think this must be very trivial. But as someone who is used to high-level-languages this is a real problem.

I got a C++ program which uses PDFium to generate an Image to a PDF. And i have a C# program which communicates with the C++ program via Named Pipes. The PDF file (Which is saved as a byte-array) gets transmitted by the pipe. And here is my Problem.

On the 374th Position of the stream is a NUL byte (00) and im too stupid to somehow reach the data after it.

Here is my Code:

LPTSTR lpszPipename2 = TEXT("\\\\.\\pipe\\myNamedPipe2"); 
hPipe2=CreateFile(lpszPipename2, GENERIC_READ, 0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
if(ReadFile( hPipe2, chBuf, dwBytesToRead, &cbRead, NULL))
{
    PDFData = chBuf;
}

dwBytes to read is the size of the file and cbRead shows the correct number. But PDFData only contains the first 373 bytes. I checked that the data beyond the 373th position is there with the Immediate Window i just don't know how to process it. I gotta put the Data into a char-array.

As I already said, i think this is very trivial. But although i know where the problem comes from, i have simply no idea how to fix it.

Many Thanks and Regards

Michael

Edit: The C#-Code. Its everything but perfect. But i'm very sure this Problem is on the C++ side.

public void SendRawData(byte[] data)
{
  while (clientse == null || clientse.stream == null)
  { }
  if (clientse.stream.CanWrite)
  {
    clientse.stream.Write(data, 0, data.Length);
    clientse.stream.Flush();
  }
}
private void ListenForClients()
    {
        while (true)
        {
            clientHandle = CreateNamedPipe(this.pipeName, DUPLEX | FILE_FLAG_OVERLAPPED, 0, 255, BUFFER_SIZE, BUFFER_SIZE, 0, IntPtr.Zero);

            //could not create named pipe
            if (clientHandle.IsInvalid)
                return;

            int success = ConnectNamedPipe(clientHandle, IntPtr.Zero);

            //could not connect client
            if (success == 0)
                return;

            clientse = new Client();
            clientse.handle = clientHandle;
            clientse.stream = new FileStream(clientse.handle, FileAccess.ReadWrite, BUFFER_SIZE, true);

            if (ClientType == 0)
            {
                Thread readThread = new Thread(new ThreadStart(Read));
                readThread.Start();
            }                
        }
    }

"Solution": Actually this never was a real problem. I just got my wires crossed. While chBuf seemed after copying it into PDFData or when i read its value is VS to only have those 373 bytes. All ~20 kilobytes were copied to that position. I knew that, but i didn't understand how the PDFium sources should know that if the string terminates after 373 chars.

Well... the PDFium-sources know it cause i have to pass the length. Which was determined by

size_t len = PDFData.length();

and was therefore of course only 373 bytes.


Solution

  • Actually this never was a real problem. I just got my wires crossed. While chBuf seemed after copying it into PDFData or when i read its value is VS to only have those 373 bytes. All ~20 kilobytes were copied to that position. I knew that, but i didn't understand how the PDFium sources should know that if the string terminates after 373 chars.

    Well... the PDFium-sources know it cause i have to pass the length. Which was determined by

    size_t len = PDFData.length();

    and was therefore of course only 373 bytes.

    I'm sorry that i bothered you with that stuff