WindowsAPI获取主机网络配置和网络连接信息
2017-07-13 09:54
316 查看
//获取主机网络配置 int GetNetConfigInfo () { /* Declare and initialize variables */ // It is possible for an adapter to have multiple // IPv4 addresses, gateways, and secondary WINS servers // assigned to the adapter. // // Note that this sample code only prints out the // first entry for the IP address/mask, and gateway, and // the primary and secondary WINS server for each adapter. PIP_ADAPTER_INFO pAdapterInfo; PIP_ADAPTER_INFO pAdapter = NULL; DWORD dwRetVal = 0; UINT i; /* variables used to print DHCP time info */ struct tm newtime; char buffer[32]; errno_t error; char szLogBuffer[512]={0}; ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO); pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(sizeof (IP_ADAPTER_INFO)); if (pAdapterInfo == NULL) { printf("Error allocating memory needed to call GetAdaptersinfo\n"); return 1; } // Make an initial call to GetAdaptersInfo to get // the necessary size into the ulOutBufLen variable if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) { FREE(pAdapterInfo); pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(ulOutBufLen); if (pAdapterInfo == NULL) { printf("Error allocating memory needed to call GetAdaptersinfo\n"); return 1; } } if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) { pAdapter = pAdapterInfo; while (pAdapter) { printf("ComboIndex: %d\n", pAdapter->ComboIndex); printf("Adapter Name: %s\n", pAdapter->AdapterName); printf("Adapter Desc: %s\n", pAdapter->Description); printf("Adapter Addr: "); for (i = 0; i < pAdapter->AddressLength; i++) { if (i == (pAdapter->AddressLength - 1)) { printf("%.2X\n", (int) pAdapter->Address[i]); } else { printf("%.2X-", (int) pAdapter->Address[i]); } } printf("Index: %d\n", pAdapter->Index); printf("Type: "); switch (pAdapter->Type) { case MIB_IF_TYPE_OTHER: printf("Other\n"); break; case MIB_IF_TYPE_ETHERNET: printf("Ethernet\n"); break; case MIB_IF_TYPE_TOKENRING: printf("Token Ring\n"); break; case MIB_IF_TYPE_FDDI: printf("FDDI\n"); break; case MIB_IF_TYPE_PPP: printf("PPP\n"); break; case MIB_IF_TYPE_LOOPBACK: printf("Lookback\n"); break; case MIB_IF_TYPE_SLIP: printf("Slip\n"); break; default: printf("Unknown type %ld\n", pAdapter->Type); break; } printf("IP Address: %s\n",pAdapter->IpAddressList.IpAddress.String); printf("IP Mask: %s\n", pAdapter->IpAddressList.IpMask.String); printf("Gateway: %s\n", pAdapter->GatewayList.IpAddress.String); printf("***\n"); if (pAdapter->DhcpEnabled) { printf("DHCP Enabled: Yes\n"); printf(" DHCP Server: %s\n",pAdapter->DhcpServer.IpAddress.String); printf(" Lease Obtained: "); /* Display local time */ error = _localtime32_s(&newtime, (__time32_t*) &pAdapter->LeaseObtained); if (error) printf("Invalid Argument to _localtime32_s\n"); else { // Convert to an ASCII representation error = asctime_s(buffer, 32, &newtime); if (error) printf("Invalid Argument to asctime_s\n"); else /* asctime_s returns the string terminated by \n\0 */ { printf("%s", buffer); } } printf(" Lease Expires: "); error = _localtime32_s(&newtime, (__time32_t*) &pAdapter->LeaseExpires); if (error) printf("Invalid Argument to _localtime32_s\n"); else { // Convert to an ASCII representation error = asctime_s(buffer, 32, &newtime); if (error) printf("Invalid Argument to asctime_s\n"); else /* asctime_s returns the string terminated by \n\0 */ { printf("%s", buffer); } } } else { printf("DHCP Enabled: No\n"); } if (pAdapter->HaveWins) { printf("Have Wins: Yes\n"); printf(" Primary Wins Server: %s\n",pAdapter->PrimaryWinsServer.IpAddress.String); printf(" Secondary Wins Server: %s\n",pAdapter->SecondaryWinsServer.IpAddress.String); } else { printf("Have Wins: No\n"); } pAdapter = pAdapter->Next; printf("\n"); } } else { printf("GetAdaptersInfo failed with error: %d\n", dwRetVal); } if (pAdapterInfo) FREE(pAdapterInfo); return 0; } //获取主机连接 int GetNetStat() { // Declare and initialize variables PMIB_TCPTABLE_OWNER_PID pTcpTable; DWORD dwSize = 0; DWORD dwRetVal = 0; char szLogBuffer[512]={0}; char szLocalAddr[128]; char szRemoteAddr[128]; struct in_addr IpAddr; int i; pTcpTable = (MIB_TCPTABLE_OWNER_PID *) MALLOC(sizeof (MIB_TCPTABLE_OWNER_PID)); if (pTcpTable == NULL) { printf("Error allocating memory\n"); return 1; } dwSize = sizeof (MIB_TCPTABLE_OWNER_PID); // Make an initial call to GetTcpTable to // get the necessary size into the dwSize variable if ((dwRetVal = GetExtendedTcpTable(pTcpTable, &dwSize, TRUE,AF_INET,TCP_TABLE_OWNER_PID_ALL,0)) == ERROR_INSUFFICIENT_BUFFER) { FREE(pTcpTable); pTcpTable = (MIB_TCPTABLE_OWNER_PID *) MALLOC(dwSize); if (pTcpTable == NULL) { printf("Error allocating memory\n"); return 1; } } // Make a second call to GetTcpTable to get // the actual data we require if ((dwRetVal = GetExtendedTcpTable(pTcpTable, &dwSize, TRUE,AF_INET,TCP_TABLE_OWNER_PID_ALL,0)) == NO_ERROR) { printf("\tNumber of entries: %d\n", (int) pTcpTable->dwNumEntries); for (i = 0; i < (int) pTcpTable->dwNumEntries; i++) { printf("\n\tTCP[%d] State: %ld - ", i,pTcpTable->table[i].dwState); switch (pTcpTable->table[i].dwState) { case MIB_TCP_STATE_CLOSED: printf("CLOSED\n"); break; case MIB_TCP_STATE_LISTEN: printf("LISTEN\n"); break; case MIB_TCP_STATE_SYN_SENT: printf("SYN-SENT\n"); break; case MIB_TCP_STATE_SYN_RCVD: printf("SYN-RECEIVED\n"); break; case MIB_TCP_STATE_ESTAB: printf("ESTABLISHED\n"); break; case MIB_TCP_STATE_FIN_WAIT1: printf("FIN-WAIT-1\n"); break; case MIB_TCP_STATE_FIN_WAIT2: printf("FIN-WAIT-2 \n"); break; case MIB_TCP_STATE_CLOSE_WAIT: printf("CLOSE-WAIT\n"); break; case MIB_TCP_STATE_CLOSING: printf("CLOSING\n"); break; case MIB_TCP_STATE_LAST_ACK: printf("LAST-ACK\n"); break; case MIB_TCP_STATE_TIME_WAIT: printf("TIME-WAIT\n"); break; case MIB_TCP_STATE_DELETE_TCB: printf("DELETE-TCB\n"); break; default: printf("UNKNOWN dwState value: %d\n", pTcpTable->table[i].dwState); break; } IpAddr.S_un.S_addr = (u_long) pTcpTable->table[i].dwLocalAddr; strcpy_s(szLocalAddr, sizeof (szLocalAddr), inet_ntoa(IpAddr)); printf("\tTCP[%d] Local Addr: %s\n", i, szLocalAddr); printf("\tTCP[%d] Local Port: %d \n", i,ntohs((u_short)pTcpTable->table[i].dwLocalPort)); IpAddr.S_un.S_addr = (u_long) pTcpTable->table[i].dwRemoteAddr; strcpy_s(szRemoteAddr, sizeof (szRemoteAddr), inet_ntoa(IpAddr)); printf("\tTCP[%d] Remote Addr: %s\n", i, szRemoteAddr); printf("\tTCP[%d] Remote Port: %d\n", i,ntohs((u_short)pTcpTable->table[i].dwRemotePort)); printf("\tTCP[%d] Pid: %d\n", i,ntohs((u_short)pTcpTable->table[i].dwOwningPid)); HANDLE Handle = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pTcpTable->table[i].dwOwningPid /* This is the PID, you can find one from windows task manager */ ); if (Handle) { TCHAR Buffer[MAX_PATH]; if (GetModuleFileNameEx(Handle, 0, Buffer, MAX_PATH)) { // At this point, buffer contains the full path to the executable printf("\tTCP[%d] name: %s\n", i,Buffer); } else { // You better call GetLastError() here } CloseHandle(Handle); } } } else { printf("\tGetTcpTable failed with %d\n", dwRetVal); FREE(pTcpTable); return 1; } if (pTcpTable != NULL) { FREE(pTcpTable); pTcpTable = NULL; } return 0; }
相关文章推荐
- 在vb中使用Iphlpapi.dll获取网络信息 第二章 第六节 获取当前UDP连接情况
- 配置Vmware Linux和主机的网络连接
- phpmyadmin 尝试连接到 mysql 服务器 但服务器拒绝连接。您应该检查配置文件中的主机、用户名和密码 并确认这些信息与 mysql 服务器管理员所给出的信息一致。 .
- Android如何获取网络连接状态(3G/Wifi)及怎样调用网络配置界面
- Android系统信息获取 之六:网络连接状态信息获取
- 在vb中使用Iphlpapi.dll获取网络信息 第二章 第十七节 设置TCP连接状态
- 获取本机网络配置信息及ARP包的发送
- 【转载】利用GetNetworkParams函数获取本地网络配置信息
- Android编程获取网络连接状态(3G/Wifi)及调用网络配置界面
- Android编程获取网络连接状态(3G/Wifi)及调用网络配置界面
- 获取网络配置信息
- Android编程获取网络连接状态(3G/Wifi)及调用网络配置界面
- Android编程获取网络连接状态(3G/Wifi)及调用网络配置界面
- android 手机获取当前连接的wifi配置信息
- 初学python-获取网络设备配置文件接口IP信息
- 活用Ping命令 轻松获取网络连接与系统信息
- Android编程获取网络连接状态(3G/Wifi)及调用网络配置界面
- 8021x 获取IP信息失败,请检查锐捷认证客户端当前配置是否符合所在网络的要求,检查完毕后尝试重新认证
- 重写 WebBrowser 获取 网络连接错误信息
- 《visual c#.net网络核心编程》学习笔记系列之:获取本地主机信息