Solved

Using objects from a DLL in a VB program

Posted on 2002-06-19
22
197 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
  • 8
  • 6
  • 2
  • +4
22 Comments
 
LVL 142

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 142

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
 

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 142

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 142

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 142

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 142

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 142

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 142

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now