PInvoke null reference exception on Marshal.PtrToStructure<T>(ptr)



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

[16:05:17.258][UNHANDLED EXCEPTION][BEGIN][16:05:18.463]System.NullReferenceException:对象引用未设置为对象的实例位于(包装未知)PI.SDK.UI.HW.PAX.ProlinKeyboardManager+InputEvent:PtrToStructure(intptr,object)位于(包装管理为本机)System.Runtime.InteropServices.Marshal:PtrToStructure(intptr,System.Type)在System.Runtime.InteropServices.Marshal.PtrToStructure[T](IntPtr-ptr)中:0位于PI.SDK.UI.HW.PAX.ProlinKeyboardManager.StartListening(),位于:0在PI.SDK.PAX.Playground.Program+c.b_37_0()中:0位于0中的System.Threading.ThreadHelper.ThreadStart_Context(System.Object状态)位于以下位置的System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext ExecutionContext,System.Threading.ContextCallback回调,System.Object状态,Boolean preserveSyncCtx):0在0中的System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext ExecutionContext,System.Threading.Context回调,System.Object状态,Boolean preserveSyncCtx)在System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext ExecutionContext,System.Threading.Context回调,System.Object状态)处:0位于0中的System.Threading.ThreadHelper.ThreadStart()

问题看起来在Marshal.PtrToStructure<T>(IntPtr)上,但我看不出它在哪里…

repo案例的托管/C#部分如下:

[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;
    }
}

有人能指出我们的错误在哪里吗?

谢谢!非常感谢您的帮助。Gutemberg

两个问题:

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

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

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

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);
      }
    }
  }

致以最良好的问候!

最新更新