0% found this document useful (0 votes)
90 views

Using Dlls and The Windows Api: Dr. John Nosek

This document discusses using Dynamic Link Libraries (DLLs) and the Windows Application Programming Interface (API) in Visual Basic applications. It provides an overview of DLLs and how the Windows API allows access to ready-made functions. It also describes how to use the API Viewer application to find and copy API declarations, and how to declare and call API functions in Visual Basic code by passing parameters and window handles. Common Windows libraries like Kernel32 and User32 are also listed.

Uploaded by

radhwany5517
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

Using Dlls and The Windows Api: Dr. John Nosek

This document discusses using Dynamic Link Libraries (DLLs) and the Windows Application Programming Interface (API) in Visual Basic applications. It provides an overview of DLLs and how the Windows API allows access to ready-made functions. It also describes how to use the API Viewer application to find and copy API declarations, and how to declare and call API functions in Visual Basic code by passing parameters and window handles. Common Windows libraries like Kernel32 and User32 are also listed.

Uploaded by

radhwany5517
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

Using DLLs and

The Windows API


Dr. John Nosek
DLLs Overview/Review
• Dynamic Link Libraries
– static versus dynamic linking
• Code available at run time
• Smaller executables, large DLLs
• Windows consists of DLLs
• Visual Basic hides many calls to DLLs
• More - VB Help: API Viewer Application
Windows
Application Programmer’s
Interface (API)
• Collection of ready-made functions and
procedures
Windows API cont.
• The file Win32api.txt, located in the \Winapi
subdirectory of the main Visual Basic directory,
contains declarations for many of the Windows
API procedures commonly used in Visual Basic.
To use a function, type, or other feature from this
file, simply copy it to your Visual Basic module.
You can view and copy procedures from
Win32api.txt by using the API Viewer application,
or by loading the file in any text editor.
API Wrappers and Custom
Controls
• Wrap around particular API code with a user
friendly, VB interface
• Plus additional features
• Create own ActiveX Components: Active X
Controls and Automation (formerly OLE
Automation) Servers (next chapter)
• This chapter: enclose some API calls into a
VB program.
Finding and Using APIs
• Not so simple - search and reference books
• Text API Viewer
– good for correct declarations
– doesn’t give real help as to what is does,
returns, etc.
Using the API Viewer Application
The API Viewer application enables you to browse
through the declares, constants, and types included in any text file
or Microsoft Jet database. After you find the procedure you want,
you can copy the code to the Clipboard and
paste it into your Visual Basic application.
You can add as many procedures as you want to your application.
To view an API file
From the Add-Ins menu, open the Add-In
Manager and load API Viewer.

Click API Viewer from the Add-Ins menu.

Open the text or database file you want to view.


To load a text file into the viewer,
click File \ Load Text File and choose the file you
want to view.

To load a database file, click File \ Load Database File.

Select the type of item you want to view from the API Types list.
To add procedures to your Visual Basic code
•Click the procedure you want to copy in the
Available Items list.

•Click Add. The item appears in the Selected Items list.

•Indicate the scope of the item by clicking Public or Private


in the Declare Scope group.

•To remove an entry from the Selected Items list box,


click the item and click Remove.

•To remove all entries from the Selected Items list box,
click Clear.
To copy the selected items to the clipboard

•Click Copy. All of the items in the Selected Items list


will be copied.

•Open your Visual Basic project and go to the module


in which you want to place the API information.

•Position the insertion point where you want to paste


the declarations, constants, and/or types,
and then choose Edit \ Paste.
Four main Windows Libraries
• Kernel32: main DLL, memory
management, multi-tasking, how Windows
actual runs.
• User32: Windows management library:
menus, timers, comms, files, and other non-
display areas of Windows.
Windows Libraries (cont.)
• GDI32: Graphics Device Interface -
drawing and redrawing on the screen.
• WINMM: Multimedia - sound, music, real-
time video, sampling, more (32bit only)
• View in Windows\System directory
Declaring an API Call
• Tell VB:
– The name of the subroutine or function
– Which DLL file it can be found in.
– The parameters it expects to receive.
– The type of value that it can return (if function)
• Use as Procedure call in code.
• Use “Declare” in Procedure Declaration in
General Declarations Section.
Private Declare Function Flash Lib "User32" Alias "FlashWindow" _
(ByVal hWnd As Long, ByVal bInvert As Long) As Long

Flash - name programmer uses in the program


Lib "User32" - tells VB which library to search
Alias "FlashWindow" - tells VB the name used in the library
(only if local declaration different)
hWnd - handle of the window that we want to blink
bInvert - flashing property - True on, False off

Private Sub Timer1_Timer()

Dim nReturnValue As Integer

nReturnValue = Flash(Form1.hWnd, True)

End Sub
Window Handles
• Windows shorthand ID for an object
• read only property - hWnd
• Accessed only at run time
• Pass to API call to identify object to
perform some action.
Passing Parameters
• Important to declare ByVal if passing by
value - otherwise memory location used as
value
Using Classes with APIs
• Encapsulate functionality
• neater
• TestMM.vbp
mciSendString
The mciSendString function sends a command string
to an MCI device. The device that the command is sent to
is specified in the command string.
MCIERROR mciSendString(
LPCTSTR lpszCommand,
LPTSTR lpszReturnString,
UINT cchReturn,
HANDLE hwndCallback
);
Parameters
lpszCommand
Address of a null-terminated string that specifies an
MCI command string. For more information about
the command strings, see Command Strings.
lpszReturnString
Address of a buffer that receives return information.
If no return information is needed, this parameter
can be NULL.
cchReturn
Size, in characters, of the return buffer specified
by the lpszReturnString parameter.
hwndCallback
Handle of a callback window if the "notify" flag
was specified in the command string.
Callbacks
• The API function can reference a procedure
in your code while the API is running.
– Declare function the API will call as Public
– Use “AddressOf” in call to API
– Example:
– Public Function MyCallback…
– nResult = SomeAPIFunction(Param1,
AddressOf MyCallback)

You might also like