namespace Microsoft.VSDesigner.WMI { using System.Runtime.Serialization; using System.Diagnostics; using System; using System.WinForms; using System.Drawing; using System.Collections; using Microsoft.VSDesigner; using Microsoft.VSDesigner.ServerExplorer; using System.ComponentModel.Design; using System.ComponentModel; using System.Resources; using WbemScripting; // // // This represents the wmi classes node under a server node. // // [UserContextAttribute("serveritem", "IntrinsicEventQuery")] public class IntrinsicEventQueryNode : Node, ISerializable { // // FIELDS // enum SubscriptionState { Started, Stopped, Error }; static Image icon; public static readonly Type parentType = typeof(WMIIntrinsicEventsNode); private string label = string.Empty; private ResourceManager rm = null; private string serverName = string.Empty; private string nsName = string.Empty; private string query = string.Empty; private ISWbemServices wbemServices = null; private SubscriptionState state = SubscriptionState.Stopped; private SWbemSink theSink = null; private IntrinsicEventQueryComponent browseObject = null; // // CONSTRUCTORS // // // // Main constructor. // Parameters: // string server (in, machine name) // string pathIn (in, WMI path without the server name , e.g. "root\default:MyClass") // // public IntrinsicEventQueryNode(string serverIn, string nsIn, string queryIn) { try { serverName = serverIn; nsName = nsIn; query = queryIn; state = SubscriptionState.Stopped; try { rm = new ResourceManager( "Microsoft.VSDesigner.WMI.Res", // Name of the resource. ".", // Use current directory. null); } catch (Exception) { //do nothing, will use static RM } //Get and cache wbemServices ISWbemLocator wbemLocator = WmiHelper.WbemLocator;//(ISWbemLocator)(new SWbemLocator()); wbemServices = wbemLocator.ConnectServer(serverName, nsName, "", //user: blank defaults to current logged-on user "", //password: blank defaults to current logged-on user "", //locale: blank for current locale "", //authority: NTLM or Kerberos. Blank lets DCOM negotiate. 0, //flags: reserved null); //context info: not needed here if (wbemServices == null) { throw new Exception(WMISys.GetString("WMISE_WMIConnectFailed")); } browseObject = new IntrinsicEventQueryComponent(serverName, nsName, query, this); } catch (Exception exc) { MessageBox.Show(WMISys.GetString("WMISE_Exception", exc.Message, exc.StackTrace)); throw (exc); } } /// /// The object retrieves its serialization info. /// public IntrinsicEventQueryNode(SerializationInfo info, StreamingContext context) { try { try { rm = new ResourceManager( "Microsoft.VSDesigner.WMI.Res", // Name of the resource. ".", // Use current directory. null); } catch (Exception) { //do nothing, will use static RM } serverName = info.GetString("Server"); nsName = info.GetString("NS"); query = info.GetString("Query"); state = SubscriptionState.Stopped; rm = new ResourceManager( "Microsoft.VSDesigner.WMI.Res", // Name of the resource. ".", // Use current directory. null); //Get and cache wbemServices ISWbemLocator wbemLocator = WmiHelper.WbemLocator;//(ISWbemLocator)(new SWbemLocator()); wbemServices = wbemLocator.ConnectServer(serverName, nsName, "", //user: blank defaults to current logged-on user "", //password: blank defaults to current logged-on user "", //locale: blank for current locale "", //authority: NTLM or Kerberos. Blank lets DCOM negotiate. 0, //flags: reserved null); //context info: not needed here browseObject = new IntrinsicEventQueryComponent(serverName, nsName, query, this); } catch (Exception exc) { MessageBox.Show(WMISys.GetString("WMISE_Exception", exc.Message, exc.StackTrace)); throw (exc); } } // // PROPERTIES // // // // Returns icon for this node. // // public Image Icon { override get { //TODO: display different icons for associations, abstract, etc. if (icon == null) { if (state == SubscriptionState.Started) { if (rm != null) { icon = (Image)rm.GetObject ("Microsoft.VSDesigner.WMI.Start.ico"); } else { icon = new Icon(GetType(), "Start.ico").ToBitmap(); } } else { if (state == SubscriptionState.Stopped) { if (rm != null) { icon = (Image)rm.GetObject ("Microsoft.VSDesigner.WMI.Stop.ico"); } else { icon = new Icon(GetType(), "Stop.ico").ToBitmap(); } } else { if (rm != null) { icon = (Image)rm.GetObject ("Microsoft.VSDesigner.WMI.ErrorNode.ico"); } else { icon = new Icon(GetType(), "ErrorNode.ico").ToBitmap(); } } } } return icon; } } /// /// Context menu for a class object contains static method names /// public override ContextMenuItem[] GetContextMenuItems() { ContextMenuItem[] theMenu = new ContextMenuItem[4]; theMenu[0] = new ContextMenuItem(WMISys.GetString("WMISE_EventQueryStart"), new EventHandler(OnStartReceiving), (state != SubscriptionState.Started)); theMenu[1] = new ContextMenuItem(WMISys.GetString("WMISE_EventQueryStop"), new EventHandler(OnStopReceiving), (state == SubscriptionState.Started)); theMenu[2] = new ContextMenuItem(WMISys.GetString("WMISE_EventQueryPurge"), new EventHandler(OnPurgeEvents)); //theMenu[3] = new ContextMenuItem("&Modify query...", // new EventHandler(OnModifyQuery)); return theMenu; } public override bool CanDeleteNode() { return true; } public override bool ConfirmDeletingNode() { return true; } /* /// /// The method returns a NewChildNode that, when double clicked, triggers add filter. /// /// /// A NewChildNode, a child node, that supports add server node. /// public override Node GetNewChildNode() { if (newChildNode == null) { newChildNode = new NewChildNode(); newChildNode.Label = ""; //VSDSys.GetString("SE_NewServerNodeLabel_AddServer"); newChildNode.WhenToSave = WhenToSaveCondition.Always; newChildNode.SetIconImage((Image)rm.GetObject ("inst.bmp")); newChildNode.DoubleClickHandler = new EventHandler(OnCreateNewInstance); } return newChildNode; } */ // // // Returns label constant for this node. // // public string Label { override get { if (label == null || label.Length == 0) { label = query; } return label; } override set { } } // // METHODS // // // // Create instance nodes under this node. // // public override Node[] CreateChildren() { try { Node[] nodes = new Node[0]; return nodes; } catch(Exception exc) { MessageBox.Show(WMISys.GetString("WMISE_Exception", exc.Message, exc.StackTrace)); return null; } } // // // This node is a singleton. // // public override int CompareUnique(Node node) { if (node is IntrinsicEventQueryNode && ((IntrinsicEventQueryNode)node).serverName == this.serverName && ((IntrinsicEventQueryNode)node).nsName == this.nsName && ((IntrinsicEventQueryNode)node).query == this.query) { return 1; } return 0; } /// /// Get component to be displayed in PB window /// public override Object GetBrowseComponent() { return browseObject; } /// /// Subscribes for event notifications /// /// /// private void OnStartReceiving(object sender, EventArgs e) { try { //TODO: enter subscription, change icon, disable "start" context menu if (theSink != null) { theSink.Cancel(); theSink = null; } theSink = new SWbemSink(); theSink.OnObjectReady += new ISWbemSinkEvents_OnObjectReadyEventHandler(OnEventReady); theSink.OnProgress += new ISWbemSinkEvents_OnProgressEventHandler(OnQueryProgress); //MessageBox.Show("About to enter notification query: " + query); wbemServices.ExecNotificationQueryAsync(theSink, query, "WQL", (int)WbemFlagEnum.wbemFlagSendStatus, null, null); state = SubscriptionState.Started; icon = (Image)rm.GetObject("Microsoft.VSDesigner.WMI.Start.ico"); this.GetNodeSite().UpdateIcon(); //GetNodeSite().AddChild(childNode); } catch(Exception exc) { state = SubscriptionState.Error; icon = (Image)rm.GetObject("Microsoft.VSDesigner.WMI.ErrorNode.ico"); this.GetNodeSite().UpdateIcon(); MessageBox.Show(WMISys.GetString("WMISE_Exception", exc.Message, exc.StackTrace)); } } /// /// Un-subscribes from event notifications /// /// /// private void OnStopReceiving(object sender, EventArgs e) { try { //TODO: cancel subscription, change icon, disable "stop" context menu if (theSink != null) { theSink.Cancel(); } state = SubscriptionState.Stopped; icon = (Image)rm.GetObject("Microsoft.VSDesigner.WMI.Stop.ico"); this.GetNodeSite().UpdateIcon(); } catch(Exception exc) { MessageBox.Show(WMISys.GetString("WMISE_Exception", exc.Message, exc.StackTrace)); } } /// /// Removes existing event entries /// /// /// private void OnPurgeEvents(object sender, EventArgs e) { try { //TODO: purge event entries } catch(Exception exc) { MessageBox.Show(WMISys.GetString("WMISE_Exception", exc.Message, exc.StackTrace)); } } /// /// To determine whether this node supports drag and drop. /// /// /// TRUE to indicate ProcessNode supports drag and drop. /// public override bool HasDragComponents() { return false; //change this! } public override IComponent[] CreateDragComponents(IDesignerHost designerHost) { /* Object comp = null; if (pathNoServer.ToLower() == "root\\cimv2:win32_share") { comp = new Win32_Share (wmiObj); } else { comp = new WMIObjectComponent(wmiObj); } return new IComponent[] {(IComponent)comp}; */ return new IComponent[0]; } /// /// This allows modification of the query string from outside, /// e.g. from the browse component /// public String Query { get { return query; } set { if (state == SubscriptionState.Started) { OnStopReceiving(null, null); } query = value; if (browseObject.Query != query) { browseObject.Query = query; } label = query; GetNodeSite().StartRefresh(); } } /// /// The object should write the serialization info. /// public virtual void GetObjectData(SerializationInfo si, StreamingContext context) { si.AddValue("Server", serverName); si.AddValue("NS", nsName); si.AddValue("Query", query); } private static void OnEventReady(ISWbemObject obj, ISWbemNamedValueSet context) { MessageBox.Show("Event received: " + obj.GetObjectText_(0)); } private static void OnQueryProgress(int numTasks, int curTask, string message, ISWbemNamedValueSet context) { MessageBox.Show("OnQueryProgress received: " + message); } } }