Skip to content

Commit 70c8ab3

Browse files
committed
Made the HostApplicationBase inherit from SafeComWrapper.
1 parent 56bff15 commit 70c8ab3

File tree

3 files changed

+30
-109
lines changed

3 files changed

+30
-109
lines changed
Lines changed: 30 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,60 @@
1-
using System;
2-
using System.Linq;
3-
using System.Runtime.InteropServices;
4-
using NLog;
1+
using System.Runtime.InteropServices;
52
using Rubberduck.VBEditor.SafeComWrappers;
63
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
74

85
namespace Rubberduck.VBEditor.Application
96
{
107
[ComVisible(false)]
11-
public abstract class HostApplicationBase<TApplication> : IHostApplication
8+
public abstract class HostApplicationBase<TApplication> : SafeComWrapper<TApplication>, IHostApplication
129
where TApplication : class
1310
{
14-
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
15-
16-
protected readonly TApplication Application;
1711
protected HostApplicationBase(string applicationName)
12+
:base(ApplicationFomComReflection(applicationName))
1813
{
1914
ApplicationName = applicationName;
15+
}
16+
17+
protected HostApplicationBase(IVBE vbe, string applicationName)
18+
:base(ApplicationFomVbe(vbe, applicationName))
19+
{
20+
ApplicationName = applicationName;
21+
}
2022

23+
private static TApplication ApplicationFomComReflection(string applicationName)
24+
{
25+
TApplication application;
2126
try
2227
{
23-
Application = (TApplication)Marshal.GetActiveObject($"{applicationName}.Application");
28+
application = (TApplication)Marshal.GetActiveObject($"{applicationName}.Application");
2429
}
2530
catch (COMException)
2631
{
27-
Application = null; // unit tests don't need it anyway.
32+
application = null; // unit tests don't need it anyway.
2833
}
34+
return application;
2935
}
3036

31-
protected HostApplicationBase(IVBE vbe, string applicationName)
37+
private static TApplication ApplicationFomVbe(IVBE vbe, string applicationName)
3238
{
33-
ApplicationName = applicationName;
34-
39+
TApplication application;
3540
try
3641
{
3742
var appProperty = ApplicationPropertyFromDocumentModule(vbe);
3843
if (appProperty != null)
3944
{
40-
Application = (TApplication)appProperty.Object;
45+
application = (TApplication)appProperty.Object;
4146
}
4247
else
4348
{
44-
Application = (TApplication)Marshal.GetActiveObject($"{applicationName}.Application");
49+
application = (TApplication)Marshal.GetActiveObject($"{applicationName}.Application");
4550
}
46-
51+
4752
}
4853
catch (COMException)
4954
{
50-
Application = null; // unit tests don't need it anyway.
55+
application = null; // unit tests don't need it anyway.
5156
}
57+
return application;
5258
}
5359

5460
private static IProperty ApplicationPropertyFromDocumentModule(IVBE vbe)
@@ -105,10 +111,7 @@ private static IProperty ApplicationPropertyFromDocumentModule(IVBE vbe)
105111
}
106112
}
107113

108-
~HostApplicationBase()
109-
{
110-
Dispose(false);
111-
}
114+
protected TApplication Application => Target;
112115

113116
public string ApplicationName { get; }
114117

@@ -119,99 +122,19 @@ public virtual object Run(string name, params object[] args)
119122
return null;
120123
}
121124

122-
private int? _rcwReferenceCount;
123-
public void Release(bool final = false)
125+
public override bool Equals(ISafeComWrapper<TApplication> other)
124126
{
125-
if (HasBeenReleased)
126-
{
127-
_logger.Warn($"Tried to release an application object type {this.GetType()} that had already been released.");
128-
return;
129-
}
130-
if (Application == null)
131-
{
132-
_rcwReferenceCount = 0;
133-
_logger.Warn($"Tried to release an application object that was null.");
134-
return;
135-
}
136-
137-
if (!Marshal.IsComObject(Application))
138-
{
139-
_rcwReferenceCount = 0;
140-
_logger.Warn($"Tried to release an application objects of type {this.GetType()} that is not a COM object.");
141-
return;
142-
}
143-
144-
try
145-
{
146-
if (final)
147-
{
148-
_rcwReferenceCount = Marshal.FinalReleaseComObject(Application);
149-
if (HasBeenReleased)
150-
{
151-
_logger.Trace($"Final released application object of type {this.GetType()}.");
152-
}
153-
else
154-
{
155-
_logger.Warn($"Final released application object of type {this.GetType()} did not release the object: remaining reference count is {_rcwReferenceCount}.");
156-
}
157-
}
158-
else
159-
{
160-
_rcwReferenceCount = Marshal.ReleaseComObject(Application);
161-
if (_rcwReferenceCount >= 0)
162-
{
163-
_logger.Trace($"Released application object of type {this.GetType()} with remaining reference count {_rcwReferenceCount}.");
164-
}
165-
else
166-
{
167-
_logger.Warn($"Released application object of type {this.GetType()} whose underlying RCW has already been released from outside the SafeComWrapper.");
168-
}
169-
}
170-
}
171-
catch (COMException exception)
172-
{
173-
var logMessage = $"Failed to release application object of type {this.GetType()}.";
174-
if (_rcwReferenceCount.HasValue)
175-
{
176-
logMessage = logMessage + $"The previous reference count has been {_rcwReferenceCount}.";
177-
}
178-
else
179-
{
180-
logMessage = logMessage + "There has not yet been an attempt to release the application object.";
181-
}
182-
183-
_logger.Warn(exception, logMessage);
184-
}
127+
return IsEqualIfNull(other) || (other != null && ReferenceEquals(other.Target, Target));
185128
}
186129

187-
public bool HasBeenReleased => _rcwReferenceCount <= 0;
188-
189-
public void Dispose()
130+
public override int GetHashCode()
190131
{
191-
Dispose(true);
192-
GC.SuppressFinalize(this);
132+
return IsWrappingNullReference ? 0 : HashCode.Compute(Target);
193133
}
194134

195-
private bool _disposed;
196-
protected virtual void Dispose(bool disposing)
135+
~HostApplicationBase()
197136
{
198-
if (_disposed)
199-
{
200-
return;
201-
}
202-
203-
// clean up managed resources
204-
if (Application != null && !HasBeenReleased)
205-
{
206-
Release();
207-
}
208-
209-
if (disposing)
210-
{
211-
// we don't have any managed resources to clean up right now.
212-
}
213-
214-
_disposed = true;
137+
Dispose(false);
215138
}
216139
}
217140
}

Rubberduck.VBEEditor/SafeComWrappers/VB6/VBE.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Linq;
33
using System.Runtime.InteropServices;
44
using System.Text;
5-
using System.Windows.Forms;
65
using Rubberduck.VBEditor.Application;
76
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
87
using Rubberduck.VBEditor.SafeComWrappers.Office.Core;

Rubberduck.VBEEditor/SafeComWrappers/VB6/VBProject.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using Microsoft.Office.Interop.MSProject;
54
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
65
using VB = Microsoft.VB6.Interop.VBIDE;
76

0 commit comments

Comments
 (0)