Advertisement

VB and DLLs

Started by October 05, 1999 04:49 PM
0 comments, last by Akura 25 years, 2 months ago
Hi

I'm having the following problem:

I have this function : GetNumber (char[80] string, int number); in a dll named Funcs.dll
this dll was done in vc and I now need to use it in vb, can someone help me ?

------------------
Bruno Sousa aka Akura
Founder and programmer - Magick.pt
magick_pt@geocities.com
http://magickpt.cjb.net

It's good to be an outcast, you don't need to explain what you do, you just do it and say you don't belong there.
It is pretty easy actually, in a module (*.bas) in the declarations section define your function like so....

Declare Function SomeFunction Lib "mydll.dll" (ByVal param1 as Integer, param2 as String) as Integer

Declare {Function|Sub} (Name of Sub) Lib "(name of dll)" [(parameters)] [as (type)]

{ | } = required but can be either or
[ ] = not required
( ) = required but may vary depending on the situation

That is the basic declaration, but let me warn you. If the dll isn't in the path (i.e. the windows\system folder) it won't find it, vb is very stingy about this for some reason, you need to specify the exact path. (Anyone out there know why this is?) So you need to name the exact path of the dll in order for vb to find it. If it is in a folder called project you would change the part "mydll.dll" to "c:\project\mydll.dll".

In the parameter list you will need to convert your C types into VB types, most will convert to long, somewhere in the help there is a list of what turns into what. The ByVal, if you didn't know, alters the way the parameter is passed. Normally VB passes by reference, this forces it to pass by value (like C normally does).

For your function I believe the C equivalent would be...

Declare Sub GetNumber Lib "path\Funcs.dll" (MyString as String, ByVal number as Long)

I could be wrong on the string conversion because the string in your function is of a definite size (try "MyString as String * 80" if it doesn't work). One last note about what I did, I changed the parameter named string to MyString. You can do this since the dll doesn't store the name of the parmeters, just the types. Since string is a keyword in vb, I changed it to MyString.

Now it should work...

If that doesn't work check the following...

First of all the answer differs if it was you who made the dll or am using someone elses.

If you made the dll here is what has to be done....

First make sure that all the functions you want to use in vb have the modifier "_stdcall"

Ex: void _stdcall somefunction();
(note the following would also be fine because they are the same)
void WINAPI somefunction(); //..or..
void PASCAL somefunction();

All of the above work, what this does is tell the compiler to compile the functions according to the standard calling convention used by most programming languages (C uses the "C calling convetion" as does C++)

Now thats done, time to tell the compiler what functions need to be sent to the dll client. Use a *.def file. VC doesn't have an express way of creating one, so your going to have to do it manually. Create a txt file but force it to use the def extention. Even if you don't tell it to use the def file it will automatically. You can also name it anything you like, and it will still use it (so long as it has the .def at the end) In the def file use the following format...

EXPORTS
FirstFunction @ 1
SecondFunction @ 2

Keeping listing them like this(just replace FirstFuction with the name of the first function you want in the dll), you don't have to give it the parameters to the function, just the name. (note the caps on EXPORTS it is needed for it to work, a def is case sensitive) One other thing, you don't need to include all the functions you made in the def file, just the ones you want other programs to use.

That covers it, if you didn't make the dll and you defined it in vb like the above try other types. If the person didn't use the _stdcall modifier on the functions then put the functions in a def file it won't work.

Quick question to those that know, why is it when you try to do dll's in VC the easy way with "__declspec(dllexport)" in combination with _stdcall it doesn't work? I would really like to know, it would make at least my life a little easier.

-Omalacon

PS I wrote this up pretty quickly so if I left anything out that you have a question on just reply.

[This message has been edited by Omalacon (edited October 05, 1999).]

This topic is closed to new replies.

Advertisement