• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

Using objects from a DLL in a VB program

Hello,

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.

Thanx
0
bonduel
Asked:
bonduel
  • 8
  • 6
  • 2
  • +4
1 Solution
 
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...

CHeers
0
 
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...
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
What is the error, what is the result if you issue this command:
regsvr32 "c:\somefolder\your.dll"

CHeers
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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...
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
0
 
kafzaalCommented:
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


0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
kafzaal:
>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.
0
 
DaveyByrneCommented:
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

    MyObj.DoSomething

end sub

also.....

If you need dependency information for your .dll, use the Dependency Walker. I can send this to you if you like.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
DaveyByrne, if you reread the comments, the Registering of the DLL doesn't work ...
0
 
DaveyByrneCommented:
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.
0
 
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).
0
 
rmichelsCommented:
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.
0
 
ebosscherCommented:
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:

http://www.codeguru.com/activex/COMStepByStep.html

please respond
0
 
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

.....



0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Did you follow all the steps, ie implementing the iunkown interface (AddRef etc)?
CHeers
0
 
Richie_SimonettiIT OperationsCommented:
hearing...
0
 
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?
0
 
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?
0
 
bonduelAuthor Commented:
Well, I think I have a solution :
In the declaration of the C function I have to add:

_stdcall

ex:

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?

0
 
Richie_SimonettiIT OperationsCommented:
Take a look at this:
HOWTO: Make C DLL More Accessible to VB with a Type Library (Q189133)
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q189133

http://support.microsoft.com/support/kb/articles/Q153/5/86.asp
0
 
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

CHeers
0
 
ebosscherCommented:
c:int -> vb:int
c:long -> vb:long

(well, depending on the compilers used of course)
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 8
  • 6
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now