Calling COMVisible VB.Net dll from C++ using CoCreateInstance

2

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

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++ 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;
}

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
c++
vb.net
dll
com
asked on Stack Overflow Jun 22, 2012 by Sideshow Bob • edited May 23, 2017 by Community

2 Answers

1

This is a TypeLoadException, HRESULT 0x80131522 is COR_E_TYPELOAD.

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.

The documentation for REGASM says:

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.

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.

answered on Stack Overflow Jun 22, 2012 by Ben • edited Jun 22, 2012 by Ben
0

FWIW, using the /codebase argument DOES NOT require strong naming.

answered on Stack Overflow Oct 2, 2013 by The Great KL

User contributions licensed under CC BY-SA 3.0