Projects

Find all our projects in development below.
All source code is GNU General Public License (GPL)

javaSpy

Browsing frmWMISpy.vb (31.09 KB)

Option Explicit On

Imports System.Management
Imports System.Threading

Public Class frmWMISpy

    Private _compName As String = ""
    Private _mngtScope As ManagementScope = Nothing
    Private _connOptions As ConnectionOptions = Nothing
    Private _lastErrorException As Exception = Nothing

    Private _workerQuery As frmWMISpyQuery = Nothing
    Private _workerThread As Thread = Nothing

    Private Const lvCol_Name As Integer = 0
    Private Const lvCol_Desc As Integer = 1
    Private Const lvCol_Path As Integer = 2

    Private Const DefaultConnectPath As String = "\\{0}\ROOT"
    Private Const DefaultNamespaceNode As String = "ROOT\CIMV2"

    Private Delegate Function WMI_EnumProperties_Func(ByVal mngmtObj As ManagementObject) As ArrayList
    Private Delegate Sub WMI_EnumMethods_Sub(ByVal className As String)
    Private Delegate Sub WMI_Query_Result_Sub()
    Private Delegate Function WMI_Grid_AddRow_Func(ByVal tagData As Object) As Integer
    Private Delegate Sub WMI_Update_Grid_Sub(ByVal rowId As Integer, ByVal propName As String, ByVal value As String)
    Private Delegate Sub WMI_Update_Status_Sub(ByVal statusText As String)

    Private Enum ConnStates
        ConnInactive
        ConnDisconnected
        ConnConnected
    End Enum

    Private Function WMI_Init() As Boolean

        _showWaitCursor(True)
        grpConnection.Enabled = False
        grpRemote.Enabled = False
        chkTopLevelClasses.Enabled = False

        If Not (_mngtScope Is Nothing) Then _
            _mngtScope = Nothing

        _setConnState(ConnStates.ConnInactive)

        tvNamespaces.Nodes.Clear()
        lvClasses.Items.Clear()

        grpNamespaces.Text = "Namespaces"
        grpNamespaces.Enabled = False
        grpClasses.Text = "Classes"
        grpClasses.Enabled = False

        tabMain.SelectedTab.Tag.TabText = "Query #" + CInt(tabMain.SelectedTab.Tag.Tag).ToString("#00")
        tabMain.SelectedTab.Tag.txtQuery.Text = ""
        tabMain.SelectedTab.Tag.dataQuery.Columns.Clear()
        tabMain.SelectedTab.Tag.dataQuery.Rows.Clear()
        tabMain.Enabled = False

        _compName = IIf(txtRemote.Text <> "" And radConnRemote.Checked, txtRemote.Text, ".")

        _connOptions = New ConnectionOptions
        With _connOptions

            .Impersonation = ImpersonationLevel.Impersonate
            .Authentication = AuthenticationLevel.Packet

            If _compName <> "." Then

                .Username = txtUsername.Text
                .Password = txtPassword.Text

                lblInfo.Text = "Connecting to remote host..."

            Else

                lblInfo.Text = "Connecting to local host..."

            End If

        End With

        Application.DoEvents()

        Try

            ' connect to wmi service
            _mngtScope = New ManagementScope(String.Format( _
                DefaultConnectPath, _compName), _connOptions)
            _mngtScope.Connect()

        Catch ex As Exception
            _lastErrorException = ex
        End Try

        If _mngtScope.IsConnected Then

            _setConnState(ConnStates.ConnConnected)
            lblInfo.Text = "Connected; loading Namespaces..."
            Application.DoEvents()

            ' enumerate all namespaces
            WMI_EnumNamespaces(_mngtScope)
            tvNamespaces.Sort()

            ' select default node
            Dim defaultNode As TreeNode = tvNamespaces.Nodes(DefaultNamespaceNode)
            If Not (defaultNode Is Nothing) Then

                tvNamespaces.SelectedNode = defaultNode

            Else

                MessageBox.Show("The default Namespace was not found.", _
                    Application.ProductName, MessageBoxButtons.OK, _
                    MessageBoxIcon.Exclamation)

                If tvNamespaces.Nodes.Count > 0 Then _
                    tvNamespaces.SelectedNode = tvNamespaces.TopNode

            End If

            lblInfo.Text = "Connected; loading Classes..."
            Application.DoEvents()

            ' enumerate all classes
            WMI_EnumClasses()
            lvClasses.Columns(lvCol_Name).AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent)

            chkTopLevelClasses.Enabled = True
            If radConnRemote.Checked Then grpRemote.Enabled = True
            grpConnection.Enabled = True
            grpNamespaces.Enabled = True
            grpClasses.Enabled = True
            tabMain.Enabled = True
            _showWaitCursor(False)

            frmWMISpy_Resize(Nothing, Nothing)

            Dim nodeCount As Integer = tvNamespaces.GetNodeCount(True)
            grpNamespaces.Text = "Namespaces (" + nodeCount.ToString + ")"
            grpClasses.Text = "Classes (" + lvClasses.Items.Count.ToString + ")"

            lblInfo.Text = "Ready; loaded " + nodeCount.ToString("###,##0") + _
                " Namespace(s) and " + lvClasses.Items.Count.ToString("###,##0") + " Class(es)"

            tvNamespaces.Focus()
            Return True

        Else

            _setConnState(ConnStates.ConnDisconnected)

            chkTopLevelClasses.Enabled = True
            If radConnRemote.Checked Then grpRemote.Enabled = True
            grpConnection.Enabled = True
            _showWaitCursor(False)

            lblInfo.Text = "Ready"
            Return False

        End If

    End Function

    Private Sub WMI_EnumNamespaces(ByVal inScope As ManagementScope, _
        Optional ByVal parentNode As TreeNode = Nothing)

        Dim parentDisplay As String = "ROOT\"
        If Not (parentNode Is Nothing) Then parentDisplay = parentNode.Text + "\"

        Dim thisClass As New ManagementClass(inScope, New ManagementPath("__Namespace"), Nothing)

        Try

            For Each objMgmt As ManagementObject In thisClass.GetInstances()

                Dim thisProp As Object = objMgmt.GetPropertyValue("Name")
                If Not (thisProp Is Nothing) Then

                    Dim thisPath As String = thisProp.ToString
                    Dim thisNode As TreeNode = Nothing

                    If Not (parentNode Is Nothing) Then
                        thisNode = parentNode.Nodes.Add(parentDisplay + thisPath, parentDisplay + thisPath)
                        thisNode.Tag = parentNode.Tag + "\" + thisPath
                    Else
                        thisNode = tvNamespaces.Nodes.Add(parentDisplay + thisPath, parentDisplay + thisPath)
                        thisNode.Tag = thisPath
                    End If

                    Try

                        Dim newScope As New ManagementScope(inScope.Path.ToString + "\" + thisPath, _connOptions)
                        newScope.Connect()

                        WMI_EnumNamespaces(newScope, thisNode)

                    Catch
                    End Try

                End If

            Next

        Catch
        End Try

    End Sub

    Private Sub WMI_EnumClasses()

        Const updateCount As Integer = 100

        Try

            Dim thisClass As New ManagementClass(_mngtScope, _
                New ManagementPath(), Nothing)

            Dim enumOptions As New EnumerationOptions
            enumOptions.UseAmendedQualifiers = True

            If Not chkTopLevelClasses.Checked Then _
                enumOptions.EnumerateDeep = True

            Dim updateCounter As Integer = updateCount

            For Each objMgmt As ManagementObject In thisClass.GetSubclasses(enumOptions)

                Dim thisClassName As String = objMgmt.GetPropertyValue("__Class").ToString

                With lvClasses.Items.Add(thisClassName)

                    Try
                        .SubItems.Add(objMgmt.Qualifiers.Item("description").Value)
                    Catch
                        .SubItems.Add("")
                    End Try

                    .SubItems.Add(objMgmt.Path.ToString)

                End With

                updateCounter -= 1

                If updateCounter = 0 Then

                    lblInfo.Text = "Connected; loading Classes (" + lvClasses.Items.Count.ToString("###,##0") + ")..."
                    Application.DoEvents()

                    updateCounter = updateCount

                End If

            Next

        Catch
        End Try

    End Sub

    Private Function WMI_EnumProperties(ByVal mngmtObj As ManagementObject) As ArrayList

        If Me.InvokeRequired Then
            Return Me.Invoke(New WMI_EnumProperties_Func(AddressOf WMI_EnumProperties), mngmtObj)
        Else

            Dim propCollection As New Hashtable

            Dim getOptions As New ObjectGetOptions
            getOptions.UseAmendedQualifiers = True

            Dim thisClass As New ManagementClass(_mngtScope, mngmtObj.ClassPath, getOptions)
            For Each thisProp As PropertyData In thisClass.Properties

                Try

                    propCollection.Add(thisProp.Name.ToLower, thisProp.Qualifiers.Item("description").Value)

                Catch
                End Try

            Next

            Dim ret As New ArrayList

            Try

                For Each thisProp As PropertyData In mngmtObj.Properties

                    Dim rowId As Integer = _workerQuery.dataQuery.Columns.Add(thisProp.Name.ToLower, thisProp.Name)

                    If rowId >= 0 And propCollection.ContainsKey(thisProp.Name.ToLower) Then _
                        _workerQuery.dataQuery.Columns(rowId).ToolTipText = propCollection(thisProp.Name.ToLower)

                    ret.Add(thisProp.Name)

                Next

            Catch
            End Try

            Return ret

        End If

    End Function

    Private Sub WMI_EnumMethods(ByVal className As String)

        If Me.InvokeRequired Then
            Me.Invoke(New WMI_EnumMethods_Sub(AddressOf WMI_EnumMethods), className)
        Else

            _workerQuery.mnuSep.Visible = False
            _workerQuery.mnuMethods.Visible = False

            _workerQuery.mnuMethods.DropDownItems.Clear()

            Dim getOptions As New ObjectGetOptions
            getOptions.UseAmendedQualifiers = True

            Dim thisClass As New ManagementClass(_mngtScope, New ManagementPath(className), getOptions)
            For Each thisMethod As MethodData In thisClass.Methods

                Try

                    Dim inProps As String = ""
                    For Each thisProp As PropertyData In thisMethod.InParameters.Properties
                        inProps = thisProp.Name + ","
                    Next

                    If inProps.Length > 0 Then inProps = inProps.Substring(0, inProps.Length - 1)

                    _workerQuery.mnuMethods.DropDownItems.Add(New ToolStripMenuItem(thisMethod.Name + "(" + inProps + ")", Nothing, New EventHandler(AddressOf methodClick), thisMethod.Name))

                Catch
                End Try

            Next

            If _workerQuery.mnuMethods.DropDownItems.Count > 0 Then

                _workerQuery.mnuSep.Visible = True
                _workerQuery.mnuMethods.Visible = True

            End If

        End If

    End Sub

    Friend Sub WMI_Query(ByVal inQuery As String)

        _workerQuery = tabMain.SelectedTab.Tag

        _workerThread = New Thread(AddressOf WMI_Query_Threaded)
        _workerThread.IsBackground = True
        _workerThread.Start(inQuery)

    End Sub

    Private Sub WMI_Query_Threaded(ByVal inQuery As Object)

        Const updateCount As Integer = 100

        Dim res As ManagementObjectCollection = Nothing

        Try

            Dim thisQuery As String = CType(inQuery, String)
            Dim objSelect As New SelectQuery(thisQuery)

            With New WMI_EnumMethods_Sub(AddressOf WMI_EnumMethods)
                .Invoke(objSelect.ClassName)
            End With

            Dim mngtObjSearcher As New ManagementObjectSearcher(_mngtScope, objSelect)
            res = mngtObjSearcher.Get()

        Catch ex As Exception
            _lastErrorException = ex
        End Try

        If Not (res Is Nothing) Then

            Dim rowCount As Integer = 0
            Dim updateCounter As Integer = updateCount

            Dim propList As ArrayList = Nothing

            Try

                For Each objMgmt As ManagementObject In res

                    If rowCount = 0 Then

                        With New WMI_EnumProperties_Func(AddressOf WMI_EnumProperties)
                            propList = .Invoke(objMgmt)
                        End With

                    End If

                    Dim rowId As Integer = -1

                    With New WMI_Grid_AddRow_Func(AddressOf WMI_Grid_AddRow)
                        rowId = .Invoke(objMgmt)
                    End With

                    If rowId >= 0 Then

                        For Each thisProp As String In propList

                            Dim value As Object = objMgmt.GetPropertyValue(thisProp)
                            If Not (value Is Nothing) Then

                                With New WMI_Update_Grid_Sub(AddressOf WMI_Update_Grid)
                                    .Invoke(rowId, thisProp.ToLower, value.ToString)
                                End With

                            End If

                        Next thisProp

                    End If

                    rowCount += 1
                    updateCounter -= 1

                    If updateCounter = 0 Then

                        With New WMI_Update_Status_Sub(AddressOf WMI_Update_Status)
                            .Invoke("Executing query (" + rowCount.ToString("###,##0") + ")...")
                        End With

                        updateCounter = updateCount

                    End If

                Next objMgmt

            Catch
            End Try

        End If

        With New WMI_Query_Result_Sub(AddressOf WMI_Query_Result)
            .Invoke()
        End With

    End Sub

    Private Sub WMI_Query_Result()

        If Me.InvokeRequired Then
            Me.Invoke(New WMI_Query_Result_Sub(AddressOf WMI_Query_Result))
        Else

            _workerThread = Nothing

            chkTopLevelClasses.Enabled = True
            If radConnRemote.Checked Then grpRemote.Enabled = True
            grpConnection.Enabled = True
            tvNamespaces.Enabled = True
            lvClasses.Enabled = True
            _showWaitCursor(False)

            lblInfo.Spring = True
            lblCancel.Visible = False

            If Not (_lastErrorException Is Nothing) Then

                lblInfo.Text = "Ready"
                _workerQuery.TabText = "Query #" + CInt(_workerQuery.Tag).ToString("#00")

                MessageBox.Show("Error: " + _lastErrorException.Message, _
                    Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)

                _lastErrorException = Nothing

            Else

                Dim rowCount As Integer = _workerQuery.dataQuery.RowCount

                _workerQuery.TabText = "Query #" + CInt(_workerQuery.Tag).ToString("#00") + " (" + rowCount.ToString("###,##0") + ")"
                lblInfo.Text = "Ready; " + rowCount.ToString("###,##0") + " instance(s) retrieved"

            End If

            _workerQuery = Nothing

        End If

    End Sub

    Private Function WMI_Grid_AddRow(ByVal tagData As Object) As Integer

        If Me.InvokeRequired Then
            Return Me.Invoke(New WMI_Grid_AddRow_Func(AddressOf WMI_Grid_AddRow), tagData)
        Else

            Dim rowId As Integer = _workerQuery.dataQuery.Rows.Add()
            If rowId >= 0 Then _workerQuery.dataQuery.Rows(rowId).Tag = tagData

            Return rowId

        End If

    End Function

    Private Sub WMI_Update_Grid(ByVal rowId As Integer, ByVal propName As String, ByVal value As String)

        If Me.InvokeRequired Then
            Me.Invoke(New WMI_Update_Grid_Sub(AddressOf WMI_Update_Grid), rowId, propName, value)
        Else
            _workerQuery.dataQuery.Rows(rowId).Cells(propName).Value = value
        End If

    End Sub

    Private Sub WMI_Update_Status(ByVal statusText As String)

        If Me.InvokeRequired Then
            Me.Invoke(New WMI_Update_Status_Sub(AddressOf WMI_Update_Status), statusText)
        Else
            lblInfo.Text = statusText
        End If

    End Sub

    Friend Function _isConnected() As Boolean

        Return Not (_mngtScope Is Nothing) AndAlso _
            _mngtScope.IsConnected

    End Function

    Friend Function _isBusy() As Boolean

        Return Not (_workerThread Is Nothing)

    End Function

    Public Sub methodClick(ByVal sender As Object, ByVal e As EventArgs)

        Dim methodItem As ToolStripMenuItem = sender
        Dim methodName As String = methodItem.Name
        Dim methodDisplay As String = methodItem.Text
        Dim _script As String = ""

        With New clsEditDialog(Me)

            .InputText = "'Call method.Args.Add(0)" + _
                vbCrLf + "Call method.Execute()"
            .Show("Invoke Method :: " + methodDisplay)

            _script = .OutputText

        End With

        If _script.Trim <> "" Then

            Dim scriptingObj As Object = CreateObject("MSScriptControl.ScriptControl")

            With scriptingObj
                .Language = "VBScript"
                .AllowUI = False
                .AddObject("method", New clsWMIMethodInvoke(tabMain.SelectedTab.Tag.dataQuery.CurrentRow.Tag, methodName))
            End With

            Try

                With scriptingObj
                    .AddCode("function execScript() " + vbCrLf + _script + vbCrLf + " end function")
                    .Eval("execScript()")
                    .Reset()
                End With

            Catch ex As Exception

                MessageBox.Show("Error: " + ex.Message, Application.ProductName, _
                    MessageBoxButtons.OK, MessageBoxIcon.Error)

            End Try

            scriptingObj = Nothing

        End If

    End Sub

    Friend Sub _showWaitCursor(ByVal bShow As Boolean)

        ' show/hide the hourglass
        If bShow Then

            Me.Cursor = Cursors.WaitCursor
            Me.UseWaitCursor = True

        Else

            Try

                tabMain.SelectedTab.Tag.dataQuery.Cursor = Cursors.Default

            Catch
            End Try

            Me.UseWaitCursor = False
            Me.Cursor = Cursors.Arrow

        End If

    End Sub

    Private Sub _setConnState(ByVal connState As ConnStates)

        If connState = ConnStates.ConnInactive Then

            lblConn.Image = GetResourceImage("ico_silver")
            lblConn.ToolTipText = "No connection"

        ElseIf connState = ConnStates.ConnDisconnected Then

            lblConn.Image = GetResourceImage("ico_red")
            lblConn.ToolTipText = "Disconnected"

        ElseIf connState = ConnStates.ConnConnected Then

            lblConn.Image = GetResourceImage("ico_green")
            lblConn.ToolTipText = "Connected"

        End If

    End Sub

    Private Sub frmWMISpy_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated

        splitMain.BackColor = Color.FromKnownColor(KnownColor.ControlDark)
        splitHorz.BackColor = Color.FromKnownColor(KnownColor.ControlDark)
        splitVert.BackColor = Color.FromKnownColor(KnownColor.ControlDark)

    End Sub

    Private Sub frmWMISpy_Deactivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Deactivate

        splitMain.BackColor = Color.FromKnownColor(KnownColor.InactiveBorder)
        splitHorz.BackColor = Color.FromKnownColor(KnownColor.InactiveBorder)
        splitVert.BackColor = Color.FromKnownColor(KnownColor.InactiveBorder)

    End Sub

    Private Sub frmWMISpy_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        If Not grpConnection.Enabled Then

            e.Cancel = True
            Exit Sub

        End If

        If Not (_workerThread Is Nothing) Then

            _workerThread.Abort()
            _workerThread = Nothing

        End If

        If Not (_mngtScope Is Nothing) Then _
            _mngtScope = Nothing

    End Sub

    Private Sub frmWMISpy_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        btnRemote.Tag = False

        Dim newQuery As New frmWMISpyQuery(Me)
        With newQuery

            .Location = New Point(0, 0)
            .Size = tabQuery.Size
            .TopLevel = False
            .Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or _
                AnchorStyles.Left Or AnchorStyles.Right

            ' add the object to the container
            tabQuery.Controls.Add(newQuery)

            ' set the parent of the object as the container
            With New clsWindowObject(newQuery.Handle)
                .Parent = New clsWindowObject(tabQuery.Handle)
            End With

            .Show()

        End With

        newQuery.Tag = 0
        tabQuery.Tag = newQuery

    End Sub

    Private Sub frmWMISpy_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize

        lvClasses.Columns(lvCol_Path).Width = Math.Round(Math.Abs(lvClasses.Width - _
            lvClasses.Columns(lvCol_Name).Width - SystemInformation.VerticalScrollBarWidth - 6) / 3)

        lvClasses.Columns(lvCol_Desc).Width = Math.Round(Math.Abs(lvClasses.Width - _
            lvClasses.Columns(lvCol_Name).Width - SystemInformation.VerticalScrollBarWidth - 6) / 3) * 2

    End Sub

    Private Sub splitMain_SplitterMoved(ByVal sender As Object, ByVal e As System.Windows.Forms.SplitterEventArgs) Handles splitMain.SplitterMoved
        Me.Refresh()
    End Sub

    Private Sub splitHorz_SplitterMoved(ByVal sender As Object, ByVal e As System.Windows.Forms.SplitterEventArgs) Handles splitHorz.SplitterMoved
        Me.Refresh()
    End Sub

    Private Sub splitVert_SplitterMoved(ByVal sender As System.Object, ByVal e As System.Windows.Forms.SplitterEventArgs) Handles splitVert.SplitterMoved

        Me.Refresh()

        If Not (btnRemote.Tag Is Nothing) Then _
            frmWMISpy_Resize(Nothing, Nothing)

    End Sub

    Private Sub btnRemote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemote.Click

        If txtRemote.Text.Trim = "" Then

            MessageBox.Show("Enter the remote computer name.", _
                Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            txtRemote.SelectAll()
            txtRemote.Focus()

        ElseIf txtUsername.Text.Trim = "" Then

            MessageBox.Show("Enter the remote computer username.", _
                Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            txtUsername.SelectAll()
            txtUsername.Focus()

        ElseIf txtPassword.Text.Trim = "" Then

            MessageBox.Show("Enter the remote computer password.", _
                Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            txtPassword.SelectAll()
            txtPassword.Focus()

        Else

            timerLoad.Start()

        End If

    End Sub

    Private Sub timerLoad_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timerLoad.Tick

        timerLoad.Stop()

        If Not WMI_Init() Then

            Dim errMsg As String = "Error connecting to WMI service."
            If Not (_lastErrorException) Is Nothing Then

                errMsg += vbCrLf + vbCrLf + "Error: " + _lastErrorException.Message
                _lastErrorException = Nothing

            End If

            MessageBox.Show(errMsg, Application.ProductName, _
                MessageBoxButtons.OK, MessageBoxIcon.Error)

        End If

    End Sub

    Private Sub tvNamespaces_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvNamespaces.AfterSelect

        _showWaitCursor(True)
        grpConnection.Enabled = False
        grpRemote.Enabled = False
        chkTopLevelClasses.Enabled = False

        If Not lblInfo.Text.Contains("Ready;") Then _
            lblInfo.Text = "Connecting to Namespace..."

        Dim thisNamespace As String = e.Node.Tag.ToString

        Try

            ' connect to wmi service using selected namespace
            _mngtScope = New ManagementScope(String.Format( _
                DefaultConnectPath + "\" + thisNamespace, _compName), _connOptions)
            _mngtScope.Connect()

        Catch ex As Exception

            _setConnState(ConnStates.ConnDisconnected)

            chkTopLevelClasses.Enabled = True
            grpConnection.Enabled = True
            _showWaitCursor(False)

            MessageBox.Show("Error connecting to Namespace: " + _
                thisNamespace + vbCrLf + vbCrLf + "Error: " + ex.Message, _
                Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

        If tabMain.Enabled Then

            chkTopLevelClasses.Enabled = True
            grpConnection.Enabled = True
            If radConnRemote.Checked Then grpRemote.Enabled = True
            _showWaitCursor(False)

        End If

        If Not lblInfo.Text.Contains("Ready;") Then _
            lblInfo.Text = "Ready"

    End Sub

    Private Sub tvNamespaces_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles tvNamespaces.NodeMouseClick

        If (e.Button = Windows.Forms.MouseButtons.Left And _
            SystemInformation.MouseButtonsSwapped) Or _
            (e.Button = Windows.Forms.MouseButtons.Right And _
            Not SystemInformation.MouseButtonsSwapped) Then

            tvNamespaces.SelectedNode = e.Node

        End If

    End Sub

    Private Sub lvClasses_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvClasses.DoubleClick

        If lvClasses.SelectedItems.Count > 0 Then

            tabMain.SelectedTab.Tag.txtQuery.Text = "select * from " + lvClasses.SelectedItems(0).Text
            tabMain.SelectedTab.Tag.btnQuery.PerformClick()

        End If

    End Sub

    Private Sub lvClasses_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lvClasses.KeyDown

        If e.KeyCode = Keys.Enter Then _
            lvClasses_DoubleClick(sender, Nothing)

    End Sub

    Private Sub radConnLocal_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radConnLocal.CheckedChanged

        If radConnLocal.Checked Then _
            timerLoad.Start()

    End Sub

    Private Sub radConnRemote_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radConnRemote.CheckedChanged

        If radConnRemote.Checked Then

            grpRemote.Enabled = True
            txtRemote.Enabled = True
            txtRemote.SelectAll()
            txtRemote.Focus()

        Else

            txtRemote.Enabled = False
            grpRemote.Enabled = False

        End If

    End Sub

    Private Sub lblCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblCancel.Click

        If Not (_workerThread Is Nothing) Then _
            _workerThread.Abort()

        WMI_Query_Result()

    End Sub

    Private Sub tabMain_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tabMain.MouseUp

        If e.Button = Windows.Forms.MouseButtons.Middle AndAlso _
            tabMain.TabPages.Count > 1 Then

            For tabIndex As Integer = 0 To tabMain.TabPages.Count - 2

                If tabMain.GetTabRect(tabIndex).Contains(tabMain.PointToClient(Control.MousePosition)) Then

                    If _workerQuery Is Nothing Or _
                        (Not (_workerQuery Is Nothing) AndAlso _
                        Not _workerQuery.Equals(tabMain.TabPages(tabIndex).Tag)) Then

                        Dim thisIndex As Integer = tabMain.SelectedIndex

                        tabMain.TabPages(tabIndex).Tag.Close()
                        tabMain.TabPages.RemoveAt(tabIndex)

                        If thisIndex > 0 Then tabMain.SelectedIndex = thisIndex - 1

                    Else

                        MessageBox.Show("Unable to close query window. Please cancel your previous query.", _
                            Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                    End If

                    Exit For

                End If

            Next

        End If

    End Sub

    Private Sub tabMain_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles tabMain.SelectedIndexChanged

        If tabMain.SelectedIndex = tabMain.TabPages.Count - 1 Then

            Static tabCount As Integer = 0
            tabCount += 1

            Dim newTab As New TabPage("Query #" + tabCount.ToString("#00"))
            Dim newQuery As New frmWMISpyQuery(Me)

            With newQuery

                .Location = New Point(0, 0)
                .Size = newTab.Size
                .TopLevel = False
                .Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or _
                    AnchorStyles.Left Or AnchorStyles.Right

                ' add the object to the container
                newTab.Controls.Add(newQuery)

                ' set the parent of the object as the container
                With New clsWindowObject(newQuery.Handle)
                    .Parent = New clsWindowObject(newTab.Handle)
                End With

                .Show()

            End With

            newTab.Tag = newQuery

            tabMain.TabPages.Insert(tabMain.TabPages.Count - 1, newTab)
            tabMain.SelectedTab = newTab

            newQuery.Tag = tabCount
            newQuery.txtQuery.Focus()

        End If

    End Sub

    Private Sub txtRemote_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRemote.GotFocus

        Me.AcceptButton = btnRemote

    End Sub

    Private Sub txtRemote_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtRemote.LostFocus

        Me.AcceptButton = Nothing

    End Sub

    Private Sub txtUsername_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtUsername.GotFocus

        Me.AcceptButton = btnRemote

    End Sub

    Private Sub txtUsername_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtUsername.LostFocus

        Me.AcceptButton = Nothing

    End Sub

    Private Sub txtPassword_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPassword.GotFocus

        Me.AcceptButton = btnRemote

    End Sub

    Private Sub txtPassword_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPassword.LostFocus

        Me.AcceptButton = Nothing

    End Sub

End Class

Download frmWMISpy.vb

Back to file list


Back to project page