Title: Using DLLs and The Windows API
1Using DLLs and The Windows API
2DLLs 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
3Windows Application Programmers Interface (API)
- Collection of ready-made functions and procedures
4Windows 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.
5API 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.
6Finding and Using APIs
- Not so simple - search and reference books
- Text API Viewer
- good for correct declarations
- doesnt give real help as to what is does,
returns, etc.
7Using 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.
8- 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.
9- 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.
10- 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.
11Four 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.
12Windows 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
13Declaring 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.
14Private 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
15Window 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.
16Passing Parameters
- Important to declare ByVal if passing by value -
otherwise memory location used as value
17Using Classes with APIs
- Encapsulate functionality
- neater
- TestMM.vbp
18mciSendString 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
)
19Parameters 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.
20Callbacks
- 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)