kdmin
kdmin

Reputation: 434

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);

Upvotes: 0

Views: 4256

Answers (2)

kdmin
kdmin

Reputation: 434

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!

Upvotes: 0

Matthias
Matthias

Reputation: 3556

You could do

CComPtr<IStream> stream;
COM_VERIFY(::CreateStreamOnHGlobal(0, TRUE, &stream));

Its also possible to use GlobalAlloc to specify "own" memory to use. (in case your german is good - see http://msdn.microsoft.com/de-de/magazine/cc163436.aspx#S5 ).

Upvotes: 3

Related Questions