IndexOutOfRangeException - 无法使用 PInvoke 查看调用堆栈
本文关键字:调用 堆栈 PInvoke IndexOutOfRangeException | 更新日期: 2024-11-08 12:29:05
我正在开发一个从串行端口获取数据的 C# 应用程序,然后它使用一个C++项目(我无法更改)来计算读取的数据。
C++项目使用一些本机 C 代码,这些代码将在计算数据时调用 C# 函数。
以下是使用 PInvoke 调用 C++ 函数的调用 C# 代码的一些示例:
[DllImport("MyLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern void PacketHandler_HandlePacket(IntPtr packetHandler, IntPtr packet, int packetType);
[SuppressUnmanagedCodeSecurity]
[DllImport("MyLib.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
private static extern float DeviceManager_AddSampleToBattery(IntPtr self, float sample, double sampleRate);
[SuppressUnmanagedCodeSecurity]
[DllImport("MyLib.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
private static extern double DeviceManager_GetTimestamp(IntPtr self, int packetType);
[DllImport("MyLib.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
private static extern void PacketHandler_AddCallbackBattery(IntPtr self, Delegate @delegate);
然后,C++代码:
extern "C"
{
__declspec(dllexport) void _cdecl PacketHandler_HandlePacket(int * packetHandler, char * packet, int packetType){
PacketHandler_handlePacket((PacketHandlerStruct *)packetHandler, packet, (PacketHandlerPacketType)packetType);}
}
这个 C 函数调用我设置的 C# 函数如下:
__declspec(dllexport) void _cdecl PacketHandler_AddCallbackBattery(int * packetHandler, void(*f)(void * obj, unsigned short int sample)){
PacketHandlerStruct * myPointer = ((PacketHandlerStruct *)packetHandler);
myPointer->delegate.addSampleToBattery = f;
}
最后,C 代码将调用"addSampleToBattery"函数,即这个 C# 回调(其中前两个是 PInvoke 调用,就像我发布的第一个调用一样)
private static float CallbackBattery(IntPtr self, ushort sample)
{
var value = DeviceManager_AddSampleToBattery(DeviceManager, sample, Const.BaseBvpSampleRate);
var timestamp = DeviceManager_GetTimestamp(DeviceManager, (int)PacketHandlerPacketType.Battery);
SocketManager.OnNewBatteryArrived(value, timestamp);
return value;
}
其他细节:
C# 委托声明如下:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate float DelegateBattery(IntPtr self, short sample);
private static readonly DelegateBattery DelegateCallbackBattery = CallbackBattery;
并设置如下:
var intptrDelegate = Marshal.GetFunctionPointerForDelegate(DelegateCallbackBattery);
var a = Marshal.GetDelegateForFunctionPointer(intptrDelegate, typeof(DelegateBattery));
PacketHandler_AddCallbackBattery(packetHandler, a);
所以,一切似乎都正常,但很多时候会出现 IndexOutOfRangeException。主要问题是,即使在加载了所有符号的调试模式下,我也看不到引发异常的行,因为只有反汇编视图可用,当然我无法从中获得有意义的信息。
Unhandled Exception: 'MyProgram.exe' (Win32):
The program '[3000] MyProgram.exe' has exited with code 0 (0x0).
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.Threading.ThreadPoolWorkQueue.Dequeue(ThreadPoolWorkQueueThreadLocals tl, IThreadPoolWorkItem& callback, Boolean& missedSteal)
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
谢谢!
与 IndexOutOfRangeException 对应的语义表示您尝试访问内存时离基本内存地址太远,因此您不应该尝试尊重内存中的该点。
想想一个包含 3 个元素的 int 数组。如果您尝试索引 [3],则取消引用 *(base_address + (sizeof(int) * 3))*,以便为内存中该位置的下一个 4 个字节提供作为 int 的值。唯一有效的索引是 0、1 和 2,因为这些偏移量在我们为其分配空间的 3 个元素的范围内。
我们仍然需要更多的代码,因为我看不到其中任何与异常消息相关的代码。