flat assembler
Message board for the users of flat assembler.

Index > Windows > wsprintf/wvsprintf

Author
Thread Post new topic Reply to topic
booter



Joined: 08 Dec 2006
Posts: 67
booter
I can't make it work Mad
I tried both wvsprintf and wsprintf, all combinations with/without addr
Please help!

Thanks

Code:
format PE console
use32
entry start

include '%fasminc%\win32ax.inc'
section '.data' data readable writeable
TstStr        db "ABC ",0
Output        db 100 dup (0)
FmtStr        db "%s",0
StdOutHdl     dd 0
WrkAddr       dd 0


section '.code' code readable executable
start:
        invoke  GetStdHandle, STD_OUTPUT_HANDLE
        mov     [StdOutHdl],eax
        stdcall WriteStdOut,"Started "
        ; if I comment next line it doesn't crash
        cinvoke wvsprintf,addr Output,addr FmtStr,addr TstStr
        stdcall WriteStdOut,Output
        stdcall WriteStdOut,"Ended"
        invoke  ExitProcess,0

proc WriteStdOut lpStr:DWORD
  local OutLen:DWORD
  invoke  lstrlen,[lpStr]
  invoke  WriteFile, [StdOutHdl],[lpStr],eax,addr OutLen,NULL
  ret
endp

section '.idata' import data readable writeable
library kernel32,'KERNEL32.DLL'
include '%fasminc%\api\kernel32.inc'
library user32,'USER32.DLL'
include '%fasminc%\api\user32.inc'
    
Post 21 Apr 2009, 06:31
View user's profile Send private message Reply with quote
pete



Joined: 20 Apr 2009
Posts: 110
pete
Well, just a guess: if using wvsprintf, no c-calling conventions are needed; they are needed only for wsprintf!
Post 21 Apr 2009, 06:46
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17278
Location: In your JS exploiting you and your system
revolution
The third argument must a be a pointer to a list of pointers.
TFM wrote:
int wvsprintf(
LPTSTR lpOutput, // pointer to buffer for output
LPCTSTR lpFormat, // pointer to format-control string
va_list arglist // variable argument list of format-control arguments
);
It is not c-call. Use invoke.
Code:
varargs dw TstStr
...
invoke wvsprintf,Output,FmtStr,varargs    
Post 21 Apr 2009, 07:37
View user's profile Send private message Visit poster's website Reply with quote
pete



Joined: 20 Apr 2009
Posts: 110
pete
That doesn't work either, revolution. It seems FASM has a problem importing the wsprintf function from user32... When debugging the application with ollydbg, the jump to wsprintf won't work.
Post 21 Apr 2009, 07:45
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17278
Location: In your JS exploiting you and your system
revolution
You have two libraries. Use this:
Code:
library kernel32,'KERNEL32.DLL',\
  user32,'USER32.DLL'
include 'api\kernel32.inc'
include 'api\user32.inc'    
Post 21 Apr 2009, 08:01
View user's profile Send private message Visit poster's website Reply with quote
pete



Joined: 20 Apr 2009
Posts: 110
pete
Yep this works now!
Post 21 Apr 2009, 08:17
View user's profile Send private message Reply with quote
booter



Joined: 08 Dec 2006
Posts: 67
booter
revolution wrote:
You have two libraries. Use this:
Code:
library kernel32,'KERNEL32.DLL',\
  user32,'USER32.DLL'     

It took me some time to get what you meant Smile
Who could imagin that
Code:
 library x,'X', y,'Y'     

may be not the same as
Code:
library x,'X'
library y,'Y'     
Question

Should it be considered a bug in FASM "include" ?
Post 22 Apr 2009, 03:03
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17278
Location: In your JS exploiting you and your system
revolution
It is a gotcha. The macros need to be treated with caution when trying to use them in different ways than the original intention.
Post 22 Apr 2009, 05:31
View user's profile Send private message Visit poster's website Reply with quote
booter



Joined: 08 Dec 2006
Posts: 67
booter
This is the final version of my test program.
Code:
; Demonstration of wvsprintf
; Pablic Domain.
; Output:
;Started<ABC abcd1234
;12345 ABC>Ended
format PE console
use32
entry start

include '%fasminc%\win32ax.inc'
section '.data' data readable writeable
StdOutHdl     dd 0
TstStr        db "ABC",0
TstHex        dd 0ABCD1234h
TstDec        dd 12345
EOL           db 10,13,0

macro WriteFmt formatstr,[arg]
 {
  common
    push  eax
    size@args = 4
    if ~ arg eq
      reverse
        pushd arg
        size@args = size@args+4
      common
    end if
    mov   eax,esp
    pushd eax
    stdcall WriteFmtLst,formatstr,eax
    if size@args
      add esp,size@args
    end if
    pop  eax
  }

section '.code' code readable executable
start:
        invoke  GetStdHandle, STD_OUTPUT_HANDLE
        mov     [StdOutHdl],eax
        stdcall WriteStdOut,"Started"
        WriteFmt "<%s %x%s%u %s>",TstStr,[TstHex],EOL,[TstDec],TstStr
        stdcall WriteStdOut,"Ended"
        invoke  ExitProcess,0

proc WriteFmtLst fmtstr:DWORD,alist:DWORD
  local strbuf[1024]:BYTE
  stdcall  [wvsprintf],addr strbuf,[fmtstr],[alist]
  stdcall  WriteStdOut,addr strbuf
  ret
endp

proc WriteStdOut lpStr:DWORD
  local OutLen:DWORD
  invoke  lstrlen,[lpStr]
  invoke  WriteFile, [StdOutHdl],[lpStr],eax,addr OutLen,NULL
  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'    
Post 23 Apr 2009, 05:37
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-2020, Tomasz Grysztar.

Powered by rwasa.