News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Storing/Reading from a .ini

Started by Telefunken, July 19, 2006, 06:35:11 AM

Previous topic - Next topic

Telefunken

Any tips on an efficiant way to read and write from a .ini file? I know basically that you would have to use CreateFile, SetFilePointer, ReadFile, and WriteFile, but maybe someone has come up with a proc or something to easily do this. If not, I'm happy to write my own. But everyone always says the first rule of coding is not to write something if its already been written  :wink

Thanks

PBrennick

Download SamiP's SendMail utility from here.  It shows a very easy way to read from an INI file.  Look at the GetSettingsFromIni procedure.  He is using GetPrivateProfileString.

hth,
Paul

The GeneSys Project is available from:
The Repository or My crappy website

ChrisLeslie

QuoteBut everyone always says the first rule of coding is not to write something if its already been written

That's not my rule. If you write your own you are learning much better, and, you may come up with a better version! Not to mention missing out on the fun. :wink

Chris

Vortex

Quote from: ChrisLeslie on July 19, 2006, 09:00:55 AM
That's not my rule. If you write your own you are learning much better, and, you may come up with a better version! Not to mention missing out on the fun. :wink

Chris

Well said Chris, that's 100% true :U  Rewriting the code can be a  programming practise as it's enchances your ability to understand various coding methods.

Shantanu Gadgil

QuoteBut everyone always says the first rule of coding is not to write something if its already been written
That would be true for companies needing to deliver stuff in time...you know..."quick time to market" and all that corporate lingo.  :green

While learning programming, especially ASM, an abstracted view (use whatever exists) will not exactly help the understanding, for which I presume we all are here!!! :) :green :green

Oh...and yes...check the "GetPrivateProfile..." API set! :)

Cheers,
Shantanu
To ret is human, to jmp divine!

P1

When using Get/WritePrivateProfileString, there are some quirks.

Use 'Search' ( Maybe even the Old Forum ) to find a past post which explains how to use these for multiline read/write usage.

Regards,  P1  :8)

Telefunken

Quote from: PBrennick on July 19, 2006, 07:30:34 AM
Download SamiP's SendMail utility from here.  It shows a very easy way to read from an INI file.  Look at the GetSettingsFromIni procedure.  He is using GetPrivateProfileString.

hth,
Paul



