[英]Populate C# struct using Mupen64Plus Unmanaged C dll API command
[英]How to call unmanaged dll to populate struct in C# using a pointer
我正在尝试使用C#使用不受管理的第三方dll中的函数从文件获取数据。 该函数将指向结构的指针作为输入,并返回状态标志以指示操作成功(以下代码中未使用)。 第三方供应商在C中提供了以下代码来调用dll函数:
DllCaller.h
#pragma pack(1)
struct Event
{
int event_type;
double time_stamp;
char event_text[200];
};
typedef enum Status (*_GetEventList)(struct Event* event_list);
_GetEventList GetEventList;
DllCaller.c
int event_list_cnt;
struct Event* event_list;
hInstLibrary = LoadLibrary(lib_name);
GetEventList = (_DWGetEventList)GetProcAddress(hInstLibrary, "GetEventList);
printf("\nEVENTS:\n");
event_list_cnt = 2;
event_list = malloc(sizeof(struct Event) * event_list_cnt);
GetEventList(event_list);
for(i = 0; i < event_list_cnt; i++)
{
printf("EVENT: type = %i, text = %s, position = %fsec \n",
event_list[i].event_type, event_list[i].event_text,
event_list[i].time_stamp);
}
free(event_list);
FreeLibrary(hInstLibrary)
在示例文件上运行此命令的输出是:
活动:
事件:类型= 1,文本=开始存储,位置= 0.000000sec
事件:类型= 2,文本=存储停止,位置= 110,825682sec
换句话说,event_list结构中的每个字段都是长度为2的数组。
在上面的代码中,我简化了供应商的示例代码,并省略了一些我认为与当前问题无关的内容。
这就是我尝试在C#中实现相同功能的方式:
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet=CharSet.Ansi)]
public struct Event
{
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.I4)]
public int[] event_type;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.R8)]
public double[] time_stamp;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.LPStr, SizeConst = 200)]
public char[] event_text;
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate Status GetEventList(IntPtr ptrToEventList);
public Event GetEventList()
{
// this.pDll is a pointer to the dll library.
IntPtr pAddressOfFunctionToCall = NativeMethods.GetProcAddress(this.pDll, "GetEventList");
GetEventList getEventList = (GetEventList)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(GetEventList));
int eventListCount;
this.GetEventListCount(out eventListCount, out errorMessage);
int mem = Marshal.SizeOf(typeof(Event));
// The multiplication by two is because I already know that
// the struct should be populated by two events.
IntPtr structPtr = Marshal.AllocCoTaskMem(2 * mem);
Event eventList;
try
{
getEventList(structPtr);
eventList = (Event)Marshal.PtrToStructure(structPtr , typeof(Event));
}
finally
{
Marshal.FreeHGlobal(structPtr);
}
return eventList;
}
如果我在C代码上方的同一示例文件上运行此代码,则eventList中的event_type和time_stamp向量的长度为1,event_text的长度为200。这些字段中的值将包含第一个事件的正确信息-一个event_type 1的-由上面的C代码打印出来。 当然应该在eventList中填充的是长度为2的向量,其中包含两个事件,但是我在修改代码以实现此目的方面一直没有成功。 上面的代码是我设法编写的唯一填充了结构中所有内容的代码。 我试图尝试将event_text字段指定为字符串而不是char,但这只会导致AccessViolationException错误,可能是因为我没有正确实现它。
谁能帮我修复上面的代码,以便正确填充eventList?
谢谢!
/艾芬达尔
编辑:更新了具有事件结构的C#代码,并在将其传递给非托管dll之前尝试在C#中分配Event []:
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet=CharSet.Ansi)]
public struct Event
{
[MarshalAs(UnmanagedType.I4)]
public int event_type;
[MarshalAs(UnmanagedType.R8)]
public double time_stamp;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.LPStr, SizeConst = 200)]
public char[] event_text;
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate Status GetEventList(ref Event[] eventList);
public Event[] GetEventList()
{
// this.pDll is a pointer to the dll library.
IntPtr pAddressOfFunctionToCall = NativeMethods.GetProcAddress(this.pDll, "GetEventList");
GetEventList getEventList = (GetEventList)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(GetEventList));
Event[] eventList = new Event[2];
getEventList(ref eventList);
return eventList;
}
我不知道上面的代码在屏幕上的显示格式是否像在我的屏幕上那样差(缩进和空白的新行被省略了),但是我一直无法看起来更好。
这是最终解决问题的代码:
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet=CharSet.Ansi)]
public struct Event
{
[MarshalAs(UnmanagedType.I4)]
public int event_type;
[MarshalAs(UnmanagedType.R8)]
public double time_stamp;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 200)]
public string event_text;
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate Status GetEventList([out]Event[] eventList);
public Event[] GetEventList()
{
// this.pDll is a pointer to the dll library.
IntPtr pAddressOfFunctionToCall = NativeMethods.GetProcAddress(this.pDll, "GetEventList");
GetEventList getEventList = (GetEventList)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(GetEventList));
Event[] eventList = new Event[2];
getEventList(eventList);
return eventList;
}
非常感谢leppie和Panos Rontogiannis帮我弄清楚了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.