ManagementObjectCollection.Count error

  • Thread starter Thread starter Ian Frawley
  • Start date Start date
I

Ian Frawley

ManagementObjectCollection.Count is it rubbish or what?
I got one of the weirdest exception due to it:

COM object that has been separated from its underlying RCW can not be used.

Weird

--
"Life should NOT be a journey to the grave
with the intention of arriving safely in an
attractive and well preserved body,
but rather to skid in sideways,
chocolate in one hand, beer in the other,
body thoroughly used up,
totally worn out and screaming
WOO HOO what a ride!"
 
What version of the framework are you running?
Is this a Windows Forms application?

Willy.
 
Ok, so this is the same issue as you posted before, right?
Could you post at least the complete function that executes the query
(including the query string) and a description how the method is called
(remote request, timer ...). Assuming you run this method on a separate
thread, did you explicitly initialize this one as an MTA thread?
Have you wrapped your ManagementObjectSearcher in a using[1] block? If you
didn't, and you don't use the variable holding the ManagementObjectSearcher
reference, there is a chance that the RCW is released when the finalizer
runs.

[1]
using(ManagementObjectSearcher mos = ....)
{

}


Willy.
 
I am getting the same error. This appears to be a bug. My code is just a
Windows Form. The error does not occur on my XP Professional system, but
does on my Windows 2003 Server (includes 1.1). Here is the code:

Public Sub New(ByVal sComputerName As String)
If sComputerName = "(local)" Then
sComputerName = Environment.MachineName
End If

moScope = New ManagementScope("\\" + sComputerName + "\root\cimv2")

End Sub

Public Function RetrieveFixedDrives() As String()
Dim oSelectQuery As SelectQuery
Dim iDriveType As Integer
Dim sDrives(0) As String
Dim oSearcher As ManagementObjectSearcher
Dim oMgmtObjColl As ManagementObjectCollection
Dim oDisk As ManagementObject
Dim iCount As Integer
Try
oSelectQuery = New SelectQuery("win32_logicalDisk")

oSearcher = New ManagementObjectSearcher(moScope, oSelectQuery)
If oSearcher Is Nothing Then
Exit Function
End If

oMgmtObjColl = oSearcher.Get

'this "appears" to work and does not throw the exception
ReDim sDrives(oMgmtObjColl.Count)

iCount = 0

'Here is where it will fail
For Each oDisk In oMgmtObjColl

iDriveType =
Integer.Parse(oDisk.Properties("DriveType").Value.ToString)

If iDriveType = 3 Then
sDrives(iCount) =
oDisk.Properties("DeviceID").Value.ToString()

iCount += 1

End If
Next oDisk

Return sDrives
Catch ex As Exception
MsgBox(ex.Message.ToString & ": " & ex.InnerException.ToString &
": " & ex.StackTrace)
End Try
End Function

I fixed the issue by rewriting the code this way:

Public Sub New(ByVal sComputerName As String)
If sComputerName = "(local)" Then
sComputerName = Environment.MachineName
End If

moScope = New ManagementScope("\\" + sComputerName + "\root\cimv2")

End Sub

Public Function RetrieveFixedDrives() As String()
Dim oSelectQuery As SelectQuery
Dim iDriveType As Integer
Dim sDrives(0) As String
Dim oSearcher As ManagementObjectSearcher
Dim oMgmtObjColl As ManagementObjectCollection
Dim oDisk As ManagementObject
Dim iCount As Integer
Try
oSelectQuery = New SelectQuery("win32_logicalDisk")

oSearcher = New ManagementObjectSearcher(moScope, oSelectQuery)
If oSearcher Is Nothing Then
Exit Function
End If

iCount = 0

For Each oDisk In oSearcher.Get()

iDriveType =
Integer.Parse(oDisk.Properties("DriveType").Value.ToString)

If iDriveType = 3 Then
ReDim Preserve sDrives(iCount)
sDrives(iCount) =
oDisk.Properties("DeviceID").Value.ToString()

iCount += 1

End If
Next oDisk

Return sDrives
Catch ex As Exception
MsgBox(ex.Message.ToString & ": " & ex.InnerException.ToString &
": " & ex.StackTrace)
End Try
End Function
 
Bruce Parker said:
I am getting the same error. This appears to be a bug. My code is just a
Windows Form. The error does not occur on my XP Professional system, but
does on my Windows 2003 Server (includes 1.1).

Works for me on W2K3 using Framework version 1.1.4322 (v1.1 SP1).

Willy
 
The system I am testing is a clean system with Framework version 1.1.4322.
It will fail every time. The other system it does work on has all the
development tools, office ... There is something else going on here,
especially if others like Ian is experiencing the exact same issue. I have
my work around, but I believe Microsoft needs to look into this further at
their labs.
 
Back
Top