User Rating:  / 1

 Using INI Files: Part 1

.ini files have been around since the early days of windows 3.0, where windows itself would store some of its own information in a win.ini file. Other programs would use the win.ini as well, to store their own information. Eventually Microsoft moved to the registry as a way of handling the data for itself and other programs - not quite a database and not quite a flat text file format - more of a hybrid type system. So why not just use the registry?

The registry is stored in a binary format, dynamically built from a particular computer's low level hardware devices and system services and users installed programs, setting and prefrences. Its not portable or easy to edit externally, and even with registry editor programs, its not necessarily a user friendly place for a user to make any changes to settings without potential consequences like system instability or failure to start of the computer - if they edit the wrong thing!

.ini files, although they dont support unicode, or provide advanced nesting of data, and are limited to 32KB of data, they do allow a user to edit them with a simple notepad or other text editor. The .ini also has a simple format: sections, defined as labels between square brackets, and key=value pairs.

So with the brief background on .ini files out of the way, my aim is to show you how to make use of them in your programs and to give you examples of how i use them in some of my projects.

INI File & Its Location

Its worth noting that although i mention .ini as the file extension, in reality it can be anything, .cfg or .dat for example works equally well, its just a matter of personal preference.

With Windows 95 to Windows XP creating an .ini file in the same directory as your executable would seem to be the preferred method. Easy to find for your program and easy to find for you and your users that you support, should the need arise to edit the file.

The simplest option is to specify the name of your .ini file in a data variable and use that throughout your program. Like so:

szIniFilename       db ".\MyApp.ini",0     ; ini filename in the current directory

What about if you wanted to dynamically create an ini filename based on the name of your executable at run time? Well one way to do that, for a program running under Win95-WinXP (or a program not installed under C:\Program Files if running under Windows Vista or newer - see later for more details), could be to do something like:

CreateIniFilename   PROTO :DWORD           ; Function prototype
szExtEXE            db ".exe",0            ; exe file extension
szExtINI            db ".ini",0            ; ini file extension
szIniFilename       db MAX_PATH dup (0)    ; buffer to hold our ini filename
; Create an .ini filename based on the executables name. 
; Example Usage:
; Invoke CreateIniFilename, Addr szIniFilename
CreateIniFilename   PROC    lpszIniFile:DWORD
    LOCAL ModuleFullPathname[MAX_PATH]:BYTE
    Invoke GetModuleFileName, NULL, Addr ModuleFullPathname, SIZEOF ModuleFullPathname
    Invoke szRep, Addr ModuleFullPathname, lpszIniFile, Addr szExtEXE, Addr szExtINI
CreateIniFilename   ENDP

GetModuleFileName returns the full path of your executable, and we simply use szRep (a string function from the MASM32 Library) to replace the '.exe' part of the string with a '.ini ' part. So for example: C:\MyAppFolder\MyGreatApp.exe becomes C:\MyAppFolder\MyGreatApp.ini, which we can then use throughout our program in conjunction with other functions we may use to read and write to our .ini file.

Category: Win32 Assembler

Login Form