当前位置:编程学习 > C#/ASP.NET >>

大家来找茬:c#调用c++ dll库出现的问题

c++原码:
struct StationInfo
{
unsigned long nID; Station ID
unsigned long nParentID;  父Station ID
char strName[MAX_TEXT_LEN];  名称
char strMemo[MAX_MEM_LEN];  描述
};
int EV_GetStationByID(IN unsigned long nStationID,OUT StationInfo deviceStation[],  OUT unsigned int* pCount);

c# 代码:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public unsafe struct StationInfo
        {
            public UInt32 nID;                     //Station ID
            public UInt32 nParentID;                  //父Station ID
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_MEM_LEN)]
            public byte[] strMemo;                          //描述
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_TEXT_LEN)]
            public byte[] strName;                         //名称X
        };
 [DllImport(DllName, EntryPoint = "EV_GetStationByID", CallingConvention = CallingConvention.Cdecl)]
        private static extern int EV_GetStationByID(UInt32 nStationID, ref IntPtr p, ref UInt32 pCount);

调用
IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(StationInfo)) * 50); 
result = EV_GetStationByID(nStationID,ref pt,ref pCount);

出现错误。请大家帮忙检查一下哪里出现错误了,谢谢 --------------------编程问答-------------------- c#原码声明为:
[DllImport(DllName, EntryPoint = "EV_GetStationByID", CallingConvention = CallingConvention.Cdecl)]
        private static extern int EV_GetStationByID(UInt32 nStationID, StationInfo[] deviceStation, ref UInt32 pCount);
在调用的时候只得到一条数据,不知为何? --------------------编程问答-------------------- StationInfo[] deviceStation
这个前面是不是要加个ref吧 --------------------编程问答-------------------- +ref 直接报错 --------------------编程问答-------------------- C++里的第二个参数定义的是数组哦
OUT StationInfo deviceStation[] --------------------编程问答-------------------- 嗯。楼上的问题  考虑过,最早定义的是
private static extern int EV_GetStationByID(UInt32 nStationID, ref StationInfo[] deviceStation, ref UInt32 pCount);
但是出错。 --------------------编程问答-------------------- IntPtr pt定义也定义成数组了? --------------------编程问答--------------------

C#调用c++dll文件是一件很麻烦的事情,首先面临的是数据类型转换的问题,相信经常做c#开发的都和我一样把学校的那点c++底子都忘光了吧(语言特性类)。
网上有一大堆得转换对应表,也有一大堆的转换实例,但是都没有强调一个更重要的问题,就是c#数据类型和c++数据类型占内存长度的对应关系。
    如果dll文件中只包含一些基础类型,那这个问题可能可以被忽略,但是如果是组合类型(这个叫法也许不妥),如结构体、类类型等,在其中的成员变量的长度的申明正确与否将决定你对dll文件调用的成败。
如有以下代码,其实不是dll文件的源码,而是厂商给的c++例子代码
  c++中的结构体申明
view plaincopy to clipboardprint?
typedef struct  
{   
  unsigned char Port;   
  unsigned long Id;   
  unsigned char Ctrl;   
  unsigned char pData[8];   
}HSCAN_MSG;  
typedef struct
{
  unsigned char Port;
  unsigned long Id;
  unsigned char Ctrl;
  unsigned char pData[8];
}HSCAN_MSG; 
c++中的函数申明(一个c++程序引用另一个c++的dll文件)
view plaincopy to clipboardprint?
extern "C" int _stdcall HSCAN_SendCANMessage(unsigned char nDevice,unsigned char nPort,HSCAN_MSG *msg,int nLength);  
extern "C" int _stdcall HSCAN_SendCANMessage(unsigned char nDevice,unsigned char nPort,HSCAN_MSG *msg,int nLength); 
c++中的调用:
view plaincopy to clipboardprint?
....   
HSCAN_MSG msg[100];   
.....   
HSCAN_SendCANMessage(m_nDevice,m_nPort,msg,nFrames);  
....
HSCAN_MSG msg[100];
.....
HSCAN_SendCANMessage(m_nDevice,m_nPort,msg,nFrames); 

