Article ID: 75639 - View products that this article applies to.
This article was previously published under 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:
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:
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.
Article ID: 75639 - Last Review: December 12, 2003 - Revision: 2.0