日期:2014-05-18  浏览次数:20866 次

C#中调用C DLL 传递结构体数组问题
C++结构体
struct BarcodeInfo //条码结构信息
{
char ShowContent[16] ;//条码显示内容
char BarcodeContent[16];//条码内容
};
//动态库LabelPrint.dll,接口如下
int PrintBarCode(BarcodeInfo* pBatchInfo,
BarcodeInfo* pBarcodeinfoArray,//条型码数组
int nSize)//数组大小

// 在C#中如果调用PrintBarCode

------解决方案--------------------
参考,看看DLL 传递结构 那部分
http://blog.csdn.net/sunboyljp/article/details/5110639
------解决方案--------------------
探讨

是传结构体数组

------解决方案--------------------
[StructLayout(LayoutKind.Sequential)]
public struct BarcodeInfo 
{
  
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public char[] ShowContent;
 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public char[] BarcodeContent;

};


[DllImport("LabelPrint.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int PrintBarCode(IntPtr pBatchInfo,IntPtr pBarcodeinfoArray);

用的时候先定义你的结构体,将数据传送进去如m_DataTransInfo

IntPtr m_DataTransInfo1 = Marshal.AllocHGlobal(Marshal.SizeOf(m_DataTransInfo));

Marshal.StructureToPtr(m_DataTransInfo, m_DataTransInfo1, true);

上面2个函数是将结构体转为IntPtr的形式,
之后你应该知道怎么做了吧!!

希望有用啦


------解决方案--------------------
完整解决:

1、结构体声明:
C# code

[StructLayout(LayoutKind.Sequential)]
public struct BarcodeInfo 
{
  
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
  public string ShowContent;  
 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
  public string BarcodeContent;  

};