The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: chemicalNova on May 05, 2005, 05:09:24 PM

Title: wsprintf [resolved]
Post by: chemicalNova on May 05, 2005, 05:09:24 PM
Howdy all,

I know how sprintf() works in C++, but was wondering as to the syntax in MASM? I'm trying to set the title of my application, by combining the currently open filename, and the application title. I believe this is the standard? "Filename - Program name". The parameter I'm using is of a DWORD type, and I'm wondering if this will work with wsprintf? Actually, I'm kinda

Ta in advance, :8)

chem
Title: Re: wsprintf
Post by: Nilrem on May 05, 2005, 05:29:44 PM

    invoke wsprintf, addr Buffer, addr var1, addr var1
    invoke StdOut, addr Buffer

Hope this helps.
Title: Re: wsprintf
Post by: chemicalNova on May 05, 2005, 05:33:44 PM
Hey Nilrem,

It kinda does, but I kinda need a bit more. For example, a translation of this would be..

sprintf (buffer, "%d plus %d is %d", a, b, a+b);

?

I just need a comparison/conversion from C++ to get it..

Thanks in advance, :8)

chem
Title: Re: wsprintf
Post by: chemicalNova on May 05, 2005, 05:50:51 PM
Ok, I got that working, it assembles fine, but when I try and set the window title, it crashes (the program, not my PC).

szWinDocTitleBuff "%s - %s",0

...

invoke SetWinDocTitle,szFileName
invoke SetWindowText,hWin,szWinDocTitleBuff

...

SetWinDocTitle proc szFN:DWORD
;set the title of the editor, as "Filename - Cdelia Edit"
invoke wsprintf,szWinDocTitleBuff,szFN,szDisplayName
SetWinDocTitle endp

Does that look correct?

Ta in advance,

chem :8)
Title: Re: wsprintf
Post by: Nilrem on May 05, 2005, 05:59:06 PM
I think I understand your question, well what you would do is define a or b as %d.
Well...

--
int wsprintf(

    LPTSTR lpOut,   // pointer to buffer for output
    LPCTSTR lpFmt,    // pointer to format-control string
    ...   // optional arguments
   );
--

so you need a buffer
addr buffer
and a format-control string
so in .data
FormatString db "%d",0
String db "Hi",0

so...


invoke wsprintf, addr buffer, addr FormatString, addr SomeString
invoke StdOut, addr buffer


hmmm I think that is right, not really sure.
Title: Re: wsprintf
Post by: Nilrem on May 05, 2005, 06:00:40 PM
No idea for the second question sorry.
Title: Re: wsprintf
Post by: chemicalNova on May 05, 2005, 06:04:07 PM
It still crashes, but assembles without error. Is anyone able to shed some light on the wsprintf function, and how I can use it to change the text of my application, in this format: "Filename - Application Name"?

Sorry, it's just bugging me. I'll keep trying. If I can't get it soon, I'll just sleep on it :)

chem
Title: Re: wsprintf
Post by: QvasiModo on May 05, 2005, 08:49:19 PM
Does this link help?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/resources/strings/stringreference/stringfunctions/wsprintf.asp
Title: Re: wsprintf
Post by: SamiP on May 05, 2005, 09:02:28 PM
Hope this helps.


.486
.MODEL FLAT, STDCALL
option casemap:none

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


Main PROTO

.const

.data
FileName db "This is your filename", 0
AppName db "This is your Applications name", 0
FormatString db "%s - %s", 0
OutputBuffer db 1024 dup (0)

.data?

.code
Start:
invoke Main
invoke ExitProcess, eax

Main proc
invoke wsprintf, ADDR OutputBuffer, ADDR FormatString, ADDR FileName, ADDR AppName
invoke StdOut, ADDR OutputBuffer
xor eax, eax

Exitus:
ret
Main endp

end Start
Title: Re: wsprintf
Post by: chemicalNova on May 06, 2005, 03:26:53 AM
I got it working all :8)

Thanks for the help guys, those were both very helpful.

invoke wsprintf,ADDR szWinDocTitleBuff, ADDR szWinDocTitleFormat, ADDR szFileName, ADDR szDisplayName
;invoke StdOut,ADDR szWinDocTitleBuff
;xor eax,eax
invoke SetWindowText,hWin,ADDR szWinDocTitleBuff

Ta :8)

chem
Title: Re: wsprintf [resolved]
Post by: pbrennick on May 06, 2005, 03:59:44 AM
chemicalNova,

I see you have solved your problem but I would do it slightly different.


invoke wsprintf,ADDR szWinDocTitleBuff, ADDR szWinDocTitleFormat, ADDR szFileName, ADDR szDisplayName
xor eax,eax
invoke SetWindowText,hWin,EAX   ; Sends a NULL so as to clear the caption first
invoke SetWindowText,hWin,ADDR szWinDocTitleBuff


Your method of building the filename buffer is simpler then the one I always use, though, I employ a subroutine to build the string without using wsprinf (never cared for it).  Also, my method will build it with the name of the editor followed by [UNTITLED] until a file is actually opened.  And it will append an asterisk if the file has been modified, think about that.

Paul