我有一个第三方 .NET dll,我想将它公开给原生 C++ dll,所以我用 C# 编写了一个包装 dll;但是在原生 C++ dll 中,每次执行到 CoCreateInstance() 时,都会返回这个 -858993460 错误;
----------------------下面是程序的结构-----------------
ThorDetectorSwitch.dll(本机 C++ dll)-> MCLWrapper.dll(COM C# dll)-> 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 );
我不认为这条线是完全执行的。
有人有什么想法吗?非常感谢。