vba - How do I pass an array of arguments ByRef with CallByName? -
i using callbyname dynamically call methods. there several methods pick daily table in server along arguments. reason, send array of arguments callbyname rather param array don't know number of arguments until runtime. given callbyname expects paramarray use private declare function bypass vba type definition.
private declare ptrsafe function rtccallbyname lib "vbe7.dll" ( _ byval object object, _ byval procname longptr, _ byval calltype vbcalltype, _ byref args() any, _ optional byval lcid long) variant public function callbynamemethod(object object, procname string, byref args () variant) assignresult callbynamemethod, rtccallbyname(object, strptr(procname), vbmethod, args) end function private sub assignresult(target, result) if vba.isobject(result) set target = result else target = result end sub
this works when pass object method changes underlying properties. however, there methods pass object , method changes values of passed arguments. example, pass array following arguments
dim name string, value1 double, value2 double, value3 double dim array(3) variant string = "name" value1 = 0 value2 = 0 value3 = 0 array(0) = name array(1) = value1 array(2) = value2 array(3) = value3
when pass array, method returns array same values, expecting double type values array(1), array(2), array(3). ideas?
the first clue answer lies in function declaration rtccallbyname (pulled exports table of vbe7.dll):
function callbyname(object: idispatch; procname: bstr; calltype: vbcalltype; args: ^safearray; out lcid: i4): variant; stdcall;
note args
declared pointer safearray
, not declared out
parameter. means com contract function saying if pass paramarray
guarantee makes won't change pointer paramarray
itself. byref
in declare function
indicates passing pointer.
as values inside paramarray
, there isn't microsoft documentation can dig specific vba, vb.net version of documentation gives second clue:
a paramarray parameter declared using byval (visual basic).
in context of callbyname
, make perfect sense. rtccallbyname
function doesn't (and can't) know of parameters called method themselves declared byref
or byval
, has to assume can't change them.
as far implementations work around limitation, i'd suggest either refactoring eliminate return values passed byref
in code called callbyname
or wrapping needed functionality in class.
Comments
Post a Comment