flat assembler
Message board for the users of flat assembler.

Index > Windows > GetLastError & Buffer

Author
Thread Post new topic Reply to topic
DerTobi



Joined: 20 May 2013
Posts: 10
Location: DE
DerTobi 20 May 2013, 13:52
Hi all,
i need the error message code in my msgbox output,
but it dont work Sad

Code:
format PE GUI 4.0
include 'D:\FASM\INCLUDE\WIN32AX.INC'

entry start

Section '.data' data readable writeable

        dwError dd 0h
       _title    db "Test",0

section '.code' code readable executable

; Entry point
start:
        invoke SetLastError,500
        ;invoke MessageBox,0,0,0,9
        invoke GetLastError
        mov [dwError], eax

        invoke FormatMessage,FORMAT_MESSAGE_ALLOCATE_BUFFER+FORMAT_MESSAGE_FROM_SYSTEM,0,eax,LANG_NEUTRAL,[dwError],0,0

        invoke MessageBox,NULL, addr dwError, _title ,MB_OK

        invoke ExitProcess,0


section ".idata" import data readable writable
        library kernel32, 'kernel32.dll', user32, 'USER32.DLL'
        
        include "d:\fasm\include\api\kernel32.inc"
        include "d:\fasm\include\api\user32.inc"  
    
Post 20 May 2013, 13:52
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 13462
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 20 May 2013, 14:27
maybe invoke MessageBox,NULL, [dwError], _title ,MB_OK
Post 20 May 2013, 14:27
View user's profile Send private message Reply with quote
DerTobi



Joined: 20 May 2013
Posts: 10
Location: DE
DerTobi 20 May 2013, 15:40
Yes you are right, i need [thishere], but it does not work.
Post 20 May 2013, 15:40
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 13462
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 20 May 2013, 17:17
Code:
; Entry point
start:
        invoke SetLastError,3
        invoke GetLastError
        invoke FormatMessage,FORMAT_MESSAGE_ALLOCATE_BUFFER+FORMAT_MESSAGE_FROM_SYSTEM,NULL,eax,0, dwError,0,NULL
        invoke MessageBox,NULL, [dwError], _title ,MB_OK 
    

above works, i tested a few error number, but not every number works, kinda weird

because 500 supposed got error code string,
ERROR_USER_PROFILE_LOAD
500 (0x1F4)
User profile cannot be loaded.
Post 20 May 2013, 17:17
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 13462
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 20 May 2013, 17:37
i added a few things for diagnose,
Code:
        invoke FormatMessage,FORMAT_MESSAGE_ALLOCATE_BUFFER+FORMAT_MESSAGE_FROM_SYSTEM+FORMAT_MESSAGE_IGNORE_INSERTS,NULL,eax,0, dwError,0,NULL
        cmp eax,0
        jne @f
        invoke GetLastError
        cinvoke wsprintf,b1,<'%lu',0>,eax
@@:
        invoke MessageBox,NULL, [dwError], b1 ,MB_OK 
    


and i got error code 317,
translated to
ERROR_MR_MID_NOT_FOUND
317 (0x13D)
The system cannot find message text for message number 0x%1 in the message file for %2.
Post 20 May 2013, 17:37
View user's profile Send private message Reply with quote
DerTobi



Joined: 20 May 2013
Posts: 10
Location: DE
DerTobi 20 May 2013, 19:00
Ah i used an unvalid errorcode, thx you Sleep.
But i can´t compile your second code.

b1 and @f are unkowed!
Post 20 May 2013, 19:00
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 13462
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 21 May 2013, 02:20
b1 rb 0xff

@f is a Jump to next @@:
@b is a jump to previous @@:

Enjoy,

But i still wonder why 500 doesnt work,
and how the way they store message, inside each dll or one dll,

Because google said, under compiler, they are in winerror.h something,
Post 21 May 2013, 02:20
View user's profile Send private message Reply with quote
DerTobi



Joined: 20 May 2013
Posts: 10
Location: DE
DerTobi 21 May 2013, 22:27
Ah thx again Sleep, i enjoy it.
Post 21 May 2013, 22:27
View user's profile Send private message Reply with quote
Force



Joined: 12 Jun 2012
Posts: 29
Force 21 May 2013, 23:46
Maybe you can try this code

Code:
              format PE GUI 4.0
include 'D:\FASM\INCLUDE\WIN32AX.INC'

entry start

