Показать сообщение отдельно
  #6  
Старый 11.10.2011, 17:18
Lucky192 Lucky192 вне форума
Прохожий
 
Регистрация: 04.10.2011
Сообщения: 28
Репутация: 1351
По умолчанию

Код:
type
  TMACAddress = packed record
    case integer of
    0: (s1,s2,s3,s4,s5,s6 : byte; );
    1: (cmp1:word; cmp2:integer;);
  end;

  TWakeupMagicPacket = packed record
    FillFF : array [0..5] of byte;
    Mac  : array [0..15] of TMACAddress;
  end;

function TryStrToMac(str:string; var mac:TMACAddress):boolean;
var a,b:integer;
const ToHex = '0123456789ABCDEF';
begin
  Result:=false;
  str:=AnsiUpperCase(trim(str));
  if length(str)<17 then begin
    mac.cmp1:=0;
    mac.cmp2:=0;
    exit;
  end;
  a:=pos(str[1],ToHex)-1; b:=pos(str[2],ToHex)-1;
  if((a>=0)and(b>=0)and(str[3]='-')) then mac.s1:=a*16+b else exit;
  a:=pos(str[4],ToHex)-1; b:=pos(str[5],ToHex)-1;
  if((a>=0)and(b>=0)and(str[6]='-')) then mac.s2:=a*16+b else exit;
  a:=pos(str[7],ToHex)-1; b:=pos(str[8],ToHex)-1;
  if((a>=0)and(b>=0)and(str[9]='-')) then mac.s3:=a*16+b else exit;
  a:=pos(str[10],ToHex)-1; b:=pos(str[11],ToHex)-1;
  if((a>=0)and(b>=0)and(str[12]='-')) then mac.s4:=a*16+b else exit;
  a:=pos(str[13],ToHex)-1; b:=pos(str[14],ToHex)-1;
  if((a>=0)and(b>=0)and(str[15]='-')) then mac.s5:=a*16+b else exit;
  a:=pos(str[16],ToHex)-1; b:=pos(str[17],ToHex)-1;
  if((a>=0)and(b>=0)) then mac.s6:=a*16+b else exit;
  Result:=true;
end;

function TryWakeUpComputer(const MacAddress: string):boolean;
var i      : integer;
    mac    : TMACAddress;
    pkt    : TWakeupMagicPacket;
begin
  Result := false;
  if not TryStrToMac(MacAddress,mac) then exit;
  FillChar(pkt.FillFF[0],SizeOf(pkt.FillFF),$FF);
  for i:=0 to High(pkt.Mac) do pkt.Mac[i]:=mac;
  with TIdUDPClient.Create(nil) do try
    BroadcastEnabled := True;
    Host := '255.255.255.255';
    Port := 2050;
    SendBuffer(pkt,sizeof(pkt));
    Result := true;
  finally
    Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if not TryWakeUpComputer('01-02-03-04-05-06') then         
  begin
    // Do something...
    ExitProcess(0);
  end;
end;
Ответить с цитированием