在C#中将字符串转换为字节数组,以在C++中的文件中写入字符串
本文关键字:字符串 C++ 以在 文件 字节数 转换 字节 数组 | 更新日期: 2023-09-27 18:21:17
我在C#中有一个包含xml数据的Memory流对象。
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时发生访问冲突。
bytes = (byte*)pSABytes
对您尝试执行的操作不合法。您需要调用SafeArrayAccessData(pSABytes, &bytes)