Solved

Releasing Com Object/Closing File

Posted on 2006-10-25
2
372 Views
Last Modified: 2009-07-29
Hello experts,

I have a bit of a problem with the code I am working on at the moment. I am attempting to write something that goes through a list of directories applying MST files to a blank MSI - then pulling out certain information from the MST. The problem is that when I move onto the next MSI it will not release the previous one although I have released all the objects. The code looks like the following:

***************************************************************

    Public Shared Function GetMSIDependency(ByVal strMST_Path As String, ByVal strReferenceMSIPath As String) As String()

        On Error Resume Next

        Dim strResults(1) As String
        File.Copy(strMST_Path, Application.StartupPath & "\dtcustom.mst", True) 'make a copy of the MST locally

        Dim objInstaller As Installer = CreateObject("Windowsinstaller.Installer") 'Crate installer object
        Dim objDatabase As Database = objInstaller.OpenDatabase(strReferenceMSIPath, 0) 'Open the reference database read only
        objDatabase.ApplyTransform(Application.StartupPath & "\dtcustom.mst", 63) 'Apply the transform - suppressing all errors

        'Create a view to return the PRODUCTNAMELONG property from property table
        Dim objDatabaseView As WindowsInstaller.View = objDatabase.OpenView("SELECT Property, Value FROM Property WHERE Property='PRODUCTNAMELONG'")
        objDatabaseView.Execute() 'Execute the view
        Dim objRecord As Record = objDatabaseView.Fetch 'Assign the view results to record
        strResults(0) = objRecord.StringData(2) 'Write the long name to the string
        objDatabaseView.Close() 'Kill the view
        Application.DoEvents() 'Catchup

        ' Create a view to return the DEPENDENCIES Property from the property table
        objDatabaseView = objDatabase.OpenView("SELECT Property, Value FROM Property WHERE Property='DEPENDENCIES'")
        objDatabaseView.Execute() ' execute the view
        objRecord = objDatabaseView.Fetch ' Assign the reulst to t a record
        strResults(1) = objRecord.StringData(2) 'Write the dependancy to the strings
        If Trim(strResults(1).ToUpper.StartsWith("ENTER CORE") = True) Then strResults(1) = "None" 'Make sure no description text gets through
        objDatabaseView.Close() 'Close the view
        Application.DoEvents() 'Catchup

        'Release Com Objects
        objRecord.ClearData()
        Marshal.ReleaseComObject(objRecord)
        Marshal.ReleaseComObject(objDatabaseView)
        Marshal.ReleaseComObject(objDatabase)
        Marshal.ReleaseComObject(objInstaller)
       GC.Collect()
Application.DoEvents()

        'Clear Variables
        objRecord = Nothing
        objDatabaseView = Nothing
        objDatabase = Nothing
        objInstaller = Nothing
 
        File.Delete(Application.StartupPath & "file.mst")

        Return strResults 'Return the string array containing MST information.

    End Function

End Class

********************************************************************************

Without the On error resume next the program will throw an exception when the file is deleted saying the file is already in use. Curiously, with the on error resume next on it will go through the MST's but fall over randomly at different point through the execution. The sub is called from another sub which is looping through a list iof MST paths.  ANy idea why the file won't delete?

Thanks for any help you can give.

Cheers,
Jamie.
0
Comment
Question by:purejamie
2 Comments
 
LVL 8

Accepted Solution

by:
razo earned 500 total points
ID: 17803461
i use this function for disposing an object
   Private Sub DisposeObject(ByRef obj As Object)
        'Wraps ReleaseCOMObject to provide a 'safe' disposal helper method.
        Dim count As Integer
        Try
            If obj Is Nothing Then
                Exit Try
            End If
            count = Marshal.ReleaseComObject(obj)
            While count > 0
                count = Marshal.ReleaseComObject(obj)
            End While
        Catch ex As SystemException
        Finally
            obj = Nothing
            GC.Collect()
            GC.WaitForPendingFinalizers()
        End Try
    End Sub
0
 

Author Comment

by:purejamie
ID: 17804170
Razo.

Many thanks for the post, seems to work very well! Please give me a day just to test it works throughout the program (I'm sure it will)

Thanks again,
Jamie.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now