惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
Google DeepMind News
Google DeepMind News
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
NISL@THU
NISL@THU
T
Threat Research - Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
Lohrmann on Cybersecurity
V
Visual Studio Blog
Cyberwarzone
Cyberwarzone
D
Docker
The Hacker News
The Hacker News
C
CERT Recently Published Vulnerability Notes
Vercel News
Vercel News
Project Zero
Project Zero
S
Schneier on Security
aimingoo的专栏
aimingoo的专栏
I
Intezer
腾讯CDC
M
MIT News - Artificial intelligence
Hugging Face - Blog
Hugging Face - Blog
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
AWS News Blog
AWS News Blog
GbyAI
GbyAI
MongoDB | Blog
MongoDB | Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
Vulnerabilities – Threatpost
G
Google Developers Blog
N
Netflix TechBlog - Medium
The Cloudflare Blog
Microsoft Security Blog
Microsoft Security Blog
Y
Y Combinator Blog
A
Arctic Wolf
S
Securelist
酷 壳 – CoolShell
酷 壳 – CoolShell
Cisco Talos Blog
Cisco Talos Blog
Recent Announcements
Recent Announcements
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LINUX DO - 热门话题
T
Threatpost
Latest news
Latest news
Blog — PlanetScale
Blog — PlanetScale
Security Latest
Security Latest
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
The GitHub Blog
The GitHub Blog
T
Tor Project blog
P
Proofpoint News Feed

博客园 - sPhinX

如何解决在Win11下卸载McgsPro失败的问题 如何离线安装WinDbg Preview 敏捷软件开发 原则、模式与实践 第9章的例子程序(C#版) Akavache简明使用指南 Oracle存储过程解析XML内容 有意思的案例: 的问题 dnSpy - 让调试镜像文件的工作变得轻松点 dnSpy调试IIS(w3wp进程) Xilium.CefGlue与SingleProcess rocketmq-client-cpp(2.0.1)编译指南 RocketMQ .NET客户端的那些坑 P/Invoke今日谈 .NET编译问题汇总 动态的世界 使用ProcDump自动生成Dump文件 Process.Start可能无法选中指定文件的问题 获取本地IP 将exe和dll打包为一个exe文件 .NET异步资料收集
P/Invoke继续谈
sPhinX · 2020-04-25 · via 博客园 - sPhinX

上回说到封装rocketmq-client-cpp的时候会碰到内存损坏的问题,本以为能收到服务端的消息就万事大吉了,但是在深入测试的过程中,发现收到的消息会有乱码出现,

之前和服务端的研发人员讨论过字符集的问题,知道服务器回传的是UTF8的字符串,所以在接收时用下面这样的方式可以在大部分情况下得到正确的内容:

return Encoding.UTF8.GetString(Encoding.Default.GetBytes(Marshal.PtrToStringAnsi(GetMessageIdNative(message))));

但是在少数情况下会有乱码内容,比较之后发现是中英文符号混杂的情况就会出现这种问题,经过反复测试和思考之后确认需要的是Marshal.PtrToStringUTF8,

可惜目前的项目都是在.NET Framework下开发,PtrToStringUTF8在.NET Framework中完全没有支持,所以只能自己写一个了

static string PtrToStringUTF8(IntPtr ptr)
{
    var bytesCount = 0;
    byte b;
    do
    {
        b = Marshal.ReadByte(ptr, bytesCount);
        bytesCount++;
    }
    while (b != 0);
    var bytes = new byte[bytesCount - 1];
    Marshal.Copy(ptr, bytes, 0, bytesCount - 1);
    return Encoding.UTF8.GetString(bytes);
}

没过几天,又遇到个接入某省定制多功能读卡器的需求,在这里碰到了一个看起来很简单的函数,声明如下:

long iReadXXX(char* pOutInfo)

这里的pOutInfo是一个长度为4096的输出参数,所以虽然也是要接收一个字符串,但是通过输出参数的形式,而不是上次的返回值,平台调用的形式也就必然不同。

这里采用的是接收字符数组的方式来进行调用,声明如下:

public static extern int iReadXXX(IntPtr pOutInfo);

调用代码如下:

byte[] bytes = new byte[4096];
GCHandle pinned = GCHandle.Alloc(bytes, GCHandleType.Pinned);
var result = iReadXXX(pinned.AddrOfPinnedObject());
pinned.Free();
int count = bytes.ToList().FindIndex(b => b == 0);
var text = Encoding.Default.GetString(bytes, 0, count);