0

我在 C# 中有一个包含 xml 数据的内存流对象。

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

当我fileEntity.Bytes在 C# 中写入文件时,它会正确生成。

但是,我需要使用 COM 访问 C++ 中的字节并将字节写入文件。

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

但是我在网上遇到了一个例外fwrite(bytes,1,ub,file)

COM.exe 中 0x5f268962 (msvcr100d.dll) 处的未处理异常:0xC0000005:访问冲突读取位置 0x000000001cf1d000。

4

1 回答 1

1

bytes = (byte*)pSABytes对于您正在尝试做的事情是不合法的。你需要打电话SafeArrayAccessData(pSABytes, &bytes)

于 2013-07-07T22:05:19.523 回答