将C#与非常老的ActiveX控件接口

时间:2018-12-17 19:44:52

标签: c# memory unmanaged software-defined-radio

我正在尝试将旧的RFSpace SDR-IQ无线电集成到现有的C#程序中。 SDR-IQ信息:http://www.rfspace.com/RFSPACE/SDR-IQ.html

SDR-IQ通过USB连接到PC。

我找到了一个非常老的ActiveX控件,该控件已成功放到C#对话框中( 手动注册DLL)。 ActiveX控件:http://www.moetronix.com/files/sdr14xsdk127.zip

以下是该控件的C#元数据:

namespace AxSDR14XLib
{
    [DefaultEvent("StatusChange")]
    [DesignTimeVisible(true)]
    public class AxSDR14X : AxHost
    {
        public AxSDR14X();

        [DispId(12)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public virtual short BootRevision { get; set; }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [DispId(1)]
        public virtual int ClockFrequency { get; set; }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [DispId(2)]
        public virtual string DeviceName { get; set; }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [DispId(3)]
        public virtual int FirmwareRevision { get; set; }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [DispId(4)]
        public virtual int HFgain { get; set; }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [DispId(5)]
        public virtual int IFfilter { get; set; }
        [DispId(10)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public virtual int IFgain { get; set; }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [DispId(6)]
        public virtual int InputSource { get; set; }
        [DispId(7)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public virtual int InterfaceVersion { get; set; }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [DispId(13)]
        public virtual double IQOffset { get; set; }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [DispId(8)]
        public virtual int NCOfrequency { get; set; }
        [DispId(11)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public virtual int SampleRate { get; set; }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [DispId(9)]
        public virtual string SerialNumber { get; set; }

        public event _DSDR14XEvents_DataRdyEventHandler DataRdy;
        public event _DSDR14XEvents_StatusChangeEventHandler StatusChange;
        public event EventHandler UartDataRdy;

        public virtual void AboutBox();
        protected override void AttachInterfaces();
        public virtual void CloseI2CPort();
        public virtual void CloseSpiPort();
        public virtual void CloseUartPort();
        protected override void CreateSink();
        protected override void DetachSink();
        public virtual int GetDoubleData(ref double dataArray);
        public virtual bool GetFftDoubleData(ref double dataArray);
        public virtual bool GetFftScreendata(int plotHeght, int plotWidth, double maxdB, double mindB, int startFreq, int endFreq, ref int plotBuf);
        public virtual int GetIntegerData(ref short dataArray);
        public virtual int GetLongData(ref int dataArray);
        public virtual short GetUartData(ref short uartRxArray);
        public virtual void InitControl();
        public virtual void OpenI2CPort(short adrWidth);
        public virtual void OpenSpiPort(short clkPolarity);
        public virtual void OpenUartPort(int baudRate, short parity, bool flowControl);
        public virtual void ResetFftAverage();
        public virtual void Send6620RawCmd(short address, short d0, short d1, short d2, short d3, short d4);
        public virtual void SendI2CData(ref short txDataArray, short numDataItems);
        public virtual void SendSpiData(int data32High, int data32Low, short numberBits);
        public virtual void SendUartData(ref short uartTxArray, short numDataItems);
        public virtual void SetupFFT(int fftPoints, short fftaverages, bool fftInverted);
        public virtual void Start(short blocks, short mode);
        public virtual void Stop();
    }

该控件的.InitControl().Start()方法可以正常工作。 StatusChangeDataRdy事件也起作用。因此,控件已成功与无线电通信。

当我尝试通过DataRdy.GetLongData()事件处理程序中的控件中获取数据时,就会出现问题。

尽管进行了很多小时的搜索,无论我尝试什么,我的代码都因以下错误而崩溃:

  

程序“ [9036] sdriq.vshost.exe:托管(v4.0.30319)”已退出   代码为-1073741819(0xc0000005)。

在Exception对象中没有有关崩溃的更多详细信息。

下面是一个呼叫.GetLongData()失败的示例:

private int[] m_SdrData = new int[40000];

private void axSDR14X1_DataRdy(object sender, AxSDR14XLib._DSDR14XEvents_DataRdyEvent e)
{
    unsafe
    {
        var unmanagedArray = (int*)Marshal.AllocHGlobal(m_SdrData.Length * sizeof(int));
        int samples = axSDR14X1.GetLongData(ref unmanagedArray[0]);
        for (int i = 0; i < m_SdrData.Length; i++) m_SdrData[i] = unmanagedArray[i];
        Marshal.FreeHGlobal((IntPtr)unmanagedArray);
    }
}

当我向控件的自述文件中的地址写信时,控件的原始作者确实做出了答复:“是的,我前一段时间写的dll是一个activex dll,我不认为uSoft允许传递内存指针了。不知道C#,但我怀疑它也不允许直接内存指针。”

因此,尽管没有明确说明,但我99%确信控件是用C ++编写的。

从C#调用C ++时,我不是很精明。给定上面显示的.GetLongData的签名,我正在寻找帮助来找到unsafeIntPtrMarshal.AllocHGlobal等的神奇组合,以获取广播的数据从非托管的C ++ ActiveX控件到托管的C#代码。

非常感谢您的帮助。

0 个答案:

没有答案