Excel crashes when passing an error as a bool parameter

Nov 7, 2014 at 1:00 PM

Since upgrading to ExcelDNA from, we've been getting crashes when using bool parameters. It's quite easy to reproduce - using Excel 2013 (or probably any other version?), just define a function like this:
[ExcelFunction(IsExceptionSafe = true)]
public static object MyTestFunction(bool testParam)
    return 0;
When you call it with one of these, where A1 contains a div by zero error:
You sometimes get a NullReferenceException in one of ExcelDNA's wrapper functions, but either way Excel crashes. You can get round it by either using an object parameter or by marking it not Exception-safe.

Is there anything we can do about this? It looks like the code in XlBooleanMarshaler.MarshalNativeToManaged is returning null on purpose, but I guess some code elsewhere isn't handling it.

Nov 11, 2014 at 3:28 PM
Hi Antonio,

I can recreate this bug. It's kind of similar to this case: https://exceldna.codeplex.com/workitem/9393

The solution is likely to be to just disallow (or ignore) the IsExceptionSafe flag for functions taking bools.
Basically when something is passed to the function that cannot be converted to a bool, we need to return #VALUE. But without the IsExceptionSafe, there is no wrapper installed that catches the error to convert to #VALUE and return.
It's an oversight in Excel-DNA, but IsExceptionSafe is a certainly a risky optimization.

Nov 12, 2014 at 12:55 PM
Hi Govert,

Ah, yes it's very similar to that, thanks. Also, thanks for the solution - I'll either do as you say or switch to object for now, where the problem doesn't occur.