Marshal.PtrToStructure<T>(ptr) 上的 PInvoke 空引用异常
PInvoke null reference exception on Marshal.PtrToStructure<T>(ptr)
我们在尝试使用 Mono 调用 linux/libc 的 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)
但我看不到它在哪里...
managed/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;
}
}
谁能指出我们的错误在哪里?
谢谢!非常感谢任何帮助。
古腾堡
两个问题:
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);
}
}
}
此致!
我们在尝试使用 Mono 调用 linux/libc 的 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)
但我看不到它在哪里...
managed/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;
}
}
谁能指出我们的错误在哪里?
谢谢!非常感谢任何帮助。 古腾堡
两个问题:
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);
}
}
}
此致!