Registriert seit: 8. Feb 2005
51 Beiträge
Delphi 2009 Professional
|
Re: Magic Packet richtig zusammenstellen
23. Mai 2009, 21:35
Hi,
hab zwar kein Delphi 2009 zum testen aber vielleicht funktioniert folgender Code:
Delphi-Quellcode:
uses winsock;
....
function WakeOnLan(szMac: String): Boolean;
var
sock : TSocket;
addr : TSockAddrIn;
WSA : TWSAData;
buf : array[0..101] of byte;
HWAddr : array[0..5] of byte;
bTrue : Boolean;
P : PChar;
I : Integer;
Code : Integer;
szTmp : String;
dwRet : Cardinal;
Seperatorcount: Integer;
begin
bTrue:= True;
Result:= False;
Seperatorcount:= 0;
if (length(szMac) <> 17) and (length(szMac) <> 12) then Exit;
for I:= 1 to length(szMac) do begin
if not (szMac[I] in ['A'..'F','0'..'9','a'..'f', '-', ':']) then Exit;
if (szMac[i] in ['-', ':']) then begin
inc(Seperatorcount);
if Seperatorcount > 5 then Exit;
end;
end;
szTmp:= szMac;
repeat
I:= pos('-', szTmp);
if I > 0 then Delete(szTmp, I, 1);
until
I <= 0;
repeat
I:= pos(':', szTmp);
if I > 0 then Delete(szTmp, I, 1);
until
I <= 0;
FillChar(buf, 6, $FF);
P:= PChar(szTmp);
for I:= 0 to 5 do begin
val('$'+P^+(P+1)^, HWAddr[i], Code);
inc(P,2);
end;
for I:= 0 to 15 do
move(HWAddr, buf[6+(i*6)], 6);
dwRet:= WSAStartup((2 shl 8)+1, WSA);
if dwRet = 0 then begin
sock:= socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if sock <> INVALID_SOCKET then begin
if setsockopt(sock, SOL_SOCKET, SO_BROADCAST, pchar(@bTrue), sizeof(bTrue)) = 0 then begin
FillChar(addr, sizeof(addr), 0);
addr.sin_family:= AF_INET;
addr.sin_addr.s_addr:= INADDR_BROADCAST;
addr.sin_port:= htons(60000);
dwRet:= sendto(sock, buf, sizeof(buf), 0, addr, sizeof(addr));
Result:= (dwRet=sizeof(buf));
end;
end;
WSACleanup;
end;
end;
|
|
Zitat
|