Hi Koustubh,
Thank you for the detailed error report - I'm able to reproduce what
you describe.
Indeed, it looks like an Excel-DNA bug. I'll have a closer look and
post back here.
Regards,
Govert
* * * * * * * * * * * * * * * * * * * * * * * * * * * *
Ensure that the Excel-DNA project continues by
making your donation -
http://excel-dna.net/support/
* * * * * * * * * * * * * * * * * * * * * * * * * * * *
On May 20, 11:20 am, Koustubh Moharir <
koustubhmoha...@gmail.com>
wrote:
> Hi,
>
> I have a function which creates and returns an ExcelReference object with a
> large number of InnerReferences (using ExcelReference.AddReference). Note
> that this uses the latest commit 79078 which supports returning
> ExcelReference objects.
>
> This works well until the number of InnerReferences is less than 4095. At
> that value, Excel crashes. If I attach a debugger, I get a
> NullReferenceException in "Unknown Module" with the message "Object
> reference not set to an instance of an object". No stack trace is available.
>
> The C# code is
>
> public static object MakeUnionRangeDna(int count)
> {
> var union = new ExcelReference(0, 0);
> for (int i = 1; i < count; i++)
> {
> union.AddReference(2 * i, 2 * i, 0, 0);
> }
> return union;
> }
>
> Attached is a sample dna file and an excel workbook with macros to
> reproduce the behavior. The macro code is shown below:
> Running TestFromVBA works. It makes a union range with 6001 disjoint cells
> in the MakeUnionRangeVBA function. (The VBA code is convoluted to improve
> performance)
> Running TestFromDNA fails if the number of cells specified is greater than
> 4094.
>
> Sub TestFromVBA()
> Dim r As Range
> Set r = Application.Run("MakeUnionRangeVBA")
> MsgBox (r.Cells.count)
> End Sub
>
> Sub TestFromDNA()
> Dim r As Range
> Set r = Application.Run("MakeUnionRangeDna", 4094)
> MsgBox (r.Cells.count)
> End Sub
>
> Function MakeUnionRangeVBA() As Range
> Dim r As Range
> Set r = Range("A1")
> Dim res As Range
> Set res = r
> For i = 1 To 200
> Dim unionaddress As String
> unionaddress = r.Address(False, False)
> For j = 1 To 30
> Set r = r.Offset(2)
> unionaddress = unionaddress & "," & r.Address(False, False)
> Next j
> Set res = Application.Union(res, Range(unionaddress))
> Next i
> Set MakeUnionRangeVBA = res
> End Function
>
> From the managed exception message I suspect that this is a problem in the
> marshaling of large ExcelReference objects. Looking at the Excel DNA source
> code, I see this:
>
> Commit: 79078
> File XlCustomMarshal.cs
> Line 1269
>
> if (!isExcel4v)
> {
> // For big allocations, ensure that Excel allows us to free
> the memory
> * if (rows * columns * 16 + cbNativeStrings + numReferences
> * 8 > 65535)*
> pOper->xlType |= XlType.XlBitDLLFree;
>
> // We are done
> return pNative;
> }
>
> 4095 * 16 = 65530. I think I am hitting the limit, and beyond that limit
> something is going wrong.
>
> Can someone help me in tracing the issue further? I do not have the full
> version of Visual Studio at the moment and the express version does not
> allow me to build Excel DNA from source (because of a dependency on
> AtlBase.h)
>
> Regards,
> Koustubh
>
> TestLargeUnionRefs.dna
> < 1KViewDownload
>
> UnionRangeSample.xlsm
> 17KViewDownload