首页 新闻 搜索 专区 学院

请教:c#调用VC++(6.0)dll方法(结构体或是指针)

0
悬赏园豆:50 [已关闭问题] 关闭于 2020-08-09 21:39

请教各位大侠:
调用第三方硬件的VC++ 6.0的dll文件中的方法

函数声明: DLL_API WORD WINAPI dsoHTSetSampleRate
(WORD nDeviceIndex,//WORD 型变量,
WORD nYTFormat,//WORD 型变量,
PRELAYCONTROL pRelayControl,
PCONTROLDATA pControl)
pControl指向 CONTROLDATA 型变量。
pRelayControl指向 RELAYCONTROL 型变量

其中,CONTROLDATA的结构如下
typedef struct _HT_CONTROL_DATA
{
...............................
}CONTROLDATA,*PCONTROLDATA;

我在c#中
//创建结构
[StructLayout(LayoutKind.Sequential)]
public struct RelayControl //define.MAX_CH_NUM=4
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst =define.MAX_CH_NUM)]
public bool[] bCHEnable ;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = define.MAX_CH_NUM)]
public ushort[] nCHVoltDIV;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = define.MAX_CH_NUM)]
public ushort[] nCHCoupling;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = define.MAX_CH_NUM)]
public bool[] bCHBWLimit;
public ushort nTrigSource;
public bool bTrigFilt;
public ushort nALT;
}

    [StructLayout(LayoutKind.Sequential)]
    public struct CONTROLDATA
    {
        public ushort nCHSet;
        public ushort nTimeDiv;
        public ushort nTriggerSource;
        public ushort nHTriggerPos;
        public ushort nVTriggerPos;
        public ushort nTriggerSlope;
        public uint nBufferLen;
        public uint nReadDataLen;
        public uint nAlreadyReadLen;
        public ushort nALT;
        public ushort nETSOpen;
        public ushort nDriverCode;
        public uint nLastAddress;
        //public ushort nFPGAVersion;
    }

使用
[DllImport("HTHardDll.dll", EntryPoint = "dsoHTSetSampleRate", CallingConvention = CallingConvention.StdCall)]

public static extern ushort dsoHTSetSampleRate(ushort DevIndex, ushort nYTFormat, ref RelayControl pRelayControl, ref CONTROLDATA pSTControl);/

并创建 public CONTROLDATA stControl;
public RelayControl rcRelayControl;

通过按钮触发:
ushort result = 0;
ushort DeviceIndex = 0;
ushort YTFormat1 = 0;

result = dsoHTSetSampleRate(DeviceIndex, YTFormat1, ref rcRelayControl,ref stControl);

总是报错对 PInvoke 函数“test_dill1!test_dill1.Form1::dsoHTSetSampleRate”的调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配。

我把ref改为out提示内存无法写入等错误,

1·请大侠们指点一下思路,入门新手研究了几天了,都没头绪!
2·还有就是这种是引用的结构体还是结构体指针呢?
3· 厂家给了一个c++的引用例子dsoHTSetSampleRate(m_nDeviceIndex,m_nYTFormat,&RelayControl,&m_stControl);//设置采样率,请问,带&符号表示类型是指针还是啥呢?结构体和指针,在c#里貌似是不同的,有的说家ref,有的说用out

篇幅写的冗余,分不多,请大虾们耐心解答下,拜谢!

rossi1984的主页 rossi1984 | 初学一级 | 园豆:24
提问于:2020-07-25 09:56
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册