Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "Config"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Description = "Configuration Data Persistance"
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"No"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
Option Explicit
Public Event CfgIOError(m_Error As String)
Public Event cfgWrite()
Public Event cfgRead()
Private Type ConfigInfo
m_Item1 As Integer
m_Item2 As Long
m_Item3 As Boolean
m_Item4 As String * 33 ' Max string length is 32 chars.
End Type
Dim cfgInfo As ConfigInfo ' Structure to hold configuration information
Dim m_hFile As Long ' File Handle
Dim m_szFilePath As String ' Fully qualified path to config file.
' Win32 API Declaration for File I/O, etc.
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
' Const Values used for above.
Private Const INVALID_HANDLE_VALUE = -1
Private Const GENERIC_READ = &H80000000
Private Const GENERIC_WRITE = &H40000000
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Const CREATE_ALWAYS = 2
Private Const CREATE_NEW = 1
Private Const OPEN_ALWAYS = 4
Private Const OPEN_EXISTING = 3
Private Const TRUNCATE_EXISTING = 5
Private Const FILE_ATTRIBUTE_NORMAL = &H80
' Configuration File Name
Private Const CFG_FILE = "myconfig.cfg" ' Change this to suit you
Public Property Let SetInt(ByVal vData As Integer)
cfgInfo.m_Item1 = vData
End Property
Public Property Get GetInt() As Integer
GetInt = cfgInfo.m_Item1
End Property
Public Property Let SetLong(ByVal vData As Long)
cfgInfo.m_Item2 = vData
End Property
Public Property Get GetLong() As Long
GetLong = cfgInfo.m_Item2
End Property
Public Property Let SetBool(ByVal vData As Boolean)
cfgInfo.m_Item3 = vData
End Property
Public Property Get GetBool() As Boolean
GetBool = cfgInfo.m_Item3
End Property
Public Property Let SetStr(ByVal vData As String)
cfgInfo.m_Item4 = vData
End Property
Public Property Get GetStr() As String
GetStr = cfgInfo.m_Item4
End Property
Public Function Initialise() As Boolean
Dim bOk As Boolean ' Initialised to False by default
m_szFilePath = "c:\Projects\Config\" & CFG_FILE
m_hFile = CreateFile(m_szFilePath, 0, 0, ByVal CLng(0), OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
If (m_hFile = INVALID_HANDLE_VALUE) Then
' The config file is missing so set sensible defaults.
RaiseEvent CfgIOError("Configuration file is Missing! These are defaults.")
' These values indicate the ListIndex value of the value's control in the properties form.
cfgInfo.m_Item1 = 1 ' Radio Button
cfgInfo.m_Item2 = 64 ' Combo Box
cfgInfo.m_Item3 = True ' Check Box
cfgInfo.m_Item4 = "George, Bungle & Zippy" ' Text Control (70s cartoon)
Else
' Close the file as we only checked to see if it existed
If (CloseFile()) Then
' Now read the configuration data proper
bOk = ReadConfig() ' Set bOk to the return value of ReadConfig()
End If
End If
' Return the Results to the Caller.
Initialise = bOk
End Function
Public Function ReadConfig() As Boolean
Dim dwRead As Long, bOk As Boolean
m_hFile = CreateFile(m_szFilePath, GENERIC_READ, 0, ByVal CLng(0), OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
If (m_hFile = INVALID_HANDLE_VALUE) Then
RaiseEvent CfgIOError("Error opening Configuration file for Reading")
Else
If (ReadFile(m_hFile, cfgInfo, Len(cfgInfo), dwRead, ByVal CLng(0))) Then
RaiseEvent cfgRead ' File read successfully so raise event.
Else
RaiseEvent CfgIOError("Error reading configuration file!") ' Ooops!
End If
bOk = CloseFile() ' Close File
End If
ReadConfig = bOk
End Function
Public Function WriteConfig() As Boolean
Dim dwWrite As Long, bOk As Boolean
' Truncate (dump) the File everytime it's written.
m_hFile = CreateFile(m_szFilePath, GENERIC_WRITE, 0, ByVal CLng(0), CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
If (m_hFile = INVALID_HANDLE_VALUE) Then
RaiseEvent CfgIOError("Error opening Configuration file for Writing!")
Else
If (WriteFile(m_hFile, cfgInfo, Len(cfgInfo), dwWrite, ByVal CLng(0))) Then
RaiseEvent cfgWrite ' File written successfully so raise event.
Else
RaiseEvent CfgIOError("Error writing configuration file!")
End If
bOk = CloseFile()
End If
WriteConfig = bOk
End Function
Private Function CloseFile() As Boolean
CloseFile = CloseHandle(m_hFile)
End Function