Wake-on-lan code in Visual Basic

Visual Basic code that you can use to send a Wake-on-LAN (WOL) packet to a computer on your network:

Imports System.Net
Imports System.Net.Sockets

Private Sub WakeOnLan(ByVal macAddress As String)
    Dim mac As Byte() = New Byte(5) {}
    Dim i As Integer = 0

    For Each part In macAddress.Split(":"c)
        mac(i) = Byte.Parse(part, NumberStyles.HexNumber)
        i += 1
    Next

    Dim client As New UdpClient()
    Dim broadcast As IPAddress = IPAddress.Parse("255.255.255.255")
    Dim sendBytes As Byte() = New Byte(17) {}

    For i = 0 To 5
        sendBytes(i) = &HFF
    Next

    For i = 1 To 16
        sendBytes(i + 1) = mac(i Mod 6)
    Next

    client.Connect(broadcast, 30000)
    client.Send(sendBytes, sendBytes.Length)
    client.Close()
End Sub

To use this code, you will need to specify the MAC address of the computer you want to wake up, and pass it as a string to the WakeOnLan function. For example:

WakeOnLan("00:11:22:33:44:55")

Keep in mind that the computer you are trying to wake up must have Wake-on-LAN enabled in the BIOS or UEFI firmware, and must be connected to the same network as the computer running this code.

Leave a Comment

Your email address will not be published. Required fields are marked *