Quantcast
Channel: VBA array with lower boundary > 0 passed to .NET - Stack Overflow
Viewing all articles
Browse latest Browse all 2

VBA array with lower boundary > 0 passed to .NET

$
0
0

I have .NET Dll with a C# method, which I use in Excel VBA. It's being called through a COM-wrapper from VBA. The method takes among others, two double arrays as arguments.

When the double arrays are defined in VBA with a lower boundary of 1, and being passed to the C# method though COM, Excel fails with following error:

Runtime error 5(Invalid procedure call or argument)

Although it may sound crazy, I would want to be able to pass a VBA/COM array with lower boudary of 1 to C# .NET. Is this possible?

This is how the C# function is declared:

public double LinearInterpolation(                                double[] psaXvector,                                double[] psaYvector,                                double Xvalue){

The COM-wrapper in C#

 [Guid("CE93D637-0673-4EBC-8FFA-6CE162959262")] [ComVisible(true)] [InterfaceType(ComInterfaceType.InterfaceIsDual)] public interface IMixedTools {   [DispId(1)]   [return: MarshalAs(UnmanagedType.R8)] double    LinearInterpolation(     [MarshalAs(UnmanagedType.SafeArray)]double[] psaXvector,      [MarshalAs(UnmanagedType.SafeArray)]double[] psaYvector,      [MarshalAs(UnmanagedType.R8)]double Xvalue);

This is what the generated .tlh file looks like

struct __declspec(uuid("ce93d637-0673-4ebc-8ffa-6ce162959262"))IMixedTools : IDispatch{  virtual HRESULT __stdcall LinearInterpolation (    /*[in]*/ SAFEARRAY * psaXvector,    /*[in]*/ SAFEARRAY * psaYvector,    /*[in]*/ double Xvalue,    /*[out,retval]*/ double * pRetVal ) = 0;

The calling VBA code

  Dim mixu As Object  Set mixu = CreateObject("TlibCOM.CMixedTools")  Dim Xarr() As Double  Dim Yarr() As Double  ReDim Xarr(1 To 4) As Double  ReDim Yarr(1 To 4) As Double  Dim x As Double  Xarr(1) = 1  Xarr(2) = 2  Xarr(3) = 5  Xarr(4) = 7.5  Yarr(1) = 14  Yarr(2) = 9  Yarr(3) = 4  Yarr(4) = -3  x = 4  result = mixu.LinearInterpolation(Xarr, Yarr, x)  Set mixu = Nothing

If I define the arrays with lower boundaries of 0, the function will work. But I have a quite large legacy codebase where arrays may be defined either with 0 or 1 as lower boundary of arrays, so the functions having arrays as arguments would need to be able to handle this.

The C# .NET DLL is a library of methods, replacing legacy ATL COM functions. Previously this was not an issue whether the double array passed from VBA was having a low boundary of 1 or 0.

Is there a way to go round this limitation, so that VBA can pass an array with the low boundary of 1, to C# methods in a DLL?


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images