Implementation of Wake On Lan procedure (Views: 300)
Problem/Question/Abstract: This procedure will switch on a machine that is connected to a LAN. The MAC address of the machine is needed to be known. See my article "Get MAC Address of Remote or Local" for a function GetMacAddress() that returns a MAC Address String. The "Wake On Lan" feature of the machine's BIOS must be enabled. The procedure works by broadcasting a UDP packet containing the "Magic Number" to all machines on the LAN. The machine with the MAC address, if switched of and BIOS WOL enabled will wake up and boot. The MAC address required is a "-" delimited 17 char string. Example : WakeOnLan('00-D0-B7-E2-A1-A0'); Answer: uses idUDPClient; // ========================================================================== // Wakes a machine on lan // AMacAddress is 17 char MAC address. // eg. '00-C0-4F-0A-3A-D7' // ========================================================================== procedure WakeOnLan(const AMacAddress: string); type TMacAddress = array[1..6] of byte; TWakeRecord = packed record Waker: TMACAddress; MAC: array[0..15] of TMACAddress; end; var i: integer; WR: TWakeRecord; MacAddress: TMacAddress; UDP: TIdUDPClient; sData: string; begin // Convert MAC string into MAC array fillchar(MacAddress, SizeOf(TMacAddress), 0); sData := trim(AMacAddress); if length(sData) = 17 then begin for i := 1 to 6 do begin MacAddress[i] := StrToIntDef('$' + copy(sData, 1, 2), 0); sData := copy(sData, 4, 17); end; end; for i := 1 to 6 do WR.Waker[i] := $FF; for i := 0 to 15 do WR.MAC[i] := MacAddress; // Create UDP and Broadcast data structure UDP := TIdUDPClient.Create(nil); UDP.Host := '255.255.255.255'; UDP.Port := 32767; UDP.BroadCastEnabled := true; UDP.SendBuffer(WR, SizeOf(TWakeRecord)); UDP.BroadcastEnabled := false; UDP.Free; end; |