gpt4 book ai didi

c# - Marshal.PtrToStructure(ptr) 上的 PInvoke 空引用异常

转载 作者:太空宇宙 更新时间:2023-11-04 09:25:55 26 4
gpt4 key购买 nike

我们在尝试 PInvoke read() 时遇到奇怪的行为linux/libc 与 Mono 的方法。

[16:05:17.258][UNHANDLED EXCEPTION][BEGIN][16:05:18.463]System.NullReferenceException: Object reference not set to an instance of an object  at (wrapper unknown) PI.SDK.UI.HW.PAX.ProlinKeyboardManager+InputEvent:PtrToStructure (intptr,object)  at (wrapper managed-to-native) System.Runtime.InteropServices.Marshal:PtrToStructure (intptr,System.Type)  at System.Runtime.InteropServices.Marshal.PtrToStructure[T] (IntPtr ptr)  in :0   at PI.SDK.UI.HW.PAX.ProlinKeyboardManager.StartListening ()  in :0   at PI.SDK.PAX.Playground.Program+c.b__37_0 ()  in :0   at System.Threading.ThreadHelper.ThreadStart_Context (System.Object state)  in :0   at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, Boolean preserveSyncCtx)  in :0   at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, Boolean preserveSyncCtx)  in :0   at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state)  in :0   at System.Threading.ThreadHelper.ThreadStart ()  in :0 

The problem looks to be on Marshal.PtrToStructure<T>(IntPtr) but I can't see where it is...

The managed/C# part of the repro case is as follow:

[StructLayout(LayoutKind.Sequential)]
private struct InputEvent
{
public int Seconds;
public int Microseconds;
public ushort type;
public ushort code;
public int value;
}

[DllImport(LIBC, EntryPoint = "read", CallingConvention = CallingConvention.Cdecl)]
private static extern int NativeRead(int fd, ref IntPtr buf, int nbytes);

public void StartListening()
{
var kbDeviceName = "/dev/keypad";
int fd = -1;
fd = NativeOpen(kbDeviceName, 2);
Console.WriteLine($"[KBD] {fd}");

if (fd < 0)
throw new InvalidOperationException($"Unable to open Keyboard device {kbDeviceName}. Return code {fd}.");

var ev = new InputEvent[64];
var size = Marshal.SizeOf<InputEvent>();
Console.WriteLine($"[EventSize] {size}");
var totalSize = size * 64;
Console.WriteLine($"[TotalEventSize] {totalSize}");

var ptr = IntPtr.Zero;

while (true)
{
var rd = NativeRead(fd, ref ptr, totalSize);
Console.WriteLine($"[KBD][Read] {rd}");
Console.WriteLine($"[PTR] {ptr}");
if (rd > size)
{
var eventNum = rd / size;
Console.WriteLine($"[Events] {eventNum}");
for (int i = 0; i < eventNum; i++)
{
var entry = Marshal.PtrToStructure<InputEvent>(ptr);
Console.WriteLine($"code: {entry.code} | type: {entry.type} | value: {entry.value}");
ptr = new IntPtr(ptr.ToInt32() + size);
}
}
}
}

那些 Console.Writeline 的输出是:

[KBD] 5
[EventSize] 16
[TotalEventSize] 1024
[KBD][Read] 32
[PTR] 1460304317
[Events] 2
[16:05:17.258][UNHANDLED EXCEPTION][BEGIN]
... THE EXCEPTION GOES HERE ...

这是我们试图复制的 C 原生示例:

int i;
int eventNum = 0;
struct input_event ev[64];

int size = sizeof(struct input_event);
int rd = 0;

memset(ev, 0, sizeof(ev));
rd = read(gfd, ev, sizeof(ev));
eventNum = rd / size;
for (i = 0; i < eventNum; ++i)
{
/* EV_KEY means type is key (not mouse, etc) */
if (ev[i].type == EV_KEY && ev[i].value == 1)
{
return ev[i].code;
}
}

谁能告诉我哪里应该是我们的错误?

谢谢!非常感谢任何帮助。古腾堡

最佳答案

两个问题:

1) NativeRead 的第二个参数不应该是指针的引用。相反,它应该是指针本身。请验证 PInvoke 声明并将“ref IntPtr”更改为“IntPtr”。示例:

private static extern int NativeRead(int fd, IntPtr buf, int nbytes);

2) read 需要指向缓冲区的指针,并且此代码传递 0。您应该改为为缓冲区分配 native 内存。示例:

var ptr = Marshal.AllocHGlobal(totalSize);

循环中还有一个问题:循环中递增的指针与用于进一步读取的指针相同。相反,读取应该始终使用缓冲区指针来完成。这应该修复它:

  var buffer_ptr = Marshal.AllocHGlobal(totalSize);

while (true)
{
var rd = NativeRead(fd, buffer_ptr, totalSize);
Console.WriteLine($"[KBD][Read] {rd}");
Console.WriteLine($"[PTR] {buffer_ptr}");
if (rd > size)
{
var eventNum = rd / size;
Console.WriteLine($"[Events] {eventNum}");
var event_ptr = buffer_ptr;
for (int i = 0; i < eventNum; i++)
{
var entry = Marshal.PtrToStructure<InputEvent>(event_ptr);
Console.WriteLine($"code: {entry.code} | type: {entry.type} | value: {entry.value}");
event_ptr = IntPtr.Add(event_ptr, size);
}
}
}

最好的问候!

关于c# - Marshal.PtrToStructure<T>(ptr) 上的 PInvoke 空引用异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36534425/

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