给大家一段可以查看网卡IP和子网的代码
实际上察看IP和子网只是我现在的程序需要,里面还有不少属性可以调用
主要 API GetNetworkParams() 和 GetAdaptersInfo()
/*
向项目中添加
C:\Program Files\Borland\BDS\4.0\lib\psdk\iphlpapi.lib
文件
*/
#include <windows.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <time.h>
void __fastcall TForm3::Button1Click(TObject *Sender)
{
DWORD Err;
PFIXED_INFO pFixedInfo;
DWORD FixedInfoSize = 0;
PIP_ADAPTER_INFO pAdapterInfo, pAdapt;
DWORD AdapterInfoSize;
PIP_ADDR_STRING pAddrStr;
//
// Get the main IP configuration information for this machine using a FIXED_INFO structure
//
if ((Err = GetNetworkParams(NULL, &FixedInfoSize)) != 0)
{
if (Err != ERROR_BUFFER_OVERFLOW)
{
// printf("GetNetworkParams sizing failed with error %d\n", Err);
return;
}
}
// Allocate memory from sizing information
if ((pFixedInfo = (PFIXED_INFO) GlobalAlloc(GPTR, FixedInfoSize)) == NULL)
{
// printf("Memory allocation error\n");
return;
}
AdapterInfoSize = 0;
if ((Err = GetAdaptersInfo(NULL, &AdapterInfoSize)) != 0)
{
if (Err != ERROR_BUFFER_OVERFLOW)
{
// printf("GetAdaptersInfo sizing failed with error %d\n", Err);
return;
}
}
if ((pAdapterInfo = (PIP_ADAPTER_INFO) GlobalAlloc(GPTR, AdapterInfoSize)) == NULL)
{
// printf("Memory allocation error\n");
return;
}
// Get actual adapter information
if ((Err = GetAdaptersInfo(pAdapterInfo, &AdapterInfoSize)) != 0)
{
// printf("GetAdaptersInfo failed with error %d\n", Err);
return;
}
pAdapt = pAdapterInfo;
ShowMessage(pAdapt->Description);
// ShowMessage(pAdapt->AdapterName);
pAddrStr = &(pAdapt->IpAddressList);
while(pAddrStr)
{
ShowMessage(pAddrStr->IpAddress.String);
ShowMessage(pAddrStr->IpMask.String);
pAddrStr = pAddrStr->Next;
}
}
//---------------------------------------------------------------------------