From: Hassan on
Hi,

I am having the same kind of problem, while using ManagementEventWatcher class in system.management namespace. I need to know that if find any solution/reason for the problem you are having.

Thanks

Hassan



jmagara wrote:

WMI Exception - System.Runtime.InteropServices.COMException
22-Sep-08

I have a Windows service written in C# that is configured to automatically
start. Sometimes - maybe 20% of the time - the service fails to start due to
an exception in WMI code. I haven't made my service dependent on WMI (or any
other service) because it appears that .net WMI calls automatically start the
WMI service if necessary. Below are the details. What causes this? How can I
make this code work 100% of the time? The code is trying to translate a
well-known group SID to its name like "Administrators". I was thinking about
wrapping my code in try catch and if it fails, waiting 5 seconds and trying
again, but this is guesswork. I also might make the service dependent on
winmgmt.

Here's the exception:

Service cannot be started.
System.Runtime.InteropServices.COMException (0x80010002)
Call was canceled by the message filter.
(Exception from HRESULT: 0x80010002 (RPC_E_CALL_CANCELED))
at
System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32
errorCode, IntPtr errorInfo)
at System.Management.ManagementScope.InitializeGuts(Object o)
at System.Management.ManagementScope.Initialize()
at System.Management.ManagementObjectSearcher.Initialize()
at System.Management.ManagementObjectSearcher.Get()
at
Magaram.TimeLimits.Server.AccountManager.GetGroupNameFromSid(SecurityIdentifier sid)
at Magaram.TimeLimits.Server.AccountManager..ctor()
at
Magaram.TimeLimits.Server.TimeLimitService.CreateRealService(SessionManager
sessionManager)
at
Magaram.TimeLimits.ServerHost.TimeLimitsWindowsService.OnStart(String[] args)
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object
state)

My code:

internal static string GetGroupNameFromSid(SecurityIdentifier sid) {
SelectQuery query = new SelectQuery("Win32_Group",
string.Format("Domain='{0}'", Environment.MachineName));
using (ManagementObjectSearcher searcher = new
ManagementObjectSearcher(query)) {
foreach (ManagementObject group in searcher.Get()) {
string sddl = group["SID"] as string;
if (sddl.ToLower().Trim() == sid.Value.ToLower().Trim()) {
string groupName = (string)(group["Name"]);
return group["Name"] as string;
}
}
}
throw new ArgumentOutOfRangeException("sid",
string.Format("Could not get the group name for sid: {0}", sid.Value));
}

EggHeadCafe - Software Developer Portal of Choice
WPF DataGrid Custom Paging and Sorting
http://www.eggheadcafe.com/tutorials/aspnet/8a2ea78b-f1e3-45b4-93ef-32b2d802ae17/wpf-datagrid-custom-pagin.aspx