Tag: microsoft

Start/Stop/Query Service in VB.NET

Imports System.ServiceProcess

Public Class Form1

Private Sub btnQueryBES_Click(sender As System.Object, e As System.EventArgs) Handles btnQueryBES.Click
 Dim txt As String = "besclient" & SC(txtIPAddress.Text, "besclient", "query")
End Sub
Private Sub btnStartBES_Click(sender As System.Object, e As System.EventArgs) Handles btnStartBES.Click
 Dim txt As String = "besclient" & SC(txtIPAddress.Text, "besclient", "start")
End Sub
Private Sub btnStopBES_Click(sender As System.Object, e As System.EventArgs) Handles btnStopBES.Click
 Dim txt As String = "besclient" & SC(txtIPAddress.Text, "besclient", "stop")
End Sub
Function SC(MachineName As String, ServiceName As String, cmd As String) As String
 Dim txt As String = ""
 Dim TermService As New System.ServiceProcess.ServiceController(ServiceName, MachineName)

Select Case LCase(cmd)
 Case "start"
 TermService.Start()
 Case "stop"
 TermService.Stop()
 Case Else
 End Select

Select Case LCase(TermService.Status)
 Case "1"
 txt = " stopped"
 Case "2"
 txt = " starting"
 Case "3"
 txt = " stop pending"
 Case "4"
 txt = " running"
 Case Else
 txt = " undetermined"
 End Select

Return txt
 End Function

End Class

VB.NET Module for SQL Database

Imports System.Data.SqlClient

Module modDatabase

Public conn As New SqlClient.SqlConnection
Public myReader As SqlDataReader

Public dsRecords As New DataSet
Public daRecords As New SqlDataAdapter
Public MaxRecords As Integer

Public Sub OpenDatabase()
CloseSQLReader()
CloseDatabase()

conn.ConnectionString = “Server=.\sqlexpress;Database=DATABASE;Trusted_Connection=True;”
conn.Open()
End Sub

 

Public Sub CloseDatabase()
Try
conn.Close()
conn.Dispose()
Catch ex As Exception
Application.DoEvents()
End Try
End Sub

 

Public Sub launchBrowser(ByVal browser As String, ByVal url As String)
Try
Process.Start(browser, url)
Catch ex As Exception
Application.DoEvents()
‘MessageBox.Show(ex.Message)
End Try
End Sub

 

Public Sub CloseSQLReader()
Try
myReader.Close()
Catch ex As Exception
Application.DoEvents()
End Try
End Sub

 

Public Sub ReadSqlData(ByVal sqlSelect As String)
Dim myCommand As New SqlCommand(sqlSelect, conn)
myReader = myCommand.ExecuteReader()
End Sub

 

Public Function ExecuteSqlCommand(ByVal sqlSelect As String) As Boolean
Dim flg As Boolean = False
Dim myCommand As New SqlCommand(sqlSelect, conn)

Try
flg = myCommand.ExecuteNonQuery
Catch
flg = False
End Try

Return flg
End Function

 

Public Sub HoldYourPants(ByVal Seconds As Integer)
System.Threading.Thread.Sleep(Seconds * 1000)
Application.DoEvents()
End Sub

End Module

Identify all user keys in HKEY_USERS

This example shows how to enumerate the user keys in HKEY_USERS

Public Sub OutputRegKey(Key As RegistryKey)
Imports Microsoft.Win32

Public Class Form1
  Public arrUsers(100) As String, arrUsersMax As Integer = 0

  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim i As Integer
    Dim txt As String = ""

    '-------------------------------------------------------------------
    ' identify the HKEY_Users
    '-------------------------------------------------------------------
    arrUsers(0) = ".DEFAULT"

    Dim regkey As RegistryKey = Registry.Users.OpenSubKey("")
    EnumerateHkeyUsers(regkey)
    regkey.Close()

    For i = 0 To arrUsersMax
      txt += arrUsers(i) & vbCrLf
    Next

    MsgBox(txt)
    End
  End Sub

  Sub EnumerateHkeyUsers(ByVal rkey As RegistryKey)
    Dim names As String() = rkey.GetSubKeyNames()
    Dim txt As String = ""

    For Each subkey In names
      txt = subkey.ToString

      If Len(txt) > 10 And InStr(LCase(txt), "_classes") = 0 Then
        arrUsersMax = arrUsersMax + 1
        arrUsers(arrUsersMax) = txt
      End If
    Next
  End Sub

End Class

SQL Left Function

The LEFT function allows you to extract a substring from a string, starting from the left-most character.

LEFT( string, number_of_characters )

Example:

SELECT LEFT('TechOnTheNet.com', 4);
Result: 'Tech'

Determine if a user belongs to a particular AD Group

This is the easiest way to determine if a user belongs to particular Active Directory user group using VB.NET without having to enumerate through all the user’s groups.

Public Function IsInGroup(ByVal GroupName As String) As Boolean
 Dim MyIdentity As System.Security.Principal.WindowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent()
 Dim MyPrincipal As System.Security.Principal.WindowsPrincipal = New System.Security.Principal.WindowsPrincipal(MyIdentity)
 Return MyPrincipal.IsInRole(GroupName)
End Function