CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2008
    Posts
    72

    How to write a COM wrapper?

    I have a DLL developed in C++. I need to write a COM wrapper for it so I can call the DLL functions from VB6.0

    Anyone show me how to start with COM wrapper? I am a beginner in this area. I highly appreciate your help.

  2. #2
    Join Date
    Jul 2009
    Location
    (Presently) Bahrah S.A.
    Posts
    24

    Re: How to write a COM wrapper?

    Private Declare Function SomeFunction Lib "registered_dll_Filename" (Parameters) as return

    You can also declare the function public. Not all functions have returns and the declaration in your code must match the declaration in the DLL.

    Then call the function

    return = SomeFunction(Arguments)

    If you have a lot of functions you want to simplify or you want to make the function calls easier or do other transparent processing for the user you can declare these in a class, write subs, functions, create properties, methods and events for your class then simplify your application code by having a reusable class you just include in your projects. This is actually what most people think of when they think of a Wrapper. You can make function calls directly in your code by declaring the functions of any registered DLL file, if you know the exact function declaration used. This is exactly what API calls are, calls to functions within DLL files.

    Just a quick FYI, you can create DLLs in VB also of commonly used functions, compile it to a DLL and make calls from it the same way.
    Last edited by arkansascontrols; July 22nd, 2009 at 12:05 AM.

  3. #3
    Join Date
    Feb 2008
    Posts
    72

    Re: How to write a COM wrapper?

    It is my first time so really, it's tough.

    My C++ dll has functions that returns an object (not a plain data such as string or int)

    in my wrapper, how to I call into these functions so that I can have the returned object?

    Example:

    Code:
    CSomeObject CMyDLLClass::MyDLLFunction()
    {
        CSomeObject returnObj;
        // stuff done here..
       return returnObj;
    }
    That code, how can a Visual Basic wrapper call into MyDLLFunction() so it can also access returnObj which is of type CSomeObject

  4. #4
    Join Date
    Jul 2009
    Location
    (Presently) Bahrah S.A.
    Posts
    24

    Re: How to write a COM wrapper?

    I hate to even comment because I really don't know C++ code, but from the looks of what you have posted there your MyDLLFunction isn't returning a value. If your MyDLLFunction doesn't provide a return value then you can't get a return value. Now maybe I'm putting my foot in my mouth because I don't under the C syntax. But for example if you want to define a variable of type object and set equal to the return value of a function then you would first have to have a function that returned an object.

    Function MyDLLFunction() as Object
    'code here
    End Function


    then you could call the function like this

    Dim myX as object
    Set myX = MyDLLFunction

    The same is true for external dll calls

    So Declare the function like this
    Private(or public) Declare Function MyDLLFunction LIB "MyRegisteredDLL" () as Object

    Then call the function in your code like this

    Dim myX as object
    Set myX = MyDLLFunction

    Again, not understanding the C syntax I may be incorrect about your function returning a value, but if it returns an Object AND the function is a public function of the DLL then you can declare it and call it as shown.

    By the way, I'm pretty sure that you have to stipulate a full path to the DLL file if it isn't in the Windows Search Path. You can look at your Path Environment variable and see if the directory your DLL is in is in there if not then you will have to give a full path to it in your declaration.

  5. #5
    Join Date
    Jul 2009
    Location
    (Presently) Bahrah S.A.
    Posts
    24

    Re: How to write a COM wrapper?

    Oh by the way, if the DLL was compiled as an ActiveX dll then you don't have bother with any of this mess you can just include the DLL as a reference in your VB Project and any public properties, methods will show up in your Intellisense drop down.

    In that case you simply include the reference then declare an object of type (ObjectClass).

    Dim myX as WhateverObjectClass
    Set myX = new WhateverObjectClass

    If there are events declared in the Class that you want to utilize then you have to Define it like this

    In your Declaration section of Form or Module
    Public / Private WithEvents myX as WhateverObjectClass

    Then you can set the variable equal to a new instance of the object like this

    Set myX = new WhateverObjectClass

    Then the events will appear in the code window under that object and properties / Methods of the object appear like subelements when you type myX with a period you'll get a list of exposed Properties / Methods (Your Function would appear as a method in the drop down)

  6. #6
    Join Date
    Feb 2008
    Posts
    72

    Re: How to write a COM wrapper?

    The DLL that I want to make a wrapper is a C++ dll. The wrapper should be callable from VB.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to write a COM wrapper?

    pinoywebdev, I moved your post from the Visual C++ forum because I already thought you had written a COM wrapper and needed to know how to call it in VB6.

    Probably the easiest way to wrap provide a wrapper around a dll is to use ATL in Visual C++ and create a simple COM server object.

    Note that you aren't going to be able to directly wrap a C++ class via COM, nor can you return a C++ object.

  8. #8
    Join Date
    Feb 2008
    Posts
    72

    Re: How to write a COM wrapper?

    "...nor can you return a C++ object. " by this you mean, C++ DLL called from VB should only return plain data types and not object types?

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to write a COM wrapper?

    Quote Originally Posted by pinoywebdev View Post
    "...nor can you return a C++ object. " by this you mean, C++ DLL called from VB should only return plain data types and not object types?
    Yes, that's right. Internally within the C++ DLL, you can use a class (or classes) to implement the functionality, but you can only export functions.

    You can export a C++ class; however, consuming the class in another C++ application is very restrictive. Typically, you need to build both the exe and the dll with the same compiler. Even a different version of compiler can break you. This is because of the C++ name mangling that occurs during the export process. Since there isn't any standard a new version of compiler could produce different name mangling. It's the same for different compiler vendors.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured