CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Calling Functions In VB.NET-Built DLL

  1. #1
    Join Date
    Aug 2007
    Location
    Farnborough, Hants, UK
    Posts
    45

    Calling Functions In VB.NET-Built DLL

    I've created a tiny VB.NET DLL as follows:

    Code:
    Imports System.Runtime.InteropServices
    
    <ClassInterface(ClassInterfaceType.AutoDual)> _
    Public Class MyClass
        Public Function SayHello() As Boolean
            MsgBox("Hello, World!")
            Return True
        End Function
    End Class
    I've set it to generate a TLB file.

    I've now got a C++ (unmanaged) project, and #imported the TLB file with raw interfaces only (if I don't use raw_interfaces_only, it won't compile - for starters it doesn't know what _TypesPtr is).

    If I write this:

    Code:
        MyDLL::_MyClassPtr *pObject = new MyDLL::_MyClassPtr(); 
        if (pObject != NULL) 
            pObject->SayHello();
    the compiler says

    error C2039: 'SayHello' : is not a member of '_com_ptr_t<_IIID>'


    If I hover over _MyClassPtr, the tooltip shows

    typedef _com_ptr_t<_com_IIID<MyDLL::_MyClass, 0x0> > MyDLL::_MyClassPtr

    so as far as I can see it's a more specific type than just '_com_ptr_t<_IIID>'.

    I also tried:

    Code:
        MyDLL::_MyClass Object ;
    but the compiler now says

    error C2259: 'MyDLL::_MyClass' : cannot instantiate abstract class

    Why is it considered abstract? It has a defined method within it, not a pure virtual one, surely? Did the raw_interfaces_only thing cause this? If so, how do I get it to compile from a plain #import of the TLB file?

    Any idea what I'm doing wrong, and how I can correctly instantiate a MyClass object and call its SayHello() method?

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Calling Functions In VB.NET-Built DLL

    You can't use operator new for a com object. Also, your object is a COM smart pointer so you're declaring it wrong. Try this:

    Code:
      MyDLL::_MyClassPtr pObject;
      HRESULT hr;
    
      hr=pObject.CreateInstance(__uuidof(MyDLL::_MyClass));
      if(hr==S_OK){
        pObject->SayHello();
      }

  3. #3
    Join Date
    Aug 2007
    Location
    Farnborough, Hants, UK
    Posts
    45

    Re: Calling Functions In VB.NET-Built DLL

    Quote Originally Posted by hoxsiew View Post
    You can't use operator new for a com object. Also, your object is a COM smart pointer so you're declaring it wrong. Try this:

    Code:
      MyDLL::_MyClassPtr pObject;
      HRESULT hr;
    
      hr=pObject.CreateInstance(__uuidof(MyDLL::_MyClass));
      if(hr==S_OK){
        pObject->SayHello();
      }
    Thanks for this. It certainly resolved the compiler issue. I guess I've become lazy by using generated wrapper C++ classes from TLB files.

    The problem now is that hr comes back 'Class not registered'. I could believe this, because I hadn't done anything to get the details and GUIDs into the registry. I found info online that explained that if I used VB.NET's Add - New Item - COM Class, then it would generate a template that would be registred automatically. So I did this and deleted the old code, so my VB.NET code now looks like this:

    Code:
    <ComClass(MyClass.ClassId, MyClass.InterfaceId, MyClass.EventsId)> _
    Public Class MyClass
    
    #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 = "d51fc555-a773-44ab-aad5-6d742366b1dd"
        Public Const InterfaceId As String = "56325381-e82c-4c85-8319-96f5dbd2753b"
        Public Const EventsId As String = "10795266-8994-4610-b11b-0c5f29f583d7"
    #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
    
        Public Function SayHello() As Boolean
            MsgBox("Hello!")
            Return True
        End Function
    
    End Class

    The problem is, even after a full recompile in case it wasn't completing all steps including registering the COM object, I'm still getting 'Class not registered'. I've checked the registry, and it *does* appear to be there, complete with the GUIDs listed in the COM GUIDS region.

    Any idea what I'm still doing wrong?

    I seem to recall I should be able to use CoCreateInstance() with the L"MyDLL::MyClass" string or something like that, but I don't think I have an IMyClass definition, so that wouldn't work, would it? And I'm guessing it wouldn't be any more likely to succeed.
    Last edited by JTeagle; May 2nd, 2010 at 04:59 AM.

  4. #4
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Calling Functions In VB.NET-Built DLL

    Make sure you have the correct CLSID (I think my example above is wrong in that the underscore should NOT be there--__uuidof(MyDLL::MyClass) v. __uuidof(MyDLL::_MyClass) ). From your example above, you should be using the CLSID for the class ("d51fc555-a773-44ab-aad5-6d742366b1dd" in your example).

    Call it explicitly if needed as a string:
    Code:
    hr=pObject.CreateInstance("d51fc555-a773-44ab-aad5-6d742366b1dd");
    Last edited by hoxsiew; May 2nd, 2010 at 10:51 AM.

  5. #5
    Join Date
    Aug 2007
    Location
    Farnborough, Hants, UK
    Posts
    45

    Talking Re: Calling Functions In VB.NET-Built DLL

    Quote Originally Posted by hoxsiew View Post
    Make sure you have the correct CLSID (I think my example above is wrong in that the underscore should NOT be there--__uuidof(MyDLL::MyClass) v. __uuidof(MyDLL::_MyClass) ).
    Thanks, removing the underscore nailed it. I should have had the brains to try that, lol.

    Thanks very much for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)