How to Access Windows Initialization Files Within Visual Basic

ID Number: Q75639

1.00

WINDOWS

Summary:

There are several Microsoft Windows API functions that can manipulate

information within a Windows initialization file. GetProfileInt,

GetPrivateProfileInt, GetProfileString, and GetPrivateProfileString

allow a Visual Basic program to retrieve information from a Windows

initialization file based on an application name and key name.

WritePrivateProfileString and WriteProfileString are used to

create/update items within Windows initialization files.

This information applies to Microsoft Visual Basic programming system

version 1.0 for Windows.

More Information:

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

subdirectory. Microsoft Windows and applications for Microsoft Windows

can use the information stored in these files to configure themselves

to meet your needs and preferences. For a description of initialization

files, read the WININI.TXT file that comes with Microsoft Windows 3.0.

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

There are four API function calls (GetProfileInt,

GetPrivateProfileInt, GetProfileString, and GetPrivateProfileString)

that you can use to retrieve information from these files. The

particular function to call depends on whether you want to obtain

string or numerical data.

The GetProfile family of API functions is used when you want to get

information from the standard WIN.INI file that is used by Windows.

The WIN.INI file should be part of your Windows subdirectory

(C:\WINDOWS). The GetPrivateProfile family of API functions is used

to retrieve information from any initialization file that you specify.

The formal arguments accepted by these API functions are described

farther below.

The WriteProfileString and WritePrivateProfileString functions write

information to Windows initialization files. WriteProfileString is

used to modify the Windows initialization file WIN.INI.

WritePrivateProfileString is used to modify any initialization file

that you specify. These functions search the initialization file for

the key name under the application name. If there is no match, the

function adds to the user profile a new string entry containing the

key name and the key value specified. If the key name is found, it will

replace the key value with the new value specified.

To declare these API functions within your program, include the

following Declare statements in the global module or the General

Declarations section of a Visual Basic form:

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$)

Note: Each Declare statement must be on a single line.

The formal arguments to these functions are described as follows:

ArgumentDescription

-------- -----------

lpAppName$ Name of a Windows 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.

Below are the steps necessary to create a Visual Basic sample program

that uses GetPrivateProfileString to read from an initialization file

that you create. The program, based on information in the

initialization file you created, shells out to the Calculator program

(CALC.EXE) that comes with Windows 3.0. The sample program

demonstrates how to use GetPrivateProfileString to get information

from any initialization file.

1. Create an initialization file from a text editor (for example,

you can use the Notepad program supplied with Windows 3.0) and

save the file under the name of "NET.INI". Type in the following

as the contents of the initialization file (NET.INI):

[NetPaths]

WordProcessor=C:\WINWORD\WINWORD.EXE

Calculator=C:\WINDOWS\CALC.EXE

Note: If CALC.EXE is not in the C:\WINDOWS subdirectory (as

indicated after "Calculator=" above), replace C:\WINDOWS\CALC.EXE

with the correct path.

2. Save the initialization file (NET.INI) to the root directory of

your hard drive (such as C:\) and exit the text editor.

3. Start Visual Basic.

4. Create a form called Form1.

5. Create a push button called Command1.

6. Within the Global Declaration section of Form1, add the

following Windows API function declarations. Note that the

Declare statement below must appear on a single line.

Declare Function GetPrivateProfileString% Lib "kernel"

(ByVal lpAppName$, ByVal lpKeyName$,ByVal lpDefault$,

ByVal lpReturnString$,ByVal nSize%, ByVal lpFileName$)

7. Within the (Command1) push button's click event add the following

code:

Sub Command1_Click ()

'* If an error occurs during SHELL statement then handle the error

On Error GoTo FileError

'* 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 reference words: 1.00