IP Address Configuration of the Programming Control System in iphelp-dephi

Source: Internet
Author: User
Abstract: The routing utility changes the network configuration information and provides corresponding sample programs.
Key words: Delphi IP address netsh

I. Introduction
With the popularization of the Internet, network security issues have become increasingly prominent. According to the policies of relevant national departments, most government agencies with high security requirements have established physically isolated internal and external networks. In this environment, workstation must switch between the Intranet and Internet of different network segments frequently, which requires that the network configurations of the workstation system be updated in a timely manner. In addition, with the popularization of mobile office, the network configuration of the laptop system also needs to change frequently. As the best development tool, Delphi uses the "Delphi + Windows API" mode to solve these problems.

Ii. Principles
IP helper is a set of APIS (application programming interfaces) used to manage local network settings. These API functions are defined in the iphlpapi. dll library file. The iphlpapi. dll library file exists in the Windows NT4/2000/system32 directory or Windows 98/ME/system directory. Just like writing header files in VC, using these APIs in Delphi requires corresponding definitions and declarations.
A computer may have more than one network adapter or modem installed. Call the getadaptersinfo function to obtain the network configuration information of the current computer. This function contains two parameters. The first parameter is the first address of the memory buffer used to save the adapter information, and the second parameter is the buffer size. Because we do not know how many network adapters are on the local machine in advance, we cannot know how much cache should be allocated, fortunately, when the buffer size is insufficient, the getadaptersinfo function will fill in the second parameter with the buffer size to be allocated. Therefore, just like some other functions, we can call this function twice when needed. The first time we get the buffer size, allocate the buffer, and then call it again to obtain the actual Nic information. The function is defined as follows:
Function getadaptersinfo (padapterinfo: ptip_adapter_info;
Poutbuflen: Pulong): DWORD;
Stdcall; External 'lplpapi. dll ';
The current computer network configuration information returned by the function exists in the ip_adapter_info structure pointed to by the first parameter padapterinfo. The data structure is organized as a static linked list.
Pipadapterinfo = ^ tipadapterinfo;
Tip_adapter_info = record
Next: pipadapterinfo; // linked list pointer field, which is used to access the entire static key table
Comboindex: DWORD; // reserved unused
Adaptername: // Nic name, max_adapter_name_length = 256
Array [0 .. max_adapter_name_length + 3] of char;
Description: // Nic description max_adapter_description_length = 128
Array [0 .. max_adapter_description_length + 3] of char;
Addresslength: uint; // The length of the physical address.
Address: // physical address, max_adapter_address_length = 8 each byte stores a hexadecimal number
Array [0 .. max_adapter_address_length-1] of byte;
Index: DWORD; // Nic Index Number
Type _: uint; // Nic type
Dhcpenabled: uint; // whether DHCP Dynamic IP allocation is enabled
Currentipaddress: pip_addr_string; // the currently used IP address.
Ipaddresslist: ip_addr_string; // list of IP addresses bound to this Nic
Gatewaylist: ip_addr_string; // gateway address linked list
Dhcpserver: ip_addr_string; // DHCP server address, valid only when dhcpenabled = true
Havewins: bool; // whether wins is enabled
Primarywinsserver: ip_addr_string; // master wins address
Secondarywinsserver: ip_addr_string; // The secondary wins address.
Leaseobtained: time_t; // time obtained by the current DHCP lease
Leaseexpires: time_t; // current DHCP lease expiration time
End;
The data is the content on the properties page of Network Properties → TCP/IP. The following describes how to update Dynamic IP addresses and modify static network configurations.
2.1 dhcpenabled = 1 indicates that the local IP address is dynamically allocated to the DHCP server. You can call ipreleaseaddress and iprenewaddress to release and obtain the system IP address again. The function is defined as follows:
Function iprenewaddress (
VaR adapterinfo: tip_adapter_index_map): DWORD;
Stdcall; External 'lplpapi. dll ';
Function ipreleaseaddress (
VaR adapterinfo: tip_adapter_index_map): DWORD;
Stdcall; External 'lplpapi. dll ';
To use these two functions, you must obtain the network interface information to specify the index number of the adapter that releases and updates the IP address. A network interface is the logical abstraction of the network card. They are one-to-one relationships. Note that the number of local interfaces returned by calling the getnumberofinterfaces function is actually one more than the number of network interfaces, because each system has a network interface for debugging, the IP address of this interface is 127.0.0.1. The subnet mask is 255.0.0.0. The getinterfaceinfo function extracts Network Interface Information:
Function getinterfaceinfo (piftable: ptip_interface_info;
Dwoutbuflen: Pulong): DWORD;
Stdcall; External 'lplpapi. dll ';
It returns a data array with a numadapters integer in the ip_interface_info structure that records the current number of native adapters and the adapter is ip_adapter_index_map structure.
Pip_interface_info = ^ tip_interface_info;
Tip_interface_info = record
Numadapters: longint;
Adapter: array [0 .. 1-1] of ip_adapter_index_map;
End;
The ip_adapter_index_map structure is as follows:
Pipadapterindexmap = ^ tipadapterindexmap;
Tip_adapter_index_map = record
Index: ulong;
Name: array [0 .. 127] of wchar;
End;
When the index value in this structure is equal to the value of the index field in the structure directed to by calling the getadaptersinfo function padapterinfo, it indicates that the network adapter matches the network interface, you can call ipreleaseaddress and iprenewaddress to release and obtain the IP address of the adapter again.
2.2 In delphi, you can directly use the netsh.exe utility to change the IP network configuration information of the Windows2000/XP system.

