Get the native IP address and MAC address

Source: Internet
Author: User

Unit Netfunc;

Interface
Uses
Sysutils, Windows, dialogs, Winsock, Classes, Comobj, WinInet, variants;

Error message Constants
Const
C_err_getlocalip = ' Get local IP failed ';
C_err_getnamebyipaddr = ' failed to get host name ';
C_err_getsqlserverlist = ' failed to get SQL Server servers ';
C_err_getuserresource = ' access to shared capital failed ';
C_err_getgrouplist = ' Get all workgroup failure ';
C_err_getgroupusers = ' Get all computers in workgroup failed ';
C_err_getnetlist = ' Get all network types failed ';
c_err_checknet = ' network not through ';
C_err_checkattachnet = ' Not logged in the network ';
c_err_internetconnected = ' no internet ';

c_txt_checknetsuccess = ' network unblocked ';
c_txt_checkattachnetsuccess = ' logged in network ';
c_txt_internetconnected = ' surfing the internet ';

Check if the machine is logged in to the network
function Islogonnet:boolean;

Get local LAN IP address of the machine
function Getlocalip (var localip:string): Boolean;

Return the machine name via IP
function getnamebyipaddr (ipaddr:string; var macname:string): Boolean;

Get a list of SQL Server on the network
function Getsqlserverlist (var list:tstringlist): Boolean;

Get all network types in the network
function Getnetlist (var list:tstringlist): Boolean;

Get a workgroup in a network
function Getgrouplist (var list:tstringlist): Boolean;

Get all computers in a workgroup
function getusers (groupname:string; var list:tstringlist): Boolean;

Get resources from the network
function Getuserresource (ipaddr:string; var list:tstringlist): Boolean;

Map Network drives
function Netaddconnection (Netpath:pchar; Password:pchar; Localpath:pchar): Boolean;

Detecting Network status
function Checknet (ipaddr:string): Boolean;

Determine if the IP protocol has any problems installing this function
function Isipinstalled:boolean;

Detect if the machine is online
function Internetconnected:boolean;

Turn off network connections
function Netcloseall:boolean;

/////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
/////////////////////////////////////////////////
Code Implementation Department////////////

{=================================================================
Function: Check whether the machine is logged into the network
Parameters: None
Return value: Success: True failed: False
Notes
Version
1.0 2002/10/03 09:55:00
=================================================================}
function Islogonnet:boolean;
Begin
Result: = False;
If GetSystemMetrics (sm_network) <> 0 Then
Result: = True;
End


