diff --git a/source/Common/Common.VBIDETools/Integration/ComTools.cs b/source/Common/Common.VBIDETools/Integration/ComTools.cs
index 3baf8a1..a27271e 100644
--- a/source/Common/Common.VBIDETools/Integration/ComTools.cs
+++ b/source/Common/Common.VBIDETools/Integration/ComTools.cs
@@ -113,7 +113,18 @@ private static object GetComObjectFromROTByFileName(string fileName)
private static object GetOfficeComObjectByProgIDAndVBEfromFirstObjectInROT(string progID, VBE vbe)
{
- var comInstance = Marshal.GetActiveObject(progID);
+ object comInstance;
+ try
+ {
+ comInstance = Marshal.GetActiveObject(progID);
+ }
+ catch (Exception ex)
+ {
+ // e.g. MK_E_UNAVAILABLE: no active object registered for this ProgID => continue with the ROT search
+ Logger.Log(ex);
+ return null;
+ }
+
if (comInstance is null)
return null;
@@ -153,6 +164,11 @@ private static object GetOfficeComObjectByProgIDAndVBEfromROT(string progID, VBE
return comInstance;
}
}
+ catch (Exception ex)
+ {
+ // a faulty ROT entry must not abort the search
+ Logger.Log(ex);
+ }
finally
{
ReleaseComObject(moniker);
@@ -282,6 +298,8 @@ public static Type GetTypeForComObject(object comObject, string progID)
using (new BlockLogger("comObject: " + comObject.GetType().Name))
{
var comType = Type.GetTypeFromProgID(progID);
+ if (comType is null)
+ return null; // ProgID not registered
// get the com object and fetch its IUnknown
var iunkwn = Marshal.GetIUnknownForObject(comObject);
diff --git a/source/Common/Common.VBIDETools/OfficeApplicationHelper.cs b/source/Common/Common.VBIDETools/OfficeApplicationHelper.cs
index 8ce15b9..019df25 100644
--- a/source/Common/Common.VBIDETools/OfficeApplicationHelper.cs
+++ b/source/Common/Common.VBIDETools/OfficeApplicationHelper.cs
@@ -67,7 +67,7 @@ protected virtual VBProject GetCheckedVbProject()
try
{
var activeDocumentFullName = FileTools.ConvertPathToUNC(GetDocumentFullName());
- if (string.IsNullOrEmpty(activeDocumentFullName) && !VbProjectMatchWithFullName(activeVBProject, activeDocumentFullName))
+ if (IsProjectLookupRequired(activeDocumentFullName, fullName => VbProjectMatchWithFullName(activeVBProject, fullName)))
{
activeVBProject = FindVbProjectEqualsFullName(activeDocumentFullName);
}
@@ -80,6 +80,15 @@ protected virtual VBProject GetCheckedVbProject()
}
}
+ ///
+ /// The active VBProject has to be replaced only if the active document is known
+ /// and the active VBProject does not belong to it.
+ ///
+ public static bool IsProjectLookupRequired(string activeDocumentFullName, Func activeVbProjectMatches)
+ {
+ return !string.IsNullOrEmpty(activeDocumentFullName) && !activeVbProjectMatches(activeDocumentFullName);
+ }
+
private string GetDocumentFullName()
{
// TODO: The strings are not sufficient to be able to invoke the infos. We also need the kind of invocation (method or getter call).
diff --git a/source/Tests/AccessCodeLib.Common.VBIDETools.Tests/AccessCodeLib.Common.VBIDETools.Tests.csproj b/source/Tests/AccessCodeLib.Common.VBIDETools.Tests/AccessCodeLib.Common.VBIDETools.Tests.csproj
index a7b338e..17608d8 100644
--- a/source/Tests/AccessCodeLib.Common.VBIDETools.Tests/AccessCodeLib.Common.VBIDETools.Tests.csproj
+++ b/source/Tests/AccessCodeLib.Common.VBIDETools.Tests/AccessCodeLib.Common.VBIDETools.Tests.csproj
@@ -126,6 +126,10 @@
+
+ ..\..\packages\ThammimTech.Microsoft.Vbe.Interop.15.0.0\lib\net20\Microsoft.Vbe.Interop.dll
+ False
+
False
True
@@ -149,6 +153,7 @@
+
diff --git a/source/Tests/AccessCodeLib.Common.VBIDETools.Tests/HostLookupTests.cs b/source/Tests/AccessCodeLib.Common.VBIDETools.Tests/HostLookupTests.cs
new file mode 100644
index 0000000..03597ad
--- /dev/null
+++ b/source/Tests/AccessCodeLib.Common.VBIDETools.Tests/HostLookupTests.cs
@@ -0,0 +1,39 @@
+using AccessCodeLib.Common.VBIDETools.Integration;
+using NUnit.Framework;
+
+namespace AccessCodeLib.Common.VBIDETools.Tests
+{
+ [TestFixture]
+ public class HostLookupTests
+ {
+ [TestCase(null, false, ExpectedResult = false, Description = "unknown document: keep active project")]
+ [TestCase("", false, ExpectedResult = false, Description = "unknown document: keep active project")]
+ [TestCase(@"C:\Test\a.xlsm", true, ExpectedResult = false, Description = "active project matches: keep it")]
+ [TestCase(@"C:\Test\a.xlsm", false, ExpectedResult = true, Description = "active project does not match: look up")]
+ public bool IsProjectLookupRequired_ReturnsExpected(string fullName, bool activeProjectMatches)
+ {
+ return OfficeApplicationHelper.IsProjectLookupRequired(fullName, _ => activeProjectMatches);
+ }
+
+ [Test]
+ public void IsProjectLookupRequired_EmptyName_DoesNotEvaluateMatch()
+ {
+ var evaluated = false;
+
+ OfficeApplicationHelper.IsProjectLookupRequired(string.Empty, _ => evaluated = true);
+
+ Assert.That(evaluated, Is.False);
+ }
+
+ [Test]
+ public void GetOfficeComObjectByProgIDAndVBE_ProgIdNotRegistered_ReturnsNullInsteadOfThrowing()
+ {
+ // not "Access.Application" => the VBE is not touched; Marshal.GetActiveObject throws for an unknown ProgID
+ object result = null;
+
+ Assert.DoesNotThrow(() => result = ComTools.GetOfficeComObjectByProgIDAndVBE("AccUnit.NotRegistered.Application", null));
+
+ Assert.That(result, Is.Null);
+ }
+ }
+}
diff --git a/vbe-add-In/AccUnit.VbeAddIn/AddInManager.cs b/vbe-add-In/AccUnit.VbeAddIn/AddInManager.cs
index e78a6a4..1bfc274 100644
--- a/vbe-add-In/AccUnit.VbeAddIn/AddInManager.cs
+++ b/vbe-add-In/AccUnit.VbeAddIn/AddInManager.cs
@@ -284,9 +284,17 @@ private void InitOfficeApplicationHelper(object hostApplication = null)
using (new BlockLogger())
{
// Note: if load RubberDuck, an instance of Access stay in memory after close => HostApplicationTools.GetOfficeApplicationHelper(..., ..., true);
+ var previousHelper = _officeApplicationHelper;
_officeApplicationHelper = HostApplicationTools.GetOfficeApplicationHelper(VBE, ref hostApplication, true);
_vbeIntegrationManager.OfficeApplicationHelper = _officeApplicationHelper;
_testSuiteManager.OfficeApplicationHelper = _officeApplicationHelper;
+
+ // Dispose the previous helper only after the new one exists: both may wrap the same
+ // application RCW, whose reference count must not drop to 0 in between.
+ if (previousHelper != null)
+ {
+ DisposeManagedResource(previousHelper);
+ }
}
}
@@ -447,11 +455,9 @@ protected virtual void Dispose(bool disposing)
private void DisposeUnmanagedResources()
{
- if (_addIn != null)
- {
- Marshal.ReleaseComObject(_addIn);
- _addIn = null;
- }
+ // The AddIn RCW is owned by Connect (it receives it in OnConnection and releases it once in Dispose),
+ // so it is only dereferenced here. A second ReleaseComObject would throw InvalidComObjectException.
+ _addIn = null;
}
private void DisposeManagedResources()