从C ++使用CoCreateInstance调用COMVisible VB.Net dll [英] Calling COMVisible VB.Net dll from C++ using CoCreateInstance

查看:304
本文介绍了从C ++使用CoCreateInstance调用COMVisible VB.Net dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我上一个问题以来的一些进展的措施,我有以下项目编译。它基于 http://stackoverflow.com/a/10949784/846550

In some measure of progress since my last question I have got the following project to compile. It is based on http://stackoverflow.com/a/10949784/846550

但是在运行时,对CoCreateInstance的调用失败。 HRESULT是0x80131522 - google这个和大多数命中似乎与我不使用的SQL服务器相关。

However at runtime the call to CoCreateInstance fails. The HRESULT is 0x80131522 - google for this and most hits seem to relate to SQL server which I am not using.

C ++代码,非托管

C++ code, unmanaged

#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代码 - 几乎完全自动生成为COM DLL VB类中的类。 (DLL也显示为使用oleview注册)。

Here is the DLL code for good measure - almost completely autogenerated as a COM DLL class in VB. (The DLL shows up as registered using oleview as well).

<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

This is a TypeLoadException, HRESULT 0x80131522 is COR_E_TYPELOAD.

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

What it means is that the CLR could not find your assembly. It needs to be either copied into the same directory as the executable, in the path, or registered in the GAC.

REGASM 说:


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

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.




  • http://msdn.microsoft.com /en-us/library/tzat5yw6(v=vs.71).aspx

    • http://msdn.microsoft.com/en-us/library/tzat5yw6(v=vs.71).aspx
    • 它不会是可创建的,除非它安装在客户端的应用程序目录中,在使用 / codebase 参数注册的路径中,或者安装在GAC中两个都需要强名称。

      I.e. it won't be creatable unless it is either installed in the application directory of the client, in the path, registered with the /codebase argument, or in the GAC, the latter two which both require a strong name.

      这篇关于从C ++使用CoCreateInstance调用COMVisible VB.Net dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