VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

VB .NET - Learn Visual Basic .NET

Systems Analysis - System analysis and Design tutorial for Software Engineering

Browse Topics

- Getting started
- Data Types
- Modules
- Operators in VB6
- VB6 Variable
- VB6 Procedures
- VB6 Control Structures
- Loops in VB6
- VB6 Exit Do & With End With
- Arrays in VB6
- User-Defined Data Types
- VB6 Constants
VB6 Built-in Functions
- Date and Time in VB6
- VB6 Controls
- TextBox Control
- ComboBox & OptionButton
- Label & Frame
- PictureBox & ImageBox
- Timer Control
- ListBox & ComboBox
- VB6 ScrollBar
- Control Arrays in VB6
- Files controls in VB6
- VB6 CheckBox
- Forms in VB6
- Menus in VB6
- MDI Form in VB6
- InputBox
- MessageBox
- Mouse events
- Mouse Move
- Error Handling
Error Handling (2)
VB6 Database

You are here: Visual Basic > VB6 (Beginners Tutorial)

Tutorial Main Page | Previous Page | Contents | Next Page

Accessing the Windows API With DLL

• 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.


Tutorial Main Page | Previous Page | Contents | Next Page


Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved