Accessing Windows Initialization Files from Visual BasicID: Q75639
|
There are several Microsoft Windows API functions that can manipulate information within a Windows initialization file. GetProfileInt(), GetPrivateProfileInt(), GetProfileString(), and GetPrivateProfileString() allow a Microsoft Visual Basic for Windows program to retrieve information from a Windows initialization file based on an application name and key name. WritePrivateProfileString() and WriteProfileString() are used to create or update items within Windows initialization files.
Windows initialization files contain information that defines your Windows
environment. Examples of Windows initialization files are WIN.INI and
SYSTEM.INI, which are commonly found in the C:\WINDOWS directory. Microsoft
Windows and Windows-based applications can use the information stored in
these files to configure themselves to meet your needs and preferences. For
a description of initialization files, review the WIN.INI file that comes
with Microsoft Windows.
An initialization file is composed of at least an application name and a
key name. The contents of Windows initialization files have the following
format:
[Application name]
keyname=value
Declare Function GetProfileInt% Lib "Kernel"(ByVal lpAppName$,
ByVal lpKeyName$, ByVal nDefault%)
Declare Function GetProfileString% Lib "Kernel" (ByVal lpAppName$,
ByVal lpKeyName$, ByVal lpDefault$, ByVal lpReturnedString$,
ByVal nSize%)
Declare Function WriteProfileString% Lib "Kernel"(ByVal lpAppName$,
ByVal lpKeyName$, ByVal lpString$)
Declare Function GetPrivateProfileInt% Lib "Kernel"
(ByVal lpAppName$, ByVal lpKeyName$, ByVal nDefault%,
ByVal lpFileName$)
Declare Function GetPrivateProfileString% Lib "Kernel"
(ByVal lpAppName$, ByVal lpKeyName$, ByVal lpDefault$,
ByVal lpReturnedString$, ByVal nSize%, ByVal lpFileName$)
Declare Function WritePrivateProfileString% Lib "Kernel"
(ByVal lpAppName$, ByVal lpKeyName$, ByVal lpString$,
ByVal lpFileName$)
Argument Description
----------------------------------------------------------------------
lpAppName$ Name of a Windows-based application that appears in
the initialization file.
lpKeyName$ Key name that appears in the initialization file.
nDefault$ Specifies the default value for the given key if the
key cannot be found in the initialization file.
lpFileName$ Points to a string that names the initialization
file. If lpFileName does not contain a path to the
file, Windows searches for the file in the Windows
directory.
lpDefault$ Specifies the default value for the given key if the
key cannot be found in the initialization file.
lpReturnedString$ Specifies the buffer that receives the character
string.
nSize% Specifies the maximum number of characters (including
the last null character) to be copied to the buffer.
lpString$ Specifies the string that contains the new key value.
[NetPaths]
WordProcessor=C:\WINWORD\WINWORD.EXE
Calculator=C:\WINDOWS\CALC.EXE
NOTE: If CALC.EXE is not in the C:\WINDOWS directory (as indicated after
"Calculator=" above), replace C:\WINDOWS\CALC.EXE with the correct path.
Declare Function GetPrivateProfileString% Lib "kernel"
(ByVal lpAppName$, ByVal lpKeyName$,ByVal lpDefault$,
ByVal lpReturnString$,ByVal nSize%, ByVal lpFileName$)
Sub Command1_Click ()
'* If an error occurs during SHELL statement then handle the
'* error.
On Error GoTo FileError
Dim lpAppName$, lpKeyName$, lpDefault$, lpReturnString$,
lpFileName$
Dim Size%, Valid%, Path$, Succ%
'* Compare these to the NET.INI file that you created in step 1
'* above.
lpAppName$ = "NetPaths"
lpKeyName$ = "Calculator"
lpDefault$ = ""
lpReturnString$ = Space$(128)
Size% = Len(lpReturnString$)
'* This is the path and name the NET.INI file.
lpFileName$ = "c:\net.ini"
'* This call will cause the path to CALC.EXE (that is,
'* C:\WINDOWS\CALC.EXE) to be placed into lpReturnString$. The
'* return value (assigned to Valid%) represents the number of
'* characters read into lpReturnString$. Note that the
'* following assignment must be placed on one line.
Valid% = GetPrivateProfileString(lpAppName$, lpKeyName$,
lpDefault$, lpReturnString$,
Size%, lpFileName$)
'* Discard the trailing spaces and null character.
Path$ = Left$(lpReturnString$, Valid%)
'* Try to run CALC.EXE. If unable to run, FileError is called.
Succ% = Shell(Path$, 1)
Exit Sub
FileError:
MsgBox "Can't find file", 16, "Error lpReturnString"
Resume Next
End Sub
Additional query words: 2.00 3.00
Keywords : kbcode
Version : WINDOWS:1.0,2.0,3.0
Platform : WINDOWS
Issue type :
Last Reviewed: May 12, 1999