Skip to content

Commit 00e0c4f

Browse files
authored
Merge pull request #4285 from MDoerner/DirtyHackToNotBlowUpForStrangeHostSetups
Dirty hack to not blow up for hosts with unexpected COM setups
2 parents 055d911 + 7ebed46 commit 00e0c4f

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

Rubberduck.VBEEditor/SafeComWrappers/Abstract/HostApplicationBase.cs

Lines changed: 41 additions & 13 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,11 +24,24 @@ 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
}
28-
catch (COMException)
29+
catch (COMException exception)
2930
{
30-
application = null; // unit tests don't need it anyway.
31+
_logger.Error(exception, $"Unexpected COM exception while acquiring the host application object for application {applicationName} via COM reflection.");
32+
application = null; // We currently really only use the name anyway.
33+
}
34+
catch (InvalidCastException exception)
35+
{
36+
//TODO: Find out why this ever happens.
37+
_logger.Error(exception, $"Unable to cast the host application object for application {applicationName} acquired via COM reflection to its PIA type.");
38+
application = null; //We currently really only use the name anyway.
39+
}
40+
catch (Exception exception)
41+
{
42+
//note: We catch all exceptions because we currently really do not need application object and there can be exceptions for unexpected system setups.
43+
_logger.Error(exception, $"Unexpected exception while acquiring the host application object for application {applicationName} from a document module.");
44+
application = null; //We currently really only use the name anyway.
3145
}
3246
return application;
3347
}
@@ -37,20 +51,34 @@ private static TApplication ApplicationFromVbe(IVBE vbe, string applicationName)
3751
TApplication application;
3852
try
3953
{
40-
var appProperty = ApplicationPropertyFromDocumentModule(vbe);
41-
if (appProperty != null)
54+
using (var appProperty = ApplicationPropertyFromDocumentModule(vbe))
4255
{
43-
application = (TApplication)appProperty.Object;
44-
}
45-
else
46-
{
47-
application = (TApplication)Marshal.GetActiveObject($"{applicationName}.Application");
56+
if (appProperty != null)
57+
{
58+
application = (TApplication) appProperty.Object;
59+
}
60+
else
61+
{
62+
application = ApplicationFromComReflection(applicationName);
63+
}
4864
}
4965

5066
}
51-
catch (COMException)
67+
catch (COMException exception)
68+
{
69+
_logger.Error(exception, $"Unexpected COM exception while acquiring the host application object for application {applicationName} from a document module.");
70+
application = null; // We currently really only use the name anyway.
71+
}
72+
catch (InvalidCastException exception)
73+
{
74+
_logger.Error(exception, $"Unable to cast the host application object for application {applicationName} acquiered from a document module to its PIA type.");
75+
application = null; //We currently really only use the name anyway.
76+
}
77+
catch (Exception exception)
5278
{
53-
application = null; // unit tests don't need it anyway.
79+
//note: We catch all exceptions because we currently really do not need application object and there can be exceptions for unexpected system setups.
80+
_logger.Error(exception, $"Unexpected exception while acquiring the host application object for application {applicationName} from a document module.");
81+
application = null; //We currently really only use the name anyway.
5482
}
5583
return application;
5684
}

0 commit comments

Comments
 (0)