The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: boogara on October 09, 2007, 03:54:42 PM

Title: C-calling convention woes
Post by: boogara on October 09, 2007, 03:54:42 PM
Edit

Fixed my problem...!

The problem was that I didn't define the variables to be empty (ie: CustName db 256 dup(?)).

/Edit

Perhaps I'm misunderstanding this...but, when you're using wsprintf, you have to use the C-calling convention, and fix the stack pointer yourself, correct?

(Side-ish note before continuing: I have searched Google and this board, and other boards on this...)

Okay, so...first off, here's my code:


; ---------------------------- in *.INC file ------------------------------------
.data

OpenSubj db 'GIRS ID: %s - Customer Update Requested',0

.data?

hInstance dd ?
CommandLine dd ?
hWnd dd ?

StrBuffer db ? ; Used to store the return result of wsprintf

CustName db ?

; --------------------- in *.ASM file ----------------------------------

invoke GetDlgItemText,hWin,IDC_CUSTNAME,addr CustName,255

push offset CustName
push offset OpenSubj
push offset StrBuffer

call wsprintf

add ESP, 4 * 3

invoke MessageBox,hWin,addr StrBuffer,addr AppName,MB_OK


What happens is StrBuffer will show this: "GIRS ID: RS ID: [...]", when it should show this: "GIRS ID: BOB JOE [...]" for example.  Also, every time I click on the button that does the above routine, it just tacks on more " RS ID: "'s to StrBuffer...

I believe I'm overlooking or underlooking something here, but after calling wsprintf, you have to do add esp, 4 * dwords_passed, correct?

(Before you may ask, I'm not using invoke wsprintf, it's because I have another variable that takes more parameters, so I want to nip this in the butt before I try pushing for the more extremes.)
Title: Re: C-calling convention woes
Post by: Vortex on October 09, 2007, 05:46:37 PM
push offset CustName
push offset OpenSubj
push offset StrBuffer

call wsprintf

add ESP, 4 * 3


That's correct. You are passing three consecutive paramaters to the stack. Since wsprint is a C function, you do the final balancing by adding 3*4=12 bytes.
Title: Re: C-calling convention woes
Post by: boogara on October 09, 2007, 05:49:15 PM
Quote from: Vortex on October 09, 2007, 05:46:37 PM
push offset CustName
push offset OpenSubj
push offset StrBuffer

call wsprintf

add ESP, 4 * 3


That's correct. You are passing three consecutive paramaters to the stack. Since wsprint is a C function, you do the final balancing by adding 3*4=12 bytes.
...SCHWING!  (sorry, heh, just...been racking my brain over this for the past couple of weeks or so as to how it works, how to know what to add, etc...)

So, no matter what type the parameter is, I still just do 4 * [parameter count]?  So, even if, say, CustName was a DWORD (dd, is it for .data?), it'd still be 12 bytes?
Title: Re: C-calling convention woes
Post by: Vortex on October 09, 2007, 06:53:06 PM
In the 32-bit Windows environment, the stack should be always aligned to DWORD. If you have a 32-bit address or a 32-bit variable, register etc, you can safely do 4*parameter_count   You can verify by this viewing the execution of your application during an Ollydbg session ( debugger )
Title: Re: C-calling convention woes
Post by: boogara on October 10, 2007, 03:51:41 PM
Quote from: Vortex on October 09, 2007, 06:53:06 PM
In the 32-bit Windows environment, the stack should be always aligned to DWORD. If you have a 32-bit address or a 32-bit variable, register etc, you can safely do 4*parameter_count   You can verify by this viewing the execution of your application during an Ollydbg session ( debugger )
Ahhh, okay.  Thanks!  Makes it much more easier now for future adventures ^_^