改进滚动条技术的版本:
使用SetScrollInfo GetScrollInfo ScrollWindow函数的技术
#include "StdAfx.h"
#include <iostream>
#include <windows.h>
//#include "IPHlpApi.h"
//#include "Iphlpapi.h"
#include "Iphlpapi.h"
#include <string.h>
#include <stdio.h>
#pragma comment(lib,"Iphlpapi.lib")
#define WINVER 0x0500
//
#define MAXNUM 300
typedef struct InFo
{
int count;
TCHAR * info[MAXNUM];
}information;
//
information MyGetInterfaceInfo(bool bOrder);
information MyGetNetworkParams(bool bOrder);
information MyGetAdaptersInfo(bool bOrder);
information MyGetIfTable(bool bOrder);
information MyGetIpAddrTable(bool bOrder);
information MyGetTcpTable(bool bOrder);
void MyAddIpAdress(bool bOrder);
//
using namespace std;
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("SysMets2") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT ("Get System Metrics No. 2"),
WS_OVERLAPPEDWINDOW | WS_VSCROLL,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int cxChar, cxCaps, cyChar, cyClient;
HDC hdc ;
int i, y ;
PAINTSTRUCT ps ;
TCHAR szBuffer[10] ;
TEXTMETRIC tm ;
static int AllRow;
information infoAll[8];
SCROLLINFO si;
int iVertPos;
infoAll[0]=MyGetInterfaceInfo(TRUE);
infoAll[1]=MyGetNetworkParams(TRUE);
infoAll[2]=MyGetAdaptersInfo(TRUE);
infoAll[3]=MyGetIpAddrTable(TRUE);
infoAll[4]=MyGetIfTable(TRUE);
infoAll[5]=MyGetTcpTable(TRUE);
AllRow=infoAll[0].count+infoAll[1].count+infoAll[2].count+infoAll[3].count+infoAll[4].count+infoAll[5].count;
switch (message)
{
case WM_CREATE:
hdc = GetDC (hwnd) ;
GetTextMetrics (hdc, &tm) ;
cxChar = tm.tmAveCharWidth ;
cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ;
cyChar = tm.tmHeight + tm.tmExternalLeading ;
ReleaseDC (hwnd, hdc) ;
return 0 ;
case WM_SIZE:
cyClient = HIWORD (lParam) ;
si.cbSize=sizeof(si);
si.fMask=SIF_RANGE |SIF_POS;
si.nMin=0;
si.nMax=AllRow-cyClient/cyChar;
si.nPage=cyClient/cyChar;
SetScrollInfo(hwnd,SB_VERT,&si,TRUE);
return 0 ;
case WM_VSCROLL:
si.cbSize=sizeof(si);
si.fMask=SIF_ALL;
GetScrollInfo(hwnd,SB_VERT,&si);
iVertPos=si.nPos;
switch(LOWORD(wParam))
{
case SB_TOP:
si.nPos=si.nMin;
break;
case SB_BOTTOM:
si.nPos=si.nMax;
case SB_LINEUP:
si.nPos--;
break;
case SB_LINEDOWN:
si.nPos++;
break;
case SB_PAGEUP:
si.nPos-=si.nPage;
break;
case SB_PAGEDOWN:
si.nPos+=si.nPage;
break;
case SB_THUMBTRACK:
si.nPos=si.nTrackPos;
break;
default:
break;
}
si.fMask=SIF_POS;
SetScrollInfo(hwnd,SB_VERT,&si,TRUE);//如果si.nPos的值超出了允许范围则在函数里面自动设置为正确值
GetScrollInfo(hwnd,SB_VERT,&si);
if(si.nPos!=iVertPos)
{
ScrollWindow(hwnd,0,cyChar*(iVertPos-si.nPos),NULL,NULL);
UpdateWindow(hwnd);
}
return 0;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
MyGetInterfaceInfo(TRUE);
si.cbSize=sizeof(si);
si.fMask=SIF_ALL;
GetScrollInfo(hwnd,SB_VERT,&si);
for(int i=0;i<infoAll[0].count;++i)
{
::TextOut(hdc,0,i*cyChar-si.nPos*cyChar,infoAll[0].info[i],::lstrlen(infoAll[0].info[i]));
}
for(int i=0;i<infoAll[1].count;++i)
{
::TextOut(hdc,0,infoAll[0].count*cyChar+i*cyChar-
si.nPos*cyChar,infoAll[1].info[i],::lstrlen(infoAll[1].info[i]));
}
for(int i=0;i<infoAll[2].count;++i)
{
::TextOut(hdc,0,infoAll[0].count*cyChar+infoAll[1].count*cyChar+
i*cyChar-si.nPos*cyChar,infoAll[2].info[i],::lstrlen(infoAll[2].info[i]));
}
for(int i=0;i<infoAll[3].count;++i)
{
::TextOut(hdc,0,infoAll[0].count*cyChar+infoAll[1].count*cyChar+
infoAll[2].count*cyChar+i*cyChar-si.nPos*cyChar,infoAll[3].info[i],::lstrlen(infoAll[3].info[i]));
}
for(int i=0;i<infoAll[4].count;++i)
{
::TextOut(hdc,0,infoAll[0].count*cyChar+infoAll[1].count*cyChar
+infoAll[2].count*cyChar+infoAll[3].count*cyChar+i*cyChar-
si.nPos*cyChar,infoAll[4].info[i],::lstrlen(infoAll[4].info[i]));
}
for(int i=0;i<infoAll[5].count;++i)
{
::TextOut(hdc,0,infoAll[0].count*cyChar+infoAll[1].count*cyChar
+infoAll[2].count*cyChar+infoAll[3].count*cyChar+infoAll[4].count*cyChar+i*cyChar-
si.nPos*cyChar,infoAll[5].info[i],::lstrlen(infoAll[5].info[i]));
}
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
//##############################################################################//
// 调用MyGetInterfaceInfo获取网络相关信息的模块开始 //
// 获取本系统网络适配器数以及每个网络适配器的索引 //
//##############################################################################//
information MyGetInterfaceInfo(bool bOrder)
{
PIP_INTERFACE_INFO pInfo;
int i;
ULONG ulOutBufLen;//后面的函数调用时获取网络信息需要的缓冲区字节数
DWORD dwRetVal;
ulOutBufLen = 0;
information InfoAdapter;
InfoAdapter.count=0;
if ( GetInterfaceInfo(NULL, &ulOutBufLen) == ERROR_INSUFFICIENT_BUFFER)// 第一次调用,目的是取得需要缓冲区的大小
{
pInfo = (IP_INTERFACE_INFO *) GlobalAlloc( GPTR, ulOutBufLen);//给pInfo分配ulOutBufLen这么大的内存
if (pInfo == NULL) //分配内存失败
{
printf("Unable to allocate memory needed to call GetInterfaceInfo/n");
}
else //分配内存成功
{
dwRetVal = GetInterfaceInfo(pInfo, &ulOutBufLen); //获取信息,并存入pInfo
if (dwRetVal == NO_ERROR )//调用GetInterfaceInfo(pInfo, &ulOutBufLen)成功,且有适配器
{
//printf("/nNum Adapters: %ld/n/n", pInfo->NumAdapters);//输出网络适配器的数目
InfoAdapter.info[InfoAdapter.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoAdapter.info[InfoAdapter.count],TEXT("适配器的数目: %ld"),pInfo->NumAdapters);
InfoAdapter.count++;
for (i=0; i < (int) pInfo->NumAdapters; i++) //依次输出每个网络适配器的信息
{
// cout<<"Adapter Index["<<i<<"]:"<<pInfo->Adapter[i].Index<<endl;
InfoAdapter.info[InfoAdapter.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoAdapter.info[InfoAdapter.count],TEXT("适配器的索引: %ld"),pInfo->Adapter[i].Index);
InfoAdapter.count++;
// cout<<"Adapter Name["<<i<<"]:"<<pInfo->Adapter[i].Name<<endl;
InfoAdapter.info[InfoAdapter.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoAdapter.info[InfoAdapter.count],TEXT("适配器的名字: %s"),pInfo->Adapter[i].Name);
InfoAdapter.count++;
}
}
else if (dwRetVal == ERROR_NO_DATA)//调用GetInterfaceInfo(pInfo, &ulOutBufLen)成功,但是没有适配器
{
cout<<"There are no network adapters with IPv4 enabled on the local system/n";
}
else//调用GetInterfaceInfo(pInfo, &ulOutBufLen)失败
{
cout<<"GetInterfaceInfo failed with error:"<<dwRetVal<<"/n";
LPVOID lpMsgBuf;
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,dwRetVal,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,0,NULL ))
{
cout<<"/tError: "<<lpMsgBuf;
LocalFree( lpMsgBuf );//释放FormatMessage执行时动态分配的内容
}
}
GlobalFree(pInfo);//释放pInfo的内存空间
}
}
else//获取缓冲区大小出错
{
printf("/n调用GetInterfaceInfo函数获取网络需求缓冲区信息有错");
}
return InfoAdapter;
}
//##################################################//
// 调用MyGetInterfaceInfo获取网络相关信息的模块结束//
// 释放类型为PMIB_TCPTABLE实参的内存空间 //
//##################################################//
//##############################################################################//
// 调用MyGetNetworkParams获取网络相关信息的模块开始 //
// 获取主机名,主机域名,DNS服务器IP地址信息 //
//##############################################################################//
information MyGetNetworkParams(bool bOrder)
{
FIXED_INFO * FixedInfo;
IP_ADDR_STRING * pIPAddr;
ULONG ulOutBufLen =0;
DWORD dwRetVal;
information InfoDns;
InfoDns.count=0;
if( ERROR_BUFFER_OVERFLOW == GetNetworkParams(NULL, &ulOutBufLen ) )//获取FixedInfo需要的内存大小,并存储到ulOutBufLen中
{
FixedInfo = (FIXED_INFO *) GlobalAlloc( GPTR, ulOutBufLen );//给FixedInfo分配ulOutBufLen大小的内存空间
if(FixedInfo==0)//调用GlobalAlloc( GPTR, ulOutBufLen )函数失败
{
printf( "Unable to allocate memory needed to call GetNetworkParams/n");
}
else//调用GlobalAlloc( GPTR, ulOutBufLen )函数成功
{
dwRetVal = GetNetworkParams( FixedInfo, &ulOutBufLen );//获取网络信息,并存储到FixedInfo
if ( dwRetVal== ERROR_SUCCESS )//调用GetNetworkParams( FixedInfo, &ulOutBufLen )函数成功
{//输出各种网络信息
// printf( "/nHost Name: %s", FixedInfo -> HostName );
InfoDns.info[InfoDns.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoDns.info[InfoDns.count],TEXT("主机名字: %s"),FixedInfo -> HostName);
InfoDns.count++;
// printf( "/nDomain Name: %s", FixedInfo -> DomainName );
InfoDns.info[InfoDns.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoDns.info[InfoDns.count],TEXT("域名: %s"),FixedInfo -> DomainName);
InfoDns.count++;
// printf( "/nDNS Servers:" );
InfoDns.info[InfoDns.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoDns.info[InfoDns.count],TEXT("DNS服务器:"),NULL);
InfoDns.count++;
// printf("/n/tDnsServer.IpAddress:%s", (FixedInfo -> DnsServerList).IpAddress.String);
InfoDns.info[InfoDns.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoDns.info[InfoDns.count],TEXT(" DNS服务器的IP地址:%s"),(FixedInfo -> DnsServerList).IpAddress.String);
InfoDns.count++;
// printf("/n/tDnsServer.IpMask:%s", (FixedInfo -> DnsServerList).IpMask.String);
InfoDns.info[InfoDns.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoDns.info[InfoDns.count],TEXT(" DNS服务器的掩码:%s"),(FixedInfo -> DnsServerList).IpMask.String);
InfoDns.count++;
pIPAddr = FixedInfo -> DnsServerList.Next; //赋值IP信息给变量pIPAddr,因为用链表存储多个dns服务器信息
while ( pIPAddr )//看是否已经到了链表的结尾
{//输出ip信息
// printf( "/n/n/tDnsServer.IpAddress:%s", pIPAddr ->IpAddress.String );
InfoDns.info[InfoDns.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoDns.info[InfoDns.count],TEXT(" DNS服务器的IP地址:%s"),pIPAddr ->IpAddress.String);
InfoDns.count++;
// printf( "/n/tDnsServer.IpMask:%s", pIPAddr ->IpMask.String );
InfoDns.info[InfoDns.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoDns.info[InfoDns.count],TEXT(" DNS服务器的掩码:%s"), pIPAddr ->IpMask.String);
InfoDns.count++;
pIPAddr = pIPAddr ->Next;
}
}
else//调用GetNetworkParams( FixedInfo, &ulOutBufLen )函数失败
{
printf("/n调用GetNetworkParams函数获取网络信息有错");
//exit(1);
}
GlobalFree(FixedInfo);
}
}
else//调用GetNetworkParams函数获取网络需求缓冲区信息有错
{
printf("/n调用GetNetworkParams函数获取网络需求缓冲区信息有错");
}
return InfoDns;
}
//##################################################//
// 调用MyGetNetworkParams获取网络相关信息的模块结束//
// 释放类型为PMIB_TCPTABLE实参的内存空间 //
//##################################################//
//##############################################################################//
// 调用MyGetAdaptersInfo获取网络相关信息的模块开始 //
// 获取适配器名字,适配器名字描述,IP地址,掩码,网关地址,DHCP服务器地址信息 //
//##############################################################################//
information MyGetAdaptersInfo(bool bOrder)
{
IP_ADAPTER_INFO *pAdapterInfo=NULL,*ptempAdapterInfo=NULL;
ULONG ulOutBufLen=0;
information InfoAdapter;
InfoAdapter.count=0;
DWORD dwRetVal=GetAdaptersInfo(NULL,&ulOutBufLen);//调用GetAdaptersInfo函数获取网络需求缓冲区信息,并存到ulOutBufLen中
if(dwRetVal==ERROR_BUFFER_OVERFLOW)//调用GetAdaptersInfo(NULL,&ulOutBufLen)成功
{
pAdapterInfo = (IP_ADAPTER_INFO *) GlobalAlloc( GPTR, ulOutBufLen);//给pAdapterInfo分配ulOutBufLen大小的内存空间
if (pAdapterInfo == NULL)//调用 GlobalAlloc( GPTR, ulOutBufLen)失败
{
printf( "Unable to allocate memory needed to call GetAdaptersInfo/n");
}
else//调用 GlobalAlloc( GPTR, ulOutBufLen)成功
{
dwRetVal=GetAdaptersInfo(pAdapterInfo,&ulOutBufLen);//获取网络信息,并存储到pAdapterInfo
if(dwRetVal==ERROR_SUCCESS)//调用GetAdaptersInfo(pAdapterInfo,&ulOutBufLen)函数成功
{
ptempAdapterInfo=pAdapterInfo; //将ip信息赋值给中间量。因为采用链表方式存放ip信息,所以用中间变量读出所有ip信息
while(ptempAdapterInfo!=NULL)
{
// printf("/nAdapterName:%s", ptempAdapterInfo->AdapterName);
InfoAdapter.info[InfoAdapter.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoAdapter.info[InfoAdapter.count],TEXT("适配器名字: %s"),ptempAdapterInfo->AdapterName);
InfoAdapter.count++;
// printf("/nAdapterName Description:%s", ptempAdapterInfo->Description);
InfoAdapter.info[InfoAdapter.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoAdapter.info[InfoAdapter.count],TEXT("适配器描述: %s"),ptempAdapterInfo->Description);
InfoAdapter.count++;
// printf("/n/tAddress:%s", (ptempAdapterInfo->IpAddressList).IpAddress.String);
InfoAdapter.info[InfoAdapter.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoAdapter.info[InfoAdapter.count],TEXT("IP地址: %s"),(ptempAdapterInfo->IpAddressList).IpAddress.String);
InfoAdapter.count++;
// printf("/n/tIpMask:%s", (ptempAdapterInfo->IpAddressList).IpMask.String);
InfoAdapter.info[InfoAdapter.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoAdapter.info[InfoAdapter.count],TEXT("IP掩码: %s"),(ptempAdapterInfo->IpAddressList).IpMask.String);
InfoAdapter.count++;
// printf("/n/tGatewayList.IpAddress:%s", (ptempAdapterInfo->GatewayList).IpAddress.String);
InfoAdapter.info[InfoAdapter.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoAdapter.info[InfoAdapter.count],TEXT("网关IP地址: %s"),(ptempAdapterInfo->GatewayList).IpAddress.String);
InfoAdapter.count++;
// printf("/n/tGatewayList.IpMask:%s", (ptempAdapterInfo->GatewayList).IpMask.String);
InfoAdapter.info[InfoAdapter.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoAdapter.info[InfoAdapter.count],TEXT("网关IP掩码: %s"),(ptempAdapterInfo->GatewayList).IpMask.String);
InfoAdapter.count++;
// printf("/n/tDhcpServer.IpAddress:%s", (ptempAdapterInfo->DhcpServer).IpAddress.String);
InfoAdapter.info[InfoAdapter.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoAdapter.info[InfoAdapter.count],TEXT("DHCP服务器IP地址: %s"),(ptempAdapterInfo->DhcpServer).IpAddress.String);
InfoAdapter.count++;
// printf("/n/tDhcpServer.IpMask:%s", (ptempAdapterInfo->DhcpServer).IpMask.String);
InfoAdapter.info[InfoAdapter.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoAdapter.info[InfoAdapter.count],TEXT("DHCP服务器IP掩码: %s"),(ptempAdapterInfo->DhcpServer).IpMask.String);
InfoAdapter.count++;
// printf("/n/tPrimaryWinsServer.IpAddress:%s", (ptempAdapterInfo->PrimaryWinsServer).IpAddress.String);
InfoAdapter.info[InfoAdapter.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoAdapter.info[InfoAdapter.count],TEXT("PrimaryWinsServer.IpAddress: %s"),(ptempAdapterInfo->PrimaryWinsServer).IpAddress.String);
InfoAdapter.count++;
// printf("/n/tPrimaryWinsServer.IpMask:%s", (ptempAdapterInfo->PrimaryWinsServer).IpMask.String);
InfoAdapter.info[InfoAdapter.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoAdapter.info[InfoAdapter.count],TEXT("PrimaryWinsServer.IpMask: %s"),(ptempAdapterInfo->PrimaryWinsServer).IpMask.String);
InfoAdapter.count++;
// printf("/n/tSecondaryWinsServer.IpAddress:%s", (ptempAdapterInfo->SecondaryWinsServer).IpAddress.String);
InfoAdapter.info[InfoAdapter.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoAdapter.info[InfoAdapter.count],TEXT("SecondaryWinsServer.IpAddress: %s"),(ptempAdapterInfo->SecondaryWinsServer).IpAddress.String);
InfoAdapter.count++;
// printf("/n/tSecondaryWinsServer.IpMask:%s", (ptempAdapterInfo->SecondaryWinsServer).IpMask.String);
InfoAdapter.info[InfoAdapter.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoAdapter.info[InfoAdapter.count],TEXT("SecondaryWinsServer.IpMask: %s"),(ptempAdapterInfo->SecondaryWinsServer).IpMask.String);
InfoAdapter.count++;
ptempAdapterInfo=ptempAdapterInfo->Next;
}
}
else//调用GetAdaptersInfo(pAdapterInfo,&ulOutBufLen)函数失败
{
printf("/n调用GetAdaptersInfo函数获取IP地址信息有错");
}
GlobalFree(pAdapterInfo);//释放pAdapterInfo的内存空间
}
}
else//调用GetAdaptersInfo函数获取网络需求缓冲区信息失败
{
printf("/n调用GetAdaptersInfo函数获取网络需求缓冲区信息有错");
}
return InfoAdapter;
}
//##################################################//
// 调用MyGetAdaptersInfo获取网络相关信息的模块结束 //
// 获取适配器名字,适配器名字描述,IP地址, //
// 掩码,网关地址,DHCP服务器地址信息 //
//##################################################//
//##############################################################################//
// 调用MyGetIpAddrTable获取网络适配器数信息开始 //
// 获取适配器相关的信息 //
//##############################################################################//
information MyGetIpAddrTable(bool bOrder)
{
// Before calling AddIPAddress we use GetIpAddrTable to get
// an adapter to which we can add the IP.
PMIB_IPADDRTABLE pIPAddrTable;
WORD dwRetVal;
ULONG ulOutBufLen = 0;
information InfoAdapter;
InfoAdapter.count=0;
// 第一次调用获取缓冲区需要的大小存在变量dwSize variable
if (GetIpAddrTable(NULL, &ulOutBufLen , 0) == ERROR_INSUFFICIENT_BUFFER)
{
pIPAddrTable = (MIB_IPADDRTABLE *) GlobalAlloc ( GPTR, ulOutBufLen );
if ( pIPAddrTable )
{// 第二次调用获取实际的数据,缓冲区的大小满足条件
dwRetVal = GetIpAddrTable( pIPAddrTable, &ulOutBufLen , 0 );
if ( dwRetVal == NO_ERROR )
{
// printf("/tAddress: %s/n", inet_ntoa(*(struct in_addr*)&pIPAddrTable->table[0].dwAddr));
InfoAdapter.info[InfoAdapter.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoAdapter.info[InfoAdapter.count],TEXT("Address: %s"),inet_ntoa(*(struct in_addr*)&pIPAddrTable->table[0].dwAddr));
InfoAdapter.count++;
// printf("/tMask: %s/n", inet_ntoa(*(struct in_addr*)&pIPAddrTable->table[0].dwMask));
InfoAdapter.info[InfoAdapter.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoAdapter.info[InfoAdapter.count],TEXT("Mask: %s"),inet_ntoa(*(struct in_addr*)&pIPAddrTable->table[0].dwMask));
InfoAdapter.count++;
// printf("/tIndex: %ld/n", pIPAddrTable->table[0].dwIndex);
InfoAdapter.info[InfoAdapter.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoAdapter.info[InfoAdapter.count],TEXT("Index: %ld"), pIPAddrTable->table[0].dwIndex);
InfoAdapter.count++;
// printf("/tBCast: %ld/n", pIPAddrTable->table[0].dwBCastAddr);
InfoAdapter.info[InfoAdapter.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoAdapter.info[InfoAdapter.count],TEXT("BCast: %ld"), pIPAddrTable->table[0].dwBCastAddr);
InfoAdapter.count++;
// printf("/tReasm: %ld/n", pIPAddrTable->table[0].dwReasmSize);
InfoAdapter.info[InfoAdapter.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoAdapter.info[InfoAdapter.count],TEXT("Reasm: %ld"),pIPAddrTable->table[0].dwReasmSize);
InfoAdapter.count++;
}
else
{
printf("Call to GetIpAddrTable failed./n");
}
}
else
{
printf( "Unable to allocate memory needed to call GetIfTable/n");
}
}
else
printf("/n调用GetIpAddrTable函数获取网络适配器信息有错");
return InfoAdapter;
}
//##################################################//
// 调用MyGetIpAddrTable获取网络适配器数信息开始 //
// 获取适配器相关的信息 //
//##################################################//
//##############################################################################//
// 调用MyGetIfTable获取网络适配器数信息开始 //
// 获取适配器相关的信息比较多,可以参考数据结构MIB_IFROW各个成员的含义 //
//##############################################################################//
information MyGetIfTable(bool bOrder)
{
PMIB_IFTABLE ifTable=NULL;
PMIB_IFROW pMibIfRow=NULL;
ULONG ulOutBufLen = 0;
WORD dwRetVal;
information InfoAdapter;
InfoAdapter.count=0;
// Before calling GetIfEntry, we call GetIfTable to make
// sure there are entries to get.
// Make an initial call to GetIfTable to get the
// necessary size into dwSize
if (GetIfTable(NULL, &ulOutBufLen, 0) == ERROR_INSUFFICIENT_BUFFER)
{ // GlobalFree(ifTable);
ifTable = (MIB_IFTABLE*) GlobalAlloc( GPTR, ulOutBufLen);
if(ifTable==0)
{
printf( "Unable to allocate memory needed to call GetIfTable/n");
}
else
{
if ((dwRetVal = GetIfTable(ifTable, &ulOutBufLen, 0)) == NO_ERROR)
{
// printf("/nNumber of Local Adapter is %d",ifTable->dwNumEntries);
InfoAdapter.info[InfoAdapter.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoAdapter.info[InfoAdapter.count],TEXT("Number of Local Adapter is %d"),ifTable->dwNumEntries);
InfoAdapter.count++;
pMibIfRow = (MIB_IFROW*) GlobalAlloc( GPTR,sizeof(MIB_IFROW));
if(pMibIfRow!=0)
{
for(int i=0;i<ifTable->dwNumEntries;i++)
{
pMibIfRow->dwIndex = i+1;
if ((dwRetVal = GetIfEntry(pMibIfRow)) == NO_ERROR)
{//MIB_IFROW结构里面包含的网络适配器的信息比较多,参考msdn
// printf("/n/tDescription of Adapter[%d]: %s/n",i,pMibIfRow->bDescr);
InfoAdapter.info[InfoAdapter.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoAdapter.info[InfoAdapter.count],TEXT("Description of Adapter[%d]:%s"),i,pMibIfRow->bDescr);
InfoAdapter.count++;
}
else
{
printf("Call to GetIfEntry failed./n");
// Here you can use FormatMessage to find out why
// it failed.
}
}
GlobalFree(pMibIfRow);
}
else
{
printf( "Unable to allocate memory needed to call GetIfEntry/n");
}
}
else
{
printf("/n调用GetIfTable函数获取网络需求信息有错");
}
GlobalFree(ifTable);
}
}
else
{
printf("/n调用GetIfTable函数获取网络需求缓冲区信息有错");
}
return InfoAdapter;
}
//##################################################//
// 调用MyGetIfTable获取网络适配器数信息结束 //
// 获取适配器相关的信息比较多,可以参考 //
// 数据结构MIB_IFROW各个成员的含义 //
//##################################################//
//##############################################################################//
// 设计MyGetTcpTable函数模块开始 //
// 获取本系统网络适配器数 //
//##############################################################################//
information MyGetTcpTable(bool bOrder)
{
PMIB_TCPTABLE pTcpTable=NULL;
DWORD dwActualSize=0;
ULONG ulOutBufLen = 0;
DWORD dwRetVal = 0;
char szLocalAddr[128];
char szRemoteAddr[128];
struct in_addr IpAddr;
information InfoTcp;
InfoTcp.count=0;
if(::GetTcpTable(NULL,&dwActualSize,bOrder)==ERROR_INSUFFICIENT_BUFFER)//这一次调用是为了获取实际需要多少内存缓冲区
{
if(pTcpTable=(PMIB_TCPTABLE)::GlobalAlloc(GPTR,dwActualSize))//给pTcpTable分配dwActualSize这么大的内存空间
{
if(::GetTcpTable(pTcpTable,&dwActualSize,bOrder)==NO_ERROR)//此处获取信息并且写到pTcpTable中
{
// printf("/nTCP TABLE/tNumber of entries1: %ld",pTcpTable->dwNumEntries);
InfoTcp.info[InfoTcp.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoTcp.info[InfoTcp.count],TEXT("TCP TABLE Number of entries1: %ld"),pTcpTable->dwNumEntries);
InfoTcp.count++;
// printf("/n/t/tLoc Addr/tLoc Port/tRem Addr/tRem Port/tState");
InfoTcp.info[InfoTcp.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoTcp.info[InfoTcp.count],TEXT("Loc Addr Loc Port Rem Addr Rem Port State"),NULL);
InfoTcp.count++;
for (int i = 0; i<(int) pTcpTable->dwNumEntries; i++)//(int) pTcpTable->dwNumEntries
{
/
IpAddr.S_un.S_addr = (u_long) pTcpTable->table[i].dwLocalAddr;
// strcpy(szLocalAddr, inet_ntoa(IpAddr) );
IpAddr.S_un.S_addr = (u_long) pTcpTable->table[i].dwRemoteAddr;
// strcpy(szRemoteAddr, inet_ntoa(IpAddr) );
/
// printf(" pTcpTable->dwNumEntries的值为2:%d",pTcpTable->dwNumEntries);
// printf("/n/tTCP[%d]",i);
// printf("/t%s",szLocalAddr);
// printf("/t%d", ntohs(pTcpTable->table[i].dwLocalPort));
// printf("/t%s", szRemoteAddr);
// printf("/t%d", ntohs(pTcpTable->table[i].dwRemotePort));
// printf("/t%ld:", pTcpTable->table[i].dwState);
InfoTcp.info[InfoTcp.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoTcp.info[InfoTcp.count],TEXT("pTcpTable->dwNumEntries的值为:%d"),pTcpTable->dwNumEntries);
InfoTcp.count++;
/*
InfoTcp.info[InfoTcp.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoTcp.info[InfoTcp.count],TEXT(" TCP[%d] %s %d %s %d %ld"),
i,szLocalAddr,ntohs(pTcpTable->table[i].dwLocalPort),szRemoteAddr,
ntohs(pTcpTable->table[i].dwRemotePort),pTcpTable->table[i].dwState);
InfoTcp.count++;
*/
InfoTcp.info[InfoTcp.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoTcp.info[InfoTcp.count],TEXT(" TCP[%d] %d %d %ld"),
i,ntohs(pTcpTable->table[i].dwLocalPort),
ntohs(pTcpTable->table[i].dwRemotePort),pTcpTable->table[i].dwState);
InfoTcp.count++;
switch (pTcpTable->table[i].dwState)
{
case MIB_TCP_STATE_CLOSED:
// printf("CLOSED/n");
InfoTcp.info[InfoTcp.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoTcp.info[InfoTcp.count],TEXT("CLOSED"),NULL);
InfoTcp.count++;
break;
case MIB_TCP_STATE_LISTEN:
// printf("LISTEN/n");
InfoTcp.info[InfoTcp.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoTcp.info[InfoTcp.count],TEXT("LISTEN"),NULL);
InfoTcp.count++;
break;
case MIB_TCP_STATE_SYN_SENT:
// printf("SYN-SENT/n");
InfoTcp.info[InfoTcp.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoTcp.info[InfoTcp.count],TEXT("SYN-SENT"),NULL);
InfoTcp.count++;
break;
case MIB_TCP_STATE_SYN_RCVD:
// printf("SYN-RECEIVED/n");
InfoTcp.info[InfoTcp.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoTcp.info[InfoTcp.count],TEXT("SYN-RECEIVED"),NULL);
InfoTcp.count++;
break;
case MIB_TCP_STATE_ESTAB:
// printf("ESTABLISHED/n");
InfoTcp.info[InfoTcp.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoTcp.info[InfoTcp.count],TEXT("ESTABLISHED"),NULL);
InfoTcp.count++;
break;
case MIB_TCP_STATE_FIN_WAIT1:
// printf("FIN-WAIT-1/n");
InfoTcp.info[InfoTcp.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoTcp.info[InfoTcp.count],TEXT("FIN-WAIT-1"),NULL);
InfoTcp.count++;
break;
case MIB_TCP_STATE_FIN_WAIT2:
// printf("FIN-WAIT-2 /n");
InfoTcp.info[InfoTcp.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoTcp.info[InfoTcp.count],TEXT("FIN-WAIT-2"),NULL);
InfoTcp.count++;
break;
case MIB_TCP_STATE_CLOSE_WAIT:
// printf("CLOSE-WAIT/n");
InfoTcp.info[InfoTcp.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoTcp.info[InfoTcp.count],TEXT("CLOSE-WAIT"),NULL);
InfoTcp.count++;
break;
case MIB_TCP_STATE_CLOSING:
// printf("CLOSING/n");
InfoTcp.info[InfoTcp.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoTcp.info[InfoTcp.count],TEXT("CLOSING"),NULL);
InfoTcp.count++;
break;
case MIB_TCP_STATE_LAST_ACK:
// printf("LAST-ACK/n");
InfoTcp.info[InfoTcp.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoTcp.info[InfoTcp.count],TEXT("LAST-ACK"),NULL);
InfoTcp.count++;
break;
case MIB_TCP_STATE_TIME_WAIT:
// printf("TIME-WAIT/n");
InfoTcp.info[InfoTcp.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoTcp.info[InfoTcp.count],TEXT("TIME-WAIT"),NULL);
InfoTcp.count++;
break;
case MIB_TCP_STATE_DELETE_TCB:
// printf("DELETE-TCB/n");
InfoTcp.info[InfoTcp.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoTcp.info[InfoTcp.count],TEXT("DELETE-TCB"),NULL);
InfoTcp.count++;
break;
default:
// printf("UNKNOWN dwState value/n");
InfoTcp.info[InfoTcp.count]=(TCHAR*)malloc(MAXNUM*sizeof(TCHAR));
::wsprintf(InfoTcp.info[InfoTcp.count],TEXT("UNKNOWN dwState value"),NULL);
InfoTcp.count++;
break;
}
}
::GlobalFree(pTcpTable);
}
else
{
::GlobalFree(pTcpTable);
printf("/n获取信息并且写到pTcpTable中有错");
}
}
else
{
printf("/n调用GlobalAlloc函数有错");
}
}
else
{
printf("/n调用GetInterfaceInfo函数获取网络需求缓冲区信息有错");
}
return InfoTcp;
}
//##################################################//
// 设计MyGetTcpTable函数模块结束 //
// 获取本系统Tcp连接信息 //
//##################################################//