Calling XLL functions from VB.NET

278 views
Skip to first unread message

Sakuragaoka

unread,
Jan 18, 2011, 1:41:57 PM1/18/11
to Excel-Dna, dive...@univecom.ch
Hi,

I'm not quite sure if this is the right place for my question, but let
me give it a try anyway.

I have an XLL (or a DLL for that matter) that was written in native C+
+ code (by a third party).

It exports functions that from within VBA are declared as follows:

Public Declare Function vFunction Lib "MyXll.xll" Function(Param1 As
Variant, Param2 As Variant) As Variant

How would I call this function from within VB.NET? Can Excel.Dna help
me do that?

Thanks for helping.

Regards,

Govert van Drimmelen

unread,
Jan 18, 2011, 1:54:45 PM1/18/11
to Excel-Dna
Hi,

Sorry, Excel-DNA can't help you in that direction.

You can look at XLL+ from Planatech - under http://www.as-ltd.co.uk/xllplus/whatsnew.htm
you should look at the "XLL Wrapper Libraries".
I have no experience using it myself.

-Govert

Govert van Drimmelen

unread,
Jan 18, 2011, 6:16:19 PM1/18/11
to Excel-Dna
Oh wait - I might have misunderstood.
Do you mean to call that library from within an Excel add-in?
In that case it is certainly possible.

The tricky case I referred to is to use the .xll from a standalone
VB.NET application.

Maybe you can expand a bit on what you need to do?

-Govert

On Jan 18, 8:54 pm, Govert van Drimmelen <gov...@icon.co.za> wrote:
> Hi,
>
> Sorry, Excel-DNA can't help you in that direction.
>
> You can look at XLL+ from Planatech - underhttp://www.as-ltd.co.uk/xllplus/whatsnew.htm

Sakuragaoka

unread,
Jan 19, 2011, 3:33:05 AM1/19/11
to Excel-Dna
Govert, thanks a lot for your replies. Unfortunately you didn't
misunderstand, as indeed I want to use an XLL from a standalone VB.NET
application. The problem I think is twofold: 1) connecting to the XLL
and calling any of the functions exported, and 2) passing VARIANT
structures in and out of the function.

Problem 1) is pretty simple to solve using the DllImport directive
and, e.g. for an integer return value, assigning it to a Int32. Works
just fine.

The tricky thing is problem 2, at least for me as I fear I don't have
enough expertise in these things. I guess I would have to build my own
marshaller (but I'm not sure), but I have never done that and so I'm a
bit at a loss how to do it. I do know the memory layout of the VARIANT
structure, but how do I "translate" that into a marshaller (if indeed
I do need to)? I have found your (rather impressive, I must say)
xlCustomMarshal12.cs source file. Once translated it into *.vb, would
that help me write that marshaller (again: if I really need to)?

In this context I was wondering: using the DllImport, will the VB app
and the XLL reside in the same address space?

Thanks!

-Msrio
> > > Regards,- Hide quoted text -
>
> - Show quoted text -

Govert van Drimmelen

unread,
Jan 19, 2011, 9:23:38 AM1/19/11
to Excel-Dna
Hi,

Those .xll functions can't have VARIANT arguments. They are likely to
take Excel's XLOPER type, but could also take primitive types like
double, and Excel's special type of strings. Even the Excel-Dna code
won't sort out writing a marshaler for this in VB.NET - it depends on
the 'unsafe' feature of C# which allows it to do some pointer
manipulation.
Internally your .xll probably depends on Excel's xlcall32.dll library,
which will only work inside the Excel process.

Another option for you might be to use automation to control Excel
from your VB.NET app, load the .xll using RegisterXLL or the
Application.AddIns collection and call Application.Run to evaluate the
functions.

Indeed, DllImport loads the library into the application's address
space.

-Govert

Sakuragaoka

unread,
Jan 19, 2011, 4:14:15 PM1/19/11
to Excel-Dna
Govert,

Of course you're right, the XLL doesn't have the VARIANT type, it
actually uses the XLOPER12 type, I had made a typo. I am also grateful
for your remark about the xlcall32.dll. For the time being it doesn't
play a role (I guess it's because I am not calling back into the Excel
C API from within the XLL), but that might change in the future, so
this is definitely a point to remain vigilant of.

Meanwhile I have succeeded (not least thanks to the XLOPER12 memory
layout definition in your xlCustomMarshal12.cs code!) to pass XLOPER12
return values to the VB.NET app. Almost, that is, as I still haven't
manages to access the array of an XlOper12Array type (access to "rows"
and "columns" works just fine). Actually I can access the first
element of the array, but not the remaining ones.

Any ideas?

-Mario
> > > - Show quoted text -- Hide quoted text -
Reply all
Reply to author
Forward
0 new messages