Marshal UTF8 Strings in .NET
原文:Marshal UTF8 Strings in .NET
Marshal UTF8 Strings in .NET
marshal them yourself.
This took me a whole day to figure out why my my .NET wrapper library wasn't working, and a whole other day to figure out how to work around it and debug the code. If this code saves at least one person the amount of time I lost then I'm satisfied.
public class MarshalPtrToUtf8 : ICustomMarshaler
{
static MarshalPtrToUtf8 marshaler = new MarshalPtrToUtf8(); public void CleanUpManagedData(object ManagedObj)
{ } public void CleanUpNativeData(IntPtr pNativeData)
{
Marshal.Release(pNativeData);
} public int GetNativeDataSize()
{
return Marshal.SizeOf(typeof(byte));
} public int GetNativeDataSize(IntPtr ptr)
{
int size = 0;
for (size = 0; Marshal.ReadByte(ptr, size) > 0; size++)
;
return size;
} public IntPtr MarshalManagedToNative(object ManagedObj)
{
if (ManagedObj == null)
return IntPtr.Zero;
if (ManagedObj.GetType() != typeof(string))
throw new ArgumentException("ManagedObj", "Can only marshal type of System.String");
byte[] array = Encoding.UTF8.GetBytes((string)ManagedObj);
int size = Marshal.SizeOf(array[0]) * array.Length + Marshal.SizeOf(array[0]);
IntPtr ptr = Marshal.AllocHGlobal(size);
Marshal.Copy(array, 0, ptr, array.Length);
Marshal.WriteByte(ptr, size - 1, 0);
return ptr;
} public object MarshalNativeToManaged(IntPtr pNativeData)
{
if (pNativeData == IntPtr.Zero)
return null;
int size = GetNativeDataSize(pNativeData);
byte[] array = new byte[size - 1];
Marshal.Copy(pNativeData, array, 0, size - 1);
return Encoding.UTF8.GetString(array);
} public static ICustomMarshaler GetInstance(string cookie)
{
return marshaler;
}
}
You'll notice that there's a lot of data copying going on and there are a few copies of string made. Yep, that's because the .NET framework can't just pin the array in memory that stores the string (remember, strings are stored as UTF16 in the .NET framework)
 and you have to make the conversion yourself.
Marshal UTF8 Strings in .NET的更多相关文章
- » Working Around JNI UTF-8 Strings Deprogramming
		private static native void printString(String text); ... void examplePrintString() { String str = &q ... 
- About using UTF-8 fields in MySQL
		https://www.adayinthelifeof.nl/2010/12/04/about-using-utf-8-fields-in-mysql/ I sometimes hear: “make ... 
- PHP 与 UTF-8
		没有一行式解决方案.小心.注意细节,以及一致性. PHP 中的 UTF-8 糟透了.原谅我的用词. 目前 PHP 在低层次上还不支持 Unicode.有几种方式可以确保 UTF-8 字符串能够被正确处 ... 
- CI框架源码学习笔记7——Utf8.php
		愉快的清明节假期结束了,继续回到CI框架学习.这一节我们来看看Utf8.php文件,它主要是用来做utf8编码,废话不多说,上代码. class CI_Utf8 { /** * Class const ... 
- utf8 string
		https://github.com/BassLC/idUTF8lib Idiot's UTF-8 Library A very (too much really) simple Utf8 libra ... 
- Go package(2) strings 用法
		go version go1.10.3 Go中的字符串用法,可以在 godoc.org 上查看语法和用法. 最简单的语法就是获取字符串中的子串 s := "hello world" ... 
- Vulkan(1)用apispec生成Vulkan库
		Vulkan(1)用apispec生成Vulkan库 我的Vulkan.net库已在(https://github.com/bitzhuwei/Vulkan.net)开源,欢迎交流. apispec. ... 
- Thinking in Java——笔记(18)
		I/O The original byte-oriented library was supplemented with char-oriented, Unicode-based I/O classe ... 
- PHP正则表达式模式修饰符 /i, /is, /s, /isU等
		模式修饰符 下面列出了当前可用的 PCRE 修饰符.括号中提到的名字是 PCRE 内部这些修饰符的名称. 模式修饰符中的空格,换行符会被忽略,其他字符会导致错误. i (PCRE_CASELESS) ... 
随机推荐
- poj 2114 Boatherds 树的分治
			还是利用点的分治的办法来做,统计的办法不一样了,我的做法是排序并且标记每个点属于哪颗子树. #include <iostream> #include <cstdio> #inc ... 
- HDU 4814 Golden Radio Base 小模拟
			链接:http://acm.hdu.edu.cn/showproblem.php?pid=4814 题意:黄金比例切割点是,如今要求把一个10进制的的数转化成一个phi进制的数,而且不能出现'11'的 ... 
- Python中的循环与跳出
			--start-- for循环: for i in range(3): user_input = input("Your username:") passwd = int(inpu ... 
- LVS集群的体系结构
			2.LVS主要组成部分为: 负载调度器(load balancer/ Director),它是整个集群对外面的前端机,负责将客户的请求发送到一组服务器上执行,而客户认为服务是来自一个IP地址(我们可称 ... 
- Spring Thread Pool 线程池的应用
			Spring and Java Thread example 扫扫关注"茶爸爸"微信公众号 坚持最初的执着,从不曾有半点懈怠,为优秀而努力,为证明自己而活. Download it ... 
- 道格拉斯—普克(Douglas一Peukcer)节点抽稀算法
			Douglas一Peukcer算法由D.Douglas和T.Peueker于1973年提出,简称D一P算法,是眼下公认的线状要素化简经典算法.现有的线化简算法中,有相当一部分都是在该算法基础上进行改进 ... 
- poj 1990
			题目链接 借鉴cxlove大神的思路 题意:听力v,位置x,2个牛交流声音为max(v1,v2)*(x1-x2),求总的 10000^2 tle 用的树状数组做的,排序,2个,小于vi的牛的总数和距离 ... 
- webwervice发布时出错 java.security.PrivilegedActionException
			错误信息: 信息: Dynamically creating response wrapper bean Class com.potevio.ws.jaxws.DealReqResponse Exce ... 
- c# winform 路径选择和文件读写
			//读文件 private void readBtn_Click(object sender, EventArgs e) { try { if (pathTxt.Text == "" ... 
- .bat脚本将windows server 2008设置成ntp时间同步服务器
			@echo off echo autor OAK @echo off echo -------------------------------- @echo off REG ADD HKEY_LOCA ... 
