Solved

Releasing Com Object/Closing File

Posted on 2006-10-25
2
375 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

22 Experts available now in Live!

Get 1:1 Help Now