flat assembler
Message board for the users of flat assembler.

Index > Windows > Picking up wrong error?

Author
Thread Post new topic Reply to topic
moriman



Joined: 01 Apr 2006
Posts: 55
Location: Northern Ireland
moriman 06 May 2006, 12:14
I'm using the following routine to display any errors in my app. Unfortunately, the routine seems to be producing an error itself Rolling Eyes

Code:
ShowError:
   pushad
   invoke  GetLastError
   invoke  FormatMessage,\
              FORMAT_MESSAGE_FROM_SYSTEM+\
              FORMAT_MESSAGE_ALLOCATE_BUFFER,\  
              NULL,\                                                         
              eax,\                                                          
              LANG_NEUTRAL,\                                                 
              LErrStrBuff,\                                                  
              0,\                                                            
              NULL                                                           
   invoke  MessageBox,[hWnd],[LErrStrBuff],ErrTitle,MB_ICONERROR+MB_OK
   popad
   ret
    


However, when I induce a deliberate error (open a non-existant file or regkey, etc.), the MessageBox always states The parameter is incorrect. This happens no matter what value GetLastError returns in eax.

Any ideas Idea what's happening here?

many thanks Wink
Post 06 May 2006, 12:14
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8428
Location: Kraków, Poland
Tomasz Grysztar 06 May 2006, 12:28
This works just as expected:
Code:
format PE GUI 4.0
entry start

include 'win32a.inc'

section '.data' data readable writeable

ErrTitle db 'Error',0
LErrStrBuff dd ?

FileName db 'c:\nothing',0


section '.code' code readable executable

  start:

        invoke  CreateFile,FileName,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0

        call    ShowError

        invoke  ExitProcess,0


ShowError:
   pushad
   invoke  GetLastError
   invoke  FormatMessage,\
              FORMAT_MESSAGE_FROM_SYSTEM+\
              FORMAT_MESSAGE_ALLOCATE_BUFFER,\  
              NULL,\                                                         
              eax,\                                                          
              LANG_NEUTRAL,\                                                 
              LErrStrBuff,\                                                  
              0,\                                                            
              NULL                                                           
   invoke  MessageBox,HWND_DESKTOP,[LErrStrBuff],ErrTitle,MB_ICONERROR+MB_OK
   popad
   ret

section '.idata' import data readable writeable

  library kernel32,'KERNEL32.DLL',user32,'USER32.DLL'

  include 'apia\kernel32.inc'
  include 'apia\user32.inc'    
Post 06 May 2006, 12:28
View user's profile Send private message Visit poster's website Reply with quote
moriman



Joined: 01 Apr 2006
Posts: 55
Location: Northern Ireland
moriman 06 May 2006, 13:22
Strange indeed¿ Yes, I tried this and get the correct error message.

In the attached routine the ShowError sub is only called from 2 places and the program skips the 2nd call because of the first error (RegOpenKey). When I close all programs and run test.exe from the Start->Run button (Win98), the first time it runs it produces the The parameter is incorrect error and starts the app (as it should). If I close the MessageBox and the app and then do the same again, it produces a The operation completed successfully error. LOL.
When I change your code above from
Code:
invoke  CreateFile,FileName,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0
    

to
Code:
invoke  RegOpenKey, HKEY_CURRENT_USER, RegKey, RegHnd
    

I get the expected error string. Confused


Description:
Download
Filename: test.asm
Filesize: 4.65 KB
Downloaded: 292 Time(s)

Post 06 May 2006, 13:22
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8428
Location: Kraków, Poland
Tomasz Grysztar 06 May 2006, 13:43
It seems that RegOpenKey doesn't do SetLastError on failure, it just returns the error code in EAX. Checked it with OllyDbg.
Post 06 May 2006, 13:43
View user's profile Send private message Visit poster's website Reply with quote
moriman



Joined: 01 Apr 2006
Posts: 55
Location: Northern Ireland
moriman 06 May 2006, 14:26
Many thanks Tomasz, when I removed the invoke GetLastError, everything works as expected Smile
Post 06 May 2006, 14:26
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.