C#中引用C的dll的问题

Dolany 2016-10-26 03:13:45
最近做项目的时候需要将一个C的dll引入到C#中,但是尝试了各种方式后还是不成功,求各位大神解答

C中的结构体定义

typedef struct
{
BYTE BTR0;
BYTE BTR1;
BYTE AcceptFilterMode;
BYTE ACR[4];
BYTE AMR[4];
BYTE EMLR;
}PELICAN_PARAM_STRUCT, *pPELICAN_PARAM_STRUCT;
typedef struct
{
HDEVICE hCard;
BYTE CardId;
} ST_DEVDSC, *HDEVICE;

C中的函数定义

BOOL __stdcall AECCANC2_Init (HDEVICE hAECCANC2, BYTE ChanNo, pPELICAN_PARAM_STRUCT pPeliCanParam);


C#中的结构定义

[StructLayout(LayoutKind.Sequential)]
public struct PELICAN_PARAM_STRUCT
{
public byte BTR0;
public byte BTR1;
public byte AcceptFilterMode;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] ACR;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] AMR;
public byte EMLR;

public PELICAN_PARAM_STRUCT(byte btr0, byte btr1, byte acceptFilterMode, byte emlr)
{
BTR0 = btr0;
BTR1 = btr1;
AcceptFilterMode = acceptFilterMode;
ACR = new byte[4];
AMR = new byte[4];
EMLR = emlr;
}
}
[StructLayout(LayoutKind.Sequential)]
public struct HDEVICE
{
public IntPtr hCard;
public byte CardId;
}

C#中的函数定义

[DllImport("AECCANC2.dll")]
public static extern bool AECCANC2_Init(HDEVICE hAECCANC2, byte ChanNo, ref PELICAN_PARAM_STRUCT pPeliCanParam);


在调用的时候会报错
"尝试读取或写入受保护的内存。这通常指示其他内存已损坏。"
...全文
197 7 打赏 收藏 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dolany 2016-10-26
  • 打赏
  • 举报
回复
引用 5 楼 Chinajiyong 的回复:
[quote=引用 3 楼 u010574800 的回复:] [quote=引用 1 楼 Chinajiyong 的回复:] 结构体定义


