Accessing Windows Initialization Files from Visual Basic

This article was previously published under Q75639
This article has been archived. It is offered "as is" and will no longer be updated.
There are several Microsoft Windows API functions that can manipulateinformation within a Windows initialization file. GetProfileInt(),GetPrivateProfileInt(), GetProfileString(), and GetPrivateProfileString()allow a Microsoft Visual Basic for Windows program to retrieve informationfrom a Windows initialization file based on an application name and keyname. WritePrivateProfileString() and WriteProfileString() are used tocreate or update items within Windows initialization files.
Windows initialization files contain information that defines your Windowsenvironment. Examples of Windows initialization files are WIN.INI andSYSTEM.INI, which are commonly found in the C:\WINDOWS directory. MicrosoftWindows and Windows-based applications can use the information stored inthese files to configure themselves to meet your needs and preferences. Fora description of initialization files, review the WIN.INI file that comeswith Microsoft Windows.

An initialization file is composed of at least an application name and akey name. The contents of Windows initialization files have the followingformat:
   [Application name]   keyname=value				

There are four API function calls [GetProfileInt(), GetPrivateProfileInt(),GetProfileString(), and GetPrivateProfileString()] that you can use toretrieve information from these files. The particular function to calldepends on whether you want to obtain string or numerical data.

The GetProfile family of API functions is used when you want to getinformation from the standard WIN.INI file that is used by Windows. TheWIN.INI file should be part of your Windows directory (C:\WINDOWS). TheGetPrivateProfile() family of API functions is used to retrieve informationfrom any initialization file that you specify. The formal argumentsaccepted by these API functions are described farther below.

The WriteProfileString() and WritePrivateProfileString() functions writeinformation to Windows initialization files. WriteProfileString() is usedto modify the Windows initialization file, WIN.INI.WritePrivateProfileString() is used to modify any initialization file thatyou specify. These functions search the initialization file for the keyname under the application name. If there is no match, the function adds tothe user profile a new string entry containing the key name and the keyvalue specified. If the key name is found, it will replace the key valuewith the new value specified.

To declare these API functions within your program, include the followingDeclare statements in the global module or the General Declarations sectionof a Visual Basic for Windows 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:
   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.				

Below are the steps necessary to create a Visual Basic for Windows sampleprogram that uses GetPrivateProfileString() to read from an initializationfile that you create. The program, based on information in theinitialization file you created, shells out to the Calculator program(CALC.EXE) that comes with Windows. The sample program demonstrates how touse GetPrivateProfileString() to get information from any initializationfile.
  1. Create an initialization file using a text editor (for example, the Notepad program supplied with Windows) and save the file with the name "NET.INI". Type in the following lines 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 directory (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 disk (for example, C:\) and exit the text editor.
  3. Start Visual Basic for Windows.
  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         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						
2.00 3.00

Article ID: 75639 - Last Review: 12/04/2015 09:09:43 - Revision: 2.0

Microsoft Visual Basic 2.0 Standard Edition, Microsoft Visual Basic 3.0 Professional Edition, Microsoft Visual Basic 2.0 Professional Edition, Microsoft Visual Basic 3.0 Professional Edition, Microsoft Visual Basic 1.0 Standard Edition

  • kbnosurvey kbarchive kbcode KB75639