<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:snippet="http://codexchange.org/schemas/snippet/1.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
  <channel>
    <title>CodeXchange: User Contributed Snippets</title>
    <link>http://www.codexchange.org</link>
    <description>Summary of the snippets published by a codexchange user</description>
    <language>en-us</language>
    <copyright>Copyright © CodeXchange, 2004-2006</copyright>
    <generator>CodeXchange RSS Feed Generator v1.0</generator>
    <webMaster>webmaster@codexchange.org</webMaster>
    <lastBuildDate>Thu, 15 Oct 2026 10:20:05 GMT</lastBuildDate>
    <ttl>20</ttl>
    <dc:language>en-us</dc:language>
    <dc:rights>Copyright © CodeXchange, 2004-2006</dc:rights>
    <dc:date>10/15/2026 10:20:05 AM</dc:date>
    <dc:publisher />
    <dc:creator>CodeXchange RSS Feed Generator v1.0</dc:creator>
    <item>
      <guid>/PreviewSnippet.aspx?SnippetID=74a7d3f8-4863-4114-a336-14a60f1fa4e3</guid>
      <title>Shutdown, Restart, LogOff or Lock Workstation</title>
      <link>/PreviewSnippet.aspx?SnippetID=74a7d3f8-4863-4114-a336-14a60f1fa4e3</link>
      <description>Shutdown, Restart, LogOff or Lock Workstation [VB.NET]</description>
      <author>Mark Brackett</author>
      <pubDate>Tue, 13 Dec 2005 12:49:11 GMT</pubDate>
      <comments>/PreviewSnippet.aspx?SnippetID=74a7d3f8-4863-4114-a336-14a60f1fa4e3#comments</comments>
      <category>bb47ac87-7cde-45ac-aa6e-e677d46fc216</category>
      <dc:title>Shutdown, Restart, LogOff or Lock Workstation</dc:title>
      <dc:date>12/13/2005 12:49:11 PM</dc:date>
      <dc:creator>Mark Brackett</dc:creator>
      <content:encoded><![CDATA[<pre>Imports System
' Import used for DLLImport Attribute
Imports System.Runtime.InteropServices

Public Module Shutdown
	' Constants
	Private Const SE_PRIVILEGE_ENABLED As Integer = &H2
	Private Const TOKEN_QUERY As Integer = &H8
	Private Const TOKEN_ADJUST_PRIVILEGES As Integer = &H20
	Private Const SE_SHUTDOWN_NAME As String = "SeShutdownPrivilege"
	' Exit Windows Constants
	Private Const EWX_LOGOFF As Integer = &H0
	Private Const EWX_SHUTDOWN As Integer = &H1
	Private Const EWX_REBOOT As Integer = &H2
	Private Const EWX_FORCE As Integer = &H4
	Private Const EWX_POWEROFF As Integer = &H8
	Private Const EWX_FORCEIFHUNG As Integer = &H10

	'Structure
	<StructLayout(LayoutKind.Sequential, Pack:=1)> _
	Private Structure Luid
		Public Count As Integer
		Public Luid As Long
		Public Attr As Integer
	End Structure	'TokPriv1Luid

	' Lock Workstation
	<DllImport("user32.dll", ExactSpelling:=True)> _
	Private Function LockWorkStation() As Boolean
	End Function

	' Get Current Processes
	<DllImport("kernel32.dll", ExactSpelling:=True)> _
	Private Function GetCurrentProcess() As IntPtr
	End Function

	' Open Process Token
	<DllImport("advapi32.dll", SetLastError:=True)> _
	Private Function OpenProcessToken(ByVal h As IntPtr, ByVal acc As Integer, ByRef phtok As IntPtr) As Boolean
	End Function

	' Look up Priviledge Value
	<DllImport("advapi32.dll", SetLastError:=True)> _
	Private Function LookupPrivilegeValue(ByVal host As String, ByVal name As String, ByRef pluid As Long) As Boolean
	End Function

	' Adjust Token Priviledges
	<DllImport("advapi32.dll", ExactSpelling:=True, SetLastError:=True)> _
	Private Function AdjustTokenPrivileges(ByVal htok As IntPtr, ByVal disall As Boolean, ByRef newst As Luid, ByVal len As Integer, ByVal prev As IntPtr, ByVal relen As IntPtr) As Boolean
	End Function

	' Exit Windows
	<DllImport("user32.dll", ExactSpelling:=True, SetLastError:=True)> _
	Private Function ExitWindowsEx(ByVal flg As Integer, ByVal rea As Integer) As Boolean
	End Function

	' Exit Windows Sub
	Private Sub DoExitWindows(ByVal flg As Integer)
		Dim tp As Luid
		Dim hproc As IntPtr = GetCurrentProcess()
		Dim htok As IntPtr = IntPtr.Zero
		OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, htok)
		tp.Count = 1
		tp.Luid = 0
		tp.Attr = SE_PRIVILEGE_ENABLED
		LookupPrivilegeValue(Nothing, SE_SHUTDOWN_NAME, tp.Luid)
		AdjustTokenPrivileges(htok, False, tp, 0, IntPtr.Zero, IntPtr.Zero)
		ExitWindowsEx(flg, 0)
	End Sub

	' Shutdown
	Public Sub Shutdown()
		DoExitWindows(EWX_SHUTDOWN)
	End Sub

	' Restart
	Public Sub Restart()
		DoExitWindows(EWX_REBOOT Or EWX_FORCE)
	End Sub

	' Log off
	Public Sub LogOff()
		DoExitWindows(EWX_LOGOFF)
	End Sub

	' Lock Workstation
	Public Sub LockTheComputer()
		LockWorkStation()
	End Sub
End Module
</pre>]]></content:encoded>
      <snippet:downloads>4</snippet:downloads>
      <snippet:rating>4</snippet:rating>
      <snippet:language>VB.NET</snippet:language>
    </item>
    <item>
      <guid>/PreviewSnippet.aspx?SnippetID=60f04190-c4cc-4e83-ba6c-23239503fd60</guid>
      <title>Test an Ip Address to see if it's contained in a subnet</title>
      <link>/PreviewSnippet.aspx?SnippetID=60f04190-c4cc-4e83-ba6c-23239503fd60</link>
      <description>Test an Ip Address to see if it's contained in a subnet [VB.NET]</description>
      <author>Mark Brackett</author>
      <pubDate>Tue, 21 Feb 2006 09:44:16 GMT</pubDate>
      <comments>/PreviewSnippet.aspx?SnippetID=60f04190-c4cc-4e83-ba6c-23239503fd60#comments</comments>
      <category>edde3ea1-24a6-4cb0-8446-ab91e1800116</category>
      <dc:title>Test an Ip Address to see if it's contained in a subnet</dc:title>
      <dc:date>2/21/2006 9:44:16 AM</dc:date>
      <dc:creator>Mark Brackett</dc:creator>
      <content:encoded><![CDATA[<pre>	Public Function ContainsAddress(ByVal ip As Net.IPAddress) As Boolean
		' Could just use ip.Address, but it's marked as Obsolete
		Return ((BitConverter.ToInt32(HostAddress.GetAddressBytes, 0) And BitConverter.ToInt32(SubnetMask.GetAddressBytes, 0)) _
		 = (BitConverter.ToInt32(ip.GetAddressBytes, 0) And BitConverter.ToInt32(SubnetMask.GetAddressBytes, 0)))
	End Function</pre>]]></content:encoded>
      <snippet:downloads>2</snippet:downloads>
      <snippet:rating>0</snippet:rating>
      <snippet:language>VB.NET</snippet:language>
    </item>
    <item>
      <guid>/PreviewSnippet.aspx?SnippetID=c1c77c13-8da5-4322-b95b-5f488eb34e09</guid>
      <title>Cidr to a Netmask</title>
      <link>/PreviewSnippet.aspx?SnippetID=c1c77c13-8da5-4322-b95b-5f488eb34e09</link>
      <description>Cidr to a Netmask [VB.NET]</description>
      <author>Mark Brackett</author>
      <pubDate>Tue, 21 Feb 2006 09:43:35 GMT</pubDate>
      <comments>/PreviewSnippet.aspx?SnippetID=c1c77c13-8da5-4322-b95b-5f488eb34e09#comments</comments>
      <category>edde3ea1-24a6-4cb0-8446-ab91e1800116</category>
      <dc:title>Cidr to a Netmask</dc:title>
      <dc:date>2/21/2006 9:43:35 AM</dc:date>
      <dc:creator>Mark Brackett</dc:creator>
      <content:encoded><![CDATA[<pre>	Public Shared Function CidrToNetmask(ByVal cidr As Integer) As Net.IPAddress
		If cidr < 0 OrElse cidr > 32 Then
			Throw New ArgumentException("cidr must be between 0 and 32")
		End If

		Dim s As String = String.Empty
		Dim post, remainder As Integer
		Dim divide As Double
		remainder = cidr Mod 8
		divide = cidr / 8

		Do While (divide > 0)
			s &= "255."
			divide -= 1
			post += 1
		Loop

		If remainder > 0 Then
			s &= ((0 Or CType(Math.Pow(2, (remainder - 1)), Integer)) << (8 - remainder)).ToString & "."
			post += 1
		End If

		Do While post < 4
			s &= "0."
			post += 1
		Loop

		If s.EndsWith("."c) Then
			Return Net.IPAddress.Parse(s.Substring(0, s.LastIndexOf("."c)))
		Else
			Return Net.IPAddress.Parse(s)
		End If
	End Function</pre>]]></content:encoded>
      <snippet:downloads>0</snippet:downloads>
      <snippet:rating>0</snippet:rating>
      <snippet:language>VB.NET</snippet:language>
    </item>
  </channel>
</rss>