在iphlpapi.dll里面有一个函数:GetAdaptersInfo() 好像是干这个用的。说明如下: GetAdaptersInfo The GetAdaptersInfo function retrieves adapter information for the local computer. DWORD GetAdaptersInfo( PIP_ADAPTER_INFO pAdapterInfo, // buffer to receive data PULONG pOutBufLen // size of data returned ); Parameters pAdapterInfo [out] Pointer to a buffer that, , receives a linked list of IP_ADAPTER_INFO structures. pOutBufLen [in] Pointer to a ULONG variable that specifies the size of the buffer pointed to by the pAdapterInfo parameter. If this size is insufficient to hold the adapter information, GetAdaptersInfo fills in this variable with the required size, and returns an error code of ERROR_BUFFER_OVERFLOW. Return Values If the function succeeds, the return value is ERROR_SUCCESS. If the function fails, the return value is one of the following error codes. Value Meaning ERROR_BUFFER_OVERFLOW The buffer size indicated by the pOutBufLen parameter is too small to hold the adapter information. The pOutBufLen parameter points to the required size. ERROR_INVALID_PARAMETER The pOutBufLen parameter is NULL, or the calling process does not have read/write access to the memory pointed to by pOutBufLen, or the calling process does not have write access to the memory pointed to by the pAdapterInfo parameter. ERROR_NO_DATA No adapter information exists for the local computer. ERROR_NOT_SUPPORTED GetAdaptersInfo is not supported by the operating system running on the local computer. Other If the function fails, use FormatMessage to obtain the message string for the returned error. Requirements Windows NT/2000: Requires Windows 2000. Windows 95/98: Requires Windows 98. Header: Declared in Iphlpapi.h.//没有 Library: Use Iphlpapi.lib.//没有 IP_ADAPTER_INFO The IP_ADAPTER_INFO structure contains information about a particular network adapter on the local computer. typedef struct _IP_ADAPTER_INFO { struct _IP_ADAPTER_INFO* Next; DWORD ComboIndex; char AdapterName[MAX_ADAPTER_NAME_LENGTH + 4]; char Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4]; UINT AddressLength; BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH]; DWORD Index; UINT Type; UINT DhcpEnabled; PIP_ADDR_STRING CurrentIpAddress; IP_ADDR_STRING IpAddressList; IP_ADDR_STRING GatewayList; IP_ADDR_STRING DhcpServer; BOOL HaveWins; IP_ADDR_STRING PrimaryWinsServer; IP_ADDR_STRING SecondaryWinsServer; time_t LeaseObtained; time_t LeaseExpires; } IP_ADAPTER_INFO, *PIP_ADAPTER_INFO; Members Next Pointer to the next adapter in the linked list of adapters. ComboIndex This member is unused. AdapterName[MAX_ADAPTER_NAME_LENGTH + 4] Specifies the name of the adapter. Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4] Specifies a description for the adapter. AddressLength Specifies the length of the hardware address for the adapter. Address[MAX_ADAPTER_ADDRESS_LENGTH] Specifies the hardware address for the adapter. //这个是不是你想要的? Index Specifies the adapter index. Type Specifies the adapter type. DhcpEnabled Specifies whether dynamic host configuration protocol (DHCP) is enabled for this adapter. CurrentIpAddress Specifies the current IP address for this adapter. IpAddressList Specifies the list of IP addresses associated with this adapter. GatewayList Specifies the IP address of the default gateway for this adapter. DhcpServer Specifies the IP address of the DHCP server for this adapter. HaveWins Specifies whether this adapter uses Windows Internet Name Service (WINS). PrimaryWinsServer Specifies the IP address of the primary WINS server. SecondaryWinsServer Specifies the IP address of the secondary WINS server. LeaseObtained Specifies the time when the current DHCP lease was obtained. LeaseExpires Specifies the time when the current DHCP lease will expire. Requirements Windows NT/2000: Requires Windows 2000. Windows 95/98: Requires Windows 98. Header: Declared in Iptypes.h.
得到工作组内其它电脑网卡Mac地址unit GetNetMacs;interfaceuses Windows,Classes,SysUtils;type PnetResourceArr = ^TNetResource; procedure GetServerList(List:TStrings); procedure GetUserList(fServer:string;List:TStrings); implementation procedure GetServerList(List:TStrings); type {$H+} PMyRec = ^MyRec; MyRec = Record dwScope : Integer; dwType : Integer; dwDisplayType : Integer; dwUsage : Integer; LocalName : String; RemoteName : String; Comment : String; Provider : String; end; {H-} var NetResource : TNetResource; TempRec : PMyRec; Buf : Pointer; Count,BufSize,Res : DWORD; lphEnum : THandle; p : PNetResourceArr; i,j : SmallInt; NetworkTypeList : TList; begin NetworkTypeList := TList.Create; List.BeginUpdate; List.Clear; GetMem(Buf, 8192); try Res:=WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, RESOURCEUSAGE_CONTAINER, nil,lphEnum); if Res <> 0 then Raise Exception(Res); Count := $FFFFFFFF; BufSize := 8192; Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize); if Res = ERROR_NO_MORE_ITEMS then Exit; if (Res <> 0) then Raise Exception(Res); P := PNetResourceArr(Buf); for I := 0 to Count-1 do begin New(TempRec); TempRec^.dwScope := P^.dwScope; TempRec^.dwType := P^.dwType ; TempRec^.dwDisplayType := P^.dwDisplayType ; TempRec^.dwUsage := P^.dwUsage ; TempRec^.LocalName := StrPas(P^.lpLocalName); TempRec^.RemoteName := StrPas(P^.lpRemoteName); TempRec^.Comment := StrPas(P^.lpComment); TempRec^.Provider := StrPas(P^.lpProvider); NetworkTypeList.Add(TempRec); Inc(P); end; Res := WNetCloseEnum(lphEnum); if Res <> 0 then Raise Exception(Res); for J := 0 to NetworkTypeList.Count-1 do begin TempRec := NetworkTypeList.Items[J]; NetResource := TNetResource(TempRec^); Res:=WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, RESOURCEUSAGE_CONTAINER, @NetResource,lphEnum); if Res <> 0 then Raise Exception(Res); while True do begin Count := $FFFFFFFF; BufSize := 8192; Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize); if Res = ERROR_NO_MORE_ITEMS then Break; if (Res <> 0) then Raise Exception(Res); P := PNetResourceArr(Buf); for I := 0 to Count - 1 do begin List.Add(P^.lpRemoteName); Inc(P); end; end; end; Res := WNetCloseEnum(lphEnum); if Res <> 0 then Raise Exception(Res); finally FreeMem(Buf); NetworkTypeList.Destroy; end; List.EndUpdate; end;procedure GetUserList(fServer:string;List:TStrings); var NetResource : TNetResource; Buf : Pointer; Count,BufSize,Res : DWord; Ind : Integer; lphEnum : THandle; Temp : PNetResourceArr; begin List.Clear; GetMem(Buf, 8192); try FillChar(NetResource, SizeOf(NetResource), 0); NetResource.lpRemoteName := @fServer[1]; NetResource.dwDisplayType := RESOURCEDISPLAYTYPE_SERVER; NetResource.dwUsage := RESOURCEUSAGE_CONTAINER; NetResource.dwScope := RESOURCETYPE_DISK; Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, RESOURCEUSAGE_CONTAINER, @NetResource,lphEnum); if Res <> 0 then Exit; while True do begin Count := $FFFFFFFF; BufSize := 8192; Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize); if Res = ERROR_NO_MORE_ITEMS then Exit; if (Res <> 0) then Exit; Temp := PNetResourceArr(Buf); for Ind := 0 to Count - 1 do begin List.Add(Temp^.lpRemoteName + 2); // Add all the network usernames to List StringList Inc(Temp); end; end; Res := WNetCloseEnum(lphEnum); if Res <> 0 then Raise Exception(Res); finally FreeMem(Buf); end; end;end.
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, winsock,psock,StdCtrls, Buttons,GetNetMacs,IniFiles, NMUDP,Registry;type TMainM = class(TForm) GetAllStuMacs_Btn: TBitBtn; StuName_ListBox: TListBox; StuMacs_ListBox: TListBox; NMUDP1: TNMUDP; Powersock1: TPowersock; Memo1: TMemo; NetGroup_Edit: TEdit; procedure GetAllStuMacs_BtnClick(Sender: TObject); private { Private declarations } public { Public declarations } //以下函数你原来定义在TMainM = class(TForm)内了,为了要被其余的unit引用, //需要定义在public段;而且,你以前定义的时候,没有用TMainM.(过程)进行定义 function GetLocalIP:string; procedure PowerMachine(aMac: String;NMUDP1:TNMUDP); function GetDomainName(Ip:string):string; function GetMacsFromName(Name:String):String; function GetRadiateIP:String; end;var MainM: TMainM;implementation{$R *.dfm} //缺少的函数 ReplaceText function ReplaceText(const S,ReplacePiece,ReplaceWith: String):String; Var Position:Integer; TempStr:String; begin Position:=Pos(ReplacePiece,S); if Position>0 then begin TempStr:=S; Delete(TempStr,1,Position-1+Length(ReplacePiece)); Result:=Copy(S,1,Position-1)+// ReplaceWith+ReplaceText(TempStr,ReplacePiece,ReplaceWith); end else Result:=S; end;//由IP得到计算机名 function TMainM.GetDomainName(Ip:string):string; var pH:PHostent; data:twsadata; ii:dword; begin WSAStartup($101, Data); ii:=inet_addr(pchar(ip)); pH:=gethostbyaddr(@ii,sizeof(ii),PF_INET); if (ph<>nil) then Result:=pH.h_name else Result:=''; WSACleanup(); end;//由计算机名得到网卡Macs地址 function TMainM.GetMacsFromName(Name:String):String; var Command:String; tempResult:String; begin Command:='Command.com /c nbtstat -a '+Name+' >temp.txt'; WinExec(Pchar(Command),SW_HIDE); Sleep(1000); Memo1.Lines.LoadFromFile('temp.txt'); tempResult:=Memo1.Lines[Memo1.Lines.Count-1]; Memo1.Lines.Clear; if tempResult='Host not found.' then Result:='Error!' else begin tempResult:=Copy(tempResult,15,17); Result:=ReplaceText(tempResult,'-',''); //[Error] Unit1.pas(69): Undeclared identifier: 'ReplaceText' //差替换字符串中的指定字串 //function ReplaceText(const S,ReplacePiece,ReplaceWith: String):String; end; end; //得到本机地址 function TMainM.GetLocalIP:string; begin Result:=Powersock1.LocalIP;//本机ip,注意加个TPowersock控件 //[Error] Unit1.pas(79): Undeclared identifier: 'Powersock1' //因为你的Powersock1申明在 TSetup_Main,所以你需要把GetLocalIP申明在private段 end;//取得广播的ip地址,比如本机是192.168.0.1,这个函数得到192.168.0.255 function TMainM.GetRadiateIP:String; var i,j,iHead:Integer; sHead,s:String; ai:array [1..3] of integer; LocalIPAdd:string; begin Result:='192.168.255.255'; LocalIPAdd:= GetLocalIP ; //得到本机地址,函数见下面 j:=1; //取出.的位置 for i:=0 to Length(LocalIPAdd) do begin if LocalIPAdd[i]='.' then begin ai[j]:=i; Inc(j); end; if j>3 then break; end; //shead为ip第一段 sHead:=Copy(LocalIpAdd,1,ai[1]-1); iHead:=StrToInt(sHead); if iHead<128 then //A类网 Result:=sHead+'.255.255.255'//a类网的广播ip else begin if iHead<192 then //B类网 begin s:=Copy(LocalIPAdd,1,ai[2]-1); Result:=s+'.255.255';//b类网的广播ip end else //C类网 begin s:=Copy(LocalIPAdd,1,ai[3]-1); Result:=s+'.255';//c类网的广播ip end; end; end; //***************远程启动函数******************* procedure TMainM.PowerMachine(aMac: String;NMUDP1:TNMUDP);//macs地址,UDP名 var Str:Array[1..102] of char; i:Integer; function StrtoHex(S:String):Byte; begin Result := 0; Case S[2] of '0': Result := 0; '1': Result := 1; '2': Result := 2; '3': Result := 3; '4': Result := 4; '5': Result := 5; '6': Result := 6; '7': Result := 7; '8': Result := 8; '9': Result := 9; 'A': Result := 10; 'B': Result := 11; 'C': Result := 12; 'D': Result := 13; 'E': Result := 14; 'F': Result := 15; end; Case S[1] of '0': Result := Result; '1': Result := Result + 16*1; '2': Result := Result + 16*2; '3': Result := Result + 16*3; '4': Result := Result + 16*4; '5': Result := Result + 16*5; '6': Result := Result + 16*6; '7': Result := Result + 16*7; '8': Result := Result + 16*8; '9': Result := Result + 16*9; 'A': Result := Result + 16*10; 'B': Result := Result + 16*11; 'C': Result := Result + 16*12; 'D': Result := Result + 16*13; 'E': Result := Result + 16*14; 'F': Result := Result + 16*15; end; end; begin for i := 1 to 6 do Str[i] := #255; i := 7; while i < 103 do begin Str[i] := chr(StrToHex(Copy(aMac,1,2))); Str[i+1] := chr(StrToHex(Copy(aMac,3,2))); Str[i+2] := chr(StrToHex(Copy(aMac,5,2))); Str[i+3] := chr(StrToHex(Copy(aMac,7,2))); Str[i+4] := chr(StrToHex(Copy(aMac,9,2))); Str[i+5] := chr(StrToHex(Copy(aMac,11,2))); inc(i,6); end; NMUDP1.LocalPort := 6666; NMUDP1.RemotePort := 6666; NMUDP1.RemoteHost:=GetRadiateIP; //这个函数是得到广播地址,以前发的没有~不好意思 NMUDP1.ReportLevel := 1; NMUDP1.SendBuffer(Str,102); end;//得到工作组内其它机器的Macs地址 procedure TMainM.GetAllStuMacs_BtnClick(Sender: TObject); var StuNameListN:Integer; LocalName:String; // GetLineNum:Integer; // GetLineStr:String; begin StuName_ListBox.Items.Clear; StuMacs_ListBox.Items.Clear; Application.MessageBox('依网络状况,该项操作可能耗时2-10分钟,请耐心等候!','提示',MB_OK); // HadChangeMacs:=True; //[Error] Unit1.pas(196): Undeclared identifier: 'HadChangeMacs' //这是我以前定义的一个全局变量,是为了点击“保存”按纽时是否进行保存 // LocalName:=GetDomainName(LocalIPPowersock.LocalIP); //[Error] Unit1.pas(197): Undeclared identifier: 'LocalIPPowersock' //控件没加上,可以不用;如果你是要得到当前计算机名,可以用下面这句 LocalName:=GetDomainName(GetLocalIP); GetUserList(NetGroup_Edit.Text,StuName_ListBox.Items); //NetGroup_Edit控件未加,已加上,用于填写 工作组名称 for StuNameListN:=StuName_ListBox.Items.Count-1 downto 0 do if StuName_ListBox.Items[StuNameListN]=LocalName then StuName_ListBox.Items.Delete(StuNameListN); Application.MessageBox('已经正确得到了所有学生机名!'+#13+#13+'下面将继续取得学生机对应的网卡地址……','提示',MB_OK); for StuNameListN:=0 to StuName_ListBox.Items.Count-1 do StuMacs_ListBox.Items[StuNameListN]:=GetMacsFromName(StuName_ListBox.Items[StuNameListN]); Application.MessageBox('操作成功!','提示',MB_OK); DeleteFile('temp.txt'); end; end.
在iphlpapi.dll里面有一个函数:GetAdaptersInfo() 好像是干这个用的。说明如下: GetAdaptersInfo The GetAdaptersInfo function retrieves adapter information for the local computer. DWORD GetAdaptersInfo( PIP_ADAPTER_INFO pAdapterInfo, // buffer to receive data PULONG pOutBufLen // size of data returned ); Parameters pAdapterInfo [out] Pointer to a buffer that, , receives a linked list of IP_ADAPTER_INFO structures. pOutBufLen [in] Pointer to a ULONG variable that specifies the size of the buffer pointed to by the pAdapterInfo parameter. If this size is insufficient to hold the adapter information, GetAdaptersInfo fills in this variable with the required size, and returns an error code of ERROR_BUFFER_OVERFLOW. Return Values If the function succeeds, the return value is ERROR_SUCCESS. If the function fails, the return value is one of the following error codes. Value Meaning ERROR_BUFFER_OVERFLOW The buffer size indicated by the pOutBufLen parameter is too small to hold the adapter information. The pOutBufLen parameter points to the required size. ERROR_INVALID_PARAMETER The pOutBufLen parameter is NULL, or the calling process does not have read/write access to the memory pointed to by pOutBufLen, or the calling process does not have write access to the memory pointed to by the pAdapterInfo parameter. ERROR_NO_DATA No adapter information exists for the local computer. ERROR_NOT_SUPPORTED GetAdaptersInfo is not supported by the operating system running on the local computer. Other If the function fails, use FormatMessage to obtain the message string for the returned error. Requirements Windows NT/2000: Requires Windows 2000. Windows 95/98: Requires Windows 98. Header: Declared in Iphlpapi.h.//没有 Library: Use Iphlpapi.lib.//没有 IP_ADAPTER_INFO The IP_ADAPTER_INFO structure contains information about a particular network adapter on the local computer. typedef struct _IP_ADAPTER_INFO { struct _IP_ADAPTER_INFO* Next; DWORD ComboIndex; char AdapterName[MAX_ADAPTER_NAME_LENGTH + 4]; char Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4]; UINT AddressLength; BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH]; DWORD Index; UINT Type; UINT DhcpEnabled; PIP_ADDR_STRING CurrentIpAddress; IP_ADDR_STRING IpAddressList; IP_ADDR_STRING GatewayList; IP_ADDR_STRING DhcpServer; BOOL HaveWins; IP_ADDR_STRING PrimaryWinsServer; IP_ADDR_STRING SecondaryWinsServer; time_t LeaseObtained; time_t LeaseExpires; } IP_ADAPTER_INFO, *PIP_ADAPTER_INFO; Members Next Pointer to the next adapter in the linked list of adapters. ComboIndex This member is unused. AdapterName[MAX_ADAPTER_NAME_LENGTH + 4] Specifies the name of the adapter. Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4] Specifies a description for the adapter. AddressLength Specifies the length of the hardware address for the adapter. Address[MAX_ADAPTER_ADDRESS_LENGTH] Specifies the hardware address for the adapter. //这个是不是你想要的? Index Specifies the adapter index. Type Specifies the adapter type. DhcpEnabled Specifies whether dynamic host configuration protocol (DHCP) is enabled for this adapter. CurrentIpAddress Specifies the current IP address for this adapter. IpAddressList Specifies the list of IP addresses associated with this adapter. GatewayList Specifies the IP address of the default gateway for this adapter. DhcpServer Specifies the IP address of the DHCP server for this adapter. HaveWins Specifies whether this adapter uses Windows Internet Name Service (WINS). PrimaryWinsServer Specifies the IP address of the primary WINS server. SecondaryWinsServer Specifies the IP address of the secondary WINS server. LeaseObtained Specifies the time when the current DHCP lease was obtained. LeaseExpires Specifies the time when the current DHCP lease will expire. Requirements Windows NT/2000: Requires Windows 2000. Windows 95/98: Requires Windows 98. Header: Declared in Iptypes.h.
object MainM: TMainM Left = 213 Top = 153 Width = 544 Height = 375 Caption = #36828#31243#24320#26426 Color = clBtnFace Font.Charset = GB2312_CHARSET Font.Color = clWindowText Font.Height = -12 Font.Name = #23435#20307 Font.Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 12 object GetAllStuMacs_Btn: TBitBtn Left = 48 Top = 24 Width = 129 Height = 25 Caption = 'GetAllStuMacs_Btn' TabOrder = 0 OnClick = GetAllStuMacs_BtnClick end object StuName_ListBox: TListBox Left = 192 Top = 48 Width = 121 Height = 97 ImeName = #20013#25991' ('#31616#20307') - '#24494#36719#25340#38899 ItemHeight = 12 TabOrder = 1 end object StuMacs_ListBox: TListBox Left = 360 Top = 48 Width = 121 Height = 97 ImeName = #20013#25991' ('#31616#20307') - '#24494#36719#25340#38899 ItemHeight = 12 TabOrder = 2 end object Memo1: TMemo Left = 32 Top = 160 Width = 497 Height = 145 ImeName = #20013#25991' ('#31616#20307') - '#24494#36719#25340#38899 Lines.Strings = ( 'Memo1') TabOrder = 3 end object NetGroup_Edit: TEdit Left = 40 Top = 64 Width = 145 Height = 20 TabOrder = 4 Text = #36825#37324#22635#20889#24037#20316#32452#21517#31216 end object NMUDP1: TNMUDP RemotePort = 0 LocalPort = 0 ReportLevel = 1 Left = 25 Top = 224 end object Powersock1: TPowersock Port = 0 ReportLevel = 0 Left = 32 Top = 160 end end *********************** program Project1;uses Forms, Unit1 in 'Unit1.pas' {MainM}, GetNetMacs in 'GetNetMacs.pas';{$R *.res}begin Application.Initialize; Application.CreateForm(TMainM, MainM); Application.Run; end. ********************以上是刚好给别人改好的^_* 如有问题,请留短消息.
Function TForm1.NBGetAdapterAddress(a:integer): String; var NCB: TNCB; ADAPTER: TADAPTERSTATUS; LANAENUM: TLANAENUM; intIdx: Integer; cRC: Char; strTemp: String; Begin Result := ''; Try ZeroMemory(@NCB, SizeOf(NCB)); NCB.ncb_command := Chr(NCBENUM); cRC := NetBios(@NCB); NCB.ncb_buffer := @LANAENUM; NCB.ncb_length := SizeOf(LANAENUM); cRC := NetBios(@NCB); If Ord(cRC)<>0 Then exit; ZeroMemory(@NCB, SizeOf(NCB)); NCB.ncb_command := Chr(NCBRESET); NCB.ncb_lana_num := LANAENUM.lana[a]; cRC := NetBios(@NCB); If Ord(cRC)<>0 Then exit; ZeroMemory(@NCB, SizeOf(NCB)); NCB.ncb_command := Chr(NCBASTAT); NCB.ncb_lana_num := LANAENUM.lana[a]; StrPCopy(NCB.ncb_callname, '*'); NCB.ncb_buffer := @ADAPTER; NCB.ncb_length := SizeOf(ADAPTER); cRC := NetBios(@NCB); strTemp := ''; For intIdx := 0 To 5 Do strTemp := strTemp + InttoHex(Integer(ADAPTER.adapter_address[intIdx]),2); Result := strTemp; Finally End; End;
好像是干这个用的。说明如下:
GetAdaptersInfo
The GetAdaptersInfo function retrieves adapter information for the local computer. DWORD GetAdaptersInfo(
PIP_ADAPTER_INFO pAdapterInfo, // buffer to receive data
PULONG pOutBufLen // size of data returned
);
Parameters
pAdapterInfo
[out] Pointer to a buffer that, , receives a linked list of IP_ADAPTER_INFO structures.
pOutBufLen
[in] Pointer to a ULONG variable that specifies the size of the buffer pointed to by the pAdapterInfo parameter. If this size is insufficient to hold the adapter information, GetAdaptersInfo fills in this variable with the required size, and returns an error code of ERROR_BUFFER_OVERFLOW.
Return Values
If the function succeeds, the return value is ERROR_SUCCESS. If the function fails, the return value is one of the following error codes. Value Meaning
ERROR_BUFFER_OVERFLOW The buffer size indicated by the pOutBufLen parameter is too small to hold the adapter information. The pOutBufLen parameter points to the required size.
ERROR_INVALID_PARAMETER The pOutBufLen parameter is NULL, or the calling process does not have read/write access to the memory pointed to by pOutBufLen, or the calling process does not have write access to the memory pointed to by the pAdapterInfo parameter.
ERROR_NO_DATA No adapter information exists for the local computer.
ERROR_NOT_SUPPORTED GetAdaptersInfo is not supported by the operating system running on the local computer.
Other If the function fails, use FormatMessage to obtain the message string for the returned error.
Requirements
Windows NT/2000: Requires Windows 2000.
Windows 95/98: Requires Windows 98.
Header: Declared in Iphlpapi.h.//没有
Library: Use Iphlpapi.lib.//没有 IP_ADAPTER_INFO
The IP_ADAPTER_INFO structure contains information about a particular network adapter on the local computer. typedef struct _IP_ADAPTER_INFO {
struct _IP_ADAPTER_INFO* Next;
DWORD ComboIndex;
char AdapterName[MAX_ADAPTER_NAME_LENGTH + 4];
char Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4];
UINT AddressLength;
BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH];
DWORD Index;
UINT Type;
UINT DhcpEnabled;
PIP_ADDR_STRING CurrentIpAddress;
IP_ADDR_STRING IpAddressList;
IP_ADDR_STRING GatewayList;
IP_ADDR_STRING DhcpServer;
BOOL HaveWins;
IP_ADDR_STRING PrimaryWinsServer;
IP_ADDR_STRING SecondaryWinsServer;
time_t LeaseObtained;
time_t LeaseExpires;
} IP_ADAPTER_INFO, *PIP_ADAPTER_INFO;
Members
Next
Pointer to the next adapter in the linked list of adapters.
ComboIndex
This member is unused.
AdapterName[MAX_ADAPTER_NAME_LENGTH + 4]
Specifies the name of the adapter.
Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4]
Specifies a description for the adapter.
AddressLength
Specifies the length of the hardware address for the adapter.
Address[MAX_ADAPTER_ADDRESS_LENGTH]
Specifies the hardware address for the adapter. //这个是不是你想要的?
Index
Specifies the adapter index.
Type
Specifies the adapter type.
DhcpEnabled
Specifies whether dynamic host configuration protocol (DHCP) is enabled for this adapter.
CurrentIpAddress
Specifies the current IP address for this adapter.
IpAddressList
Specifies the list of IP addresses associated with this adapter.
GatewayList
Specifies the IP address of the default gateway for this adapter.
DhcpServer
Specifies the IP address of the DHCP server for this adapter.
HaveWins
Specifies whether this adapter uses Windows Internet Name Service (WINS).
PrimaryWinsServer
Specifies the IP address of the primary WINS server.
SecondaryWinsServer
Specifies the IP address of the secondary WINS server.
LeaseObtained
Specifies the time when the current DHCP lease was obtained.
LeaseExpires
Specifies the time when the current DHCP lease will expire.
Requirements
Windows NT/2000: Requires Windows 2000.
Windows 95/98: Requires Windows 98.
Header: Declared in Iptypes.h.
PnetResourceArr = ^TNetResource;
procedure GetServerList(List:TStrings);
procedure GetUserList(fServer:string;List:TStrings);
implementation
procedure GetServerList(List:TStrings);
type
{$H+}
PMyRec = ^MyRec;
MyRec = Record
dwScope : Integer;
dwType : Integer;
dwDisplayType : Integer;
dwUsage : Integer;
LocalName : String;
RemoteName : String;
Comment : String;
Provider : String;
end;
{H-}
var NetResource : TNetResource;
TempRec : PMyRec;
Buf : Pointer;
Count,BufSize,Res : DWORD;
lphEnum : THandle;
p : PNetResourceArr;
i,j : SmallInt;
NetworkTypeList : TList;
begin
NetworkTypeList := TList.Create;
List.BeginUpdate;
List.Clear;
GetMem(Buf, 8192);
try
Res:=WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, RESOURCEUSAGE_CONTAINER, nil,lphEnum);
if Res <> 0 then Raise Exception(Res);
Count := $FFFFFFFF;
BufSize := 8192;
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
if Res = ERROR_NO_MORE_ITEMS then Exit;
if (Res <> 0) then Raise Exception(Res);
P := PNetResourceArr(Buf);
for I := 0 to Count-1 do
begin
New(TempRec);
TempRec^.dwScope := P^.dwScope;
TempRec^.dwType := P^.dwType ;
TempRec^.dwDisplayType := P^.dwDisplayType ;
TempRec^.dwUsage := P^.dwUsage ;
TempRec^.LocalName := StrPas(P^.lpLocalName);
TempRec^.RemoteName := StrPas(P^.lpRemoteName);
TempRec^.Comment := StrPas(P^.lpComment);
TempRec^.Provider := StrPas(P^.lpProvider);
NetworkTypeList.Add(TempRec);
Inc(P);
end;
Res := WNetCloseEnum(lphEnum);
if Res <> 0 then Raise Exception(Res);
for J := 0 to NetworkTypeList.Count-1 do
begin
TempRec := NetworkTypeList.Items[J];
NetResource := TNetResource(TempRec^);
Res:=WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, RESOURCEUSAGE_CONTAINER, @NetResource,lphEnum);
if Res <> 0 then Raise Exception(Res);
while True do
begin
Count := $FFFFFFFF;
BufSize := 8192;
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
if Res = ERROR_NO_MORE_ITEMS then Break;
if (Res <> 0) then Raise Exception(Res);
P := PNetResourceArr(Buf);
for I := 0 to Count - 1 do
begin
List.Add(P^.lpRemoteName);
Inc(P);
end;
end;
end;
Res := WNetCloseEnum(lphEnum);
if Res <> 0 then Raise Exception(Res);
finally
FreeMem(Buf);
NetworkTypeList.Destroy;
end;
List.EndUpdate;
end;procedure GetUserList(fServer:string;List:TStrings);
var NetResource : TNetResource;
Buf : Pointer;
Count,BufSize,Res : DWord;
Ind : Integer;
lphEnum : THandle;
Temp : PNetResourceArr;
begin
List.Clear;
GetMem(Buf, 8192);
try
FillChar(NetResource, SizeOf(NetResource), 0);
NetResource.lpRemoteName := @fServer[1];
NetResource.dwDisplayType := RESOURCEDISPLAYTYPE_SERVER;
NetResource.dwUsage := RESOURCEUSAGE_CONTAINER;
NetResource.dwScope := RESOURCETYPE_DISK;
Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, RESOURCEUSAGE_CONTAINER, @NetResource,lphEnum);
if Res <> 0 then Exit;
while True do
begin
Count := $FFFFFFFF;
BufSize := 8192;
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
if Res = ERROR_NO_MORE_ITEMS then Exit;
if (Res <> 0) then Exit;
Temp := PNetResourceArr(Buf);
for Ind := 0 to Count - 1 do
begin
List.Add(Temp^.lpRemoteName + 2);
// Add all the network usernames to List StringList
Inc(Temp);
end;
end;
Res := WNetCloseEnum(lphEnum);
if Res <> 0 then Raise Exception(Res);
finally
FreeMem(Buf);
end;
end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, winsock,psock,StdCtrls, Buttons,GetNetMacs,IniFiles, NMUDP,Registry;type
TMainM = class(TForm)
GetAllStuMacs_Btn: TBitBtn;
StuName_ListBox: TListBox;
StuMacs_ListBox: TListBox;
NMUDP1: TNMUDP;
Powersock1: TPowersock;
Memo1: TMemo;
NetGroup_Edit: TEdit;
procedure GetAllStuMacs_BtnClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
//以下函数你原来定义在TMainM = class(TForm)内了,为了要被其余的unit引用,
//需要定义在public段;而且,你以前定义的时候,没有用TMainM.(过程)进行定义
function GetLocalIP:string;
procedure PowerMachine(aMac: String;NMUDP1:TNMUDP);
function GetDomainName(Ip:string):string;
function GetMacsFromName(Name:String):String;
function GetRadiateIP:String;
end;var
MainM: TMainM;implementation{$R *.dfm}
//缺少的函数 ReplaceText
function ReplaceText(const S,ReplacePiece,ReplaceWith: String):String;
Var Position:Integer;
TempStr:String;
begin
Position:=Pos(ReplacePiece,S);
if Position>0 then
begin
TempStr:=S;
Delete(TempStr,1,Position-1+Length(ReplacePiece));
Result:=Copy(S,1,Position-1)+//
ReplaceWith+ReplaceText(TempStr,ReplacePiece,ReplaceWith);
end
else Result:=S;
end;//由IP得到计算机名
function TMainM.GetDomainName(Ip:string):string;
var pH:PHostent;
data:twsadata;
ii:dword;
begin
WSAStartup($101, Data);
ii:=inet_addr(pchar(ip));
pH:=gethostbyaddr(@ii,sizeof(ii),PF_INET);
if (ph<>nil) then Result:=pH.h_name
else Result:='';
WSACleanup();
end;//由计算机名得到网卡Macs地址
function TMainM.GetMacsFromName(Name:String):String;
var Command:String;
tempResult:String;
begin
Command:='Command.com /c nbtstat -a '+Name+' >temp.txt';
WinExec(Pchar(Command),SW_HIDE);
Sleep(1000);
Memo1.Lines.LoadFromFile('temp.txt');
tempResult:=Memo1.Lines[Memo1.Lines.Count-1];
Memo1.Lines.Clear;
if tempResult='Host not found.' then Result:='Error!'
else
begin
tempResult:=Copy(tempResult,15,17);
Result:=ReplaceText(tempResult,'-','');
//[Error] Unit1.pas(69): Undeclared identifier: 'ReplaceText'
//差替换字符串中的指定字串
//function ReplaceText(const S,ReplacePiece,ReplaceWith: String):String;
end;
end;
//得到本机地址
function TMainM.GetLocalIP:string;
begin
Result:=Powersock1.LocalIP;//本机ip,注意加个TPowersock控件
//[Error] Unit1.pas(79): Undeclared identifier: 'Powersock1'
//因为你的Powersock1申明在 TSetup_Main,所以你需要把GetLocalIP申明在private段
end;//取得广播的ip地址,比如本机是192.168.0.1,这个函数得到192.168.0.255
function TMainM.GetRadiateIP:String;
var i,j,iHead:Integer;
sHead,s:String;
ai:array [1..3] of integer;
LocalIPAdd:string;
begin
Result:='192.168.255.255';
LocalIPAdd:= GetLocalIP ; //得到本机地址,函数见下面
j:=1;
//取出.的位置
for i:=0 to Length(LocalIPAdd) do
begin
if LocalIPAdd[i]='.' then
begin ai[j]:=i; Inc(j); end;
if j>3 then break;
end;
//shead为ip第一段
sHead:=Copy(LocalIpAdd,1,ai[1]-1);
iHead:=StrToInt(sHead);
if iHead<128 then //A类网
Result:=sHead+'.255.255.255'//a类网的广播ip
else
begin
if iHead<192 then //B类网
begin
s:=Copy(LocalIPAdd,1,ai[2]-1);
Result:=s+'.255.255';//b类网的广播ip
end
else //C类网
begin
s:=Copy(LocalIPAdd,1,ai[3]-1);
Result:=s+'.255';//c类网的广播ip
end;
end;
end;
//***************远程启动函数*******************
procedure TMainM.PowerMachine(aMac: String;NMUDP1:TNMUDP);//macs地址,UDP名
var Str:Array[1..102] of char;
i:Integer;
function StrtoHex(S:String):Byte;
begin
Result := 0;
Case S[2] of
'0': Result := 0;
'1': Result := 1;
'2': Result := 2;
'3': Result := 3;
'4': Result := 4;
'5': Result := 5;
'6': Result := 6;
'7': Result := 7;
'8': Result := 8;
'9': Result := 9;
'A': Result := 10;
'B': Result := 11;
'C': Result := 12;
'D': Result := 13;
'E': Result := 14;
'F': Result := 15;
end;
Case S[1] of
'0': Result := Result;
'1': Result := Result + 16*1;
'2': Result := Result + 16*2;
'3': Result := Result + 16*3;
'4': Result := Result + 16*4;
'5': Result := Result + 16*5;
'6': Result := Result + 16*6;
'7': Result := Result + 16*7;
'8': Result := Result + 16*8;
'9': Result := Result + 16*9;
'A': Result := Result + 16*10;
'B': Result := Result + 16*11;
'C': Result := Result + 16*12;
'D': Result := Result + 16*13;
'E': Result := Result + 16*14;
'F': Result := Result + 16*15;
end;
end;
begin
for i := 1 to 6 do Str[i] := #255;
i := 7;
while i < 103 do
begin
Str[i] := chr(StrToHex(Copy(aMac,1,2)));
Str[i+1] := chr(StrToHex(Copy(aMac,3,2)));
Str[i+2] := chr(StrToHex(Copy(aMac,5,2)));
Str[i+3] := chr(StrToHex(Copy(aMac,7,2)));
Str[i+4] := chr(StrToHex(Copy(aMac,9,2)));
Str[i+5] := chr(StrToHex(Copy(aMac,11,2)));
inc(i,6);
end;
NMUDP1.LocalPort := 6666;
NMUDP1.RemotePort := 6666;
NMUDP1.RemoteHost:=GetRadiateIP; //这个函数是得到广播地址,以前发的没有~不好意思
NMUDP1.ReportLevel := 1;
NMUDP1.SendBuffer(Str,102);
end;//得到工作组内其它机器的Macs地址
procedure TMainM.GetAllStuMacs_BtnClick(Sender: TObject);
var StuNameListN:Integer;
LocalName:String;
// GetLineNum:Integer;
// GetLineStr:String;
begin
StuName_ListBox.Items.Clear;
StuMacs_ListBox.Items.Clear;
Application.MessageBox('依网络状况,该项操作可能耗时2-10分钟,请耐心等候!','提示',MB_OK);
// HadChangeMacs:=True;
//[Error] Unit1.pas(196): Undeclared identifier: 'HadChangeMacs'
//这是我以前定义的一个全局变量,是为了点击“保存”按纽时是否进行保存
// LocalName:=GetDomainName(LocalIPPowersock.LocalIP);
//[Error] Unit1.pas(197): Undeclared identifier: 'LocalIPPowersock'
//控件没加上,可以不用;如果你是要得到当前计算机名,可以用下面这句
LocalName:=GetDomainName(GetLocalIP);
GetUserList(NetGroup_Edit.Text,StuName_ListBox.Items);
//NetGroup_Edit控件未加,已加上,用于填写 工作组名称
for StuNameListN:=StuName_ListBox.Items.Count-1 downto 0 do
if StuName_ListBox.Items[StuNameListN]=LocalName then
StuName_ListBox.Items.Delete(StuNameListN);
Application.MessageBox('已经正确得到了所有学生机名!'+#13+#13+'下面将继续取得学生机对应的网卡地址……','提示',MB_OK);
for StuNameListN:=0 to StuName_ListBox.Items.Count-1 do
StuMacs_ListBox.Items[StuNameListN]:=GetMacsFromName(StuName_ListBox.Items[StuNameListN]);
Application.MessageBox('操作成功!','提示',MB_OK);
DeleteFile('temp.txt');
end;
end.
好像是干这个用的。说明如下:
GetAdaptersInfo
The GetAdaptersInfo function retrieves adapter information for the local computer. DWORD GetAdaptersInfo(
PIP_ADAPTER_INFO pAdapterInfo, // buffer to receive data
PULONG pOutBufLen // size of data returned
);
Parameters
pAdapterInfo
[out] Pointer to a buffer that, , receives a linked list of IP_ADAPTER_INFO structures.
pOutBufLen
[in] Pointer to a ULONG variable that specifies the size of the buffer pointed to by the pAdapterInfo parameter. If this size is insufficient to hold the adapter information, GetAdaptersInfo fills in this variable with the required size, and returns an error code of ERROR_BUFFER_OVERFLOW.
Return Values
If the function succeeds, the return value is ERROR_SUCCESS. If the function fails, the return value is one of the following error codes. Value Meaning
ERROR_BUFFER_OVERFLOW The buffer size indicated by the pOutBufLen parameter is too small to hold the adapter information. The pOutBufLen parameter points to the required size.
ERROR_INVALID_PARAMETER The pOutBufLen parameter is NULL, or the calling process does not have read/write access to the memory pointed to by pOutBufLen, or the calling process does not have write access to the memory pointed to by the pAdapterInfo parameter.
ERROR_NO_DATA No adapter information exists for the local computer.
ERROR_NOT_SUPPORTED GetAdaptersInfo is not supported by the operating system running on the local computer.
Other If the function fails, use FormatMessage to obtain the message string for the returned error.
Requirements
Windows NT/2000: Requires Windows 2000.
Windows 95/98: Requires Windows 98.
Header: Declared in Iphlpapi.h.//没有
Library: Use Iphlpapi.lib.//没有 IP_ADAPTER_INFO
The IP_ADAPTER_INFO structure contains information about a particular network adapter on the local computer. typedef struct _IP_ADAPTER_INFO {
struct _IP_ADAPTER_INFO* Next;
DWORD ComboIndex;
char AdapterName[MAX_ADAPTER_NAME_LENGTH + 4];
char Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4];
UINT AddressLength;
BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH];
DWORD Index;
UINT Type;
UINT DhcpEnabled;
PIP_ADDR_STRING CurrentIpAddress;
IP_ADDR_STRING IpAddressList;
IP_ADDR_STRING GatewayList;
IP_ADDR_STRING DhcpServer;
BOOL HaveWins;
IP_ADDR_STRING PrimaryWinsServer;
IP_ADDR_STRING SecondaryWinsServer;
time_t LeaseObtained;
time_t LeaseExpires;
} IP_ADAPTER_INFO, *PIP_ADAPTER_INFO;
Members
Next
Pointer to the next adapter in the linked list of adapters.
ComboIndex
This member is unused.
AdapterName[MAX_ADAPTER_NAME_LENGTH + 4]
Specifies the name of the adapter.
Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4]
Specifies a description for the adapter.
AddressLength
Specifies the length of the hardware address for the adapter.
Address[MAX_ADAPTER_ADDRESS_LENGTH]
Specifies the hardware address for the adapter. //这个是不是你想要的?
Index
Specifies the adapter index.
Type
Specifies the adapter type.
DhcpEnabled
Specifies whether dynamic host configuration protocol (DHCP) is enabled for this adapter.
CurrentIpAddress
Specifies the current IP address for this adapter.
IpAddressList
Specifies the list of IP addresses associated with this adapter.
GatewayList
Specifies the IP address of the default gateway for this adapter.
DhcpServer
Specifies the IP address of the DHCP server for this adapter.
HaveWins
Specifies whether this adapter uses Windows Internet Name Service (WINS).
PrimaryWinsServer
Specifies the IP address of the primary WINS server.
SecondaryWinsServer
Specifies the IP address of the secondary WINS server.
LeaseObtained
Specifies the time when the current DHCP lease was obtained.
LeaseExpires
Specifies the time when the current DHCP lease will expire.
Requirements
Windows NT/2000: Requires Windows 2000.
Windows 95/98: Requires Windows 98.
Header: Declared in Iptypes.h.
Left = 213
Top = 153
Width = 544
Height = 375
Caption = #36828#31243#24320#26426
Color = clBtnFace
Font.Charset = GB2312_CHARSET
Font.Color = clWindowText
Font.Height = -12
Font.Name = #23435#20307
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 12
object GetAllStuMacs_Btn: TBitBtn
Left = 48
Top = 24
Width = 129
Height = 25
Caption = 'GetAllStuMacs_Btn'
TabOrder = 0
OnClick = GetAllStuMacs_BtnClick
end
object StuName_ListBox: TListBox
Left = 192
Top = 48
Width = 121
Height = 97
ImeName = #20013#25991' ('#31616#20307') - '#24494#36719#25340#38899
ItemHeight = 12
TabOrder = 1
end
object StuMacs_ListBox: TListBox
Left = 360
Top = 48
Width = 121
Height = 97
ImeName = #20013#25991' ('#31616#20307') - '#24494#36719#25340#38899
ItemHeight = 12
TabOrder = 2
end
object Memo1: TMemo
Left = 32
Top = 160
Width = 497
Height = 145
ImeName = #20013#25991' ('#31616#20307') - '#24494#36719#25340#38899
Lines.Strings = (
'Memo1')
TabOrder = 3
end
object NetGroup_Edit: TEdit
Left = 40
Top = 64
Width = 145
Height = 20
TabOrder = 4
Text = #36825#37324#22635#20889#24037#20316#32452#21517#31216
end
object NMUDP1: TNMUDP
RemotePort = 0
LocalPort = 0
ReportLevel = 1
Left = 25
Top = 224
end
object Powersock1: TPowersock
Port = 0
ReportLevel = 0
Left = 32
Top = 160
end
end
***********************
program Project1;uses
Forms,
Unit1 in 'Unit1.pas' {MainM},
GetNetMacs in 'GetNetMacs.pas';{$R *.res}begin
Application.Initialize;
Application.CreateForm(TMainM, MainM);
Application.Run;
end.
********************以上是刚好给别人改好的^_*
如有问题,请留短消息.
var
NCB: TNCB;
ADAPTER: TADAPTERSTATUS;
LANAENUM: TLANAENUM;
intIdx: Integer;
cRC: Char;
strTemp: String; Begin
Result := '';
Try
ZeroMemory(@NCB, SizeOf(NCB)); NCB.ncb_command := Chr(NCBENUM);
cRC := NetBios(@NCB); NCB.ncb_buffer := @LANAENUM;
NCB.ncb_length := SizeOf(LANAENUM);
cRC := NetBios(@NCB);
If Ord(cRC)<>0 Then
exit; ZeroMemory(@NCB, SizeOf(NCB));
NCB.ncb_command := Chr(NCBRESET);
NCB.ncb_lana_num := LANAENUM.lana[a];
cRC := NetBios(@NCB);
If Ord(cRC)<>0 Then
exit; ZeroMemory(@NCB, SizeOf(NCB));
NCB.ncb_command := Chr(NCBASTAT);
NCB.ncb_lana_num := LANAENUM.lana[a];
StrPCopy(NCB.ncb_callname, '*');
NCB.ncb_buffer := @ADAPTER;
NCB.ncb_length := SizeOf(ADAPTER);
cRC := NetBios(@NCB); strTemp := '';
For intIdx := 0 To 5 Do
strTemp := strTemp + InttoHex(Integer(ADAPTER.adapter_address[intIdx]),2);
Result := strTemp;
Finally
End;
End;