Creating objects (by assignment) in VB6

  • Thread starter Thread starter Bartholomew Simpson
  • Start date Start date
B

Bartholomew Simpson

I know this is a VB.Net ng. But hopefully, someone remembers some
classic (VB6) coding - besides I've not had any response when I posted
this to the VB6 specific ng.

I wrote a C++ librray that I want to use in VB6. I have exported a C API
(via a win32 DLL) that may be used in VB6 using the 'Declare Function'
syntax.

However, I need to group related functions together (in VB6), by
wrapping them together in a VB6 class.

Assuming CreateNewObject is an exposed C function that returns a pointer
to a C++ object is declared in VB6 as follows:

Declare function CreateNewObject lib "mylib" () as long

Using the plain API (without wrapping the C++ objects in a VB6 class), I
would have code that looks like this :

dim lngPtr as long
lngPtr = CreateNewObject() 'calls Dll function


With the exposed C functions wrapped up in VB6 classes, the code looks
something like this:

dim objVBWrapper as MyVBWrapperClass

// which of these statements are correct?
objVBWrapper = CreateNewObject() //OR
Set objVBWrapper = CreateNewObject()

The key difference here is that I am creating the object by assignment.
Since there is no explicit copy constructor in VB6 I'm not sure how this
may be implemented. Ideas please ...
 
Bartholomew Simpson said:
I know this is a VB.Net ng. But hopefully, someone remembers some
classic (VB6) coding - besides I've not had any response when I
posted this to the VB6 specific ng.

I wrote a C++ librray that I want to use in VB6. I have exported a C
API (via a win32 DLL) that may be used in VB6 using the 'Declare
Function' syntax.

However, I need to group related functions together (in VB6), by
wrapping them together in a VB6 class.

Assuming CreateNewObject is an exposed C function that returns a
pointer to a C++ object is declared in VB6 as follows:

Declare function CreateNewObject lib "mylib" () as long

Using the plain API (without wrapping the C++ objects in a VB6
class), I would have code that looks like this :

dim lngPtr as long
lngPtr = CreateNewObject() 'calls Dll function


With the exposed C functions wrapped up in VB6 classes, the code
looks something like this:

dim objVBWrapper as MyVBWrapperClass

// which of these statements are correct?
objVBWrapper = CreateNewObject() //OR
Set objVBWrapper = CreateNewObject()

The key difference here is that I am creating the object by
assignment. Since there is no explicit copy constructor in VB6 I'm
not sure how this may be implemented. Ideas please ...

VB6 objects are COM/ActiveX objects. The C++ objects are (normally) not, so
you can not assign a pointer to a C++ object to a VB object variable. The
infrastructure is different.

The two options you have is either write COM objects in C++, or export
native functions from C++ and wrap them into a VB6 class. You did export
functions, but what you are trying here is using the members of a C++
object. There is no built-in way to access a C++ object from VB6.


Armin
 
Back
Top