Solved

Using objects from a DLL in a VB program

Posted on 2002-06-19
22
207 Views
Last Modified: 2010-05-02
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
Comment
Question by:bonduel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 2
  • +4
22 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7092220
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
 

Author Comment

by:bonduel
ID: 7092231
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
 
LVL 143

Expert Comment

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

CHeers
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:bonduel
ID: 7092337
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
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 7092352
0
 

Expert Comment

by:kafzaal
ID: 7092449
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7092464
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
 
LVL 1

Expert Comment

by:DaveyByrne
ID: 7092515
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7092538
DaveyByrne, if you reread the comments, the Registering of the DLL doesn't work ...
0
 
LVL 1

Expert Comment

by:DaveyByrne
ID: 7092578
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7092583
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
 
LVL 3

Expert Comment

by:rmichels
ID: 7092585
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
 
LVL 6

Expert Comment

by:ebosscher
ID: 7092716
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
 

Author Comment

by:bonduel
ID: 7092789
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7092808
Did you follow all the steps, ie implementing the iunkown interface (AddRef etc)?
CHeers
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7093327
hearing...
0
 

Author Comment

by:bonduel
ID: 7095129
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
 

Author Comment

by:bonduel
ID: 7095453
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
 

Author Comment

by:bonduel
ID: 7095460
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7096597
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7097902
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
 
LVL 6

Expert Comment

by:ebosscher
ID: 7098893
c:int -> vb:int
c:long -> vb:long

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

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question