📄 locinfo.cpp
字号:
// LocInfo.cpp: implementation of the LocInfo class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "LocInfo.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
LocInfo::LocInfo()
{
m_nCurIndex = -1;
}
LocInfo::~LocInfo()
{
}
bool LocInfo::GetLocalIpAddr( char *pIP, int size )
{
if (!Requery())
return false;
if ( m_nCurIndex != 0 || size < 16 )
return false;
if ( m_paAdapterInfo[0]->strIP.IsEmpty() )
return false;
strcpy( pIP, m_paAdapterInfo[0]->strIP.GetBuffer(0));
m_nCurIndex = 1;
return true;
}
bool LocInfo::GetNextLocalIpAddr( char *pIP, int size )
{
if ( m_nCurIndex <= 0 || m_nCurIndex >= m_paAdapterInfo.GetSize() || size < 16 )
return false;
if ( m_paAdapterInfo[m_nCurIndex]->strIP.IsEmpty() )
return false;
strcpy( pIP, m_paAdapterInfo[m_nCurIndex]->strIP.GetBuffer(0));
m_nCurIndex++;
return true;
}
bool LocInfo::Requery()
{
m_paAdapterInfo.RemoveAll();
return GetAdapterInfo();
}
//-----------------------------------------------------------------
// 取得所有网卡信息
//-----------------------------------------------------------------
bool LocInfo::GetAdapterInfo()
{
// 这里的代码适合WINDOWS2000,对于NT需要读取
//HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards
HKEY hKey, hSubKey, hNdiIntKey;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"System\\CurrentControlSet\\Control\\Class\\{4d36e972-e325-11ce-bfc1-08002be10318}",
0, KEY_READ, &hKey) != ERROR_SUCCESS)
{
return FALSE;
}
bool bGetInf = false;
//AfxMessageBox("Error");
DWORD dwIndex = 0;
DWORD dwBufSize = 256;
DWORD dwDataType;
char szSubKey[256];
unsigned char szData[256];
//遍历hKey的所有子键
while(RegEnumKeyEx(hKey, dwIndex++, szSubKey, &dwBufSize, 0, 0, 0, 0) == ERROR_SUCCESS)
{
// 打开第一层主键
if ( RegOpenKeyEx(hKey, szSubKey, 0, KEY_READ, &hSubKey ) // hSubKey
!= ERROR_SUCCESS )
{
dwBufSize = 256;
continue;
}
// 打开更深一层主键
if ( RegOpenKeyEx(hSubKey, "Ndi\\Interfaces", 0, KEY_READ, &hNdiIntKey) // hNdiIntKey
!= ERROR_SUCCESS )
{
dwBufSize = 256;
RegCloseKey( hSubKey );
continue;
}
dwBufSize = 256;
if ( RegQueryValueEx(hNdiIntKey, "LowerRange", 0, &dwDataType, szData, &dwBufSize)
!= ERROR_SUCCESS )
{
dwBufSize = 256;
RegCloseKey( hNdiIntKey );
RegCloseKey( hSubKey );
continue;
}
if ( strcmp((char*)szData, "ethernet") != 0 ) // 判断是不是以太网卡
{
dwBufSize = 256;
RegCloseKey( hNdiIntKey );
RegCloseKey( hSubKey );
continue;
}
dwBufSize = 256;
if ( RegQueryValueEx(hSubKey, "DriverDesc", 0, &dwDataType, szData, &dwBufSize)
!= ERROR_SUCCESS )
{
dwBufSize = 256;
RegCloseKey( hNdiIntKey );
RegCloseKey( hSubKey );
continue;
}
ADAPTER_INFO *pAI = new ADAPTER_INFO; // pAI
pAI->strDriverDesc = (LPCTSTR)szData; // pAI->strDriverDesc
dwBufSize = 256;
if ( RegQueryValueEx(hSubKey, "NetCfgInstanceID", 0, &dwDataType, szData, &dwBufSize)
!= ERROR_SUCCESS )
{
dwBufSize = 256;
RegCloseKey( hNdiIntKey );
RegCloseKey( hSubKey );
continue;
}
pAI->strName = (LPCTSTR)szData; // pAI->strName
RegGetIP(pAI); //, (LPCTSTR)szData);
if ( m_paAdapterInfo.Add(pAI) >= 0 ) // 加入到容器中
{
m_nCurIndex = 0; //
bGetInf = true;
}
RegCloseKey( hNdiIntKey );
RegCloseKey( hSubKey );
dwBufSize = 256; //
} /* end of while */
RegCloseKey(hKey);
return bGetInf;
}
//-----------------------------------------------------------------
// 得到注册表中的IP信息
// nIndex暂时未处理
//-----------------------------------------------------------------
bool LocInfo::RegGetIP( ADAPTER_INFO *pAI ) //, LPCTSTR lpszAdapterName, int nIndex/* =0 */)
{
ASSERT(pAI);
CString strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
strKeyName += pAI->strName;//lpszAdapterName; // {A9A3D16F-0F2E-4C47-B5CF-1AE8753094D0}
HKEY hKey;
if ( RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
strKeyName,
0,
KEY_READ,
&hKey ) != ERROR_SUCCESS )
return false;
unsigned char szData[256];
DWORD dwDataType, dwBufSize;
dwBufSize = 256;
if(RegQueryValueEx(hKey, "IPAddress", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
pAI->strIP = szData;
dwBufSize = 256;
if(RegQueryValueEx(hKey, "SubnetMask", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
pAI->strNetMask = szData;
dwBufSize = 256;
if(RegQueryValueEx(hKey, "DefaultGateway", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
pAI->strNetGate = szData;
dwBufSize = 256;
if(RegQueryValueEx(hKey, "NameServer", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
pAI->strDNSSever = szData;
RegCloseKey(hKey);
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -