2015-06-02 48 views
0

结构1:如何为结构在C#阵列分配内存

typedef struct _wfs_cdm_cu_info 
{ 
    USHORT usTellerID; 
    USHORT usCount; 
    LPWFSCDMCASHUNIT * lppList; 
} WFSCDMCUINFO, * LPWFSCDMCUINFO; 

结构2:

typedef struct _wfs_cdm_cashunit 
{ 
    USHORT usNumber; 
    USHORT usType; 
    LPSTR lpszCashUnitName; 
    CHAR cUnitID[5]; 
    CHAR cCurrencyID[3]; 
    ULONG ulValues; 
    ULONG ulInitialCount; 
    ULONG ulCount; 
    ULONG ulRejectCount; 
    ULONG ulMinimum; 
    ULONG ulMaximum; 
    BOOL bAppLock; 
    USHORT usStatus; 
    USHORT usNumPhysicalCUs; 
    LPWFSCDMPHCU * lppPhysical; 
} WFSCDMCASHUNIT, * LPWFSCDMCASHUNIT; 

结构3:

typedef struct _wfs_cdm_physicalcu 
{ 
    LPSTR lpPhysicalPositionName; 
    CHAR cUnitID[5]; 
    ULONG ulInitialCount; 
    ULONG ulCount; 
    ULONG ulRejectCount; 
    ULONG ulMaximum; 
    USHORT usPStatus; 
    BOOL bHardwareSensor; 
} WFSCDMPHCU, * LPWFSCDMPHCU; 

C#的结构: -

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi, Pack = 1)] 
public struct WFSCDMPHCU {  [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] 
public string lpPhysicalPositionName;[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=5)] 
public string cUnitID; 
public uint ulInitialCount; 
public uint ulCount; 
public uint ulRejectCount; 
public uint ulMaximum; 
public ushort usPStatus; 
public int bHardwareSensor; 
} 

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi, Pack = 1)] 
    public struct WFSCDMCASHUNIT { 
    public ushort usNumber; 
    public ushort usType;   [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] 
    public string lpszCashUnitName;[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=5)] 
    public string cUnitID;  [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=3)] 
    public string cCurrencyID; 
    public uint ulValues; 
    public uint ulInitialCount; 
    public uint ulCount; 
    public uint ulRejectCount; 
    public uint ulMinimum; 
    public uint ulMaximum; 
    public int bAppLock; 
    public ushort usStatus; 
    public ushort usNumPhysicalCUs; 
    public System.IntPtr lppPhysical; 
    } 

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, Pack = 1)] 
public struct WFSCDMCUINFO {  
public ushort usTellerID;  
public ushort usCount;  
public System.IntPtr lppList; 
} 

的DllImport

[DllImport(@"Dispenser.dll")] 
public static extern int CDM_SetCashUnit(out WFSCDMCUINFO cuinfo); 

1)我的主要问题是我应该如何编组或分配内存这个结构从C#发送数据到C++,第二个和第三个结构是结构数组?

2)如果我使用指针将有多高效。

3)如果使用C++/CLI包装,那么我如何通过C#访问它。

这是很长一段时间我正在工作,我还没有弄清楚如何填充C#中的结构数组。

Follwing代码是我揣摩......

元帅代码:

面对的一个错误“来定义一个扩展非泛型静态类”

public static IntPtr GetIntPtr(this object obj) { 
      try { 
       var handle = GCHandle.Alloc(obj, GCHandleType.Pinned); 
       var thread = new Thread(() => { 
        Thread.Sleep(20000); 
        handle.Free(); 
       }); 
       thread.Start(); 

       return handle.AddrOfPinnedObject(); 
      } catch (ArgumentException) { 
       var ptr = Marshal.AllocHGlobal(Marshal.SizeOf(obj)); 

       Marshal.StructureToPtr(obj, ptr, false); 

       return ptr; 
      } 
     } 

     public static T FromIntPtr<T>(this IntPtr ptr) { 
      if (ptr == IntPtr.Zero) 
       return default(T); 

      return (T) Marshal.PtrToStructure(ptr, typeof (T)); 
     } 

给出了一个C++代码的链接,以及我如何调用函数的链接。link

+0

然后,请发布您在尝试实施解决方案时编写的代码,并描述发生的情况,以便我们帮助您找出其无效的原因。 – phoog

+0

@phoog:我不明白如何编组这些结构,以及我得到一个扩展方法错误,当我发布以前的链接代码..我cnt使类为静态,因为它影响其他功能已创建 – TechBrkTru

+0

是否有任何其他分配结构的方式? @phoog – TechBrkTru

回答

0

这是在c#我的结构映射:

[StructLayout(LayoutKind.Sequential)] 
public struct Message 
{ 
    public uint MsgId; 
    public uint DLC; 
    public uint Interval; 
    public uint Handle; 
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)] 
    public byte[] Data; 
}; 

C++这里:

struct Message { 
    unsigned int MsgId; 
    unsigned int DLC; 
    unsigned int Interval; 
    unsigned int Handle; 
    unsigned char Data[64]; 

};

我在C++中它需要这样的结构作为参数方法:

extern "C" __declspec(dllexport) int _stdcall MessageWrapper(Message *msg) 

这是我如何调用此方法从C#:

  Message frame = new Message(); 
      frame.MsgId = (uint)MsgId; 
      frame.DLC = (uint)Dlc; 
      frame.Interval = (uint)Interval; 
      frame.Data = new byte[64]; 


      int rawsize = Marshal.SizeOf(frame); 
      IntPtr frameBuffer = Marshal.AllocHGlobal(rawsize); 
      Marshal.StructureToPtr(frame, frameBuffer, false); 

调用此方法:

  int response = HwWrapper.MessageWrapper(frameBuffer); 

您可能会对C++进行一些修改,您可以在c#中阅读它们:

  frame = (Message)(Marshal.PtrToStructure(frameBuffer, typeof(Message))); 


      Marshal.FreeHGlobal(frameBuffer); 
+0

那么这是为了简单的结构,对于结构数组呢? atleast for third strucutre .... @ Olaru Mircea – TechBrkTru

+0

事实上,我的例子并没有执行,但这里是一个答案:https://social.msdn.microsoft.com/Forums/vstudio/en-US/dcfd6310-b03b- 4552-b4c7-6c11c115eb45 /传递阵列与阵列从C到C?论坛= clr –

+0

什么是hwWrapper在上面的代码? @Olaru Mircea – TechBrkTru