0

I have a Memory stream object in C# containing xml data.

fileEntity = new FileEntity();
fileEntity.Bytes = new byte[stream[0].Length];
fileEntity.FileName = ConfigurationManager.AppSettings["BackupPath"].ToString() + "\\" + backupEntity.BackupFileName;

stream.Position = 0;
stream.Read(fileEntity.Bytes, 0, (int)stream[0].Length);

When I write the fileEntity.Bytes to a file in C#, it gets generated correctly.

However, I need to access bytes in C++ using COM and write the bytes to file.

pSABytes = fileentity->GetBytes();
bytes = (byte*)pSABytes;
LONG ub;
HRESULT res = SafeArrayGetUBound(pSABytes, 1, &ub);
FILE* file = fopen("c:\\Abc.xml", "w+");
fwrite( bytes, 1, ub, file );
fclose(file);

However I get an exception on line fwrite(bytes,1,ub,file)

Unhandled exception at 0x5f268962 (msvcr100d.dll) in COM.exe: 0xC0000005: Access violation reading location 0x000000001cf1d000.

0

1 Answer 1

1

bytes = (byte*)pSABytes is not legal for what you are trying to do. You need to call SafeArrayAccessData(pSABytes, &bytes)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.