c ++ access to the local IP address

//#define _WINSOCK_DEPRECATED_NO_WARNINGS #include <WinSock2.h> #include <Iphlpapi.h> #include <iostream> using namespace std; #pragma comment(lib,"Iphlpapi.lib") //需要添加Iphlpapi.lib库 //#pragma comment(lib, "ws2_32.lib")

int main (int argc, char * argv []) {// PIP_ADAPTER_INFO native structure pointer memory card information PIP_ADAPTER_INFO pIpAdapterInfo = new IP_ADAPTER_INFO (); // get the size of the structure, the parameters for GetAdaptersInfo unsigned long stSize = sizeof (IP_ADAPTER_INFO ); // call GetAdaptersInfo function, filling pIpAdapterInfo pointer variable; wherein stSize parameter is both an input amount is also an output int nRel = GetAdaptersInfo (pIpAdapterInfo, & stSize); (& netCardNum) GetNumberOfInterfaces;; // record card number DWORD netCardNum = 0 cout << "number of network:" << netCardNum << endl; netCardNum = 0; IP address number recorded on each card // int IPnumPerNetCard = 0; if (ERROR_BUFFER_OVERFLOW == nRel) {// If the function returns ERROR_BUFFER_OVERFLOW // then the parameters passed GetAdaptersInfo memory space is insufficient, while its spread stSize, expressed the need for the space // this is also why stSize both an input // output is a release of the original memory space delete pIpAdapterInfo;

 

    } // release memory space if (pIpAdapterInfo) {delete pIpAdapterInfo;} return 0;}

Reprinted: https: //www.cnblogs.com/lzpong/p/6137652.html

Guess you like

Origin www.cnblogs.com/freedomworld/p/11607364.html
Recommended