The function below shows techniques for handling unfixed amounts of parameters when using the stdcall calling convention.
This level of flexibility is occasionally necessary when making interfaces for interpreters.
'-----------------------------------------------------
function callflex(long fun, long count) as long export
'=====================================================
'
'THIS PROTOCOL FOR STDCALL AND A VARIABLE NUMBER OF PARAMS
'IF DOUBLES ARE BEING PASSED BYVAL THEN ADD 1 EXTRA COUNT FOR EACH DOUBLE
'
'ACCESSING THE PARAMETERS
'------------------------
'
long param at & fun
'
'indexbase 1
'param[1]==fun
'param[2]==count
'param[3]... the rest of the params passed on the stack
'
'
if fun<=0 then return 0
'
'GET THE ADDRESS OF THE FUNCTION STORED IN THE ARRAY 'MAP'
'---------------------------------------------------------
'
sys f
f=map[fun]
'
'PASS ALL THE PARAMS FORWARD TO FUNCTION F
'-----------------------------------------
'
mov ecx,count
mov edx,ecx
add edx,7 'items on already stack: eip/ebp/edi/esi/ebx/fun/count
shl edx,2 'scale 4
add edx,ebp 'base ebp
(
dec ecx 'downcount params
jl exit
sub edx,4 'param address
push [edx] 'push param
repeat
)
call f 'call mapped function
'
'CLEAN UP THE STACK AND RETURN
'-----------------------------
'
mov edx,count 'to clean stack
add edx,2 'for fun and count params
shl edx,2 'scale 4
mov esp,ebp 'restore stack pointer (dump locals)
pop ebp 'restore regs ...
pop edi '
pop esi '
pop ebx '
pop ecx 'get return address
add esp,edx 'disallocate params
jmp ecx 'jump to return address
'
end function
Charles