News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Typical problem

Started by hador, January 05, 2005, 01:22:46 AM

Previous topic - Next topic

hador

Hi..

I want make simply thing : connect MessageBox and wsprintf (probably you see this problem many times  :8) )

...include file...
;
; INCLUDEs
;
   include windows.inc
   include user32.inc
   include kernel32.inc
;
; LIBRARIEs
;
   includelib user32.lib
   includelib kernel32.lib

;
; PROTOTYPEs
;
WinMain     proto   :DWORD, :DWORD, :DWORD, :DWORD
MessageBoxf proto C :DWORD, :DWORD, :DWORD, :DWORD, :VARARG

;
; DATA?
;
.data?
   hInstance HINSTANCE ?
   CommandLine LPSTR ?


..source file...

.586
.model flat,stdcall
option casemap:none

   include math.inc

.data
Text db "Some number: %d",0
Capt db "I made it",0

.code
start:
invoke GetModuleHandle, NULL
mov    hInstance,eax
invoke GetCommandLine
mov    CommandLine,eax
invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT
invoke ExitProcess,eax


WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD

invoke MessageBoxf,NULL, ADDR Text, ADDR Capt, MB_OK, 94000

xor eax, eax
ret
WinMain endp

MessageBoxf proc C hwnd:HWND, lpText:LPCTSTR, lpCaption:LPCTSTR, uType:UINT, args:VARARG
LOCAL buffer[1024]:BYTE

mov eax, esp
push args
push lpText
push eax
call wsprintf
;invoke wsprintf, ADDR buffer, lpText, args ;<== error A2114: INVOKE argument type mismatch : argument : 0
invoke MessageBox, hwnd, ADDR buffer, lpCaption, uType

ret
MessageBoxf endp

end start


This code is working but i don't like it . I would like to use syntax similar to:

MessageBoxf proc C hwnd:HWND, lpText:LPCTSTR, lpCaption:LPCTSTR, uType:UINT, args:VARARG
LOCAL buffer[1024]:BYTE

invoke wsprintf, ADDR buffer, lpText, args  ;<== error A2114: INVOKE argument type mismatch : argument : 0
invoke MessageBox, hwnd, ADDR bufor, lpCaption, uType

ret
MessageBoxf endp


--Hador

petezl

Hador,
You seem to be mixed up with many things. You need to start with getting some simple working code before experimenting. To make life easier, see if you can get hold of an early masm32 download package with less macros, it will be easier to learn from. Or look at the messageBox example on the Test Departments site.

Here is some guides to the code you supplied:

Quote
1) Make sure the right paths are added to the includes.
2) No need for some prototypes (like MessageBox) when you include windows.inc.
3) Why not use .386 if the code is simple enough?
4) No need for adding WinMain Prototype, (it's not used here!).
5) Your program is left in memory because ExitProcess hasn't been called.on your second example.

Here is a simple MessageBox Program. The flow is obvious:

.386
.model flat, stdcall
option casemap :none

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

.data
szDlgTitle db "Minimum MASM",0
szMsg db " --- Assembler Pure and Simple --- ",0

.code
start:

invoke MessageBox,0,ADDR szMsg,ADDR szDlgTitle,MB_OK
invoke ExitProcess,0

end start


Just out of interest, wsprintf is an awfully slow and somewhat awkward thing to write. Much better to use other ways to produce text. If you feel you need to use it then here is a simple guide I made up some time ago.

.386
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
;Version one: CtrlVar uses variables: note how wsprintf is arranged
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
; .data
; Heading db "Using wsprintf",0
; Chapter db "answer=",0
; Result dd 12345
; CtrlVar db " %s def %lu ",0 ; arranged as text o/p, "text", number o/p
;
; .data?
; TargetBuffer db 62 dup(?)
;
; .code
; start:
;
; ; 1= Target buffer, 2= printing code, 3= offset of text wanted, 4= data(note no offset)
; invoke wsprintf,addr TargetBuffer, addr CtrlVar,addr Chapter,Result
;
; invoke MessageBox,NULL,addr TargetBuffer,addr Heading,MB_OK
;
; invoke ExitProcess,0h
; end start
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
;Version Two: CtrlVar uses immediate values for Text: note how wsprintf is arranged
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

.data
Heading db "Using wsprintf",0
Result dd 12345
CtrlVar db " Answer= %lu bytes",0 ; arranged as immediate "text", number o/p, immediate "text"

.data?
TargetBuffer db 62 dup(?)

.code
start:

; 1= Target buffer, 2= printing code, 3= data(note no offset)
invoke wsprintf,addr TargetBuffer, addr CtrlVar,Result

invoke MessageBox,NULL,addr TargetBuffer,addr Heading,MB_OK
invoke ExitProcess,0h
end start


Hope this helps!
Peter.



Cats and women do as they please
Dogs and men should realise it.

hador

What can I say... thank you...

petezl

Cats and women do as they please
Dogs and men should realise it.