トップへ(mam-mam.net/)

Wake-on-LAN in Delphi Send Magic Packets to Power On Remote PCs

Japanese

Wake-on-LAN in Delphi Send Magic Packets to Power On Remote PCs

Create a New Delphi Project and Place the Components

Start Delphi and select “File” → “New” → “Windows VCL Application – Delphi”.
From the Tool Palette, drag and drop a TMaskEdit onto the form.
Set the following properties:
Name: E_Mac
EditMask: >AA\-AA\-AA\-AA\-AA\-AA;1;_
Text: 00-00-00-00-00-00
Drag and drop a TEdit onto the form.
Set the following property:
Name: E_BroadcastIP
Drag and drop a TButton onto the form.
Drag and drop a TIdUDPClient onto the form as well.

Programming

Double‑click Button1 and enter the following source code.

...
implementation

{$R *.dfm}

uses IdGlobal;

// Send Wake-on-LAN magic packet
procedure TForm1.Button1Click(Sender: TObject);
var
  b: TIdBytes;
  i: Integer;
  st: String;
  ar: TArray;
begin
  st := E_Mac.Text;
  ar := st.Split([':']);
  if Length(ar) <> 6 then Exit;

  // Create magic packet buffer (6 × FF + 16 × MAC address)
  SetLength(b, 6 * 17);
  for i := 0 to 5 do b[i] := $FF;

  for i := 1 to 16 do
  begin
    b[i*6+0] := StrToIntDef('$' + ar[0], 0);
    b[i*6+1] := StrToIntDef('$' + ar[1], 0);
    b[i*6+2] := StrToIntDef('$' + ar[2], 0);
    b[i*6+3] := StrToIntDef('$' + ar[3], 0);
    b[i*6+4] := StrToIntDef('$' + ar[4], 0);
    b[i*6+5] := StrToIntDef('$' + ar[5], 0);
  end;

  IdUDPClient1.BroadcastEnabled := True;
  IdUDPClient1.Host := E_BroadcastIP.Text;
  IdUDPClient1.SendBuffer(b);

  ShowMessage('The Wake-on-LAN magic packet has been sent. The PC should start shortly.');
end;

end.

Running the Application

Click the Run button to compile and start the application.
In Edit1, enter the MAC address of the PC you want to wake via WOL.
In Edit2, enter the broadcast IP address of your network.
(Example) 192.168.1.255
[When the network address is 192.168.1.0 and the subnet mask is 255.255.255.0]
When you click Button1, a message saying “The Wake-on-LAN magic packet has been sent. The PC should start shortly.” will appear,
and a magic packet will be broadcast via UDP/IP to the specified broadcast address.

Explanation

First, the target PC must be configured in its UEFI (BIOS) settings to allow Wake-on-LAN. (Some PCs do not support WOL.)
You also need to know the MAC address of the target PC’s LAN adapter.
You can obtain it using my free tool "Mam Master Browser Search".
A magic packet consists of 6 bytes of &HFF followed by the target MAC address repeated 16 times, for a total of 102 bytes.
Wake-on-LAN works by sending this magic packet via UDP/IP to the network’s broadcast address (UDP can broadcast to all nodes; TCP cannot broadcast).