2

I have found out in the answer to a previous question a way to call a function defined in a (C++) xll without registering it. I was previously using the registering infrastructure provided by XLW and I was using the XlfOper type to pass the arguments between VBA and the xll.

The c++ function goes like this:

extern "C" {
LPXLOPER EXCEL_EXPORT a_function(XlfOper arg1, XlfOper arg2);
}

Following the answer to my previous solution, I am directly declaring the function, something like that:

Declare Function an_exported_function Lib "MyDrive:\MyPath\myxll.xll"_
          Alias "a_function" (arg1 As Object, arg2 As Object) as Object

By attaching to the excel process I can see that the function is being called but I am getting garbage or null data in the arguments. This, I guess, comes from the fact that I have not told to VBA how to convert to the correct data type.

How can I correctly call an XlfOper argument? I think that calling an LPXLOPER should do the job, since xlw states the following:

It is important not to add any data members or virtual functions to this class. The design of xlw relies on the fact that class XlfOper has the same size as LPXLOPER/LPXLOPER12, this assumption allows values of those types received from Excel to be interpreted by the library as instances of XlfOper.

Community
  • 1
  • 1
Francesco
  • 3,200
  • 1
  • 34
  • 46
  • Have you tried using Application.Run from VBA? – Charles Williams Oct 23 '13 at 18:16
  • It can't work in my case since I have more than 20 arguments. The problem is described in detail in the linked question. Thanks for your suggestion @Charles. – Francesco Oct 23 '13 at 19:10
  • Aplogies, I had not read your original question. I use XLL Plus rather than XLW, and after 20 it repeats the argument description. There may be a 20 argument limit in XLW and there is certainly a function wizard limit of 20 argument descriptions but there is not a limit of 20 arguments for a function. Don't know about a limit for Application.run. Maybe try a VBA function wrapper? – Charles Williams Oct 23 '13 at 23:33
  • What values are you trying to pass? Normally you declare the VBA arguments "as range" because that is the kind of thing you want the xloper to be. – david Nov 28 '13 at 03:01
  • @david I have tried that and it didn't work. Since I needed a fix, I resorted to a less nice solution. I will check my notes and commits and see if I can find out what exactly "didn't work", maybe I'm misremembering. – Francesco Dec 05 '13 at 13:32
  • 1
    Try to define input parameters as **Variant** (instead **Object**). Does **XlfOper** is a structured data type? – Maciej Los Dec 30 '13 at 10:18
  • Have a look here: [Calling DLL Functions](http://msdn.microsoft.com/en-us/library/office/aa165195%28v=office.10%29.aspx) – Maciej Los Jan 08 '14 at 17:03

1 Answers1

1

After some research i have found it: Passing User-Defined Types

MSDN wrote: Many DLL functions require you to pass in a data structure by using a predefined format. When calling a DLL function from VBA, you pass a user-defined type that you have defined according to the function's requirements.

So, you need to define XlfOper data structure ;)

Maciej Los
  • 8,468
  • 1
  • 20
  • 35