Solved

Releasing Com Object/Closing File

Posted on 2006-10-25
2
380 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 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