• Using a DLL procedure from Visual Basic is not much different from calling
a general basic function or procedure. Just make sure you pass it the correct
number and correct type of arguments. Say DLLFcn is a DLL function and DLLProc
is a DLL procedure. Proper syntax to invoke these is, respectively (ignoring arguments
for now):
ReturnValue = DLLFcn()
Call DLLProc()
• Before you call a DLL procedure, it must be declared
in your Visual Basic program using the Declare statement. Declare statements go
in the general declarations area of form and code modules. The Declare statement
informs your program about the name of the procedure, and the number and type
of arguments it takes. This is nearly identical to function prototyping in the
C language. For a DLL function (DLLFcn), the syntax is:
Declare Function DLLFcn Lib DLLname [(argument list)] As type
where DLLname is a string specifying the name of the DLL file
that contains the procedure and type is the returned value type.
For a procedure (DLLProc), use:
Declare Sub DLLProc Lib DLLname [(argument list)]
In code modules, you need to preface the Declare statements
with the keywords Public or Private to indicate the procedure scope. In form modules,
preface the Declare statement with Private, the default (and only possible) scope
in a form module.
• Nearly all arguments to DLL procedures are passed by
value (use the ByVal keyword), so the argument list has the syntax:
ByVal argname1 As type, ByVal argname2 As type, ...
Again, it is very important, when calling DLL procedures, that
the argument lists be correct, both regarding number and type. If the list is
not correct, very bad things can happen.
• And, it is critical that the Declare statement be exactly
correct or very bad things can happen. Fortunately, there is a program included
with Visual Basic called the API Text Viewer, which provides a complete list of
Declare statements for all API procedures. The viewer is available from the Start
Menu folder for Visual Basic 6.0 (choose Visual Basic 6.0 Tools folder, then API
Text Viewer). Most of the Declare statements are found in a file named win32api.txt
(load this from the File menu).

Always use this program to establish Declare statements for
your DLL calls. The procedure is simple. Scroll through the listed items and highlight
the desired routine. Choose the scope (Public or Private ). Click Add to move
it to the Selected Items area. Once all items are selected, click Copy. This puts
the desired Declare statements in the Windows clipboard area. Then move to the
General Declarations area of your application and choose Paste from the Edit menu.
The Declare statements will magically appear. The API Text Viewer can also be
used to obtain any constants your DLL routine may need.
• To further confuse things, unlike Visual Basic routine
names, DLL calls are case-sensitive, we must pay attention to proper letter case
when accessing the API.
• Lastly, always, always, always save your Visual Basic
application before testing any DLL calls. More good code has gone down the tubes
with GPF’s - they are very difficult to recover from. Sometimes, the trusty
on-off switch is the only recovery mechanism.