#Region _WakeOnLan()
; ===================================================================
; _WakeOnLan($sMAC, $sIP = @IPAddress1, $nPort = 9)
;
; Sends a Wake-On-LAN packet to the broadcast address of the specified IP.
; Parameters:
; $sMAC - IN - The MAC address of the target NIC.
; $sIP - IN/OPTIONAL - The IP address or broadcast address to send on.
; $nPort - IN/OPTIONAL - The port to send on.
; Returns:
; Success - True.
; Failure - False with @error set:
; 1 = Invalid MAC address.
; 2 = Invalid IP address.
; 3 = Invalid port.
; 4 = Unable to open communication with broadcast address.
; 5 = Unable to send magic packet to the broadcast address.
; ===================================================================
Func _WakeOnLan($sMAC, $sIP = @IPAddress1, $nPort = 9)
Local $sMsg
; Convert the MAC address to the format we need.
$sMAC = StringReplace($sMAC, ":", "")
$sMAC = StringReplace($sMAC, "-", "")
$sMsg = $sMAC
$sMAC = ""
For $i = 1 To StringLen($sMsg) - 1 Step 2
$sMAC &= Chr(Dec(StringMid($sMsg, $i, 2)))
Next
$sMsg = ""
; Do sanity checks on input
If StringLen($sMAC) <> 6 Then Return SetError(1, 0, False)
$sIP = StringRegExpReplace($sIP, "(d{1,3}.d{1,3}.d{1,3}).*", "1.255")
If @error Then Return SetError(2, 0, False)
If Not StringIsDigit($nPort) Then Return SetError(3, 0, False)
; Add the FF FF FF FF FF FF header.
For $i = 1 To 6
$sMsg &= Chr(255)
Next
; Duplicate MAC address 16 times.
For $i = 1 To 16
$sMsg &= $sMAC
Next
Local $nError, $nExtended
Do
UDPStartup()
Local $aSocket = UDPOpen($sIP, $nPort)
If @error Then
$nError = 4
$nExtended = @error
EndIf
UDPSend($aSocket, $sMsg)
If @error Then
$nError = 5
$nExtended = @error
EndIf
Until True
UDPCloseSocket($aSocket)
UDPShutdown()
Return SetError($nError, $nExtended, $nError = 0)
EndFunc ; _WakeOnLan()
#EndRegion _WakeOnLan()