3. Program Implementation
3.1 run the winipcfg utility in WIN98 and run ipconfig in Windows2000/XP to release and obtain the system (all) ip address again. The following uses Delphi programming:
First Declare Public indexstrs: tstrings; // save all network cable quotation marks of the Local Machine
Procedure tform1.button1click (Sender: tobject );
VaR
Err, buflen: DWORD;
P: pointer;
Adapterinfo: ptip_adapter_info;
Begin
Buflen: = sizeof (adapterinfo ^ );
New (adapterinfo );
Err: = getadaptersinfo (adapterinfo, @ buflen );
P: = adapterinfo;
Indexstrs: = tstringlist. Create;
If err = no_error then
Begin
While P <> nil do
With tip_adapter_info (P ^) Do
Begin
If dhcpenabled = 1 then
Indexstrs. Add (inttostr (INDEX ));
P: = next; // tip_adapter_info (P ^). Next
End //
End // while
Else
Showmessage (syserrormessage (error ));
Dispose (adapterinfo );
End;
Click Event of button1: when the local IP address is dynamically allocated to the DHCP server, all the network cable quotation marks on the local machine are saved in the tstrings type indexstrs.
Procedure tform1.button2click (Sender: tobject );
VaR
Res, err, Suc, bufsize: DWORD;
Ip_map: tip_adapter_index_map;
CNT, dwindex, I: longint;
Interfaceinfo: ptip_interface_info;
Begin
Bufsize: = sizeof (interfaceinfo ^ );
Interfaceinfo: = allocmem (bufsize );
Err: = getinterfaceinfo (interfaceinfo, @ bufsize );
If err = error_insufficient_buffer then
Begin
Freemem (interfaceinfo );
Interfaceinfo: = allocmem (bufsize );
Try
Res: = getinterfaceinfo (interfaceinfo, @ bufsize );
If res = no_error then
Begin
For I: = 0 to indexstrs. Count-1 do
Begin
Dwindex: = strtoint (indexstrs [I]);
For CNT: = 0 to interfaceinfo ^. numadapters-1 do
If interfaceinfo ^. Adapter [CNT]. Index = dwindex then
Suc: = ipreleaseaddress (interfaceinfo ^. Adapter [CNT]);
If suc = no_error then showmessage ('IP address released successfully! ');
End; // For I: = 0 to... release all IP addresses
For I: = 0 to indexstrs. Count-1 do
Begin
Dwindex: = strtoint (indexstrs [I]);
For CNT: = 0 to interfaceinfo ^. numadapters-1 do
If interfaceinfo ^. Adapter [CNT]. Index = dwindex then
Suc: = iprenewaddress (interfaceinfo ^. Adapter [CNT]);
If suc = no_error then showmessage ('IP address retrieved again! ');
End; // For I: = 0 to... obtain all IP addresses again
End;
Finally
Freemem (interfaceinfo );
End;
End;
Indexstrs. Free;
End;
Click Event of button2: match the index in the network interface information with the value in indexstrs, find the corresponding adapter to release the IP address, release it cyclically, and obtain all the IP addresses again.
3.2 use the netsh utility in Delphi to change the network configuration.
The netsh.exe utility is deployed in the operating system behind windows2000. Netsh is a command line utility. It generally starts netsh from a command prompt and changes it to the context that contains the command to be used. It can be used to display or modify the network configuration of the currently running computer, the following describes how to change the network configuration in Delphi.
Procedure tform1.button3click (Sender: tobject );
Begin
ShellExecute (handle, 'open', 'netsh', pchar ('interface IP Set address "Local Connection" static 192.168.0.10 255.255.255.0 192.168.0.1 1'), nil, sw_hide );
End;
Whether the current system IP address is dynamic or static, The button3click event changes the current network configuration to the static IP address 192168.0.10, The subnet mask 255.255.0, the gateway: 192.168.0.1, the number of hops: 1; also execute ShellExecute (handle, 'open', 'netsh', pchar ('interface IP Set address "Local Connection" DHCP '), nil, sw_hide) change the local IP address to dynamic. Netsh is powerful. In this form, you can modify, delete, add DNS, wins, gateway, and other network configuration information, and the changed information takes effect directly, you do not need to disable → enable Nic or restart Windows.

Iv. Conclusion
In fact, you can change the network configurations such as the system IP Address by modifying the system registry. The network configuration information of different operating systems varies in the registry. Modify the registry for static IP addresses of Win95 and Win98 systems to change IP configuration information, however, all IP configuration information changed by modifying the system registry key must be "disabled" → "enabled" Nic or restarted windows to take effect. In addition, the IP help API is invalid for Win95.

References:
[1] Platform SDK: IP helper. http://msdn.microsoft.com/library/default.asp [dB/Ol], 2004-01.
[2] Steve Teixeira, Xavier Pacheco. Delphi 5 Developer Guide [M]. Translated by Ren xujun. Beijing: Press of Machinery Industry, 2000

Related Article

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.