Ensuring a COM object has been released.

J

Jason Kendall

I'm writing a little app to search a computer for all revisions of a
COM object and clean them up. All I'm requiring from the user is the
ProgID of one public class in the library.

The app works great but I want to make make sure that the ProgID
entered is valid, so I wrote a simple function to make sure that this
object can be instantiated (code below).

The problem is that I cannot delete the DLL if I use this test because
I get the following error:
Access to the path "C:\WINDOWS\Downloaded Program
Files\CONFLICT.15\ClientUtilities.dll" is denied.

However, if I do not test and simple run the process, then I can
remove the DLL and, in this case, the 'Conflict.15' folder too.

This leads be to believe that the COM object is not being properly
released so that the DLL can be unloaded from memory and, therefore,
the DLL is locked when I try to delete it.

Any ideas?


Public Function TestCOMObject(ByVal ProgID As String) As Boolean
Try
Dim lcTest As Object
Dim TheType As Type
TheType = Type.GetTypeFromProgID(ProgID, True)
lcTest = CreateObject(ProgID)
If Not IsNothing(lcTest) Then
If (TheType.IsCOMObject) Then
Do While
(System.Runtime.InteropServices.Marshal.ReleaseComObject(lcTest) > 0)
Loop
End If
End If
lcTest = Nothing

Return True
Catch ex As Exception
Return False
Finally
GC.Collect()
GC.WaitForPendingFinalizers()
End Try
End Function
 
M

Mattias Sjögren

Jason,
The app works great but I want to make make sure that the ProgID
entered is valid, so I wrote a simple function to make sure that this
object can be instantiated (code below).

Wouldn't it be easier to just look in the Registry?

Any ideas?

You can try calling the CoFreeUnusedLibraries API.



Mattias
 
J

Jason Kendall

Jason,


Wouldn't it be easier to just look in the Registry?

I don't just want to know if the ProgID can be found in the registry,
though thanks for the suggstion, I also want to know if it can be
instantiated (all external dependencies exist and such.)
You can try calling the CoFreeUnusedLibraries API.

Excellent idea! Thanks a ton.
 
J

Jason Kendall

That worked. Thanks!

If anyone else is reading and cares about more info on the solution,
the simple declare for this API is:

Public Declare Sub CoFreeUnusedLibraries Lib "ole32.dll" ()

-Jason
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top