'Excel XLL - how to register (xlfRegister) a function via its export ordinal?
I'm building an XLL for Excel, and am trying to register functions via XlfRegister.
According to Microsoft's documentation , the target procedure to be invoked (pxProcedure) can be given either as a string (xltypeStr) to bind to an exported name, or as a number (xltypeNum) to bind to the ordinal of the exported function in the XLL.
Has anyone ever got this to work though, or is this bugged?
I'm trying the following:
XLOPER12 name;
Excel12(xlGetName, &name, 0); // return value check not shown here, but.. this call succeeds
auto args = new LPXLOPER12[10]();
args[0] = &name; // dll name as returnd by xlGetName
args[1] = TempNum12(1); // Exported ordinal of the XLL's function to invoke (1)
args[2] = MakeStr12(L"Q"); // type text
args[3] = MakeStr12(L"Test"); // name of the registered function in Excel
args[5] = MakeInt12(1); // register as macro
XLOPER12 regResultOper;
int xlResult = Excel12v(xlfRegister, ®ResultOper, 10, args);
// memory clean up ommitted as not relevant to question
xlResult returns with 0 (xlretSuccess), however regResultOper has type xltypeErr with .val.err = 15 (xlerrValue).
If I use the very same function, but use
args[1] = TempStr12(L"invoke1"); // Exported ordinal of the XLL's function to invoke (1)
the registration works just fine.
My def file is below, and I've confirmed via dumpbin that the XLL does export invoke1 with ordinal 1.
LIBRARY
EXPORTS
xlAutoOpen
xlAutoAdd
xlAutoRemove
xlAutoClose
invoke1 @1
Is there any way to register a function via its ordinal (in practice, I want to avoid exporting by name completely due to file size of the XLL), or is this simply bugged?
I'm using Excel 2010 to test this currently.
Thanks
Solution 1:[1]
I have been looking into this (for no good reason), and I have found that the 'ordinal export number of the function to call' only works if that function has already been exported by name.
So if you create an XLL that exports three functions (func1, func2, func3) with ordinals (1,2,3) then you can register them all by name and see them appear in Excel.
If you then register func3 using 1 for pxProcedure it will appear in the function wizard but it will call func1.
If you register func3 using 2 for pxProcedure it will appear in the function wizard but it will call func2.
If you register func3 using 3 for pxProcedure it will NOT appear in the function wizard because it fails to register.
I've tested this with a few combinations (i.e. registering func2 by number and it will register for 1 and 3 but NOT 2). You can also create a func4 and map it to any of the 1,2,3 ordinal functions and it works, but it doesn't for 4.
This would also explain why you can't register a single function by number in your example because it hasn't already been exported. If you export it twice, first by name and then by ordinal 1 I am guessing it will work.
I'm not sure what the benefit of this is...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | leafcutter |