MIS288 Lecture 9

Report
Windows Software Development
Lecture 9
MIS288
Instructor – Larry Langellier
Where Are We?

Last Lecture
• Creating an ActiveX Control
• Create an ActiveX control from constituent
controls
• Understand design time and run time when
developing ActiveX controls
• Create ActiveX controls that support Property
Pages

Tonight – Understanding the Windows
Application Programming Interface
• Programming the Windows Registry
• Dynamic Link Libraries
• Executing other programs from VB
Introduction to the Registry




Replacement for .ini files
Stores machine hardware and software
configuration
User list
… and just about anything else
Registry Structure

Divided into sections
• HKEY_CLASSES_ROOT - software configuration
settings - Points to HKEY_LOCAL_MACHINE
• HKEY_LOCAL_MACHINE - contains the machine
configuration
• Divided into Hardware and Software sections
• HKEY_CURRENT_USER - profile of the current
user
• HKEY_CURRENT_CONFIG - current system
configuration
VB Registry Functions




GetSetting retrieves a registry setting
SaveSetting writes a registry setting
Reading and writing the complete
registry requires DLL calls
VB writes to a protected Registry area
so no danger exists of corrupting critical
information
• HKEY_USERS\.Default\Software\VB and VBA Program Settings

To read and write from other Registry
locations, you must use WinAPI
functions
GetSetting Example
pstrSetting = GetSetting _
(App.Title, _ Application Name
"section", _
Registry Section
"key", _
Registry Key
"setting")
Value
SaveSetting Example
SaveSetting _
(App.Title, _
Application Name
"section", _
Registry Section
"key", _
Registry Key
"setting")
Value
DeleteSetting Function

Removes key/value pair, section, or
application settings
DeleteSetting App.Title, "Files", "File1"
DeleteSetting App.Title, "Files"
DeleteSetting App.Title

Example – Ch16_C.vbp
•
•
•
•
Recently Opened Files first
Form_Load
Form_Unload
mnuFileOpen_Click
RegEdit

Not available from Start menu so as to hide from
ordinary users – type RegEdit from the Run menu
 Locate Registry information
 Edit Registry sections / keys

Be careful
Manual Server Registration


RegSvr32 registers and unregisters
servers
Register
• RegSvr32 servername

Unregister
• RegSvr32 /u servername
Dynamic Link Libraries
(DLLs)



Windows Application Programming
Interface (API) made up of callable
dynamic link libraries
Programmer interface into operating
system functions
Used to perform tasks not supported by
Visual Basic
Static Linking vs Dynamic
Linking

Static
• All program code
contained in
executable

Dynamic
• Program stores a
reference to external
libraries
• Linking occurs
dynamically at run
time hence the name
Declaring a DLL Function

Library name must be made known to Visual
Basic
 Function in that library must also be made
known
 Declare statement does this
 Syntax
• Declare Function name Lib “libname” [Alias
“aliasname”] [(arglist)] As Type
• name is the name used by VB program code
• libname contains name of DLL
• aliasname contains name of the function as named
inside the dll
• arglist contains the function arguments
DLL Function Declarations
(cont.)

Example:
Declare Function SetWindowPos _
Lib "user32" _
(ByVal hwnd As Long, _
ByVal hWndInsertAfter As Long, _
ByVal x As Long, _
ByVal y As Long, _
ByVal cx As Long, _
ByVal cy As Long, _
ByVal wFlags As Long) As Long

Uses of aliases
• simplify names
• allow VB to use functions with illegal characters
• Functions with leading underscores
• override VB functions
• Beep
DLL Function Arguments




Usually passed by value
Correct data types are critical
Accuracy of return type is also critical
C / Visual Basic data types
C Data Type
Visual Basic
Data type
Atom
Integer
Bool
Long
Char
Byte
Long
Long
LPSTR, LPCSTR
String
Null
Long
Short
Integer
API Text Viewer

API Text Viewer
helps to locate:
• function names
• constants
• types

Start Menu
• MS VS 6.0 Tools
• Load Text File
• Common\Tools\WinAPI\Win32API.txt

Uses clipboard to copy and paste function
prototypes to VB
Using DLL’s

Public Declare restricted to standard modules
 Private Declare can be used in form modules
 Create a wrapper procedure in a standard
module to hide the DLL from other
programmers
 Example
Public Sub SetTop(frm As Form)
Dim plngFlags As Long, plngResult As Long
plngFlags = SWP_NOSIZE Or SWP_NOMOVE
frm.Show
plngResult = SetWindowPos(frm.hWnd, _
HWND_TOPMOST, 0, 0, 0, 0, plngFlags)
End Sub
ByRef and ByVal



Most DLL arguments passed by value
(ByVal)
When passing strings ByVal, Visual
Basic passes a pointer to the string
Suggest using Fixed length strings and
manually terminate with null byte Chr(0)
• Cannot pass string properties directly

Example
• SetTop in API.BAS module
Making Temporary Files




Many programs use scratch / temporary
files
File names must be unique
GetTempPathA gets Windows temp
directory
GetTempFileNameA gets a unique file
name
Temporary File Wrapper
Public Function GetTempFile() As String
Dim
Dim
Dim
Dim
Dim
pstrPrefix As String * 4
pstrPath As String * 255
pstrFileName As String * 1024
pintSize As Integer
plngSize As Long
Fixed length strings
pstrPrefix = "VBT" & Chr(0)
Null terminate
pintSize = GetTempPathA(255, pstrPath)
plngSize = GetTempFileNameA(pstrPath, _
pstrPrefix, 0, pstrFileName)
GetTempFile = pstrFileName
End Function
Passing Properties

Properties cannot be passed directly
• Create a variable of the proper type and
store the property value
Dim pstrTempPath As String * 255
pstrTempPath = Text1.Text & Chr(0)
pintSize = GetTempPath(255, pstrTempPath)
Executing Other Programs


Shell function runs executable programs
Syntax
• Shell (pathname,windowstyle)
• pathname contains program to execute
• windowstyle determines initial appearance

Example
On Error Resume Next
Dim pintID As Long
cdl1.Filter = "Executable files (*.exe)|*.exe"
cdl1.ShowOpen
pintID = Shell(cdl1.filename, vbNormalFocus)
What Next?

Next Week
• Spring Break!!!

Following Week
• Read Chapter 15 (Section B) – HTML Help
• Creating and compiling an HTML Help System
• Connecting the help system to an application
• Midterm Project is due at the end of class

similar documents