Exception while using WMI

hauptra

Well-known member
Joined
Feb 17, 2007
Messages
72
Location
Cary, NC
Programming Experience
3-5
I have a function that is generating Null Reference Exceptions. My problem is that it only occurs after a long duration of the app running (It is being used as a service). Here is the function


VB.NET:
Private Shared Function GetWMIResults(ByVal className As String, ByVal conditions As String, ByVal properties() As String) _
As Management.ManagementObjectCollection
'Initializes variables
Try
Dim WMIScope As New System.Management.ManagementScope(Management.ManagementPath.DefaultPath)
Dim WMIEnumOptions As New System.Management.EnumerationOptions
WMIEnumOptions.Rewindable = True
 
'Creates the query and the searcher
Dim WMIQuery As New Management.SelectQuery(className, conditions, properties)
Dim WMISearcher As New System.Management.ManagementObjectSearcher(WMIScope, WMIQuery, WMIEnumOptions)
 
'Gets the results
Dim WMIObjCol As System.Management.ManagementObjectCollection = WMISearcher.Get()
 
WMISearcher.Dispose()
 
Return WMIObjCol
Catch ex As NullReferenceException
GlobalModule.WriteToLog(ex.ToString)
Return Nothing
End Try
End Function
I get the following exceptions

System.NullReferenceException: Object reference not set to an instance of an object.
at System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType)
at System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(Type objectType)
at System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(Type serverType)
at System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(Type serverType, Object[] props, Boolean bNewObj)
at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Management.MTAHelper.CreateInMTA(Type type)
at System.Management.SecurityHandler..ctor(ManagementScope theScope)
at System.Management.ManagementScope.InitializeGuts(Object o)
at System.Management.ManagementScope.Initialize()
at System.Management.ManagementObjectSearcher.Initialize()
at System.Management.ManagementObjectSearcher.Get()
at <MYAPP>.AdapterCol.GetWMIResults(String className, String conditions, String[] properties)

System.NullReferenceException: Object reference not set to an instance of an object.
at System.Management.IWbemPath.GetNamespaceCount_(UInt32& puCount)
at System.Management.ManagementPath.GetWbemPath(IWbemPath wbemPath)
at System.Management.ManagementScope.set_prvpath(ManagementPath value)
at System.Management.ManagementScope..ctor(ManagementPath path, ConnectionOptions options)
at System.Management.ManagementScope..ctor(ManagementPath path)
at <MYAPP>.AdapterCol.GetWMIResults(String className, String conditions, String[] properties)
I only get the above errors after a good hour or two of operation. Before that this function will function correctly.

Any thoughts?

Rob

PS: I wasn't sure where to post this, so forgive me if this is the wrong forum.
 
Back
Top