[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct PELICAN_PARAM_STRUCT {
    
    /// BYTE->unsigned char
    public byte BTR0;
    
    /// BYTE->unsigned char
    public byte BTR1;
    
    /// BYTE->unsigned char
    public byte AcceptFilterMode;
    
    /// BYTE[4]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=4, ArraySubType=System.Runtime.InteropServices.UnmanagedType.I1)]
    public byte[] ACR;
    
    /// BYTE[4]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=4, ArraySubType=System.Runtime.InteropServices.UnmanagedType.I1)]
    public byte[] AMR;
    
    /// BYTE->unsigned char
    public byte EMLR;
}

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct ST_DEVDSC {
    
    /// HDEVICE->Anonymous_aa027289_d9d1_4a7b_8e4e_cf41a8b4b147*
    public System.IntPtr hCard;
    
    /// BYTE->unsigned char
    public byte CardId;
}
方法

  [System.Runtime.InteropServices.DllImportAttribute("AECCANC2.dll", EntryPoint="AECCANC2_Init", CallingConvention=System.Runtime.InteropServices.CallingConvention.StdCall)]
    [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern  bool AECCANC2_Init(ref ST_DEVDSC hAECCANC2, byte ChanNo, ref PELICAN_PARAM_STRUCT pPeliCanParam) ;

诶,ST_DEVDSC 也要传引用么?那这个函数怎么写啊?

BOOL __stdcall AECCANC2_Open (HDEVICE *phAECCANC2, BYTE CardId);
[/quote] 你先试一下


    [System.Runtime.InteropServices.DllImportAttribute("AECCANC2.dll", EntryPoint="AECCANC2_Open", CallingConvention=System.Runtime.InteropServices.CallingConvention.StdCall)]
    [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern  bool AECCANC2_Open(ref System.IntPtr phAECCANC2, byte CardId) ;
[/quote] 感谢大神相助,确实是引用的问题。。。 C/C++的代码放到C#中就是各种恶心。。
漠落雨 2016-10-26
  • 打赏
  • 举报
回复
函数传的第一个参数是 HDEVICE 类型的 ,应该是和 typedef struct { HDEVICE hCard; BYTE CardId; } ST_DEVDSC, *HDEVICE;中的 HDEVICE hCard;里的HDEVICE的类型是一样的,你定义的那个 HDEVICE的结构体不对 找到HDEVICE 它的结构体定义 重新定义一下 应该就可以了
EnForGrass 2016-10-26
  • 打赏
  • 举报
回复
引用 3 楼 u010574800 的回复:
[quote=引用 1 楼 Chinajiyong 的回复:] 结构体定义


[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct PELICAN_PARAM_STRUCT {
    
    /// BYTE->unsigned char
    public byte BTR0;
    
    /// BYTE->unsigned char
    public byte BTR1;
    
    /// BYTE->unsigned char
    public byte AcceptFilterMode;
    
    /// BYTE[4]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=4, ArraySubType=System.Runtime.InteropServices.UnmanagedType.I1)]
    public byte[] ACR;
    
    /// BYTE[4]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=4, ArraySubType=System.Runtime.InteropServices.UnmanagedType.I1)]
    public byte[] AMR;
    
    /// BYTE->unsigned char
    public byte EMLR;
}

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct ST_DEVDSC {
    
    /// HDEVICE->Anonymous_aa027289_d9d1_4a7b_8e4e_cf41a8b4b147*
    public System.IntPtr hCard;
    
    /// BYTE->unsigned char
    public byte CardId;
}
方法

  [System.Runtime.InteropServices.DllImportAttribute("AECCANC2.dll", EntryPoint="AECCANC2_Init", CallingConvention=System.Runtime.InteropServices.CallingConvention.StdCall)]
    [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern  bool AECCANC2_Init(ref ST_DEVDSC hAECCANC2, byte ChanNo, ref PELICAN_PARAM_STRUCT pPeliCanParam) ;

诶,ST_DEVDSC 也要传引用么?那这个函数怎么写啊?

BOOL __stdcall AECCANC2_Open (HDEVICE *phAECCANC2, BYTE CardId);
[/quote] 你先试一下


    [System.Runtime.InteropServices.DllImportAttribute("AECCANC2.dll", EntryPoint="AECCANC2_Open", CallingConvention=System.Runtime.InteropServices.CallingConvention.StdCall)]
    [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern  bool AECCANC2_Open(ref System.IntPtr phAECCANC2, byte CardId) ;
Dolany 2016-10-26
  • 打赏
  • 举报
回复
引用 2 楼 Libby1984 的回复:
去掉你C#代码中第一个结构体里面的构造函数试试,不需要。
还是一样的问题。
Dolany 2016-10-26
  • 打赏
  • 举报
回复
引用 1 楼 Chinajiyong 的回复:
结构体定义


[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct PELICAN_PARAM_STRUCT {
    
    /// BYTE->unsigned char
    public byte BTR0;
    
    /// BYTE->unsigned char
    public byte BTR1;
    
    /// BYTE->unsigned char
    public byte AcceptFilterMode;
    
    /// BYTE[4]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=4, ArraySubType=System.Runtime.InteropServices.UnmanagedType.I1)]
    public byte[] ACR;
    
    /// BYTE[4]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=4, ArraySubType=System.Runtime.InteropServices.UnmanagedType.I1)]
    public byte[] AMR;
    
    /// BYTE->unsigned char
    public byte EMLR;
}

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct ST_DEVDSC {
    
    /// HDEVICE->Anonymous_aa027289_d9d1_4a7b_8e4e_cf41a8b4b147*
    public System.IntPtr hCard;
    
    /// BYTE->unsigned char
    public byte CardId;
}
方法

  [System.Runtime.InteropServices.DllImportAttribute("AECCANC2.dll", EntryPoint="AECCANC2_Init", CallingConvention=System.Runtime.InteropServices.CallingConvention.StdCall)]
    [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern  bool AECCANC2_Init(ref ST_DEVDSC hAECCANC2, byte ChanNo, ref PELICAN_PARAM_STRUCT pPeliCanParam) ;

诶,ST_DEVDSC 也要传引用么?那这个函数怎么写啊?

BOOL __stdcall AECCANC2_Open (HDEVICE *phAECCANC2, BYTE CardId);
  • 打赏
  • 举报
回复
去掉你C#代码中第一个结构体里面的构造函数试试,不需要。
EnForGrass 2016-10-26
  • 打赏
  • 举报
回复
结构体定义


[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct PELICAN_PARAM_STRUCT {
    
    /// BYTE->unsigned char
    public byte BTR0;
    
    /// BYTE->unsigned char
    public byte BTR1;
    
    /// BYTE->unsigned char
    public byte AcceptFilterMode;
    
    /// BYTE[4]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=4, ArraySubType=System.Runtime.InteropServices.UnmanagedType.I1)]
    public byte[] ACR;
    
    /// BYTE[4]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=4, ArraySubType=System.Runtime.InteropServices.UnmanagedType.I1)]
    public byte[] AMR;
    
    /// BYTE->unsigned char
    public byte EMLR;
}

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct ST_DEVDSC {
    
    /// HDEVICE->Anonymous_aa027289_d9d1_4a7b_8e4e_cf41a8b4b147*
    public System.IntPtr hCard;
    
    /// BYTE->unsigned char
    public byte CardId;
}
方法

  [System.Runtime.InteropServices.DllImportAttribute("AECCANC2.dll", EntryPoint="AECCANC2_Init", CallingConvention=System.Runtime.InteropServices.CallingConvention.StdCall)]
    [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern  bool AECCANC2_Init(ref ST_DEVDSC hAECCANC2, byte ChanNo, ref PELICAN_PARAM_STRUCT pPeliCanParam) ;

111,128

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