I have a third party .NET dll which I want to expose to a native C++ dll, so I wrote a wrapper dll in C#; But in the native C++ dll, every time when it excutes to CoCreateInstance(), it returns this -858993460 erroe;
----------------------Below is the structure of the program-----------------
ThorDetectorSwitch.dll(native C++ dll) -> MCLWrapper.dll(COM C# dll) -> mcl_RF_Switch_Controller64.dll(third part .NET dll)
----------------------Below is some o my code-----------------------
The C# wrapper dll (MCLWrapper.dll, COM callable wrapper 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();
}
}
}
The constructor of ThorDetctorSwitch.dll (The native C++ that calls the CCW MCLWrapper.dll):
#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;
}
The command line I used to register MCLWrapper.dll
regasm MCLWrapper.dll /tlb:MCLWrapper.tlb /codebase
and it returns registry successfully.
Error: The error occurs at
hr = ::CoCreateInstance(__uuidof(MCLWrapper::MCLControlClass), NULL,CLSCTX_ALL, __uuidof(MCLWrapper::MCLControl), (void**)&mclSmartPtr );
and I don't think this line is completely excuted.
Anyone has any idea? Thanks a lot.