Marshal.PtrToStructure上的PInvoke空引用异常<t>(ptr)

时间:2016-04-10 19:21:03

标签: c# linux mono pinvoke

在尝试使用Mono的Linux / libc的PInvoke read()方法时,我们遇到了一种奇怪的行为。

[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 

问题似乎在Marshal.PtrToStructure<T>(IntPtr),但我看不到它在哪里......

repro案例的托管/ 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 个答案:

答案 0 :(得分:2)

两个问题:

1)NativeRead的第二个参数不应该是指针的引用。相反它应该是指针本身。请验证PInvoke声明并更改&#34; ref IntPtr&#34;到&#34; IntPtr&#34;。例如:

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

祝你好运!