gpt4 book ai didi

c++ - 使用 CoCreateInstance 从 C++ 调用 COMVisible VB.Net dll

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:31:51 26 4
gpt4 key购买 nike

自从我上一个问题以来,在某种程度上取得了进展,我有以下项目需要编译。它基于 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

最佳答案

这是一个TypeLoadException,HRESULT 0x80131522 是COR_E_TYPELOAD

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

REGASM 的文档说:

After registering an assembly using Regasm.exe, you can install it in the global assembly cache so that it can be activated from any COM client. If the assembly is only going to be activated by a single application, you can place it in that application's directory.

即它不会被创建,除非它安装在客户端的应用程序目录中,在路径中,用 /codebase 参数注册,或者在 GAC 中,后两者都需要一个强大的姓名。

关于c++ - 使用 CoCreateInstance 从 C++ 调用 COMVisible VB.Net dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11154708/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com