Call Excel / DLL / XLL functions from C #

I have a special function in Excel addin (xll). The addon is proprietary and we do not have access to the source code. However, we need to call some functions contained in the add-on, and we would like to call it from the C # program.

Currently, I was thinking of writing a C ++ interface calling an Excel function using xlopers, and then calling that C ++ interface from C #.

Does anyone who has previous experience with this kind of problem know what would be the best solution for this?

Anthony

+7
c ++ c # excel xll
source share
5 answers

You need to create fake xlcall32.dll , put it in the same directory as your XLL (do not put excel's own xlcall32.dll in PATH). Here is the code:

# include <windows.h> typedef void* LPXLOPER; extern "C" void __declspec(dllexport) XLCallVer ( ) {} extern "C" int __declspec(dllexport) Excel4 (int xlfn, LPXLOPER operRes, int count,... ) { return 0; } extern "C" int __declspec(dllexport) Excel4v(int xlfn, LPXLOPER operRes, int count, LPXLOPER far opers[]) {return 0;} 

Now suppose I have an XLL called xll-dll.xll with a function called (use "depend.exe" to find out the names of the exported functions) xlAdd, which adds two doubles well: extern "C" __declspec (dllexport) XLOPER * __cdecl xlAdd (XLOPER * pA, XLOPER * pB);

The following code calls it:


 # include <windows.h> # include <iostream> // your own header that defines XLOPERs # include <parser/xll/xloper.hpp> // pointer to function taking 2 XLOPERS typedef XLOPER * (__cdecl *xl2args) (XLOPER* , XLOPER* ) ; void test(){ /// get the XLL address HINSTANCE h = LoadLibrary("xll-dll.xll"); if (h != NULL){ xl2args myfunc; /// get my xll-dll.xll function address myfunc = (xl2args) GetProcAddress(h, "xlAdd"); if (!myfunc) { // handle the error FreeLibrary(h); } else { /// build some XLOPERS, call the remote function XLOPER a,b, *c; a.xltype = 1; a.val.num = 1. ; b.xltype = 1; b.val.num = 2. ; c = (*myfunc)(&a,&b); std::cout << " call of xll " << c->val.num << std::endl; } FreeLibrary(h); } } int main() {test();} 

My exe actually works (in my own surprise) and outputs 3 as expected. You should have some idea of ​​what your XLL expects from options. If it allocates some memory, you should check if #define xlbitDLLFree 0x4000 is installed on your type XLOPER c-> and calls back "xlAutoFree".

+4
source share

You might want to try XLL Plus http://www.planatechsolutions.com/xllplus/default.htm . It's a bit expensive, but the XLL Wrapper Libraries is exactly what you are looking for:

“Sometimes it's useful to be able to call Excel add-in functions from other environments, such as command-line programs or interactive applications written in C ++, Java, C # or Visual Basic. The Xll Wrapper toolkit contains tools, a runtime library, samples, and documentation, which will help in developing COM modules and .NET collections that integrate Excel XLL add-ons. "

+3
source share

An Excell add-in add-in can be written in C #. If so, then you can probably get around Excel at all. You may need a wrapper.

-one
source share

check if the assembly is available in the files of the program where you install plguin, the link to the assembly directly to your C # project - check the object browser for your class, method or object

-one
source share

You must be able to use reflection to access your add-on. try using Reflector to find out what is available.

-4
source share

All Articles