C# Source Code: Releasing a reference to a COM object in .NET
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
C# Source Code
Releasing a reference to a COM object in .NET
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Wednesday, June 02, 2004
Hits:
728
Category:
COM/Interop
Article:
For obvious reasons, it is recommended that you do not use enforced garbage collections to deterministically unload COM objects. Instead the following is a simple method which decrements the COM reference counter and unloads the object. Note, I would strongly recommend reading the following article on this complex subject: http://samgentile.com/blog/archive/2004/04/28/11476.aspx using System.Runtime.InteropServices; ///
/// Routine to perform the deterministic unloading of a COM object. ///
///
The COM object to release the reference of static void ReleaseComObject(ref object comObject) { if ( comObject != null && Marshal.IsComObject(comObject) ) { // Release reference to COM object (reduce the pUnk count to zero) do { } while( Marshal.ReleaseComObject(comObject) > 0 ); comObject= null; } }
Terms and Conditions
Support this site
Download a trial version of the best FTP application on the internet