Calling Excel/DLL/XLL functions from C#
Asked Answered
C

5

7

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

Currently, I was thinking of writing a C++ interface calling the Excel function with xlopers, then calling this C++ interface from C#.

Does anybody who has prior experience of this kind of issues know what would be the best solution for that ?

Anthony

Carmellacarmelle answered 21/12, 2009 at 15:28 Comment(2)
This might be a stupid question, but have you tried just using the xll as an ordinary dll? (An xll is just a dll with an extra interface for Excel.) Or is the problem that the functions you want to call take arguments that are meant to be supplied by Excel when it calls, and you're having trouble making the call work?Anya
Currently, I have done more or less what you said. I call the XLL as a DLL using xlopers as arguments (the function I need is taking XLOPERs as argument) in C++. Then I write a C# layer converting Object[,] into xlopers and calling the C++ function. I was just wondering if someone had an easier solution.Carmellacarmelle
M
4

You need to create a fake xlcall32.dll, put it in the same directory as your XLL (do not put excel's own xlcall32.dll in the PATH). Here is some 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 "depends.exe" to find out the names of the exported functions) xlAdd that well adds two doubles: 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 (to my own surprise), and output 3 as expected. You must have some knowledge of what your XLL actually expects for parameters. If it allocates some memory, you must check if the #define xlbitDLLFree 0x4000 is set on your XLOPER c->type, and call back "xlAutoFree".

Mil answered 16/5, 2013 at 16:14 Comment(0)
I
3

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

"Sometimes it is useful to be able to call your 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 to help with the development of COM modules and .NET assemblies that wrap Excel XLL add-ins"

Iberian answered 19/5, 2010 at 11:28 Comment(0)
J
-1

An Excell add-in DLL might be written in C#. If so, then you can probably bypass Excel altogether. Might need a wrapper though.

Jeri answered 21/12, 2009 at 15:34 Comment(1)
In this case I get the addin as-is. It is not developed by us and we do not access the source code however we would like to use some functions contained within the XLL in other langugages. I will edit my post to mention this fact.Carmellacarmelle
S
-1

check whether assembly is available in program files where you install the plguin, reference that assembly directly to your c# project - check the object browser for your class, method or object

Succinate answered 23/12, 2009 at 12:24 Comment(0)
N
-4

You should be able to use reflection to get access to your addin. try using The Reflector to see what's available.

Nonparticipation answered 30/12, 2009 at 20:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.