Section '.data' data readable writeable


        hWnd    dd ?
        buf     dd ?
       _title    db "Test",0


section '.code' code readable executable

; Entry point
start:
       mov ebx,500
       invoke  SetLastError, ebx

       invoke GetLastError
        mov [hWnd],eax
        lea eax,[buf]
       invoke   FormatMessage,FORMAT_MESSAGE_ALLOCATE_BUFFER+FORMAT_MESSAGE_FROM_SYSTEM,0,ebx,0,eax,0,0

invoke  MessageBox,[hWnd],[buf],_title,MB_ICONERROR+MB_OK

        invoke ExitProcess,0


section ".idata" import data readable writable
        library kernel32, 'kernel32.dll', user32, 'USER32.DLL'
        
        include "d:\fasm\include\api\kernel32.inc"
        include "d:\fasm\include\api\user32.inc"

    
Post 21 May 2013, 23:46
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 13462
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 22 May 2013, 09:11
@Force,
i dont quite get it,
the return value from GetLastError in EAX is 500,
why you want to use that value for MessageBox?

and
lea eax,[buf] doesnt seems right, because buf is ? unknown value.
Post 22 May 2013, 09:11
View user's profile Send private message Reply with quote
Force



Joined: 12 Jun 2012
Posts: 29
Force 22 May 2013, 10:28
hi Slepp ...

There is a similar example here

http://board.flatassembler.net/topic.php?t=14582
Post 22 May 2013, 10:28
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 13462
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 22 May 2013, 10:47
i assume that is different because it was inside a proc,
Post 22 May 2013, 10:47
View user's profile Send private message Reply with quote
Force



Joined: 12 Jun 2012
Posts: 29
Force 22 May 2013, 11:29
Yes it is confusing a bit

then simpliest way ...

Code:
format PE GUI 4.0
include 'fasm\include\win32ax.inc'

entry start

Section '.data' data readable writeable

 buffer db MAX_PATH dup(?)
 title db "FORCE",0

section '.code' code readable executable


start:

       invoke  SetLastError,500
       invoke GetLastError
       mov edi,eax

 invoke FormatMessage,FORMAT_MESSAGE_FROM_SYSTEM,\
                           NULL,edi,0,buffer,512,NULL
 invoke MessageBox,NULL,buffer,title,MB_ICONERROR

 invoke ExitProcess,0


section ".idata" import data readable writable
        library kernel32, 'kernel32.dll', user32, 'USER32.DLL'
        
        include "\fasm\include\api\kernel32.inc"
        include "\fasm\include\api\user32.inc"
    
Post 22 May 2013, 11:29
View user's profile Send private message Reply with quote
randomdude



Joined: 01 Jun 2012
Posts: 83
randomdude 22 May 2013, 14:35
for what do you need to call GetLastError/SetLastError?

and if you are using FORMAT_MESSAGE_ALLOCATE_BUFFER:

Quote:
The caller should use the LocalFree function to free the buffer when it is no longer needed.


this is what i use

Code:
proc ShowLastError

locals
        syserroraddr dd ?
        tempstring rb 100h
endl

        push    ebx edi esi
        invoke  GetLastError
        mov     ebx,eax
        test    ebx,ebx
        jz      .end
        lea     edi,dword[tempstring]
        cinvoke wsprintf,edi,lpfmt,errorstring,ebx
        lea     esi,dword[syserroraddr]
        invoke  FormatMessage,FORMAT_MESSAGE_ALLOCATE_BUFFER+FORMAT_MESSAGE_FROM_SYSTEM,0,ebx,LANG_NEUTRAL,esi,0,0
        invoke  MessageBox,0,dword[esi],edi,MB_ICONERROR+MB_OK
        invoke  LocalFree,dword[esi]
        mov     eax,ebx
        .end:
        pop     esi edi ebx
        ret
endp

errorstring     db 'ERROR :',0
lpfmt           db '%s %#x',0     
Post 22 May 2013, 14:35
View user's profile Send private message Reply with quote
DerTobi



Joined: 20 May 2013
Posts: 10
Location: DE
DerTobi 24 May 2013, 20:02
@Sleep
500 was just for testing and not important.

@Force
yes i found this example too, so i tried to import it without the use of a dll
and your code doesnt work too, but thx you for your participation.

@random
Your solution i do realy like , thx.
I need it to debug win api´s.
Post 24 May 2013, 20:02
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.