flat assembler
Message board for the users of flat assembler.

Index > Windows > Strange wsprintf problem

Author
Thread Post new topic Reply to topic
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel 10 Jul 2008, 16:29
I have a very strange problem with wsprintf.

When I use it in a dialog box (created with DialogBoxParam) it crashes the program with a "xxxx.exe has generated errors and will be closed..." message.

However, it works when I call it before or after "DialogBoxParam".
It also works with programs that use CreateWindowEx to create windows and in "windowless" programs.

It crashes on Win2000 SP4 only. It works fine on Win98SE and WinXP.

Following is the test code. When you close the dialog box wsprintf is successfully called but if you click the button it will crash.

Any help is appreciated.


Code:
format PE GUI 4.0
entry start

include '%fasminc%\win32ax.inc'

ID_DIALOG     = 50
IDC_BTN_TEST    = 100

TITLE              equ 'test'


section '.data' data readable writeable
szAppName  db TITLE,0
szOutput  db 32 dup (0)
szFormat       db '%u',0


section '.code' code readable executable
start:         invoke  GetModuleHandle,0
           invoke  DialogBoxParam,eax,ID_DIALOG,HWND_DESKTOP,DialogProc,0

          mov     eax,2008
            invoke  wsprintf,szOutput,szFormat,eax
              invoke  MessageBox,0,szOutput,szAppName,0

               invoke  ExitProcess,0

proc               DialogProc hwnd,msg,wparam,lparam
           push    ebx esi edi
         cmp     [msg],WM_INITDIALOG
         je      wminitdialog
                cmp     [msg],WM_COMMAND
            je      wmcommand
           cmp     [msg],WM_CLOSE
              je      wmclose
             xor     eax,eax
             jmp     finish
wminitdialog:     jmp     processed

wmcommand: .if     [wparam] = BN_CLICKED shl 16 + IDC_BTN_TEST
                 mov     eax,2008
                    invoke  wsprintf,szOutput,szFormat,eax
                      invoke  MessageBox,0,szOutput,szAppName,0
                   jmp     processed
           .endif
              jmp     processed

wmclose:   invoke  EndDialog,[hwnd],0
          jmp     processed

processed: mov     eax,1
finish:            pop     edi esi ebx
         ret

endp


section      '.idata' import data readable writeable
library    kernel32,'KERNEL32.DLL',user32,'USER32.DLL'
include '%fasminc%\api\kernel32.inc'
include '%fasminc%\api\user32.inc'


section        '.rsrc' resource data readable

directory RT_DIALOG,dialogs

resource dialogs,ID_DIALOG,LANG_ENGLISH or SUBLANG_DEFAULT,demonstration

dialog demonstration,TITLE,-1,-1,100,40,WS_CAPTION or WS_POPUP or WS_SYSMENU or DS_MODALFRAME or DS_CENTER
       dialogitem 'BUTTON','test',IDC_BTN_TEST,10,10,60,15,WS_VISIBLE or BS_PUSHBUTTON
enddialog
    
Post 10 Jul 2008, 16:29
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 10 Jul 2008, 16:36
wsprintf is a CDELC function. You should call it with ccall.
Post 10 Jul 2008, 16:36
View user's profile Send private message Visit poster's website Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel 10 Jul 2008, 19:19
Quote:
wsprintf is a CDELC function.

Yes, indeed. Thank you.

Quote:
You should call it with ccall.

I prefer cinvoke.


Still, I'm very surprised. Why would it work somewhere and crash somewhere else in the same program?

I used it that way for years on Win98SE without any problem. Are Win98SE and WinXP more fault tolerant than Win2000?
Post 10 Jul 2008, 19:19
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 10 Jul 2008, 19:24
Don't think "more fault tolerant", just accept that different windows versions handle things slightly different Smile
Post 10 Jul 2008, 19:24
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 11 Jul 2008, 05:14
ManOfSteel wrote:
I prefer cinvoke.
Code:
ccall [wsprintf],...
cinvoke wsprintf,...    
Same number of characters to type, perhaps just a matter of style. But I like the ccall because it looks clearer to me that it is different from the other invokes.
Post 11 Jul 2008, 05:14
View user's profile Send private message Visit poster's website Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel 11 Jul 2008, 07:32
Yes, it's only a matter of style. I prefer it for the opposite reason of yours: it's just 1 character away from the normal "invoke" and doesn't need the extra brackets, even if I need to type as much and they both operate the same way.
Post 11 Jul 2008, 07:32
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 11 Jul 2008, 11:42
...and you can argue that brackets are more bother to type, especially if you're not using a uk/us keymap (I tend to switch from dk->uk keymap when doing longer coding sessions, though, exactly because various coding chars are annoying in dk-keymap).
Post 11 Jul 2008, 11:42
View user's profile Send private message Visit poster's website 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.