That link is not working for me...  :(

Ehtyar


PBrennick

Ehtyar,
Thank you for the support.  Actually, the logs say it has been downloaded 43 times.

Telefunken,
If you continue to have a problem try the mirror, located here.

Paul
The GeneSys Project is available from:
The Repository or My crappy website

Telefunken

Ok, i got everything down except for one thing. As some of you probably saw my dialog deals with user-set hotkeys. When I save the hotkeys to the ini everything goes fine, unless I use a modifier key. When I use a modifier key with the hotkey it saves the key to the ini, followed by a square symbol . I guess this has something to do with converting the high-order byte (the modifier flags) into a string. Since it's not saving right, it won't load the settings right either. Does anyone have an idea on how i can get it to save the modifier keys correctly?

heres my code for saving...

.if eax==1010

invoke SendDlgItemMessage,hWin,1002,HKM_GETHOTKEY,0,0
mov storehotkey1, eax
invoke SendDlgItemMessage,hWin,1003,HKM_GETHOTKEY,0,0
mov storehotkey2, eax
invoke SendDlgItemMessage,hWin,1015,HKM_GETHOTKEY,0,0
mov storehotkey3, eax
invoke SendDlgItemMessage,hWin,1009,BM_GETCHECK,0,0
mov storecheck, eax

.if storecheck==1
invoke WritePrivateProfileString,addr iniCheckSec,addr iniExitCheck,addr storecheck2,addr inifile
.else
invoke WritePrivateProfileString,addr iniCheckSec,addr iniExitCheck,addr storecheck0,addr inifile
.endif

invoke WritePrivateProfileString,addr iniKeySec,addr key1box,addr storehotkey1,addr inifile
invoke WritePrivateProfileString,addr iniKeySec,addr key2box,addr storehotkey2,addr inifile
invoke WritePrivateProfileString,addr iniKeySec,addr key3box,addr storehotkey3,addr inifile

PBrennick

This is a buffer problem,  if you are using something like 25 dup (?), remember that garbage is stored in those areas so your string is not zero terminated.  You need to clear the buffer to zeroes before using it and you need the buffer to be at least on byte larger than the expected string to ensure it will be zero terminated.

Paul
The GeneSys Project is available from:
The Repository or My crappy website

Telefunken

Quote from: PBrennick on July 20, 2006, 12:03:30 AM
This is a buffer problem,  if you are using something like 25 dup (?), remember that garbage is stored in those areas so your string is not zero terminated.  You need to clear the buffer to zeroes before using it and you need the buffer to be at least on byte larger than the expected string to ensure it will be zero terminated.

Paul


Thanks for the reply

I don't see how this could be. If I save the hotkey to a variable, and then load directly from that variable, it will include modifier keys. But, when I save that same variable to a .ini file, it does not work. Instead it just saved the key, plus that square, without any modifier keys

EDIT: I think I've found the problem, but I don't know how to fix it. I think it is converting the string produced by HKM_GETHOTKEY into letters/numbers. Which means it's not going to save properly into the ini...

PBrennick

None of what you just said makes sense.  You are using ADDR so it is a pointer to a string buffer and what I said is true.  It is difficult to help you when you only show us select portions of your code.  Attach the project so I can properly test it.

Paul
The GeneSys Project is available from:
The Repository or My crappy website

Telefunken

Sorry, I wasn't meaning to be offinsive with my post, I just didn't understand...



.386
.model flat, stdcall  ;32 bit memory model
option casemap :none  ;case sensitive

include windows.inc
include kernel32.inc
include user32.inc
include Comctl32.inc
include shell32.inc

includelib kernel32.lib
includelib user32.lib
includelib Comctl32.lib
includelib shell32.lib

DlgProc PROTO :HWND,:UINT,:WPARAM,:LPARAM

.const

IDD_DIALOG1 equ 101

;#########################################################################

.data?

hInstance dd ?

prehotkey1 dd ?
prehotkey2 dd ?
prehotkey3 dd ?
precheck dd ?
storehotkey1 dd ?
storehotkey2 dd ?
storehotkey3 dd ?
storecheck dd ?






hotkey1 dd ?
hotkey2 dd ?
exithotkey dd ?
check dd ?
stage dd ?
;#########################################################################

.data

intboxCap db "Starting",0
intboxText db "Starting...",0
dir db "models",0
newdir db "models1",0
key1box db "hotkey1",0
key2box db "hotkey2",0
key3box db "hotkey3",0


;##########  ini files #######

iniKeySec db "hotkey",0
iniCheckSec db "checkbox",0
iniExitCheck db "exit",0
inifile db "./hotkeys.ini",0
storecheck2 db "1",0
storecheck0 db "0",0


.code

start:

invoke GetModuleHandle,NULL
mov hInstance,eax

    invoke InitCommonControls
invoke DialogBoxParam,hInstance,IDD_DIALOG1,NULL,addr DlgProc,NULL
invoke ExitProcess,0

;########################################################################

DlgProc proc hWin:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM

mov eax,uMsg
.if eax==WM_INITDIALOG
        invoke LoadIcon, hInstance, 3001
    invoke SendMessage, hWin, WM_SETICON, ICON_BIG or ICON_SMALL, eax

.elseif eax==WM_COMMAND
mov eax,wParam
mov edx,eax
shr edx,16
and eax,0FFFFh
.if edx==BN_CLICKED

.if eax==1007
invoke ExitProcess,0
.endif

.if eax==1006
invoke SendDlgItemMessage,hWin,1002,HKM_GETHOTKEY,0,0
mov hotkey1,eax
invoke RegisterHotKey,hWin,001h,NULL,hotkey1
invoke SendDlgItemMessage,hWin,1003,HKM_GETHOTKEY,0,0
mov hotkey2,eax
invoke RegisterHotKey,hWin,002h,NULL,hotkey2
invoke SendDlgItemMessage,hWin,1015,HKM_GETHOTKEY,0,0
mov exithotkey,eax
invoke RegisterHotKey,hWin,003h,NULL,exithotkey

invoke SendDlgItemMessage,hWin,1009,BM_GETCHECK,0,0
mov check, eax

.endif
;####### load/save #######

;loading
.if eax==1008
invoke GetPrivateProfileString,addr iniKeySec,addr key1box,NULL,addr prehotkey1,sizeof prehotkey1,addr inifile
invoke GetPrivateProfileString,addr iniKeySec,addr key2box,NULL,addr prehotkey2,sizeof prehotkey2,addr inifile
invoke GetPrivateProfileString,addr iniKeySec,addr key3box,NULL,addr prehotkey3,sizeof prehotkey3,addr inifile
invoke GetPrivateProfileString,addr iniCheckSec,addr iniExitCheck,NULL,addr precheck,sizeof precheck,addr inifile

invoke SendDlgItemMessage,hWin,1002,HKM_SETHOTKEY,prehotkey1,0
;invoke MessageBox,hWin,addr storehotkey1,addr storehotkey1,MB_OK OR MB_ICONINFORMATION
invoke SendDlgItemMessage,hWin,1003,HKM_SETHOTKEY,prehotkey2,0
invoke SendDlgItemMessage,hWin,1015,HKM_SETHOTKEY,prehotkey3,0

.if precheck==1
invoke SendDlgItemMessage,hWin,1009,BM_SETCHECK,BST_CHECKED,0
.elseif precheck==0
invoke SendDlgItemMessage,hWin,1009,BM_SETCHECK,BST_UNCHECKED,0
.endif


.endif
;saving
.if eax==1010

invoke SendDlgItemMessage,hWin,1002,HKM_GETHOTKEY,0,0
mov storehotkey1, eax
invoke SendDlgItemMessage,hWin,1003,HKM_GETHOTKEY,0,0
mov storehotkey2, eax
invoke SendDlgItemMessage,hWin,1015,HKM_GETHOTKEY,0,0
mov storehotkey3, eax
invoke SendDlgItemMessage,hWin,1009,BM_GETCHECK,0,0
mov storecheck, eax

.if storecheck==1
invoke WritePrivateProfileString,addr iniCheckSec,addr iniExitCheck,addr storecheck2,addr inifile
.else
invoke WritePrivateProfileString,addr iniCheckSec,addr iniExitCheck,addr storecheck0,addr inifile
.endif

invoke WritePrivateProfileString,addr iniKeySec,addr key1box,addr storehotkey1,addr inifile
invoke WritePrivateProfileString,addr iniKeySec,addr key2box,addr storehotkey2,addr inifile
invoke WritePrivateProfileString,addr iniKeySec,addr key3box,addr storehotkey3,addr inifile




.endif

.endif

.elseif eax==WM_HOTKEY
.if wParam==001h
invoke MoveFileEx,addr dir,addr newdir,MOVEFILE_REPLACE_EXISTING
.endif

.if wParam==002h
invoke MoveFileEx,addr newdir,addr dir,MOVEFILE_REPLACE_EXISTING
.if check==1
invoke ExitProcess,0
.endif

.endif


.if wParam==003h
invoke ExitProcess,0
.endif


.elseif eax==WM_CLOSE
invoke EndDialog,hWin,0
.else
mov eax,FALSE
ret
.endif
mov eax,TRUE
ret

DlgProc endp





end start


:)

EDIT: oops, you wanted the whole project... one second

If your wondering what it is, it's a modification for a game... nothing illegal I promise  :bg





[attachment deleted by admin]

Telefunken

#14
I still haven't been able to figure this out. Please, if anyone has ideas, I would like to hear them.

Thanks.

edit: oh yeah zooba i gave you in the credits cause you helped me so much  :lol