{=================================================================
Function: Returns the local area network IP address of the machine
Parameters: None
Return value: Success: True, and padding Localip failed: False
Notes
Version
1.0 2002/10/02 21:05:00
=================================================================}
function Getlocalip (var localip:string): Boolean;
Var
Hostent:phostent;
ip:string;
Addr:pchar;
Buffer:array [0..63] of Char;
Wsdata:twsadata;
Begin
Result: = False;
Try
WSAStartup (2, Wsdata);
GetHostName (buffer, SizeOf (buffer));
buffer:= ' ZhiDa16 ';
Hostent: = gethostbyname (Buffer);
If hostent = nil then exit;
ADDR: = hostent^.h_addr_list^;
IP: = Format ('%d.%d.%d.%d ', [Byte (Addr[0]), Byte (Addr[1]),
Byte (addr[2]), Byte (Addr[3])
);
Localip: = Ip;
Result: = True;
Finally
WSACleanup;
End
End

{=================================================================
Function: Returns the machine name via IP
Parameters
IPADDR: Want to get the name of the IP
Return value: Success: Machine name failed: '
Notes
inet_addr function converts a string containing an Internet
Protocol dotted address into an in_addr.
Version
1.0 2002/10/02 22:09:00
=================================================================}
function getnamebyipaddr (Ipaddr:string;var macname:string): Boolean;
Var
Sockaddrin:tsockaddrin;
Hostent:phostent;
Wsadata:twsadata;
Begin
Result: = False;
if ipaddr = "then exit;
Try
WSAStartup (2, wsadata);
SOCKADDRIN.SIN_ADDR.S_ADDR: = inet_addr (PChar (ipaddr));
Hostent: = gethostbyaddr (@SockAddrIn. sin_addr. S_ADDR, 4, af_inet);
If hostent <> Nil Then
Macname: = Strpas (Hostent^.h_name);
Result: = True;
Finally
WSACleanup;
End
End

{=================================================================
function: Returns the SQL Server list on the network
Parameters
List: list that needs to be populated
Return value: Success: True, and populate list failed False
Notes
Version
1.0 2002/10/02 22:44:00
=================================================================}
function Getsqlserverlist (Var list:tstringlist): boolean;
Var
I:integer;
sretvalue:string;
Sqlserver:variant;
Serverlist:variant;
Begin
Result: = False;
List.clear;
Try
SQL Server: = Createoleobject (' SQLDMO. Application ');
ServerList: = sqlserver.listavailablesqlservers;
For I: = 1 to Serverlist.count do
List. ADD (Serverlist.item (i));
Result: = True;
Finally
SQL Server: = NULL;
ServerList: = NULL;
End
End

{=================================================================
Function: Determine if IP protocol is installed
Parameters: None
Return value: Success: True failed: False;
Note: This function also has a problem
Version
1.0 2002/10/02 21:05:00
=================================================================}
function Isipinstalled:boolean;
Var
Wsdata:twsadata;
Protoent:pprotoent;
Begin
Result: = True;
Try
If WSAStartup (2,wsdata) = 0 Then
Begin
Protoent: = Getprotobyname (' IP ');
If protoent = Nil Then
Result: = False
End
Finally
WSACleanup;
End
End


{=================================================================
Function: Returns the shared resource on the network
Parameters
IPADDR: Machine IP
List: list that needs to be populated
Return value: Succeeded: True, and the list failed to populate: False;
Notes
WNetOpenEnum function starts an enumeration of network
Resources or existing connections.
WNetEnumResource function continues a network-resource
Enumeration started by the WNetOpenEnum function.
Version
1.0 2002/10/03 07:30:00
=================================================================}
function Getuserresource (ipaddr:string; var list:tstringlist): Boolean;
Type
Tnetresourcearray = array of ^tnetresource;//network types
Var
I:integer;
Buf:pointer;
Temp:tnetresourcearray;
Lphenum:thandle;
Netresource:tnetresource;
Count,bufsize,res:dword;
Begin
Result: = False;
List.clear;
If copy (ipaddr,0,2) <> ' \ \ ' Then
IPAddr: = ' \ \ ' +ipaddr; Populating IP address information
Fillchar (Netresource, SizeOf (Netresource), 0);//Initialize Network hierarchy information
Netresource.lpremotename: = @IpAddr [1];//Specify computer name
Gets the network resource handle for the specified computer
Res: = WNetOpenEnum (Resource_globalnet, Resourcetype_any,
Resourceusage_connectable, @NetResource, lphenum);
Buf:=nil;
If Res <> No_error then exit;//execution failed
While True do//enumerates network resources for the specified workgroup
Begin
Count: = $FFFFFFFF;//unlimited number of resources
BufSize: = 8192;//buffer size set to 8K
Getmem (Buf, BufSize);//Request for memory to get workgroup information
Gets the network resource name for the specified computer
Res: = WNetEnumResource (Lphenum, Count, Pointer (Buf), BufSize);
If Res = Error_no_more_items then break;//Resource enumeration complete
if (Res <> no_error) then exit;//execution failed
Temp: = Tnetresourcearray (BUF);
For I: = 0 to Count-1 do
Begin
Gets the name of the shared resource on the specified computer, +2 means delete "\ \",
such as \\192.168.0.1 = 192.168.0.1
List.add (Temp^.lpremotename + 2);
INC (Temp);
End
End
Res: = Wnetcloseenum (Lphenum);//Close Once enumeration
If Res <> No_error then exit;//execution failed
Result: = True;
Freemem (BUF);
End;

{=================================================================
Function: Returns a workgroup in the network
Parameters
List: list that needs to be populated
Return value: Succeeded: True, and the list failed to populate: False;
Notes
Version
1.0 2002/10/03 08:00:00
=================================================================}
function Getgrouplist (var list:tstringlist): Boolean;
Type
Tnetresourcearray = array of ^tnetresource;//network types
Var
Netresource:tnetresource;
Buf:pointer;
Count,bufsize,res:dword;
Lphenum:thandle;
P:tnetresourcearray;
I,j:smallint;
Networktypelist:tlist;
Begin
Result: = False;
Networktypelist: = tlist.create;
List.clear;
Gets a handle to the file resource in the entire network, Lphenum for the return name handle
Res: = WNetOpenEnum (Resource_globalnet, Resourcetype_disk,
Resourceusage_container, Nil,lphenum);
If res <> no_error then exit;//raise Exception (Res);//Execution failed
Get network type information across your network
Count: = $FFFFFFFF;//unlimited number of resources
BufSize: = 8192;//buffer size set to 8K
Getmem (Buf, BufSize);//Request for memory to get workgroup information
Res: = WNetEnumResource (Lphenum, Count, Pointer (Buf), BufSize);
Resource enumeration completed//execution failed
if (res = error_no_more_items) or (res <> no_error) then Exit;
P: = Tnetresourcearray (BUF);
For I: = 0 to Count-1 do//record information for each network type
Begin
Networktypelist.add (P);
INC (P);
End
Res: = Wnetcloseenum (Lphenum);//Close Once enumeration
If Res <> no_error then exit;
For J: = 0 to Networktypelist.count-1 do//Lists all workgroup names in each network type
begin//Lists all workgroup names in a network type
Netresource: = Tnetresource (networktypelist.items[j]^);//Network type information
Gets a handle to the file resource for a network type, Netresource for the network type information, and Lphenum as the return name handle
Res: = WNetOpenEnum (Resource_globalnet, Resourcetype_disk,
Resourceusage_container, @NetResource, lphenum);
If Res <> No_error then break;//execution failed
While true do//lists information for all workgroups of a network type
Begin
Count: = $FFFFFFFF;//unlimited number of resources
BufSize: = 8192;//buffer size set to 8K
Getmem (Buf, BufSize);//Request for memory to get workgroup information
Gets the file resource information for a network type,
Res: = WNetEnumResource (Lphenum, Count, Pointer (Buf), BufSize);
Resource enumeration completed//execution failed
if (res = error_no_more_items) or (res <> no_error) then break;
P: = Tnetresourcearray (BUF);
For I: = 0 to Count-1 do//enumerate the information for each workgroup
Begin
List.add (Strpas (P^.lpremotename));//Get the name of a workgroup
INC (P);
End
End
Res: = Wnetcloseenum (Lphenum);//Close Once enumeration
If Res <> No_error then break;//execution failed
End
Result: = True;
Freemem (BUF);
Networktypelist.destroy;
End;

Get the native IP address and MAC address

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.