Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exposing an ISO C++ class to a VB6 application

Tags:

c++

class

com

vb6

I have developed a C++ class to access the software protection dongle on USB. The C++ class has been developed with g++ under Linux, but I can compile it successfully using MinGW under Windows. I have a scientific application which is built with VB6 (Visual Basic 6).

The question is how can I expose my C++ class to a VB6 app? What are the possible ways to do it? Do I need to use COM? (Well, a "Microsoft-less" solution is more preferable:)

Your help is much appreciated!

Update 1. Reading your comments I realized I need to implement a COM wrapper to the C++ class. Are there any good and recent examples around? Can I implement the COM wrapper using MinGW (and avoiding MSVC)?

Update 2. Finally, I decided to offer a bounty for this question. Below are the things I would like to understand better:

1. I realized I need to implement a COM wrapper to my C++ class. Can someone please provide me with working example on how to make this for a class like this:

class ValueMapper
{
public:
    ValueMapper( ) { }

    ValueMapper( double fmin, double fmax, int ilength ) {
        SetMapping( fmin, fmax, ilength );
    }

    inline double GetMin() { return min; }
    inline double GetMax() { return max; }
    inline int GetLength() { return length; }

    virtual inline void SetMapping( double fmin, double fmax, int ilength )
    {
        min = fmin;
        max = fmax;
        length = ilength;
    }

    virtual inline int MapValue( double value ) {
        double factor = length / (max - min);
        return (int)RoundTo( (value-min) * factor );
    }

    static double RoundTo( double value, double eps = 1 ) {
        return floor(value/eps + 0.5) * eps;
    }

protected:
    double  min;
    double  max;
    int     length;
};

2. How to use the resulting COM wrapper from the VB6? Do I need to register a COM server or so? Can I use it without registering?

3. Optional. Is it possible to implement a COM wrapper using MinGW?

like image 882
ezpresso Avatar asked Apr 07 '11 13:04

ezpresso


2 Answers

To make access to a COM C++ class from Visual Basic you will need to specify its interface in the meta-language known as IDL. It will be used to generate type library that will be read by Visual Basic and class skeleton in C++ to be implemented like i.e. delegating calls to a ValueMapper object. It is done by the tool called midl.exe. It comes with Windows Platform SDK or any VC++ environment. If you do not own one you can download it along with VC++ 10 Express Edition for free.

The IDL file for you object could look like:

import "oaidl.idl"; 
[
 uuid(C6907FD4-9F56-499A-A784-6168AB3352D6),
 version(1.0),
 local,
 oleautomation
]
interface IValueMapper : IDispatch
{
  [id(1), propget] double Min();
  [id(2),propget] double Max();
  [id(3),propget] int Length();
  [id(4)] void SetMapping( [in] double fmin, [in] double fmax, [in] int ilength );
  [id(5)] int MapValue([in] double value);
  [id(6)] double RoundTo( [in] double value,[in,defaultvalue(1)] double eps );
};

[
  uuid(1C110E43-A56F-41A2-8052-EF85FF96082F),
  version(1.0),
  helpstring("Value Mapper Library"),
] library ValueMapperLib
{
  importlib("stdole32.tlb");
  interface IValueMapper;
  [
  uuid(D1A2F830-994E-4495-A9C3-1440155578A9),
  helpstring("Value Mapper Component Class")
  ] 
  coclass ValueMapperClassObject
  {
    [default] interface IValueMapper;
  }; //end coclass def
};

Now you can provide the required functionality by deriving a class from IValueMapper and implement its functionality by using a ValueMapper member. Unfortunately you will also need to provide some boilerplate code to implement the class object, and IUnknown and IDispatch interfaces as well as the DLL registration code. You can find relevant code snippets on the Internet i.e., on the pages like:

http://www.dcl.hpi.uni-potsdam.de/LV/Components04/VL5/MSDN/DrGUI-on-COM.html http://www.codeproject.com/KB/COM/simplecomserver.aspx

Also I have assembled a VC++ 10 complete project that implements this interface so let me know if you would like to take a look at it.

like image 83
jszpilewski Avatar answered Nov 01 '22 16:11

jszpilewski


There are two possible ways I am aware of:

1) Write a COM wrapper around your class and access it directly from VB6.

2) Make a C interface of your class (by "flattening it") and expose it in a plain DLL.

The former approach is more common, but it sounds like you might prefer the latter.

like image 5
Nemanja Trifunovic Avatar answered Nov 01 '22 18:11

Nemanja Trifunovic