Skip to content

Commit acd485f

Browse files
committed
Catching InvalidCastExceptions when getting the host application in order to not blow up.
1 parent 055d911 commit acd485f

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

Rubberduck.VBEEditor/SafeComWrappers/Abstract/HostApplicationBase.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Runtime.InteropServices;
1+
using System;
2+
using System.Runtime.InteropServices;
23

34
namespace Rubberduck.VBEditor.SafeComWrappers.Abstract
45
{
@@ -23,12 +24,18 @@ private static TApplication ApplicationFromComReflection(string applicationName)
2324
TApplication application;
2425
try
2526
{
26-
application = (TApplication)Marshal.GetActiveObject($"{applicationName}.Application");
27+
application = (TApplication) Marshal.GetActiveObject($"{applicationName}.Application");
2728
}
2829
catch (COMException)
2930
{
3031
application = null; // unit tests don't need it anyway.
3132
}
33+
catch (InvalidCastException exception)
34+
{
35+
//TODO: Find out why this ever happens.
36+
_logger.Error(exception, $"Unable to cast the host Application {applicationName} to its PIA type.");
37+
application = null; //We currently really only use the name anyway.
38+
}
3239
return application;
3340
}
3441

@@ -52,6 +59,11 @@ private static TApplication ApplicationFromVbe(IVBE vbe, string applicationName)
5259
{
5360
application = null; // unit tests don't need it anyway.
5461
}
62+
catch (InvalidCastException exception)
63+
{
64+
_logger.Error(exception, $"Unable to cast the host Application {applicationName} to its PIA type.");
65+
application = null; //We currently really only use the name anyway.
66+
}
5567
return application;
5668
}
5769

0 commit comments

Comments
 (0)