如何从C#中获取UTF-8中的字符串到C DLL
How can I get and send string in UTF-8 from c# to c++ dll
我对此有问题:
FileStream stream = new FileStream("Configuration.xml", FileMode.Open);
Encoding u8 = new UTF8Encoding(true, true);
StreamReader reader = new StreamReader(stream, u8);
string str = reader.ReadToEnd();
configuration.xml在UTF-8中,但是str不在,然后我应该将此str发送到我的dll,dll函数不适用于noutf-8
如果您确实需要从C#字符串开始,则必须将该字符串转换为UTF-8,存储在字节数组中:
byte[] utf8 = Encoding.UTF8.GetBytes(str);
然后将该字节数组传递到DLL。请注意,字节数组不会被终止终止,因此,如果DLL需要空末端,则需要明确添加它。
在另一方面,避免从UTF-8到UTF-16然后再次回到UTF-8的行程可能会容易得多。因此,不要使用StreamReader
在字符串中读取。将文件的内容直接读取到字节数组中。
byte[] utf8 = File.ReadAllBytes("Configuration.xml");
再次,这将没有零端,因此,如果需要,则必须添加它。
如果您确实需要一个null末端,则FileStream
可能会更容易:
- 在读取模式下创建
FileStream
。 - 通过阅读
Length
属性来查找文件的大小。 - 分配长度
Stream.Length+1
字节的字节阵列。新的内存将被零定位化,因此最终的字节已经具有您的null末端。 - 在文件流上调用
Read
,以将Stream.Length
字节读取到字节数组中。
类似的东西:
byte[] ReadAsNullTerminatedByteArray(string filename)
{
using (FileStream fs = File.OpenRead(filename))
{
byte[] bytes = new byte[fs.Length+1];
fs.Read(bytes, 0, fs.Length);
return bytes;
}
}
相关文章:
- Visual c ++,使用字符串引用/指针调用 dll 函数
- 从DLL导出函数,LoadLibrary()需要用TEXT转换的字符串才能编译而不会出错
- 如何将 VB.Net 字符串作为字符*传递给C++dll?
- C# 和 C++ dll - 封送字符串
- 为什么在调用C#DLL时不需要提供字符串缓冲区
- 随机字符,从强制转换 Arduino 字符串到 Excel BSTR 通过 c++ DLL
- Java 调用 dll 字符串返回类型方法
- 如何在 VBA/Excel 中创建字符串数组并将其发送到C++ DLL,以便可以在 DLL 中循环访问它
- 将字符串向量从 C# 传递到C++ DLL
- 使用 DLL 在 c++ 和 C# 之间交换字符串
- 为什么 C# 封送字符串不适用于C++ DLL
- C++中具有"::"字符串名称的 DLL 导出调用
- 放置在外部 DLL 中的类中的字符串数据的内存损坏
- 从 dll 返回多个字符串
- 将字符串从 C# 发送到 dll 文件C++
- 从dll传递c字符串,穿过外部C
- 如何将字符串从C++DLL返回到MetaTrader 4代码执行生态系统?
- 将字符串[]从C#发送到SafeArrayAccessData()上的C++ Dll 'E_UNEXPECTEDCatastrophic Error'
- 编译时如何隐藏DLL文件中的字符串?
- 将C#中的C++类与字符串(Dll)一起使用