CoCreateInstance() -858993460 (in comip.h) C++

本文关键字:C++ comip -858993460 CoCreateInstance in | 更新日期: 2023-09-27 18:18:24

我将花一点时间解释我的项目结构:

有三个 dll:

  1. mclController.dll - 用C#编写的第三方dll来控制硬件。
  2. MCLWrapper.dll - 我用 C# 编写了这个 ll,以便它将作为 COM 向本机C++ dll 公开 mclControl.dll。
  3. ThorDetectorSwitch.dll - 我用本机C++写了这个dll。

结构:

  • ThorDetectorSwitch.dll调用MCLWrapper.dll它包装了mclController.dll。
  • 我正在C++TDSTest中实现一个小型测试控制台应用程序.exe以调用ThorDetecttorSwitch.dll。

所以它基本上是这样的:TDSTest.exe -> ThorDetectorSwitch.dll -> MCLWrapper -> mclController.dll

一些代码:

TDSTest.exe(Windows控制台应用程序,使用x64配置构建(如何调用ThorDetectorSwitch.dll:

#include "stdafx.h"    
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <math.h>
#include <windows.h>
#include "TDSTest.h"
typedef long (*TDSFindDevices)(long&);
typedef long (*TDSGetParam)(const long, double&);
typedef long (*TDSTeardownDevice)();
typedef long (*TDSStartPosition)();
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
        if (argc < 2) 
        {
             cout<<"This is ThorDetecttorSwitch test program."<<endl;
             return 1;  
        }   
        HINSTANCE hInst = LoadLibrary(_T(".''Modules_Native''ThorDetectorSwitch.dll"));
        if( hInst == NULL )
        {
            DWORD err = GetLastError();
            cout<<"Error loading ThorDetectorSwitch.dll. Program exiting..."<<endl;
            return 1;
        }
}

-雷神探测器开关的构造函数.dll编辑! 于 06/15/2013, 中部时间 19:41

ThorDetectorSwitch::ThorDetectorSwitch() :_mcSwitch(ComHelper(__uuidof(MCLControlClass)))
{
    CoInitialize(NULL);
    MCLWrapper::MCLControlPtr mclSmartPtr;
    HRESULT hr = CoCreateInstance(__uuidof(MCLWrapper::MCLControlClass), NULL, CLSCTX_ALL, __uuidof(MCLWrapper::MCLControl), (void**)&mclSmartPtr); // program breaks right here!!!
    _mcSwticth = mclSmartPtr;
    _A  = WstringToBSTR(L"A"); 
    _B  = WstringToBSTR(L"B");
    _C  = WstringToBSTR(L"C");
    _D  = WstringToBSTR(L"D");
    _deviceDetected = FALSE;
}

制作 COM 对象的 MCLWrapper

// C# COM wrapper 
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("7C312A7C-2E77-4de7-A76F-990F268AB818")]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    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("BEC33A1D-BB98-4332-B326-92D480ECC246"), 
    ClassInterface(ClassInterfaceType.None)]
    public class MCLControlClass : MCLControl
    {
        private USB_RF_SwitchBox _sb = new USB_RF_SwitchBox();
        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();
        }
    }
}

我的问题:

当执行 TDSTest 时,它首先命中

HINSTANCE hInst = LoadLibrary(_T(".''Modules_Native''ThorDetectorSwitch.dll"));

然后它在以下位置中断: hr = CoCreateInstance(......)在 ThorDetectorSwitch 中.cpp

hr = -858993460是回报;

一些附加内容

  1. 我被告知这是因为CoInitialized()没有被调用,这就是原因,但我觉得这不是原因,因为这个 ThorDetectorSwitch.dll 与另一个应用程序完美配合,我相信我已经在我的代码中调用了CoInitialized()
  2. 我已经注册了我的MCLWrapper.dll regasm MCLWrapper.dll /tlb:MCLWrapper.tlb /codebase
  3. 调试器输出:"尝试在操作系统加载程序锁定内托管执行。不要尝试在 DllMain 或图像初始化函数中运行托管代码,因为这样做可能会导致挂起的应用程序。

所以现在我不知道我应该朝哪个方向走,我已经在这个问题上挣扎了好几天。所以我真的希望有人能为我提供一些指导。谢谢!

CoCreateInstance() -858993460 (in comip.h) C++

您需要懒惰地构造对象,而不是将其作为在 DLL 加载时创建的全局变量。

也许你可以让你的DLL提供一个由客户端调用的Initialize()函数?当然,假设你不能让你的对象"根本不全局"。