【问题标题】:Calling COMVisible VB.Net dll from C++ using CoCreateInstance使用 CoCreateInstance 从 C++ 调用 COMVisible VB.Net dll
【发布时间】:2012-06-24 15:56:59
【问题描述】:

自从我上一个问题以来,在某种程度上,我已经编译了以下项目。它基于https://stackoverflow.com/a/10949784/846550

但是在运行时调用 CoCreateInstance 会失败。 HRESULT 是 0x80131522 - 谷歌为此而大多数点击似乎与我没有使用的 SQL 服务器有关。

C++ 代码,非托管

#include <iostream>
#include <atlbase.h>
#import "..\ClassLibrary1\bin\debug\ClassLibrary1.tlb" no_namespace

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    ATLENSURE_SUCCEEDED(CoInitialize(NULL));

    CLSID clsid;
    _ComClass1 *t; 
    ATLENSURE_SUCCEEDED(::CoInitialize(NULL));
    ATLENSURE_SUCCEEDED(CLSIDFromProgID(OLESTR("ClassLibrary1.ComClass1"), &clsid));

    HRESULT hr = CoCreateInstance(clsid,NULL,CLSCTX_INPROC_SERVER,
                                   __uuidof(_ComClass1),(LPVOID*) &t);
    ATLENSURE_SUCCEEDED(hr);

    ATLENSURE_SUCCEEDED(t->showform());
    ATLENSURE_SUCCEEDED(t->increment());

    CoUninitialize();
    return 0;
}

这里是很好的测量的 DLL 代码 - 在 VB 中几乎完全自动生成为 COM DLL 类。 (DLL 也显示为使用 oleview 注册的)。

<ComClass(ComClass1.ClassId, ComClass1.InterfaceId, ComClass1.EventsId)> _
Public Class ComClass1

#Region "COM GUIDs"
    ' These  GUIDs provide the COM identity for this class 
    ' and its COM interfaces. If you change them, existing 
    ' clients will no longer be able to access the class.
    Public Const ClassId As String = "46783a4a-5ca9-4930-addb-cde91a377c02"
    Public Const InterfaceId As String = "e2ad7298-7339-45fb-aad1-cb82188bf067"
    Public Const EventsId As String = "b7e10cdf-dc2a-4052-9b0f-f6f9d5a1f0ac"
#End Region

    ' A creatable COM class must have a Public Sub New() 
    ' with no parameters, otherwise, the class will not be 
    ' registered in the COM registry and cannot be created 
    ' via CreateObject.
    Public Sub New()
        MyBase.New()
    End Sub


    Private f1 As Form1

    Public Sub showform()
        f1 = New Form1()
        f1.Show()
    End Sub

    Public Sub increment()
        f1.ProgressBar1.Increment(5)
    End Sub

End Class

【问题讨论】:

    标签: c++ vb.net dll com


    【解决方案1】:

    这是TypeLoadException,HRESULT 0x80131522 是COR_E_TYPELOAD

    这意味着 CLR 找不到您的程序集。它需要复制到与可执行文件相同的目录、路径中或在 GAC 中注册。

    REGASM 的文档说:

    使用 Regasm.exe 注册程序集后,您可以将其安装在全局程序集缓存中,以便可以从任何 COM 客户端激活它。如果程序集仅由单个应用程序激活,您可以将其放在该应用程序的目录中。

    即除非它安装在客户端的应用程序目录、路径中、使用/codebase 参数注册,或者安装在 GAC 中(后两者都需要强名称),否则它将无法创建。

    【讨论】:

      【解决方案2】:

      FWIW,使用 /codebase 参数不需要强命名。

      【讨论】:

      • 对不起。它真的回答了这个问题吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多