?
Solved

Releasing Com Object/Closing File

Posted on 2006-10-25
2
Medium Priority
?
384 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 2000 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

Technology Partners: 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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

771 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