Two ways to dynamically allocate IP when dial-up Internet access
1: Use the socket function. Get all local IP before dialing, record it into the list of strings. After the dial is successful, it will pass all local IPs again, and the IP comparisons have been recorded in the list list. The newly added IP address is dynamically assigned IP when dialing.
2: Use and RAS about the API. It can be convenient to use and RAS-related APIs in the VC. But there will be errors that are different from the version in C Builder 6. Therefore, the key in the BCB is to specify the size of the function.
The following code compiled in the Win2000 C Builder 6 environment.
Add 2 Edit, 3 Button in Form;
/ / -------------------------------------------------------------------------------------------- ---------------------------
#ifndef unit1H # Define unit1H // ------------------------------------------------------------------------------------------------------------------------------------ --------------------------------- # include
/ / -------------------------------------------------------------------------------------------- ---------------------------
#include
#include "unit1.h" #include
IF (0! = WSAStartup (WVersionRequested, & WSADATA) {wsacleanup (); showMessage ("Socket version number is wrong);}
IF (0 == gethostname (chOSTNAME, 64)) {phost = gethostByname (chOSTNAME); if (phost! = null) {for (int i = 0; phost-> h_addr_list [i]! = null; i ) {IN_ADDR * p = (in_addr *) (phost-> h_addr_list [i]); psip = inet_ntoa (* p); m_slip-> add (psip); // IP address deposit into string list}} else {wsacleanup (); ShowMessage ("Take IP Address Failed");}} else {showMessage ("Take the machine name failed");}
WSACLEANUP ();
} // ----------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------- Void __fastcall tform1 :: button2click (Tobject * sender) {rasconn rasconn; // activity connection DWORD BUFFSIZE // Active connection accounts for the memory size; DWORD connUM; // Event connection number rasconn.dwsize = 692; // must specify a connection of memory size; buffsize = 692; raspppip rip; Ansistring modemip; zeromeMory (& Rip, Sizeof )); DWORD SIZE; SIZE = 40; rip.dwsize = 40;
DWORD dwReturn = RasEnumConnections (& RASconn, & BuffSize, & ConnNum); if (dwReturn == 0) {if (ConnNum> 0) {// ShowMessage ( "dial-up connection is not!"); If ((RasGetProjectionInfo (RASconn.hrasconn, RASP_PppIp, & rip, & size))! = 0) {ShowMessage ("Take IP Address Failure");} else {edit2-> text = rip.szipaddress;}} else {showMessage ("no dial-up connection!");}}} else showmessage ("RaseNumConnections function failed!");
} // ----------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------- Void __fastcall tform1 :: button3click (TOBJECT * Sender) {Word WvelSionRequested; Wsadata Wsadata; Char Chostname [64 ] = {0}; hostent * phost = null; ansistring psip; int index; wversionRequested = MakeWord (2, 0);
IF (0! = WSAStartup (WVersionRequested, & WSADATA) {wsacleanup (); showMessage ("Socket version number is wrong);}
IF (0 == gethostname (chOSTNAME, 64)) {phost = gethostByname (chOSTNAME); if (phost! = null) {for (int i = 0; phost-> h_addr_list [i]! = null; i ) {IN_ADDR * p = (in_addr *) (phost-> h_addr_list [i]); PSIP = INET_NTOA (* P); if (m_slip-> indexof (psip) <0) {edit1-> text = psip;}}} else { Washanup (); showMessage ("Take IP address failed");}} else {showMessage ("Take the machine name failed");} wsacleanup (); delete m_slip;} // ----------- -------------------------------------------------- ----------------
First click on Button1 before the Internet, and then click Butto3 to get the dial IP.