gpt4 book ai didi

c# - 如何在 C# 中将 IntPtr 转换为 Boolean[]?

转载 作者:太空宇宙 更新时间:2023-11-03 23:32:31 32 4
gpt4 key购买 nike

背景信息

我在 C 中有一个函数返回一个 void*

我开始测试两个项目之间的转换,将 truefalse 分配给指针,回到 C# 中我得到了 IntPtr,我使用了 IntPtr.ToPointer() 函数,然后将其转换为一个字节,根据我的阅读,这相当于 c# 中的 bool 值。

这对我来说很好。

DLIMPORT:

[DllImport(Library.ruta,
SetLastError = true,
CallingConvention = CallingConvention.Cdecl)]
public extern static IntPtr test_boolean(int flag);

使用函数:

x = Library.test_boolean(1);

z = x.ToPointer();
Console.WriteLine("Prueba");

if (Convert.ToBoolean((byte)z))
Console.WriteLine("True");
else
Console.WriteLine("False");

问题
当我尝试做同样的事情但将一个 bool 数组分配给指针时,我无法在 C# 中取回它。

C 中的函数是这样的

...
vpointer *chromosome;

chromosome = s_malloc(2*sizeof(boolean *));


if (flag == 1)
{
chromosome[0] = true;
chromosome[1] = true;
}
...
Return chromosome;

我在 C# 中尝试做的事情:

(1) 我尝试了同样的“转换”,这次是一个字节数组。这给了我一个空引用异常:

if (Convert.ToBoolean(((byte*)z)[1]))
Console.WriteLine("True");
else
Console.WriteLine("False");

(2) 由于这没有用,经过一番研究后,我尝试了 Marshal.Copy 函数,将我的指针复制到一个字节数组。尽管这次我没有得到任何异常,但我没有得到正确的结果。我的意思是,例如,如果数组的第一个 bool 值是 false,我就会变成 true。

这是 C# 代码的样子:

x = Library.test_boolean(1);

byte [] managedArray = new byte[2];

Marshal.Copy(x, managedArray, 0, 2);

foreach (var qq in managedArray)
{
Boolean a = Convert.ToBoolean(qq);
Console.WriteLine(a.ToString());
}

问题
所以我的问题是从 C# 中的 IntPtr 转换为 bool 数组的正确方法是什么?

最佳答案

… assigning true or false to the pointer, back in C# I got the IntPtr, and I used the IntPtr.ToPointer() function and then just cast it into a byte …

这是错误的,即使它可能有效。如果返回类型是void*,那么它实际上应该是一个指针,而不是 bool 值。如果你真的想执行这种暴行,那么你不需要使用指针的中间步骤,你可以将 IntPtr 直接转换为 byte

现在,对于您的实际问题:如果您知道该函数将始终返回两个 bool 值的数组,我认为最好在 C# 中使用适当的方式将其声明为返回 bool[]属性(类似于 [return: MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.I1, SizeConst=2)])。

但是您的 C# 代码也应该可以工作。我认为问题出在您的 C 代码中:您返回的数组声明为 vpointer*,根据您的评论,它等同于 void**。这意味着在 32 位机器上,C 编译器将此变量视为 4 字节值的数组。如果您希望它把它当作 1 字节值的数组,您需要这样声明它(例如 boolean *chromosome;)。

关于c# - 如何在 C# 中将 IntPtr 转换为 Boolean[]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12444654/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com