Using objects from a DLL in a VB program


I've developed a DLL in C++ Builder containing Objects.
I'm able to use this DLL and the objects in it in another C++ Builder programm, but I would like to use these objects and their methods in a Visual Basic program.
How can I do that?
Since I'm new to VB, could you give some sample code?

I need an answer very quickly, that's why I give 500 points for this question.

Who is Participating?
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
Guy Hengel [angelIII / a3]Billing EngineerCommented:
is the DLL a ActiveX dll or a function DLL (ie do you need to register the DLL or not).
In case of ActiveX, you will be able to add the dll in the project references (which i guess is the case, as you are speaking of Objects). In the other case, you would need to declare functions like you do for API calls...

bonduelAuthor Commented:
I can't add the dll in the project references (I have an error).
I knew I had to declare the functions, but they are encapsulated in objects.

Thank you for posting this comment so quickly...
Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
What is the error, what is the result if you issue this command:
regsvr32 "c:\somefolder\your.dll"

bonduelAuthor Commented:
I tried regsvr32 but I have an error message:
"c:\somefolder\my.dll was loaded, but the DllRegisterServer entry point was not found.
DllRegisterServer may not be exported, or a corrupt version of c:\somfolder\my.dll may be in memory"

Since the second solution is not true, it means that the DllRegisterServer may not be exported...
What does it mean.

Thank you again angellll, even if you don't post an answer, but only some comments, I'll give you the points...
The error you are recieving in registering dll is most probably due to the noavailablity of any depended file

such as if you used any object in your dll then that reference (supporting) dll should be present in system directory

Guy Hengel [angelIII / a3]Billing EngineerCommented:
>DllRegisterServer entry point was not found.
This error indicates that the dll doesn't export that function...
The error when a dependant file isn't present, the error is something else.
See if you can build a setup program for your .dll - and then install it on your machine using this.

Then go and add a reference to this .dll in your VB IDE...

Menu : [Project] - [References]
Select the name of the .dll in the list.

Then in the VB code do something like.

sub main()

Dim MyObj as MyDll


end sub


If you need dependency information for your .dll, use the Dependency Walker. I can send this to you if you like.
Guy Hengel [angelIII / a3]Billing EngineerCommented:
DaveyByrne, if you reread the comments, the Registering of the DLL doesn't work ...
Let's be constructive here.

When you go into your VB IDE - follow the menu to where it lets you add a reference to a .dll.

[Project] - [References]

Instead of choosing your .dll from a list - click the "Browse" button, and try to add a reference to the actual .dll file itself.

Then instantiate your objects as I have shown in the VB code above.

Give it a whirl.
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Then review this comment:
From: bonduel  Date: 06/19/2002 03:15AM PST  
I can't add the dll in the project references (I have an error).
The classes you have created in the DLL are accessable to other C++ programs, but you cannot access them through VB directly.  The calling convention (and naming) is different for C++ and C.  VB uses straight C functions.  So I believe the answer is no, you cannot do what you want, without creating a COM object interface.

What I have done in the past is to create C functions that wrap the C++ classes.  For example, if I had a file object, I would create a simple C function called File().  It would return a long value.  This is actually the pointer to a New C++ class.  For subsequent operations on the file, I passed in this long value (handle).  These operations were also written as a C function.  They would then cast the long value to a class pointer.

Kind of kludgy I guess, but it allowed me to reuse much of the objects, between C++ and VB.
ummm.. I dunno why this is so hard,
wasn't the answer provided earlier by angellll?

bonduel we need some feedback on where you are now.  angellll's suggestion to reveiew a tutorial on COM was a good one.  here's the link again if you missed it:

please respond
bonduelAuthor Commented:
OK, thank you all.
To make simple, I've created a new project and here is what I did :
1) Create a DLL with the DllRegisterServer function etc...
2) Create a new exported class MyClass
3) Add one method to MyClass::MyMethod
4) After compilation, I registered the DLL successfully
5) I created a new VB project and add successfully a reference to the DLL.

But now I can't use MyClass. How can I do? What's wrong?

PS: in Dependecy walker, Here is what I can see:
Export table
Name: MyDLL.dll
Number of Functions: 00000006

Oridnal    Entry Point   Name
0000       00001010      ??0MyClass@@AAE@XZ
0001       00001000      ??4MyClass@@QAEAAV0@@Z
0002       00001020      ?MyMethod@MyClass@@QAEXXZ
0003       00001070      DllCanUnloadNow
0004       00001030      DllGetClassObject
0005       000010a0      DllRegisterServer


Guy Hengel [angelIII / a3]Billing EngineerCommented:
Did you follow all the steps, ie implementing the iunkown interface (AddRef etc)?
Richie_SimonettiIT OperationsCommented:
bonduelAuthor Commented:
I'm sorry, but I would like to make something very simple.
Creating an COM object is not (for the moment) what I want to do.
The idea to create C functions that wrap C++ method sounds like simple.
So I tried this.
I just created a poor little DLL with one exported C function with C++ Builder.
Here is how I declare this method :
extern "C" __declspec(dllexport) int simpleFunc(int i)
{ some code }

when I look at the DLL in Dependency walker, I can see the method exported but with a name begining with "_" (_simpleFunc). So I can't use it in VB!!!

Is it not possible to do something very very simple?
bonduelAuthor Commented:
OK, sorry.
I made a mistake when declaring the function in VB.
Now I'm able to use the method in VB but only if there is no parameters.
For instance if I create a C function like this:

extern "C" __declspec(dllexport) int MyFunc();

then I can use it in VB.
If I create the C function with a parameter:
extern "C" __declspec(dllexport) int MyFunc(int i)

then calling it in VB:

Dim i as Integer
i = MyFunc(10)

I have an error:

Bad DLL calling convention

Any idea?
bonduelAuthor Commented:
Well, I think I have a solution :
In the declaration of the C function I have to add:



extern "C" __declspec(dllexport) int _stdcall MyFunc(int i)

Now it works.
For the moment, it is what I need.
Thank you all for your help.
What about the point, how can I distribute them?

Richie_SimonettiIT OperationsCommented:
Take a look at this:
HOWTO: Make C DLL More Accessible to VB with a Type Library (Q189133);EN-US;q189133
Guy Hengel [angelIII / a3]Billing EngineerCommented:
isn't the c:int -> vb:long ?

Regarding the points, you should post a 0-point q in the community support topic area, with a link to this question.
Ask what you would like to do:
* split points
* close the question

c:int -> vb:int
c:long -> vb:long

(well, depending on the compilers used of course)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.