Skip to content

Fix COM ownership and host lookup issues found in #99 - #100

Merged
josef-poetzl merged 1 commit into
devfrom
feature/vbe-addin-com-followup
Nov 17, 2026
Merged

josef-poetzl merged 1 commit into
devfrom
feature/vbe-addin-com-followup

Conversation

@josef-poetzl

Copy link
Copy Markdown
Member

Follow-up to #99: fixes the four findings listed there under "Not changed (found on the way)".

Changes

  1. AddIn RCW released twice: Connect owns the RCW (it receives it in OnConnection) and releases it exactly once. AddInManager.DisposeUnmanagedResources only drops its reference. Before, the second ReleaseComObject threw InvalidComObjectException inside Connect.Dispose(true), skipping GC.Collect, _disposed = true and the Disposed event.
  2. AddInBridgeHostApplicationInitialized: the previous OfficeApplicationHelper is disposed after the new one was created (both may wrap the same application RCW, so the reference count must not drop to 0 in between).
  3. ComTools.GetOfficeComObjectByProgIDAndVBE: Marshal.GetActiveObject failures are logged and the lookup continues with the ROT search. A faulty ROT entry no longer aborts the enumeration (try/catch per moniker). GetTypeForComObject returns null for an unregistered ProgID instead of throwing.
  4. OfficeApplicationHelper.GetCheckedVbProject: the condition was inverted (IsNullOrEmpty && !match, i.e. the project lookup never took effect). Now IsProjectLookupRequired = !IsNullOrEmpty && !match. Note: this makes the lookup active for non-Access hosts for the first time; failures still fall back to ActiveVBProject via the existing catch.

Tests

  • Unit tests (HostLookupTests): IsProjectLookupRequired cases; GetOfficeComObjectByProgIDAndVBE with a ProgID that is not registered returns null instead of throwing. Test project got a reference to Microsoft.Vbe.Interop.
  • MSBuild (VS 18) Release build of source\AccessCodeLib.AccUnit.sln and vbe-add-In\AccessCodeLib.AccUnit.VbeAddIn.sln: passed. NUnit tests not run locally (CI).
  • Manual with real Access (DLLs replaced in access-add-in/lib, no COM registration, own visible instance only):
    • ROT file moniker lookup for a file that is not open / not registered under that path: returns null, Access process count unchanged (no new instance).
    • Lookup via GetOfficeComObjectByProgIDAndVBE finds the correct host.
    • Add-in run (database opened via a junction path, VBA editor opened): add-in loaded, AccUnit command bar present, one Access process, clean exit, no error events.
  • Not tested live: GetActiveObject throwing (covered by the unit test only); a real VBE whose FileName is missing in the ROT (Access registers the moniker under the path it was opened with).

Not changed

  • OfficeApplicationHelper.FindVbProjectEqualsFullName uses First() and throws instead of returning null when no project matches (caught in GetCheckedVbProject).

- Release the AddIn RCW only in Connect (AddInManager no longer releases it)
- Dispose the previous OfficeApplicationHelper when the host application is re-initialized
- ComTools: continue with the ROT search if GetActiveObject fails; skip faulty ROT entries
- OfficeApplicationHelper: fix inverted condition in GetCheckedVbProject
- Add unit tests for the project lookup condition and the unregistered ProgID case
@josef-poetzl
josef-poetzl merged commit d9ecc9c into dev Nov 17, 2026
1 check passed
@josef-poetzl
josef-poetzl deleted the feature/vbe-addin-com-followup branch November 17, 2026 22:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant