C ++ COM CoCreateInstace()返回-858993460

时间:2013-06-17 13:35:12

标签: c# c++ dll com ccw

我有第三方.NET dll,我希望将其暴露给本机C ++ DLL,所以我在C#中编写了一个包装器dll;但是在原生的C ++ DLL中,每当它出现在CoCreateInstance()中时,它都会返回-858993460错误;

----------------------以下是程序结构----------------- < / p>

ThorDetectorSwitch.dll(原生C ++ dll) - &gt; MCLWrapper.dll(COM C#dll) - &gt; mcl_RF_Switch_Controller64.dll(第三部分.NET dll)

----------------------下面是我的代码------------------- ----

C#包装器DLL(MCLWrapper.dll,COM可调用包装器dll):

// C# COM wrapper for mcl_RF_Switch_Controller64.dll 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using mcl_RF_Switch_Controller64;
using System.Runtime.InteropServices;
// for function reference see miniCircuit RF controller manual

namespace MCLWrapper
{
    [Guid("727C569D-09AF-472c-8032-2AC9BC7CDC30")]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    [ComVisible(true)]
    public interface MCLControl
    {
        [DispId(1)]
        void Connect(string SerialNumber);

        [DispId(2)]
        void Set_Switch(string SwitchName, int Val);

        [DispId(3)]
        void Set_SwitchesPort(byte binVal);

        [DispId(4)]
        void GetSwitchesStatus(int statusRet);

        [DispId(5)]
        void Disconnect();
    };

    [Guid("68A7F8A1-6347-4bb1-9809-EE18E1E9BDD6")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("MCLWrapper.MCLControlClass")]
    public class MCLControlClass : MCLControl
    {
        public USB_RF_SwitchBox _sb = new USB_RF_SwitchBox();

        //public MCLControlClass() { }    
        public void Connect(string SerialNumber)
        {
            _sb.Connect(ref SerialNumber);
        }

        public void Set_Switch(string SwitchName, int Val)
        {
            _sb.Set_Switch(ref SwitchName, ref Val);
        }

        public void Set_SwitchesPort(byte binVal)
        {
            _sb.Set_SwitchesPort(ref binVal);
        }

        public void GetSwitchesStatus(int statusRet)
        {
            _sb.GetSwitchesStatus(ref statusRet);
        }

        public void Disconnect()
        {
            _sb.Disconnect();
        }
    }
}

ThorDetctorSwitch.dll的构造函数(调用CCW MCLWrapper.dll的本机C ++):

#import "../MCLWrapper/MCLWrapper/bin/Debug/MCLWrapper.tlb" raw_interfaces_only

using namespace MCLWrapper;

MCLWrapper::MCLControl *_mcSwitch;

ThorDetectorSwitch::ThorDetectorSwitch()
{
    HRESULT hr = CoInitialize(NULL);
    MCLWrapper::MCLControlPtr mclSmartPtr;
    hr = ::CoCreateInstance(__uuidof(MCLWrapper::MCLControlClass), NULL,CLSCTX_ALL, __uuidof(MCLWrapper::MCLControl), (void**)&mclSmartPtr    );
    _mcSwitch = mclSmartPtr;

    _A  = WstringToBSTR(L"A"); 
    _B  = WstringToBSTR(L"B");
    _C  = WstringToBSTR(L"C");
    _D  = WstringToBSTR(L"D");

    _deviceDetected = FALSE;
}

我用来注册MCLWrapper.dll的命令行

regasm MCLWrapper.dll /tlb:MCLWrapper.tlb /codebase

并成功返回注册表。

错误: 错误发生在

hr = ::CoCreateInstance(__uuidof(MCLWrapper::MCLControlClass), NULL,CLSCTX_ALL, __uuidof(MCLWrapper::MCLControl), (void**)&mclSmartPtr    );

我不认为这条线是完全可行的。

任何人都有任何想法?非常感谢。

0 个答案:

没有答案