Mono C# PInvoke - 从外部库获取 CFString
我有一个返回 CFString 的库,我正在尝试在 C# 中获取该字符串值。 问题是我不知道如何在 C# 中执行此操作。
使外部函数返回 CFString 将不起作用,因为它会引发异常“传递给非托管代码的 Type MonoMac.CoreFoundation.CFString 必须具有 StructLayout属性。”
然后我想我可以将字符串作为字节数组获取,然后将其转换为 C# 中的字符串,但是,我有另一个问题,我不知道如何在 C 中将 CFString 转换为字节数组:/
C# Dll Import stuff
[DllImport("lib")]
public static extern MonoMac.CoreFoundation.CFString test();
[DllImport("lib")]
public static extern byte[] test();
C Library sample
CFStringRef test()
{
return CFSTR("test string");
}
If有人知道如何做到这一点请帮助我;)
谢谢
I have a lib that returns a CFString, and I'm trying to get that string value in C#..
The problem is that I don't know how to do this in C#..
Making the external function return a CFString won't work as it throws an exception "Type MonoMac.CoreFoundation.CFString which is passed to unmanaged code must have a StructLayout attribute."
Then I thought that I could get the string as a byte array and then convert it to a string in C#, but then, I have another problem, I don't know how to convert in C the CFString to a byte array :/
C# Dll Import stuff
[DllImport("lib")]
public static extern MonoMac.CoreFoundation.CFString test();
[DllImport("lib")]
public static extern byte[] test();
C Library sample
CFStringRef test()
{
return CFSTR("test string");
}
If anyone knows a way to do this please help me out ;)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试:
然后使用:
Try:
Then use: