flat assembler
Message board for the users of flat assembler.

Index > Windows > WritePrivateProfileStringA - always failing?

Author
Thread Post new topic Reply to topic
Pinecone_



Joined: 28 Apr 2008
Posts: 180
Pinecone_ 01 Aug 2008, 10:54
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.

Code:
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.
Post 01 Aug 2008, 10:54
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1614
Location: Ukraine
shoorick 01 Aug 2008, 11:36
use 12 instead of 11. you did not aligned stack with your allocation, use multiplication of 4 (32 bits) in future for this
Post 01 Aug 2008, 11:36
View user's profile Send private message Visit poster's website Reply with quote
Pinecone_



Joined: 28 Apr 2008
Posts: 180
Pinecone_ 01 Aug 2008, 11:52
Other than for debugging purposes, does it really matter if the stack is not aligned to 4 bytes? I allocated 11 bytes because that's the maximum it would require.
Post 01 Aug 2008, 11:52
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1614
Location: Ukraine
shoorick 01 Aug 2008, 12:18
some API checks stack aligning and fail if it is not aligned. also, it is required by CPU performance.
Post 01 Aug 2008, 12:18
View user's profile Send private message Visit poster's website Reply with quote
Pinecone_



Joined: 28 Apr 2008
Posts: 180
Pinecone_ 01 Aug 2008, 12:36
Awesome Smile Thanks for the info and the help. Didn't know that before.
Post 01 Aug 2008, 12:36
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.