Search code examples
c++windowsxmllite

Write array to IStream


I recieve some data as a char array. I want to pass this data to a method that recieves a stream (IUnknown *pInput).

I tried to debug this piece of code (using visual c++):


#include "xmllite.h"
#include <atlbase.h>

CHAR acTemp [100] = {0};
CComPtr<IStream> pDataStream;
HRESULT hr;

hr = IStream_Write (pDataStream, acTemp, sizeof (acTemp));

I get the error:

Unhandled exception at 0x75e49875 in SSL.exe: 0xC0000005: Access violation reading location 0x00000000.

I know that the following code that creates a strem from a file will work, but I didn't want to create a file just because I can't convert from the original array.

CComPtr<IStream> pFileStream;
LPCSTR szFileName = "FileName";
HRESULT hr;

hr = SHCreateStreamOnFile(szFileName, STGM_READ, &pFileStream);

Solution

  • To specifically answer the question, the snippet is:

    CHAR *acTemp;
    acTemp = (CHAR *) GlobalAlloc (GMEM_FIXED, dwBytes);
    memcpy (acTemp, acXml, dwBytes);
    hr = ::CreateStreamOnHGlobal(acTemp, TRUE, &pDataStream);
    

    In this case acXml is the char array containing the data to put in the stream.

    I didn't find out why the previous did not work, but this one worked fine so far. Thanks @Matthias for the help!