Accessing Windows Initialization Files from Visual BasicLast reviewed: April 3, 1997Article ID: Q75639 |
1.00 2.00 3.00
WINDOWS
kbprg kbcode
The information in this article applies to: - Standard and Professional Editions of Microsoft Visual Basic for Windows, versions 2.0 and 3.0- Microsoft Visual Basic programming system for Windows, version 1.0
SUMMARYThere 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 INFORMATIONWindows 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=valueThere 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.
|
Additional reference words: 1.00 2.00 3.00
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |