110,534
社区成员
发帖
与我相关
我的任务
分享
public unsafe struct WFS_CDM_STATUS
{
public ushort fwDevice;
public ushort fwSafeDoor;
public ushort fwDispenser;
public ushort fwIntermediateStacker;
public IntPtr lppPositions;
public string lpszExtra;
public fixed uint dwGuidLights[CDMDefinition.WFS_CDM_GUIDLIGHTS_SIZE];
public ushort wDevicePosition;
public ushort usPowerSaveRecoveryTime;
}
//lppPositions 指向WFS_CDM_OUTPOS 结构以NULL结尾的指针数组。此结构体所有参数均有效且能够被分配
public struct WFS_CDM_OUTPOS
{
public ushort fwPosition;
public ushort fwShutter;
public ushort fwPositionStatus;
public ushort fwTransport;
public ushort fwTransportStatus;
}
WFS_CDM_STATUS state = new WFS_CDM_STATUS();
XFSUtil.PtrToStructure(result.lpBuffer, ref state);
WFS_CDM_OUTPOS pos=new WFS_CDM_OUTPOS();
XFSUtil.PtrToStructure(state.lppPositions, ref pos);
public static T[] XFSPtrToArray<T>(IntPtr ptr)//T表示的需要指向的结构体类型
{
if (ptr != IntPtr.Zero)
{
int len = 0;
for (int i = 0; Marshal.ReadIntPtr(IntPtr.Add(ptr, i)) != IntPtr.Zero; i += IntPtrSize)
++len;
if (len > 0)
{
T[] arr = new T[len];
for (int i = 0; i < len; ++i)
{
arr[i] = (T)Marshal.PtrToStructure(Marshal.ReadIntPtr(IntPtr.Add(ptr, i * IntPtrSize)), typeof(T));
}
return arr;
}
}
return new T[0];//我这需要的是第一个指针,所以就取了这个。
}
然后利用这个函数,最终解决我遇到的问题。感恩,希望对以后遇到这方面问题的人有所帮助