Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion source/Common/Common.VBIDETools/Integration/ComTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 10 additions & 1 deletion source/Common/Common.VBIDETools/OfficeApplicationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -80,6 +80,15 @@ protected virtual VBProject GetCheckedVbProject()
}
}

/// <summary>
/// The active VBProject has to be replaced only if the active document is known
/// and the active VBProject does not belong to it.
/// </summary>
public static bool IsProjectLookupRequired(string activeDocumentFullName, Func<string, bool> 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).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.Vbe.Interop, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL">
<HintPath>..\..\packages\ThammimTech.Microsoft.Vbe.Interop.15.0.0\lib\net20\Microsoft.Vbe.Interop.dll</HintPath>
<EmbedInteropTypes>False</EmbedInteropTypes>
</Reference>
<Reference Include="Microsoft.Office.Interop.Access, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<EmbedInteropTypes>True</EmbedInteropTypes>
Expand All @@ -149,6 +153,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AccessAssemblyTests.cs" />
<Compile Include="HostLookupTests.cs" />
<Compile Include="UseApplication.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
16 changes: 11 additions & 5 deletions vbe-add-In/AccUnit.VbeAddIn/AddInManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -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()
Expand Down