Accessing Windows Initialization Files from Visual Basic

Article translations Article translations
Article ID: 75639 - View products that this article applies to.
This article was previously published under Q75639
Expand all | Collapse all

SUMMARY

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.

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

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 directory (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 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 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. The sample program demonstrates how to use GetPrivateProfileString() to get information from any initialization file.
  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
    
    						

Properties

Article ID: 75639 - Last Review: December 12, 2003 - Revision: 2.0
APPLIES TO
  • 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
Keywords: 
kbcode KB75639
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com