由上述代码可见,msg是个结构体的数组。
下面是我的c#的代码
c#结构体申明:(申明成)
view plaincopy to clipboardprint?
[StructLayout(LayoutKind.Sequential)]   
   public struct HSCAN_MSG   
   {   
    // UnmanagedType.ByValArray, [MarshalAs(UnmanagedType.U1)]这个非常重要,就是申明对应类型和长度的   
   [MarshalAs(UnmanagedType.U1)]   
   public byte Port;   
   [MarshalAs(UnmanagedType.U4)]   
   public uint nId;   
   [MarshalAs(UnmanagedType.U1)]   
   public byte nCtrl;   
   [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]   
   public byte[] pData;   
   };  
[StructLayout(LayoutKind.Sequential)]
   public struct HSCAN_MSG
   {
    // UnmanagedType.ByValArray, [MarshalAs(UnmanagedType.U1)]这个非常重要,就是申明对应类型和长度的
   [MarshalAs(UnmanagedType.U1)]
   public byte Port;
   [MarshalAs(UnmanagedType.U4)]
   public uint nId;
   [MarshalAs(UnmanagedType.U1)]
   public byte nCtrl;
   [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
   public byte[] pData;
   }; 
c#函数申明
view plaincopy to clipboardprint?
[DllImport("HS2106API.dll")]   
   public static extern int HSCAN_SendCANMessage(   
   byte nDevice, byte nPort, HSCAN_MSG[] pMsg, int nLength);  
[DllImport("HS2106API.dll")]
   public static extern int HSCAN_SendCANMessage(
   byte nDevice, byte nPort, HSCAN_MSG[] pMsg, int nLength); 
C#函数调用
view plaincopy to clipboardprint?
HSCAN_MSG[] msg = new HSCAN_MSG[1]; //发送缓冲区大小可根据需要设置;   
   for (int yy = 0; yy < msg.Length; yy++)   
   {   
   msg[yy] = new HSCAN_MSG();   
   }   
    //...结构体中的成员的实例化略   
    HSCAN_SendCANMessage(0x0, 0x0, msg, 1)  
HSCAN_MSG[] msg = new HSCAN_MSG[1]; //发送缓冲区大小可根据需要设置;
   for (int yy = 0; yy < msg.Length; yy++)
   {
   msg[yy] = new HSCAN_MSG();
   }
    //...结构体中的成员的实例化略
    HSCAN_SendCANMessage(0x0, 0x0, msg, 1) 

那些只能用指针不能用结构体和类的地方
c++中的结构体申明
view plaincopy to clipboardprint?
typedef struct  
{   
  unsigned char Port;   
  unsigned long Id;   
  unsigned char Ctrl;   
  unsigned char pData[8];   
}HSCAN_MSG;  
typedef struct
{
  unsigned char Port;
  unsigned long Id;
  unsigned char Ctrl;
  unsigned char pData[8];
}HSCAN_MSG; 
c++中的函数申明(一个c++程序引用另一个c++的dll文件)
view plaincopy to clipboardprint?
extern "C" int _stdcall HSCAN_SendCANMessage(unsigned char nDevice,unsigned char nPort,HSCAN_MSG *msg,int nLength);  
extern "C" int _stdcall HSCAN_SendCANMessage(unsigned char nDevice,unsigned char nPort,HSCAN_MSG *msg,int nLength); 
c#中的结构体申明:
view plaincopy to clipboardprint?
[StructLayout(LayoutKind.Sequential)]   
   public struct HSCAN_MSG   
   {   
   [MarshalAs(UnmanagedType.U1)]   
   public byte Port;   
   /// <summary>   
   /// 节点标识,nEFF=1 时(扩展帧),为29 位nEFF=0(标准帧)时,为11 位;   
   /// </summary>   
   [MarshalAs(UnmanagedType.U4)]   
   public uint nId;   
   [MarshalAs(UnmanagedType.U1)]   
   public byte nCtrl;   
   [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]   
   public byte[] pData;   
   };  
[StructLayout(LayoutKind.Sequential)]
   public struct HSCAN_MSG
   {
   [MarshalAs(UnmanagedType.U1)]
   public byte Port;
   /// <summary>
   /// 节点标识,nEFF=1 时(扩展帧),为29 位nEFF=0(标准帧)时,为11 位;
   /// </summary>
   [MarshalAs(UnmanagedType.U4)]
   public uint nId;
   [MarshalAs(UnmanagedType.U1)]
   public byte nCtrl;
   [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
   public byte[] pData;
   }; 
c#函数的调用:包含使用指针IntPtr替代结构体数组和读取IntPtr的方法
view plaincopy to clipboardprint?
HSCAN_MSG[] msg1 = new HSCAN_MSG[10];   
   for (int i = 0; i < msg1.Length; i++)   
   {   
   msg1[i] = new HSCAN_MSG();   
   msg1[i].pData = new byte[8];   
   }   
   IntPtr[] ptArray = new IntPtr[1];   
   ptArray[0] = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(HSCAN_MSG)) * 10);   
   IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(HSCAN_MSG)));   
   Marshal.Copy(ptArray, 0, pt, 1);   
     
   int count = HSCAN_ReadCANMessage(0x0, 0,pt, 10);   
     
   textBoxStatus.Text += "\r\n" + "读取0口:" + count.ToString() + "帧数据";   
   for (int j = 0; j < 10; j++)   
   {   
   msg1[j] =   
   (HSCAN_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)pt+ j * Marshal.SizeOf(typeof(HSCAN_MSG)))   
   , typeof(HSCAN_MSG));   
   textBoxStatus.Text += "\r\n收到0口" + Convert.ToByte(msg1[j].pData[0]).ToString()   
   + "|" + Convert.ToByte(msg1[j].pData[1]).ToString()   
   + "|" + Convert.ToByte(msg1[j].pData[2]).ToString()   
   + "|" + Convert.ToByte(msg1[j].pData[3]).ToString()   
   + "|" + Convert.ToByte(msg1[j].pData[4]).ToString()   
   + "|" + Convert.ToByte(msg1[j].pData[5]).ToString()   
   + "|" + Convert.ToByte(msg1[j].pData[6]).ToString()   
   + "|" + Convert.ToByte(msg1[j].pData[7]).ToString();   
   }  
