怎么获取子网掩码?
怎么获取子网掩码?
[解决办法]
好事做到底,直接上代码:
#include <tchar.h>#include <iphlpapi.h>#include <stdio.h>#pragma link "iphlpapi.lib"void __fastcall TForm1::Button1Click(TObject *Sender){ PMIB_IPADDRTABLE pIPTable = NULL; DWORD dwSize = 0; ::GetIpAddrTable(pIPTable, &dwSize, TRUE); pIPTable = (PMIB_IPADDRTABLE)::GlobalAlloc(GPTR, dwSize); ::GetIpAddrTable(pIPTable, &dwSize, TRUE); TCHAR szIPAddr[17], szSubMask[17]; for (DWORD i=0; i < pIPTable->dwNumEntries; i++) { // IP memset(szIPAddr, 0x0, sizeof(szIPAddr)); _stprintf(szIPAddr, TEXT("%d.%d.%d.%d"), LOWORD(pIPTable->table[i].dwAddr) & 0x00FF, LOWORD(pIPTable->table[i].dwAddr) >> 8, HIWORD(pIPTable->table[i].dwAddr) & 0x00FF, HIWORD(pIPTable->table[i].dwAddr) >> 8); // Subnet Mask memset(szSubMask, 0x0, sizeof(szSubMask)); _stprintf(szSubMask, TEXT("%d.%d.%d.%d"), LOWORD(pIPTable->table[i].dwMask) & 0x00FF, LOWORD(pIPTable->table[i].dwMask) >> 8, HIWORD(pIPTable->table[i].dwMask) & 0x00FF, HIWORD(pIPTable->table[i].dwMask) >> 8); Memo1->Lines->Add(String().sprintf(TEXT("IP: %s 子网掩码: %s"), szIPAddr, szSubMask)); } ::GlobalFree(pIPTable);}