230 likes | 356 Views
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
E N D
Windows Software DevelopmentLecture 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, _ "section", _ "key", _ "setting") Application Name Registry Section Registry Key Value
SaveSetting Example SaveSetting _ (App.Title, _ "section", _ "key", _ "setting") Application Name Registry Section Registry Key Value
DeleteSetting Function • Removes key/value pair, section, or application settings • Example – Ch16_C.vbp • Recently Opened Files first • Form_Load • Form_Unload • mnuFileOpen_Click DeleteSetting App.Title, "Files", "File1" DeleteSetting App.Title, "Files" DeleteSetting App.Title
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 • RegSvr32servername • 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 • DeclareFunctionnameLib“libname” [Alias “aliasname”] [(arglist)] AsType • 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: • Uses of aliases • simplify names • allow VB to use functions with illegal characters • Functions with leading underscores • override VB functions • Beep 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
DLL Function Arguments • Usually passed by value • Correct data types are critical • Accuracy of return type is also critical • C / Visual Basic data types
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 pstrPrefix As String * 4 Dim pstrPath As String * 255 Dim pstrFileName As String * 1024 Dim pintSize As Integer Dim plngSize As Long pstrPrefix = "VBT" & Chr(0) pintSize = GetTempPathA(255, pstrPath) plngSize = GetTempFileNameA(pstrPath, _ pstrPrefix, 0, pstrFileName) GetTempFile = pstrFileName End Function Fixed length strings Null terminate
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