How would I go about reading information from a txt/cfg file?

Started by Sean1337, March 25, 2008, 02:28:42 PM

Previous topic - Next topic

Sean1337

Hey all,

I would like to be able to enable users of my program to customize settings through the use of either a txt file or a cfg file.

E.g. the settings for a certain part of the application would be retrieved from the cfg file.

such as ..

MsgBox = 0

If the user set MsgBox to zero then when the program runs it would read the cfg file compare the values set to MsgBox and either display a message box or not.

Is this possible?

Thanks,
-Sean

thomas_remkus

TXT/CFG is very possible. A popular way of doing this is from an INI file. The structure of INI is easy and documented on the web.

sebart7

To save Your time, You ask about functions WritePrivateProfileString and GetPrivateProfileString from kernel32.


; Example of WritePrivateProfileString that writes MsgBox=0 to file "c:\configFile.cfg"
; If file not exsist, it is created.

include     \masm32\include\kernel32.inc
includelib  \masm32\lib\kernel32.lib

.data
szSection   db          "demosection",0
szKey1Name  db          "MsgBox",0
szValue1    db          "0",0
szFileName  db          "c:\configFile.cfg",0

.code
            invoke WritePrivateProfileString,addr szSection,addr szKey1Name,addr szValue1,addr szFileName

.
; Example of GetPrivateProfileString that Reads MsgBox value from file "c:\configFile.cfg"
; and shows MessageBox if MsgBox=1. Note that default value for MsgBox is set to 1
; at szDefaultValue in .data section, so if file not exsist or MsgBox key not exsist within of configFile.cfg
; default value "1" is returned instead.

include     \masm32\include\kernel32.inc
includelib  \masm32\lib\kernel32.lib

.data
szSection      db        "demosection",0
szKey1Name     db        "MsgBox",0
szDefaultValue db        "1",0          ; by default msgbox will be shown
szFileName     db        "c:\configFile.cfg",0
buffer         db        32 dup (?)
szMsgBox       db        "hello",0

.code
invoke GetPrivateProfileString, addr szSection,\
addr szKey1Name,\
addr szDefaultValue,\
addr buffer,32,\
addr szFileName
.if eax > 0 && byte ptr[buffer] == 031h
invoke MessageBox,       0,\
addr szMsgBox,\
0,\
MB_OK or MB_ICONINFORMATION
.endif