DELPHI 中如何向一个MAC地址发一个信息呀,还有就是如何知道一个机器的MAC地址?(100分)

  • 主题发起人 主题发起人 gdcqs
  • 开始时间 开始时间
G

gdcqs

Unregistered / Unconfirmed
GUEST, unregistred user!
DELPHI 中如何向一个MAC地址发一个信息呀,还有就是如何知道一个机器的MAC地址?
谢谢
 
function GetMacAddresses(const Machine: string; const Addresses: TStrings): Integer;

var

NCB: TNCB;

Enum: TLanaEnum;

I, L, NameLen: Integer;

Adapter: ASTAT;

MachineName: string;

begin

Result := -1;

Addresses.Clear;

MachineName := UpperCase(Machine);

if MachineName = '' then

MachineName := '*';

NameLen := Length(MachineName);

L := NCBNAMSZ - NameLen;

if L > 0 then

begin

SetLength(MachineName, NCBNAMSZ);

FillChar(MachineName[NameLen + 1], L, ' ');

end;

FillChar(NCB, SizeOf(NCB), #0);

NCB.ncb_command := NCBENUM;

NCB.ncb_buffer := Pointer(@Enum);

NCB.ncb_length := SizeOf(Enum);

if NetBios(@NCB) = NRC_GOODRET then

begin

Result := Enum.Length;

for I := 0 to Ord(Enum.Length) - 1 do

begin

FillChar(NCB, SizeOf(NCB), #0);

NCB.ncb_command := NCBRESET;

NCB.ncb_lana_num := Enum.lana;

if NetBios(@NCB) = NRC_GOODRET then

begin

FillChar(NCB, SizeOf(NCB), #0);

NCB.ncb_command := NCBASTAT;

NCB.ncb_lana_num := Enum.lana;

Move(MachineName[1], NCB.ncb_callname, SizeOf(NCB.ncb_callname));

NCB.ncb_buffer := PChar(@Adapter);

NCB.ncb_length := SizeOf(Adapter);

if NetBios(@NCB) = NRC_GOODRET then

Addresses.Add(AdapterToString(Adapter.adapt));

end;

end;

end;

end;

************************

//获取网卡地址,使用netbios 3.0调用 uses nb30.pas shipped with delphi 4.0

function _NetCardAddress : String ;

var

NCB : TNCB ; // Netbios control block

ADAPTER : TADAPTERSTATUS ; // Netbios adapter status

LANAENUM : TLANAENUM ; // Netbios lana

intIdx : Integer ; // Temporary work value

CRC : Char ; // Netbios return code

strTemp : String ; // Temporary string

begin

Result := '' ;

try

ZeroMemory(@NCB,SizeOf(NCB)) ; // Zero control blocl

NCB.ncb_command := Chr(NCBENUM) ; // Issue enum command

cRC := NetBios(@NCB) ;

if Ord(cRC)>0 then begin

result:=_NBReturnMSG(crc);

exit ;

end;




NCB.ncb_buffer := @LANAENUM ; // Reissue enum command

NCB.ncb_length := SizeOf(LANAENUM);

cRC := NetBios(@NCB) ;

if Ord(cRC)>0 then begin

result:=_NBReturnMSG(crc);

exit ;

end;

ZeroMemory(@NCB,SizeOf(NCB)) ; // Reset adapter

NCB.ncb_command := Chr(NCBRESET) ;

NCB.ncb_lana_num := LANAENUM.lana[0] ;

cRC := NetBios(@NCB) ;

if Ord(cRC)>0 then begin

result:=_NBReturnMSG(crc);

exit ;

end;

ZeroMemory(@NCB,SizeOf(NCB)) ; // Get adapter address

NCB.ncb_command := Chr(NCBASTAT) ;

NCB.ncb_lana_num := LANAENUM.lana[0] ;

StrPCopy(NCB.ncb_callname,'*') ;

NCB.ncb_buffer := @ADAPTER ;

NCB.ncb_length := SizeOf(ADAPTER) ;

cRC := NetBios(@NCB) ;

if Ord(cRC)>0 then begin

result:=_NBReturnMSG(crc);

exit ;

end; // Convert Netcard address it to string

strTemp := '' ;

for intIdx := 0 to 5 do strTemp := strTemp + _Char2Hex(ADAPTER.adapter_address[intIdx]) ;

Result := strTemp ;

finally

end ;

end ;


不知你指的是在Window3.2(16Bit Delphi)还是Windows95&98&2000(32Bit Delphi),如果是32位Delphi,使用Netbios(nb30.pas),如果是16位Delphi,使用NetBiosCall(汇编语言)。
 
那如何向MAC地址发信息呢,我发什么他都会接收吗?

如果MAC地址所在机器是关机状态,怎么办呢?

小弟出学,问的可能幼稚些,
 
ASTAT 是什么类型,程序编译错误
 
我的编译环境是DELPHI6
 
TASTAT 是什么类型,程序编译错误
 
收藏!!
 
1 加NB30到uses列表
2 ASTA在D7中的描述

TAdapterStatus = packed record
adapter_address: array[0..5] of Char;
rev_major: Char;
reserved0: Char;
adapter_type: Char;
rev_minor: Char;
duration: Word;
frmr_recv: Word;
frmr_xmit: Word;
iframe_recv_err: Word;
xmit_aborts: Word;
xmit_success: DWORD;
recv_success: DWORD;
iframe_xmit_err: Word;
recv_buff_unavail: Word;
t1_timeouts: Word;
ti_timeouts: Word;
reserved1: DWORD;
free_ncbs: Word;
max_cfg_ncbs: Word;
max_ncbs: Word;
xmit_buf_unavail: Word;
max_dgram_size: Word;
pending_sess: Word;
max_cfg_sess: Word;
max_sess: Word;
max_sess_pkt_size: Word;
name_count: Word;
end;
 
可用Delphi自带的UDP控件向该MAC地址发送信息!关机的状态下也可以!网络唤醒就是这样实现的(当然需要主板和电源支持)!
在发送的数据中以6个FF为先导,后面跟相应MAC地址重复16次即可(AMD协议):
procedure TForm1.Button1Click(Sender: TObject);
var
SendCode:string;
TempStr:string;
i:byte;
begin
SendCode:=StringofChar(#255,6);
for i:=0 to 15 do
begin
TempStr:=TempStr
+string(char($00))//<---MAC地址
+string(char($50))
+string(char($BA))
+string(char($EC))
+string(char($71))
+string(char($A6));//---->结束
end;
SendCode:=SendCode+TempStr;
UDPSend(SendCode);
end;
procedure TForm1.UDPSend(SendCode: String);
var
SendMStream:TMemoryStream;
begin
SendMStream:=TMemoryStream.Create;
try
NMUDP1.ReportLevel:=1;
NMUDP1.RemoteHost:='188.0.255.255';
NMUDP1.RemotePort:=7010;
SendMStream.Write(SendCode[1],length(SendCode));
NMUDP1.SendStream(SendMStream);
finally
SendMStream.Free;
end;
end;
 
多人接受答案了。
 
后退
顶部