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) ...
随机推荐
- 浅谈HTML之模仿人人网登陆界面(新手必学)
为方便大家对web相关知识的了解,现谈谈新手如何从HTML css Javascript到以后后台的发展.首先,让大家看看HTML仿人人登陆界面: <!doctype html> < ...
- JVM调优总结(三)-基本垃圾回收算法
可以从不同的的角度去划分垃圾回收算法: 按照基本回收策略分 引用计数(Reference Counting): 比较古老的回收算法.原理是此对象有一个引用,即增加一个计数,删除一个引用则减少一个计数. ...
- 1、Zookeeper熟悉和用途综述
集群 配置: 192.168.32.80 192.168.32.81 192.168.32.82 server 1: zjtest7-redis:/opt/zookeeper/conf# cat zo ...
- java调用163邮箱发送邮件
1:注册一个163邮箱,http://mail.163.com 调用发送邮件代码,查询smtp.163.com,作为发送邮件的服务器ip,类似的邮箱服务器应该也可以. MailSenderInfo m ...
- anroid里面的post请求
一.需要用到的场景 在jQuery中使用$.post()就可以方便的发起一个post请求,在android程序中有时也要从服务器获取一些数据,就也必须得使用post请求了. 二.需要用到的主要类 在a ...
- Eclipse验证码
package MyEclipse; import java.io.*; public class MyEclipseGen { private static final String ...
- Android 之 Fragment
一 左侧标题列表 1.1 布局 left_fragment.xml <ListView xmlns:android="http://schemas.android.com/apk/ ...
- 基于visual Studio2013解决面试题之1309求子集
题目
- 基于visual Studio2013解决C语言竞赛题之1042字符串比较
题目 解决代码及点评 /********************************************************************** ...
- 如何使用NArrange进行代码优化
Narrange是一个.NET代码管理工具.它可以对源代码自动进行美化和把类成员分成一个组.区域.目前支持C#.VB.NET,将来会支持更多.NET上的语言. 主要的作用是: ◆ 减少程序员的开发时间 ...