原文:Marshal UTF8 Strings in .NET

Marshal UTF8 Strings in .NET

Wow, what a pain in the butt. .NET strings are stored internally as UTF16, not UTF8, so if you're marshaling strings to and from a library that wants strings as UTF8, you have to manually
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的更多相关文章

  1. » Working Around JNI UTF-8 Strings Deprogramming

    private static native void printString(String text); ... void examplePrintString() { String str = &q ...

  2. 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 ...

  3. PHP 与 UTF-8

    没有一行式解决方案.小心.注意细节,以及一致性. PHP 中的 UTF-8 糟透了.原谅我的用词. 目前 PHP 在低层次上还不支持 Unicode.有几种方式可以确保 UTF-8 字符串能够被正确处 ...

  4. CI框架源码学习笔记7——Utf8.php

    愉快的清明节假期结束了,继续回到CI框架学习.这一节我们来看看Utf8.php文件,它主要是用来做utf8编码,废话不多说,上代码. class CI_Utf8 { /** * Class const ...

  5. utf8 string

    https://github.com/BassLC/idUTF8lib Idiot's UTF-8 Library A very (too much really) simple Utf8 libra ...

  6. Go package(2) strings 用法

    go version go1.10.3 Go中的字符串用法,可以在 godoc.org 上查看语法和用法. 最简单的语法就是获取字符串中的子串 s := "hello world" ...

  7. Vulkan(1)用apispec生成Vulkan库

    Vulkan(1)用apispec生成Vulkan库 我的Vulkan.net库已在(https://github.com/bitzhuwei/Vulkan.net)开源,欢迎交流. apispec. ...

  8. Thinking in Java——笔记(18)

    I/O The original byte-oriented library was supplemented with char-oriented, Unicode-based I/O classe ...

  9. PHP正则表达式模式修饰符 /i, /is, /s, /isU等

    模式修饰符 下面列出了当前可用的 PCRE 修饰符.括号中提到的名字是 PCRE 内部这些修饰符的名称. 模式修饰符中的空格,换行符会被忽略,其他字符会导致错误. i (PCRE_CASELESS) ...

随机推荐

  1. poj1920 Towers of Hanoi

    关于汉诺塔的递归,记住一个结论是,转移n个盘子至少需要2^n-1步 #include<iostream> #include<cstdio> #include<cmath& ...

  2. C Tips:显示点阵汉字的小样例

    非常简陋的一段小程序,演示怎样显示点阵字库.有时间的时候再详解. #include <stdio.h> #include <stdlib.h> struct HzkInfoSt ...

  3. struct 和 class 不同点

    在 C++ 里面 struct 和 class 没有本质的差别 仅仅是成员和继承方式的默认不同 struct 是 public class 是 private 我的个人建议是仅仅要须要实现成员函数的就 ...

  4. 打开sa属性报错

    --如果打开sa属性报错如下:无法显示请求的对话框.属性IsLocked不可用于“登录名sa".该对象可能没有此属性,也可能是访问权限不足而无法检索 --解决办法:首先用windows登录, ...

  5. SGU 495. Kids and Prizes( 数学期望 )

    题意: N个礼品箱, 每个礼品箱内的礼品只有第一个抽到的人能拿到. M个小孩每个人依次随机抽取一个,  求送出礼品数量的期望值. 1 ≤ N, M ≤ 100, 000 挺水的说..设f(x)表示前x ...

  6. ThreadLocal用法和实现原理(转)

    如果你定义了一个单实例的java bean,它有若干属性,但是有一个属性不是线程安全的,比如说HashMap.并且碰巧你并不需要在不同的线程中共享这个属性,也就是说这个属性不存在跨线程的意义.那么你不 ...

  7. android代码控制seekbar的样式

    package com.zte; import android.app.Activity; import android.graphics.Color; import android.graphics ...

  8. [置顶] 殊途同归——总结asp.net

    怀着期望,忐忑的心情看完了asp.net的一部分视频,这部分的学习也到了一个段落,颗粒归仓的工作还是要做的,但是有什么比一张图来得更直观有效呢? 先来张图,这次真的是有图有真相: 通过asp.net的 ...

  9. isotope/masonry 使用jQuery.sortable

    function goMasonry() { // if ($container.data('masonry') != undefined) { $container.isotope('destroy ...

  10. opencv之haar特征+AdaBoos分类器算法流程(二)

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...