Author
Topic: Varargs and stdcall (Read 635 times)

I have run into problems using the VARARG keyword. According to ml VARARG cannot work with STDCALL. However, STDCALL is the default lcalling convention for masm32 programs as defined in the include files. I have written a PROC that wants VARARG in its parameter list. It's being called by a main PROC with STDCALL as its calling convention. If I define the called PROC as C then I get into mangling issues when the called PROC is invoked.

Perhaps someone could post some snippets that show how to work around this.

Thanks for the quick response. Looking it over, I decided to send the code that's failing. I'm having trouble even when I forego include /masm32/include/masm32rt.inc. I declare the whole thing as .model flat, c. The code is more complicated than needs be to illustrate the problem. This stuff assembles fine, but the linker complains about an unresolved external: _addup3.

Quote

.486.model flat, c

includelib \masm32\lib\msvcrt.lib

printf PROTO :ptr, :vararg

.DATAfrmt1 BYTE "The result of adding the three integers is %d",13,10,0

The problem is that by making the default C you add more problems to using STDCALL. Do as I suggested, write the prototype for your function as a C function.YourProc PROTO C arg1:DWORD,arg2:DWORD,extra_args:VARARG

Then you call it with "invoke" as normal.

invoke YourProc,item1,item2,var1,var2,var3

Note that with a C procedure that you write yourself, you only use RET on exit, you do not balance the stack in the procedure, MASM balances the stack from the location where you called the C function.

Thanks folks. I amended the code as the following shows. There is still a bug in my code, but all the names are resolved correctly. Hutch- you're quite right as always. STDCALL and VARARG (C) can coexist! Nicel!!

Quote

.486.model flat, stdcall

includelib \masm32\lib\msvcrt.lib

printf PROTO C :ptr, :varargaddup3 PROTO C :DWORD, :VARARG

.DATAfrmt1 BYTE "The result of adding the three integers is %d",13,10,0

generic proc SYSCALL uses esi edi Keyword:dword,Parms:vararglocal NumParms,Parm1,Parm2,Parm3mov eax,[ebp+4] ; get the return address (address after the call in the calling procedure)movzx eax,byte ptr [eax+2] ; which will contain the number of bytes to remove from the stack; **** note, this will have to be changed if more than 48 parameters are possible shr eax,2; convert to countsub eax,1; skip fixed argument(s) before the varargmov NumParms,eaxinvoke SetDlgItemInt,hWin,1003,eax,FALSEm2m Parm1,Parms[0*4]; get first argm2m Parm2,Parms[1*4]; etc.m2m Parm3,Parms[2*4]