<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Matthew Wills @ Readify &#187; COM Interop</title>
	<atom:link href="http://ausdotnet.wordpress.com/category/technical/com-interop/feed/" rel="self" type="application/rss+xml" />
	<link>http://ausdotnet.wordpress.com</link>
	<description>Avoiding blogdeath, one day at a time</description>
	<lastBuildDate>Wed, 21 Jan 2009 11:11:45 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='ausdotnet.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/460faa9f8eb1130deeee29e126f25f37?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Matthew Wills @ Readify &#187; COM Interop</title>
		<link>http://ausdotnet.wordpress.com</link>
	</image>
			<item>
		<title>COM Interop Principle #3 &#8211; Fear the (Hidden) Period</title>
		<link>http://ausdotnet.wordpress.com/2008/06/04/com-interop-principle-3-fear-the-hidden-period/</link>
		<comments>http://ausdotnet.wordpress.com/2008/06/04/com-interop-principle-3-fear-the-hidden-period/#comments</comments>
		<pubDate>Wed, 04 Jun 2008 11:39:26 +0000</pubDate>
		<dc:creator>mjwills</dc:creator>
				<category><![CDATA[COM Interop]]></category>
		<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://ausdotnet.wordpress.com/?p=22</guid>
		<description><![CDATA[This post is the fourth in my series on getting your head around COM Interop. My aim here is not to teach you all the innards of COM Interop, but instead to communicate a few key principles. These principles will (hopefully) make your life a little simpler when coding in .NET against COM components (particularly [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ausdotnet.wordpress.com&blog=2718711&post=22&subd=ausdotnet&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><em>This post is the fourth in my series on getting your head around COM Interop. My aim here is not to teach you all the innards of COM Interop, but instead to communicate a few key principles. These principles will (hopefully) make your life a little simpler when coding in .NET against COM components (particularly in relation to getting the COM objects to clean themselves up).</em></p>
<p><em></em></p>
<h3>COM Interop Principle #3: Fear the (Hidden) Period</h3>
<p>Last week&#8217;s <a href="http://ausdotnet.wordpress.com/2008/05/26/com-interop-principle-2-fear-the-period/">post</a> covered the <strong>key </strong>principle of COM Interop &#8211; <strong>Fear the Period</strong> (aka Full Stop).</p>
<p>As a result, we had the following code:</p>
<pre class="brush: vb;">Option Strict Off
Option Explicit On
Imports msE = Microsoft.Office.Interop.Excel
Imports System.Runtime.InteropServices
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim wb As msE.Workbook
        Dim ws As msE.Worksheet
        Dim xlApp As msE.Application
        '==========================
        xlApp = New msE.Application
        xlApp.DisplayAlerts = False
        xlApp.Interactive = False
        Dim wbs As msE.Workbooks
        wbs = xlApp.Workbooks
        wbs.Add()
        Marshal.ReleaseComObject(wbs)
        wb = xlApp.Workbooks(1)
        Dim wss As msE.Sheets
        wss = wb.Worksheets
        'Clear all but the first worksheet
        For Each wrk As msE.Worksheet In wss
            If wrk.Index &gt; 1 Then wrk.Delete()
        Next
        Marshal.ReleaseComObject(wss)
        ws = wb.Sheets(1)
        xlApp.Visible = True
        Threading.Thread.Sleep(5000)    'So we can see Excel
        wb.Close(False)
        xlApp.Quit()
        Marshal.ReleaseComObject(ws)
        Marshal.ReleaseComObject(wb)
        Marshal.ReleaseComObject(xlApp)
    End Sub
End Class</pre>
<p>In the above code, we have diligently followed the two key ways that we learnt to <strong><a href="http://ausdotnet.wordpress.com/2008/05/26/com-interop-principle-2-fear-the-period/">Fear the Period</a></strong>:</p>
<ul>
<li>Do not ever allow two periods to be consecutive (as in the above example of xlApp.Workbooks<strong>.Add</strong>).</li>
<li>Do not ever allow any periods to appear in the &#8216;In&#8217; section of a For Each declaration (as in For Each wrk As msE.Worksheet In wb<strong>.Worksheets</strong>).</li>
</ul>
<p>Or have we?</p>
<p><strong>The Hidden Period</strong></p>
<p>While not evident at first glance, it actually turns out that in the above code there are <em>hidden </em>periods.</p>
<p>Huh? What are you talking about?</p>
<p>Well, lets take a look at one of the lines of code&#8230;</p>
<pre class="brush: vb;">        ws = wb.Sheets(1)</pre>
<p>The code is simple enough. We want to get the first worksheet of the workbook &#8211; so we are calling the Sheets function on the wb (Workbook) object, passing 1.</p>
<p>Or are we?</p>
<p><a href="http://ausdotnet.files.wordpress.com/2008/06/functionorproperty.png"><img class="alignnone size-full wp-image-24" src="http://ausdotnet.files.wordpress.com/2008/06/functionorproperty.png" alt="Is it a function?" /></a></p>
<p>Whoops, it seems that Sheets isn&#8217;t a function &#8211; it is actually a <a href="http://msdn.microsoft.com/en-us/library/2b6akew6(VS.71).aspx">Default Property</a>. This means that:</p>
<pre class="brush: vb;">        ws = wb.Sheets(1)</pre>
<p>is just shorthand for:</p>
<pre class="brush: vb;">        ws = wb.Sheets.Item(1)</pre>
<p>See &#8211; there it is &#8211; our <em>hidden</em> period.</p>
<p>So, lets&#8217;s add a third practical way to <strong>Fear the Period</strong> (hidden or otherwise):</p>
<ul>
<li>Do not ever allow two periods to be consecutive (as in the above example of xlApp.Workbooks<strong>.Add</strong>).</li>
<li>Do not ever allow any periods to appear in the &#8216;In&#8217; section of a For Each declaration (as in For Each wrk As msE.Worksheet In wb<strong>.Worksheets</strong>).</li>
<li><strong>Do not ever call a Default Property.</strong></li>
</ul>
<p>So, lets incorporate this principle into the <a href="../2008/05/15/com-interop-welcome-to-pain/">original code</a>:</p>
<pre class="brush: vb;">Option Strict Off
Option Explicit On
Imports msE = Microsoft.Office.Interop.Excel
Imports System.Runtime.InteropServices
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim wb As msE.Workbook
        Dim ws As msE.Worksheet
        Dim xlApp As msE.Application
        '==========================
        xlApp = New msE.Application
        xlApp.DisplayAlerts = False
        xlApp.Interactive = False
        Dim wbs As msE.Workbooks
        wbs = xlApp.Workbooks
        wbs.Add()
        wb = wbs.Item(1)
        Marshal.ReleaseComObject(wbs)
        Dim wss As msE.Sheets
        wss = wb.Worksheets
        'Clear all but the first worksheet
        For Each wrk As msE.Worksheet In wss
            If wrk.Index &gt; 1 Then wrk.Delete()
        Next
        ws = wss.Item(1)
        Marshal.ReleaseComObject(wss)
        xlApp.Visible = True
        Threading.Thread.Sleep(5000)    'So we can see Excel
        wb.Close(False)
        xlApp.Quit()
        Marshal.ReleaseComObject(ws)
        Marshal.ReleaseComObject(wb)
        Marshal.ReleaseComObject(xlApp)
    End Sub
End Class</pre>
<p><strong>So, did that fix the issue?</strong></p>
<p>Unfortunately not. I know the tension must be killing you, but there is still one more gotcha to address…</p>
<p>So:</p>
<h3>COM Interop Principle #3: Fear the (Hidden) Period</h3>
<p>Coming up next &#8211; <strong>COM Interop Principle #4: Watch the Loops</strong>.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/ausdotnet.wordpress.com/22/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/ausdotnet.wordpress.com/22/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ausdotnet.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ausdotnet.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ausdotnet.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ausdotnet.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ausdotnet.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ausdotnet.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ausdotnet.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ausdotnet.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ausdotnet.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ausdotnet.wordpress.com/22/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ausdotnet.wordpress.com&blog=2718711&post=22&subd=ausdotnet&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://ausdotnet.wordpress.com/2008/06/04/com-interop-principle-3-fear-the-hidden-period/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1fd6ab8fef6804cf40c16bc193002ade?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mjwills</media:title>
		</media:content>

		<media:content url="http://ausdotnet.files.wordpress.com/2008/06/functionorproperty.png" medium="image">
			<media:title type="html">Is it a function?</media:title>
		</media:content>
	</item>
		<item>
		<title>COM Interop Principle #2 &#8211; Fear the Period</title>
		<link>http://ausdotnet.wordpress.com/2008/05/26/com-interop-principle-2-fear-the-period/</link>
		<comments>http://ausdotnet.wordpress.com/2008/05/26/com-interop-principle-2-fear-the-period/#comments</comments>
		<pubDate>Mon, 26 May 2008 12:37:09 +0000</pubDate>
		<dc:creator>mjwills</dc:creator>
				<category><![CDATA[COM Interop]]></category>
		<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://ausdotnet.wordpress.com/?p=21</guid>
		<description><![CDATA[This post is the third in my series on getting your head around COM Interop. My aim here is not to teach you all the innards of COM Interop, but instead to communicate a few key principles. These principles will (hopefully) make your life a little simpler when coding in .NET against COM components (particularly [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ausdotnet.wordpress.com&blog=2718711&post=21&subd=ausdotnet&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><em>This post is the third in my series on getting your head around COM Interop. My aim here is not to teach you all the innards of COM Interop, but instead to communicate a few key principles. These principles will (hopefully) make your life a little simpler when coding in .NET against COM components (particularly in relation to getting the COM objects to clean themselves up).</em></p>
<p><em></em></p>
<h3>COM Interop Principle #2: Fear the Period</h3>
<p>Last week&#8217;s <a href="http://ausdotnet.wordpress.com/2008/05/19/com-interop-cheating/">post</a> was about what <strong>not </strong>to do when dealing with COM Interop.</p>
<p>So, what should you do instead? Well, first and foremost you must <strong>Fear the Period</strong>.</p>
<p>The most powerful weapon in your arsenal when correctly doing COM Interop is <strong>Marshal.ReleaseComObject</strong>. You can see some examples of its use in the previous <a href="http://ausdotnet.wordpress.com/2008/05/15/com-interop-welcome-to-pain/">two</a> <a href="http://ausdotnet.wordpress.com/2008/05/19/com-interop-cheating/">posts</a> in this series. Unfortunately, ReleaseComObject has a natural enemy &#8211; the <a href="http://en.wikipedia.org/wiki/Full_stop">period</a> (or full stop).</p>
<p>So, what does Marshal.ReleaseComObject do? Well, it decrements the reference count of the RCW that you pass to it. Or, in English, it releases the underlying COM object (<a href="#r1">*</a>).</p>
<p>So, what does the period have to do with anything? Well, lets look at some of the code from the original post&#8230;</p>
<pre class="brush: vb;">Option Strict Off
Option Explicit On
Imports msE = Microsoft.Office.Interop.Excel
Imports System.Runtime.InteropServices
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim xlApp As msE.Application
        '==========================
        xlApp = New msE.Application
        xlApp.DisplayAlerts = False
        xlApp.Interactive = False
        xlApp.Workbooks.Add()		&lt;---- Focus your attention on this line
        &lt;snip&gt;
        Marshal.ReleaseComObject(xlApp)
    End Sub
End Class</pre>
<p>Looking at the code we can see that xlApp is being cleaned up correctly at the end of the code. But have a look at:</p>
<pre class="brush: vb;">        xlApp.Workbooks.Add()		&lt;---- Focus your attention on this line</pre>
<p>Let&#8217;s have a think about what that code is doing. We are calling a property on the xlApp object which is returning a &#8216;temporary&#8217; Workbooks object. Then we are calling the Add method (<a href="#r2">**</a>) on that &#8216;temporary&#8217; Workbooks object.</p>
<p>But hold on, where are we cleaning up that &#8216;temporary&#8217; Workbooks object? <em>(Hint: We aren&#8217;t!)</em></p>
<p>This is why we must <strong>Fear the Period</strong>. To get the above code to work you must introduce an explicit Workbooks variable &#8211; this then allows you to explicitly clean it up. So the code would become:</p>
<pre class="brush: vb;">        Dim wbs As msE.Workbooks
        wbs = xlApp.Workbooks
        wbs.Add()
        Marshal.ReleaseComObject(wbs)</pre>
<p>So, how do you <strong>Fear the Period</strong> in practice?</p>
<ul>
<li>Do not ever allow two periods to be consecutive (as in the above example of xlApp.Workbooks<strong>.Add</strong>).</li>
<li>Do not ever allow any periods to appear in the &#8216;In&#8217; section of a For Each declaration (as in For Each wrk As msE.Worksheet In wb<strong>.Worksheets</strong>).</li>
</ul>
<p>So, lets incorporate this principle into the <a href="http://ausdotnet.wordpress.com/2008/05/15/com-interop-welcome-to-pain/">original code</a>:</p>
<pre class="brush: vb;">Option Strict Off
Option Explicit On
Imports msE = Microsoft.Office.Interop.Excel
Imports System.Runtime.InteropServices
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim wb As msE.Workbook
        Dim ws As msE.Worksheet
        Dim xlApp As msE.Application
        '==========================
        xlApp = New msE.Application
        xlApp.DisplayAlerts = False
        xlApp.Interactive = False
        Dim wbs As msE.Workbooks
        wbs = xlApp.Workbooks
        wbs.Add()
        Marshal.ReleaseComObject(wbs)
        wb = xlApp.Workbooks(1)
        Dim wss As msE.Sheets
        wss = wb.Worksheets
        'Clear all but the first worksheet
        For Each wrk As msE.Worksheet In wss
            If wrk.Index &gt; 1 Then wrk.Delete()
        Next
        Marshal.ReleaseComObject(wss)
        ws = wb.Sheets(1)
        xlApp.Visible = True
        Threading.Thread.Sleep(5000)    'So we can see Excel
        wb.Close(False)
        xlApp.Quit()
        Marshal.ReleaseComObject(ws)
        Marshal.ReleaseComObject(wb)
        Marshal.ReleaseComObject(xlApp)
    End Sub
End Class</pre>
<p><strong>So, did that fix the issue?</strong></p>
<p>Unfortunately not. While Principle #2 is definitely the <a href="http://blogs.msdn.com/geoffda/archive/2007/09/07/the-designer-process-that-would-not-terminate-part-2.aspx">most important principle</a> when dealing with COM Interop, there are still a couple of gotchas in the original sample that we have yet to address&#8230;</p>
<p>So:</p>
<h3>COM Interop Principle #2: Fear the Period</h3>
<p>Coming up next &#8211; <strong>COM Interop Principle #3: Fear the (Hidden) Period</strong>.</p>
<p><a name="r1"></a><em>* This is an over-simplification, and there a number of finer points that I am purposely ignoring &#8211; to keep the discussion simple.</em><br />
<a name="r2"></a><em>** Let&#8217;s ignore what the Add method actually does for now &#8211; it isn&#8217;t relevant to this post.</em></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/ausdotnet.wordpress.com/21/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/ausdotnet.wordpress.com/21/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ausdotnet.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ausdotnet.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ausdotnet.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ausdotnet.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ausdotnet.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ausdotnet.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ausdotnet.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ausdotnet.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ausdotnet.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ausdotnet.wordpress.com/21/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ausdotnet.wordpress.com&blog=2718711&post=21&subd=ausdotnet&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://ausdotnet.wordpress.com/2008/05/26/com-interop-principle-2-fear-the-period/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1fd6ab8fef6804cf40c16bc193002ade?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mjwills</media:title>
		</media:content>
	</item>
		<item>
		<title>COM Interop Principle #1 &#8211; Don&#8217;t Cheat</title>
		<link>http://ausdotnet.wordpress.com/2008/05/19/com-interop-cheating/</link>
		<comments>http://ausdotnet.wordpress.com/2008/05/19/com-interop-cheating/#comments</comments>
		<pubDate>Mon, 19 May 2008 12:22:07 +0000</pubDate>
		<dc:creator>mjwills</dc:creator>
				<category><![CDATA[COM Interop]]></category>
		<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://ausdotnet.wordpress.com/?p=19</guid>
		<description><![CDATA[This post is the second in my series on getting your head around COM Interop. My aim here is not to teach you all the innards of COM Interop, but instead to communicate a few key principles. These principles will (hopefully) make your life a little simpler when coding in .NET against COM components (particularly [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ausdotnet.wordpress.com&blog=2718711&post=19&subd=ausdotnet&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><em>This post is the second in my series on getting your head around COM Interop. My aim here is not to teach you all the innards of COM Interop, but instead to communicate a few key principles. These principles will (hopefully) make your life a little simpler when coding in .NET against COM components (particularly in relation to getting the COM objects to clean themselves up).</em></p>
<p><em></em></p>
<h3>COM Interop Principle #1: Don’t Cheat (by relying on the Garbage Collector)</h3>
<p>In my <a href="http://ausdotnet.wordpress.com/2008/05/15/com-interop-welcome-to-pain/">previous post</a>, I listed a short code sample that was not behaving as I might have liked. Specifically, it was not closing down Excel when I wanted it to. In that post I called for attempts to get the code ‘working’ <strong>without calls to GC.Collect()</strong>.</p>
<p>But, what if you ignored my requirement to not use GC.Collect()? Well, you would likely have come up with a solution something like this:</p>
<pre class="brush: vb;">Option Strict Off
Option Explicit On
Imports msE = Microsoft.Office.Interop.Excel
Imports System.Runtime.InteropServices
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim wb As msE.Workbook
        Dim ws As msE.Worksheet
        Dim xlApp As msE.Application
        '==========================
        xlApp = New msE.Application
        xlApp.DisplayAlerts = False
        xlApp.Interactive = False
        xlApp.Workbooks.Add()
        wb = xlApp.Workbooks(1)
        'Clear all but the first worksheet
        For Each wrk As msE.Worksheet In wb.Worksheets
            If wrk.Index &gt; 1 Then wrk.Delete()
        Next
        ws = wb.Sheets(1)
        xlApp.Visible = True
        Threading.Thread.Sleep(5000)    'So we can see Excel
        wb.Close(False)
        xlApp.Quit()
        Marshal.ReleaseComObject(ws)
        Marshal.ReleaseComObject(wb)
        Marshal.ReleaseComObject(xlApp)
        GC.Collect()
        GC.Collect()' A couple more for good measure
        GC.Collect()' A couple more for good measure
        GC.WaitForPendingFinalizers()

    End Sub
End Class</pre>
<p>So, what is the problem?</p>
<p>There are two:</p>
<ol>
<li>It doesn’t work (well, not on my machines running Visual Studio 2008 on Vista 32-bit, nor my machines running Visual Studio 2003 on XP 32-bit).</li>
<li>Even if it did work on <strong>your</strong> PC (or in your version of the .Net Framework) there is no guarantee that it would work in the future or on other machines.</li>
</ol>
<p>So:</p>
<h3>COM Interop Principle #1: Don’t Cheat (by relying on the Garbage Collector)</h3>
<p>As with all principles, there are exceptions! Such as:</p>
<ul>
<li>If the lifetime of your RCWs (COM components) is the same as your application (eg you will be loading Excel when your application starts, and closing it when your application closes) then there is little point in cleaning up correctly. You can be confident (not 100% sure, but confident) that all RCWs will be cleaned up when your process shuts down. So, cheating is OK. <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
</ul>
<p>Coming up next &#8211; <strong>COM Interop Principle #2: Fear the Period</strong>.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/ausdotnet.wordpress.com/19/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/ausdotnet.wordpress.com/19/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ausdotnet.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ausdotnet.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ausdotnet.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ausdotnet.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ausdotnet.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ausdotnet.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ausdotnet.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ausdotnet.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ausdotnet.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ausdotnet.wordpress.com/19/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ausdotnet.wordpress.com&blog=2718711&post=19&subd=ausdotnet&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://ausdotnet.wordpress.com/2008/05/19/com-interop-cheating/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1fd6ab8fef6804cf40c16bc193002ade?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mjwills</media:title>
		</media:content>
	</item>
		<item>
		<title>COM Interop &#8211; The Appetiser</title>
		<link>http://ausdotnet.wordpress.com/2008/05/15/com-interop-welcome-to-pain/</link>
		<comments>http://ausdotnet.wordpress.com/2008/05/15/com-interop-welcome-to-pain/#comments</comments>
		<pubDate>Thu, 15 May 2008 12:39:07 +0000</pubDate>
		<dc:creator>mjwills</dc:creator>
				<category><![CDATA[COM Interop]]></category>
		<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://ausdotnet.wordpress.com/?p=17</guid>
		<description><![CDATA[So, you want to get your head around COM Interop? (You don&#8217;t? Well, just imagine you did.) You could read Adam Nathan&#8217;s 1500 page classic (which I thoroughly recommend) or you could start with something a little simpler.  
I am hoping over the next couple of weeks to put together a set of blog posts discussing [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ausdotnet.wordpress.com&blog=2718711&post=17&subd=ausdotnet&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>So, you want to get your head around COM Interop? (You don&#8217;t? Well, just imagine you did.) You could read <a href="http://www.amazon.com/NET-COM-Complete-Interoperability-Guide/dp/067232170X">Adam Nathan&#8217;s 1500 page classic</a> (which I thoroughly recommend) or you could start with something a little simpler. <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I am hoping over the next couple of weeks to put together a set of blog posts discussing some tips on how to do COM Interop correctly &#8211; particularly in terms of cleaning up after yourself.</p>
<p>As part of this I will be starting with a block of code that isn&#8217;t doing what I want (it is based on some real production code). You can copy and paste the code into a new VB.NET project. The question I will be looking at over the coming blog posts is:</p>
<p><strong>What code changes are required to (reliably) make sure Excel is not still running when the method finishes (without calls to GC.Collect)?</strong></p>
<p><em>Make sure you add a reference to Excel in your project&#8230;</em></p>
<pre class="brush: vb;">Option Strict Off
Option Explicit On
Imports msE = Microsoft.Office.Interop.Excel
Imports System.Runtime.InteropServices  
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim wb As msE.Workbook
        Dim ws As msE.Worksheet
        Dim xlApp As msE.Application
        '==========================  
        xlApp = New msE.Application
        xlApp.DisplayAlerts = False
        xlApp.Interactive = False
        xlApp.Workbooks.Add()
        wb = xlApp.Workbooks(1)
        'Clear all but the first worksheet  
        For Each wrk As msE.Worksheet In wb.Worksheets
            If wrk.Index &gt; 1 Then wrk.Delete()
        Next
        ws = wb.Sheets(1)
        xlApp.Visible = True
        Threading.Thread.Sleep(5000)    'So we can see Excel  
        wb.Close(False)
        xlApp.Quit()
        Marshal.ReleaseComObject(ws)
        Marshal.ReleaseComObject(wb)
        Marshal.ReleaseComObject(xlApp)
        'Your objective is for the EXCEL.EXE process to not be  
        'running by the time you get here...  
        'No use of GC.Collect allowed  
    End Sub
End Class</pre>
<p> </p>
<p>Feel free to post comments (or email me) with solutions, questions, comments etc&#8230;</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/ausdotnet.wordpress.com/17/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/ausdotnet.wordpress.com/17/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ausdotnet.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ausdotnet.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ausdotnet.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ausdotnet.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ausdotnet.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ausdotnet.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ausdotnet.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ausdotnet.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ausdotnet.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ausdotnet.wordpress.com/17/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ausdotnet.wordpress.com&blog=2718711&post=17&subd=ausdotnet&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://ausdotnet.wordpress.com/2008/05/15/com-interop-welcome-to-pain/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1fd6ab8fef6804cf40c16bc193002ade?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mjwills</media:title>
		</media:content>
	</item>
	</channel>
</rss>