I was writing a program the other day where i wanted to store some information in INI files. When looking through the MSDN, i found there was no WritePrivateProfileInt function for writing integers to ini files.
So i wrote a procedure which calls wsprintf to convert the number to a string, then writes the string to the ini file with WritePrivateProfilestring. As far as i can see, im passing all the corect paramaters to the functions, however WritePrivateProfileString is failing every time, with last error being ERROR_NOACCESS. I searched for a few hours to try and find why however could not find a reason. Does anyone know why this could be?
I write a simple test case which reproduces the problem. The following code tries to write 0x100 (256) to Test.ini and (on my system at least) fails as described above.
FORMAT PE GUI 4.0
Entry _Main
Include '%Include%\Win32A.inc'
Section '.code' Code Readable Executable
_Main:
; Get full path name of INI file
push NULL
push szFilename
push MAX_PATH
push szIniFile
call [GetFullPathName]
; call my WritePrivateProfileInt function
push szFilename
push 0x100
push szKeyname
push szSectionName
call WritePrivateProfileInt
invoke ExitProcess, 0
proc WritePrivateProfileInt, szApplication, szKey, Value, szFilename
; allocate 11 bytes of data for the string to go in on the stack
push edi ; a decimal integer in string form
sub esp, 11 ; will only take a maximum of 10 bytes
mov edi, esp ; extra byte for null terminator
; reset the buffer to NULL bytes
xor al, al
mov ecx, 11
push edi
rep stosb
pop edi
; get string equivalent of value...
push [Value]
push .FormatString
push edi
call [wsprintf]
; write string to ini file.
push [szFilename]
push edi
push [szKey]
push [szApplication]
call [WritePrivateProfileString]
; ollydebug should show last error to
; be ERROR_NOACCESS here if your getting
; the same problem as me.
; restore the stack
add esp, 11
pop edi
ret
; the string used by wsprintf to generate the string format of the number
.FormatString DB '%i', 0
endp
Section '.data' Data Readable Writeable
Data Import
library kernel32, 'kernel32.dll',\
user32, 'user32.dll'
import kernel32,\
GetFullPathName, 'GetFullPathNameA',\
WritePrivateProfileString, 'WritePrivateProfileStringA',\
ExitProcess, 'ExitProcess'
import user32,\
wsprintf, 'wsprintfA'
End Data
szIniFile DB 'Test.ini', 0
szSectionName DB 'TestSection', 0
szKeyname DB 'TestKey', 0
szFilename RB MAX_PATH
any help is appreciated. Thank you.