1

I'm a bit mystified as to how to do this. I have followed instructions on Best way to use a VB.NET class library from a C++ DLL? but being new to VB I don't know if I've got the VB part right let alone the C++. If anyone can help me here I'll correct the code below for posterity's sake!

Here they are: VB first; the project is a Class Library, all settings default except that "Register for COM interop" is switched on in the project properties.

Public Class Class1
    Public Sub New()
        'do stuff
    End Sub

    Public Sub increment()
        'do stuff
    End Sub
End Class

And here's the C++, it's a console application, with CLR support switched on:

#include <iostream>
#import "..\ClassLibrary1\bin\Debug\ClassLibrary1.tlb" raw_interfaces_only

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    cout << "hello world" << endl;
    HRESULT hr = CoInitialize(NULL);
    long lResult = 0;

    Class1 c1; //compiler fails on this, doesn't know what Class1 is
    c1.increment();

    //wait for console key press then exit
    char x;
    cin >> x;
    CoUninitialize();
    return 0;
}

Specific questions:

  1. is the VB correct? Do I need to add anything like an interface to it?
  2. Assuming I want to call it over COM not C++/CLI, how do I do that. (This seems like the logical choice as the client already calls other stuff over COM; however, I'm not sure where to get the IDispatch pointer from, in my other code it's passed to me by the client).
  3. If I went the C++/CLI route, when moving up from toy project to actual implementation, that would mean changing my existing C++ code from "no clr support" to "/clr" - is that likely to break it?
Community
  • 1
  • 1
Sideshow Bob
  • 4,566
  • 5
  • 42
  • 79

1 Answers1

0

For the future reference of anyone as confused as I was when I wrote this question. I went the COM route (not C++/CLI as that would have involved changing settings on an existing project). Correct code here:

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

Community
  • 1
  • 1
Sideshow Bob
  • 4,566
  • 5
  • 42
  • 79