0
我知道WinAPI的GetBestInterface
返回的网络接口索引。如何根据接口索引获取接口属性(IPv4地址)?检索接口到IP地址映射表
这里是工作的C++代码,但我需要它在C#中。
PMIB_IPADDRTABLE pAddrTable;
PMIB_IPADDRROW pAddrRow;
in_addr ia;
CBasePage::OnSetActive();
m_edit1.SetFont(&m_font);
m_edit1.SetWindowText("");
GetIpAddrTable((PMIB_IPADDRTABLE) m_pBuffer, &m_ulSize, TRUE);
m_pBuffer = new BYTE[m_ulSize];
if (NULL != m_pBuffer)
{
m_dwResult = GetIpAddrTable((PMIB_IPADDRTABLE) m_pBuffer, &m_ulSize, TRUE);
if (m_dwResult == NO_ERROR)
{
pAddrTable = (PMIB_IPADDRTABLE) m_pBuffer;
for (int x = 0; x < pAddrTable->dwNumEntries; x++)
{
pAddrRow = (PMIB_IPADDRROW) &(pAddrTable->table[x]);
ia.S_un.S_addr = pAddrRow->dwAddr;
m_strText.Format(" IP address: %s\r\n", inet_ntoa(ia));
m_edit1.ReplaceSel(m_strText);
m_strText.Format(" Interface index: %lu\r\n", pAddrRow->dwIndex);
m_edit1.ReplaceSel(m_strText);
ia.S_un.S_addr = pAddrRow->dwMask;
m_strText.Format(" Subnet mask: %s\r\n", inet_ntoa(ia));
m_edit1.ReplaceSel(m_strText);
ia.S_un.S_addr = pAddrRow->dwBCastAddr;
m_strText.Format("Broadcast address: %s\r\n", inet_ntoa(ia));
m_edit1.ReplaceSel(m_strText);
m_edit1.ReplaceSel("\r\n");
}
}
else
{
m_strText.Format("GetIpAddrTable() failed. Result = %lu\r\n", m_dwResult);
m_edit1.ReplaceSel(m_strText);
}
delete [] m_pBuffer;
}
我已经试过example on pinvoke,但它所有的接口返回0.0.0.0
。
它只是工作。谢谢。我一定错过了一些东西 – technology