HSCAN_MSG[] msg1 = new HSCAN_MSG[10];
   for (int i = 0; i < msg1.Length; i++)
   {
   msg1[i] = new HSCAN_MSG();
   msg1[i].pData = new byte[8];
   }
   IntPtr[] ptArray = new IntPtr[1];
   ptArray[0] = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(HSCAN_MSG)) * 10);
   IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(HSCAN_MSG)));
   Marshal.Copy(ptArray, 0, pt, 1);
  
   int count = HSCAN_ReadCANMessage(0x0, 0,pt, 10);
  
   textBoxStatus.Text += "\r\n" + "读取0口:" + count.ToString() + "帧数据";
   for (int j = 0; j < 10; j++)
   {
   msg1[j] =
   (HSCAN_MSG)Marshal.PtrToStructure((IntPtr)((UInt32)pt+ j * Marshal.SizeOf(typeof(HSCAN_MSG)))
   , typeof(HSCAN_MSG));
   textBoxStatus.Text += "\r\n收到0口" + Convert.ToByte(msg1[j].pData[0]).ToString()
   + "|" + Convert.ToByte(msg1[j].pData[1]).ToString()
   + "|" + Convert.ToByte(msg1[j].pData[2]).ToString()
   + "|" + Convert.ToByte(msg1[j].pData[3]).ToString()
   + "|" + Convert.ToByte(msg1[j].pData[4]).ToString()
   + "|" + Convert.ToByte(msg1[j].pData[5]).ToString()
   + "|" + Convert.ToByte(msg1[j].pData[6]).ToString()
   + "|" + Convert.ToByte(msg1[j].pData[7]).ToString();
   } 

--------------------编程问答-------------------- 具体如何修改?楼上这个帖子我看的不是很明白 --------------------编程问答-------------------- 定义成
private static extern int EV_GetStationByID(UInt32 nStationID, ref StationInfo[] deviceStation, ref UInt32 pCount);
pCount能获得正确的值,但是stationinfo数组无数据 --------------------编程问答-------------------- 我没仔细看题目
是不是要加上__stdcall? --------------------编程问答-------------------- 另外要写好.def文件
引用 10 楼 wardensky 的回复:
我没仔细看题目
是不是要加上__stdcall?
--------------------编程问答-------------------- 楼上:改为stdcall直接报错。 --------------------编程问答-------------------- 我前两天刚刚写的用C#调用c++ dll的程序,应该没有问题。

我写的某个函数
long __stdcall getLocalSize(char localFile[])
{

}

记得修改.def文件,内容大概如下:
LIBRARY      "cppdll"

EXPORTS

getDownRate @1
getUpRate @2
ftpRemoteFileSize @3
getLocalSize @4
引用 12 楼 weixq1982 的回复:
楼上:改为stdcall直接报错。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,