How do I call a function from an xll add-in which could not be registered to the function wizard?

StackOverflow https://stackoverflow.com/questions/19325258

Question

I have an xll (a rather complex c++ project) exporting a function which, for historical reasons, takes a large number of arguments, and precisely 20.

This is a magic number: apparently in Excel 2003 there is a maximum number of 30 arguments (" In Microsoft Office Excel 2003, the maximum number of arguments that any function can take is 30, although most take fewer than this. "), but over 20 the function cannot be registered in the function wizard.

Now, as it happens and as you could guess, I have been required to add 3 more arguments. Ok, so the argument count goes up to 23 (at least, this function is not intended for "human consumption" but is always called by a VBA wrapper).

By attaching to the Excel process with VC++ debugger, I get at registration time an error code 4, which stands for xlretInvCount.

I have made sure that the string of comma separated argument names is shorter than 255 characters. Btw, I am using xlw 4 (old version, I know).

So, if the limit is 30 I expect to be able to invoke my function via

Application.Run("function name", ..... very long list of arguments) 

but not to use the wizard. Trouble is that VBA tells me that the function is not registered.

So, how do I properly use a function which takes more than 20 arguments and less than 30?

NB: please refrain from stating the obvious. I know where the real crux of the problem is. A refactoring is not possible at the moment.

Was it helpful?

Solution

Since you call your function via VBA you might call it directly, not even registering it, i.e., NOT USING XLW.

  • Declare in C/C++: extern “C” int __declspec(dllexport) _stdcall myfunc(double arg1, double* arg2, ...)
  • Declare in VBA: Function MyFunc& Lib "C:\myL.dll" Alias "_myfunc@1" (ByVal arg1 As Double, ByRef arg2 As Double, ...)

The @1 refers to the position in of the function in the .def file.

Now you can call the function form VBA. See http://aandreasen.wordpress.com/2008/05/05/how-to-create-a-dll-for-ms-excel-vba-with-microsoft-visual-c-2008-command-line-tools/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top