~~求助~~~离线状态获取本机ip 离线状态(把网线断开)怎么获取本机ip阿,我的程序只得到127.0.0.1,而不是例如192.168.1.11 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果gethostname方式获得不到可以通过注册表\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\interfaces int ServerIP[4];void GetServerIP(){ WORD wVersionRequested; WSADATA wsaData; wVersionRequested = MAKEWORD( 2, 2 ); WSAStartup( wVersionRequested, &wsaData ); char szHostName[128]; if(gethostname(szHostName,128) == 0) { struct hostent * pHost; int i; int j; pHost = gethostbyname(szHostName); for(i =0;pHost!= NULL && pHost->h_addr_list[i]!=NULL; i++) { for(j = 0; j <pHost->h_length; j++) { ServerIP[j] = (unsigned int)((unsigned char*)pHost->h_addr_list[i])[j]; } }}调用这个函数后,ServerIP里就包含了本机ip,有公网ip的话就是公网ip,没公网ip的话就是本地ip aj3423(a.j.)的程序我试了,没有达到预期的效果阿,不过,也是谢谢了! loverx(烟花---俺想有系列之俺想有部车) 大侠能给个例子程序不,嘎嘎,要求有点多了,谢谢 就是对注册表表阿,然后把对应ipaddress的键值读取出来唯一注意的地方,不同的操作 系统可能路径不一样------------------------------------------------正常情况下,aj3423(a.j.) 的代码就可以了 aj3423(a.j.) 的程序在断网的情况下得到的ip是127.0.0.1,基本上gethostname方式断网情况都是这样而不是例如192.168.1.11这样的ip 注册表的方法我也试了,但有一个问题,我怎么确定机子的GUID,我的比如是{E3D57ABD-7FD6-4BBD-A210-1C2EB9B699B2},但别人的这个字符串怎么获得,要有这个vc的函数,哪位大侠发个上来阿,先行谢过~~ //----------------------------------------------------------------- // 取得所有网卡信息 //----------------------------------------------------------------- BOOL 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; DWORD dwIndex = 0; DWORD dwBufSize = 256; DWORD dwDataType; char szSubKey[256]; unsigned char szData[256]; while(RegEnumKeyEx(hKey, dwIndex++, szSubKey, &dwBufSize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS) { //AfxMessageBox(szSubKey); if(RegOpenKeyEx(hKey, szSubKey, 0, KEY_READ, &hSubKey) == ERROR_SUCCESS) { if(RegOpenKeyEx(hSubKey, "Ndi\\Interfaces", 0, KEY_READ, &hNdiIntKey) == ERROR_SUCCESS) { dwBufSize = 256; if(RegQueryValueEx(hNdiIntKey, "LowerRange", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS) { if(strcmp((char*)szData, "ethernet") == 0) // 判断是不是以太网卡 { dwBufSize = 256; if(RegQueryValueEx(hSubKey, "DriverDesc", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS) { ADAPTER_INFO *pAI = new ADAPTER_INFO; pAI->strDriverDesc = (LPCTSTR)szData; //AfxMessageBox((LPCTSTR)szData); dwBufSize = 256; if(RegQueryValueEx(hSubKey, "NetCfgInstanceID", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS) { pAI->strName = (LPCTSTR)szData; //AfxMessageBox((LPCTSTR)szData); RegGetIP(pAI, (LPCTSTR)szData); //AfxMessageBox((LPCTSTR)szData); } AdapterInfoVector.push_back(pAI); // 加入到容器中 } } } RegCloseKey(hNdiIntKey); } RegCloseKey(hSubKey); } dwBufSize = 256; } /* end of while */ RegCloseKey(hKey); /* // 可以使用GetAdaptersInfo来取得网卡信息,但其显示的名称不是很具体 ULONG ulAdapterInfoSize = sizeof(IP_ADAPTER_INFO); IP_ADAPTER_INFO *pAdapterInfoBkp, *pAdapterInfo = (IP_ADAPTER_INFO*)new char[ulAdapterInfoSize]; if( GetAdaptersInfo(pAdapterInfo, &ulAdapterInfoSize) == ERROR_BUFFER_OVERFLOW ) // 缓冲区不够大 { delete pAdapterInfo; pAdapterInfo = (IP_ADAPTER_INFO*)new char[ulAdapterInfoSize]; pAdapterInfoBkp = pAdapterInfo; } if( GetAdaptersInfo(pAdapterInfo, &ulAdapterInfoSize) == ERROR_SUCCESS ) { do { if (pAdapterInfo->Type == MIB_IF_TYPE_ETHERNET) { ADAPTER_INFO *pAI = new ADAPTER_INFO; pAI->strDriverDesc = pAdapterInfo->Description; pAI->strName = pAdapterInfo->AdapterName; RegGetIP(pAI, (LPCTSTR)pAdapterInfo->AdapterName); // 因为IP_ADAPTER_INFO中未包含掩码信息,所以干脆直接读注册表 AdapterInfoVector.push_back(pAI); } pAdapterInfo = pAdapterInfo->Next; } while(pAdapterInfo); } delete pAdapterInfoBkp; */ return TRUE; } //----------------------------------------------------------------- // 得到注册表中的IP信息 // nIndex暂时未处理 //----------------------------------------------------------------- BOOL RegGetIP(ADAPTER_INFO *pAI, LPCTSTR lpszAdapterName, int nIndex/* =0 */) { ASSERT(pAI); HKEY hKey; string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\"; strKeyName += lpszAdapterName; if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, strKeyName.c_str(), 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 = (LPCTSTR)szData; dwBufSize = 256; if(RegQueryValueEx(hKey, "SubnetMask", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS) pAI->strNetMask = (LPCTSTR)szData; dwBufSize = 256; if(RegQueryValueEx(hKey, "DefaultGateway", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS) pAI->strNetGate = (LPCTSTR)szData; RegCloseKey(hKey); return TRUE; } //----------------------------------------------------------------- // 设置注册表中的IP信息 //----------------------------------------------------------------- BOOL RegSetIP(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate) { HKEY hKey; string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\"; strKeyName += lpszAdapterName; if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, strKeyName.c_str(), 0, KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE; char mszIPAddress[100]; char mszNetMask[100]; char mszNetGate[100]; strncpy(mszIPAddress, pIPAddress, 98); strncpy(mszNetMask, pNetMask, 98); strncpy(mszNetGate, pNetGate, 98); int nIP, nMask, nGate; nIP = strlen(mszIPAddress); nMask = strlen(mszNetMask); nGate = strlen(mszNetGate); *(mszIPAddress + nIP + 1) = 0x00; nIP += 2; *(mszNetMask + nMask + 1) = 0x00; nMask += 2; *(mszNetGate + nGate + 1) = 0x00; nGate += 2; RegSetValueEx(hKey, "IPAddress", 0, REG_MULTI_SZ, (unsigned char*)mszIPAddress, nIP); RegSetValueEx(hKey, "SubnetMask", 0, REG_MULTI_SZ, (unsigned char*)mszNetMask, nMask); RegSetValueEx(hKey, "DefaultGateway", 0, REG_MULTI_SZ, (unsigned char*)mszNetGate, nGate); RegCloseKey(hKey); return TRUE; } //----------------------------------------------------------------- // 通知IP地址的改变 //----------------------------------------------------------------- BOOL NotifyIPChange(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask) { BOOL bResult = FALSE; HINSTANCE hDhcpDll; DHCPNOTIFYPROC pDhcpNotifyProc; WCHAR wcAdapterName[256]; MultiByteToWideChar(CP_ACP, 0, lpszAdapterName, -1, wcAdapterName,256); if((hDhcpDll = LoadLibrary("dhcpcsvc")) == NULL) return FALSE; if((pDhcpNotifyProc = (DHCPNOTIFYPROC)GetProcAddress(hDhcpDll, "DhcpNotifyConfigChange")) != NULL) if((pDhcpNotifyProc)(NULL, wcAdapterName, TRUE, nIndex, inet_addr(pIPAddress), inet_addr(pNetMask), 0) == ERROR_SUCCESS) bResult = TRUE; FreeLibrary(hDhcpDll); return bResult; } //----------------------------------------------------------------- // 设置IP地址 // 如果只绑定一个IP,nIndex = 0,暂时未处理一个网卡绑定多个地址 //----------------------------------------------------------------- BOOL SetIP(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate) { if(!RegSetIP(lpszAdapterName, nIndex, pIPAddress, pNetMask, pNetGate)) return FALSE; if(!NotifyIPChange(lpszAdapterName, nIndex, pIPAddress, pNetMask)) return FALSE; return TRUE; } Cfile.flush(),快速写文件 VS2005开发环境,程序退出时,有内存块被CORE DUMP,如何查找是什么变量未释放? 如何用IStream在word文档中写入信息? 简单问题,请求帮助 如何修改INT里面的项的值 容易的问题。在创建MFC工程时如果没有选CDocument\CView support那么编译成的可执行程序会出现非法操作 在98安全模式下,怎样自动加载驱动程序,或者应用程序?? 各位大虾,如何给对话框程序添加主菜单? 请教:VC+MapX做了动态图层,刷新时带动下面地图一起刷新,如何解决? VC中添加ADO控件的问题 判断焦点出错 经典问题-对话框重画
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD( 2, 2 );
WSAStartup( wVersionRequested, &wsaData ); char szHostName[128]; if(gethostname(szHostName,128) == 0) {
struct hostent * pHost;
int i;
int j;
pHost = gethostbyname(szHostName); for(i =0;pHost!= NULL && pHost->h_addr_list[i]!=NULL; i++) {
for(j = 0; j <pHost->h_length; j++) {
ServerIP[j] = (unsigned int)((unsigned char*)pHost->h_addr_list[i])[j]; }
}
}调用这个函数后,ServerIP里就包含了本机ip,有公网ip的话就是公网ip,没公网ip的话就是本地ip
正常情况下,aj3423(a.j.) 的代码就可以了
// 取得所有网卡信息
//-----------------------------------------------------------------
BOOL 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;
DWORD dwIndex = 0;
DWORD dwBufSize = 256;
DWORD dwDataType;
char szSubKey[256];
unsigned char szData[256];
while(RegEnumKeyEx(hKey, dwIndex++, szSubKey, &dwBufSize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
{
//AfxMessageBox(szSubKey);
if(RegOpenKeyEx(hKey, szSubKey, 0, KEY_READ, &hSubKey) == ERROR_SUCCESS)
{
if(RegOpenKeyEx(hSubKey, "Ndi\\Interfaces", 0, KEY_READ, &hNdiIntKey) == ERROR_SUCCESS)
{
dwBufSize = 256;
if(RegQueryValueEx(hNdiIntKey, "LowerRange", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
{
if(strcmp((char*)szData, "ethernet") == 0) // 判断是不是以太网卡
{
dwBufSize = 256;
if(RegQueryValueEx(hSubKey, "DriverDesc", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
{
ADAPTER_INFO *pAI = new ADAPTER_INFO;
pAI->strDriverDesc = (LPCTSTR)szData;
//AfxMessageBox((LPCTSTR)szData);
dwBufSize = 256;
if(RegQueryValueEx(hSubKey, "NetCfgInstanceID", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
{
pAI->strName = (LPCTSTR)szData;
//AfxMessageBox((LPCTSTR)szData);
RegGetIP(pAI, (LPCTSTR)szData);
//AfxMessageBox((LPCTSTR)szData);
}
AdapterInfoVector.push_back(pAI); // 加入到容器中
}
}
}
RegCloseKey(hNdiIntKey);
}
RegCloseKey(hSubKey);
}
dwBufSize = 256;
} /* end of while */
RegCloseKey(hKey);
/*
// 可以使用GetAdaptersInfo来取得网卡信息,但其显示的名称不是很具体
ULONG ulAdapterInfoSize = sizeof(IP_ADAPTER_INFO);
IP_ADAPTER_INFO *pAdapterInfoBkp, *pAdapterInfo = (IP_ADAPTER_INFO*)new char[ulAdapterInfoSize];
if( GetAdaptersInfo(pAdapterInfo, &ulAdapterInfoSize) == ERROR_BUFFER_OVERFLOW ) // 缓冲区不够大
{
delete pAdapterInfo;
pAdapterInfo = (IP_ADAPTER_INFO*)new char[ulAdapterInfoSize];
pAdapterInfoBkp = pAdapterInfo;
}
if( GetAdaptersInfo(pAdapterInfo, &ulAdapterInfoSize) == ERROR_SUCCESS )
{
do {
if (pAdapterInfo->Type == MIB_IF_TYPE_ETHERNET)
{
ADAPTER_INFO *pAI = new ADAPTER_INFO;
pAI->strDriverDesc = pAdapterInfo->Description;
pAI->strName = pAdapterInfo->AdapterName;
RegGetIP(pAI, (LPCTSTR)pAdapterInfo->AdapterName); // 因为IP_ADAPTER_INFO中未包含掩码信息,所以干脆直接读注册表
AdapterInfoVector.push_back(pAI);
}
pAdapterInfo = pAdapterInfo->Next;
} while(pAdapterInfo);
}
delete pAdapterInfoBkp;
*/
return TRUE;
}
// 得到注册表中的IP信息
// nIndex暂时未处理
//-----------------------------------------------------------------
BOOL RegGetIP(ADAPTER_INFO *pAI, LPCTSTR lpszAdapterName, int nIndex/* =0 */)
{
ASSERT(pAI);
HKEY hKey;
string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
strKeyName += lpszAdapterName;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
strKeyName.c_str(),
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 = (LPCTSTR)szData;
dwBufSize = 256;
if(RegQueryValueEx(hKey, "SubnetMask", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
pAI->strNetMask = (LPCTSTR)szData;
dwBufSize = 256;
if(RegQueryValueEx(hKey, "DefaultGateway", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
pAI->strNetGate = (LPCTSTR)szData;
RegCloseKey(hKey);
return TRUE;
}
//-----------------------------------------------------------------
// 设置注册表中的IP信息
//-----------------------------------------------------------------
BOOL RegSetIP(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate)
{
HKEY hKey;
string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
strKeyName += lpszAdapterName;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
strKeyName.c_str(),
0,
KEY_WRITE,
&hKey) != ERROR_SUCCESS)
return FALSE;
char mszIPAddress[100];
char mszNetMask[100];
char mszNetGate[100];
strncpy(mszIPAddress, pIPAddress, 98);
strncpy(mszNetMask, pNetMask, 98);
strncpy(mszNetGate, pNetGate, 98);
int nIP, nMask, nGate;
nIP = strlen(mszIPAddress);
nMask = strlen(mszNetMask);
nGate = strlen(mszNetGate);
*(mszIPAddress + nIP + 1) = 0x00;
nIP += 2;
*(mszNetMask + nMask + 1) = 0x00;
nMask += 2;
*(mszNetGate + nGate + 1) = 0x00;
nGate += 2;
RegSetValueEx(hKey, "IPAddress", 0, REG_MULTI_SZ, (unsigned char*)mszIPAddress, nIP);
RegSetValueEx(hKey, "SubnetMask", 0, REG_MULTI_SZ, (unsigned char*)mszNetMask, nMask);
RegSetValueEx(hKey, "DefaultGateway", 0, REG_MULTI_SZ, (unsigned char*)mszNetGate, nGate);
RegCloseKey(hKey);
return TRUE;
}
//-----------------------------------------------------------------
// 通知IP地址的改变
//-----------------------------------------------------------------
BOOL NotifyIPChange(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask)
{
BOOL bResult = FALSE;
HINSTANCE hDhcpDll;
DHCPNOTIFYPROC pDhcpNotifyProc;
WCHAR wcAdapterName[256];
MultiByteToWideChar(CP_ACP, 0, lpszAdapterName, -1, wcAdapterName,256);
if((hDhcpDll = LoadLibrary("dhcpcsvc")) == NULL)
return FALSE;
if((pDhcpNotifyProc = (DHCPNOTIFYPROC)GetProcAddress(hDhcpDll, "DhcpNotifyConfigChange")) != NULL)
if((pDhcpNotifyProc)(NULL, wcAdapterName, TRUE, nIndex, inet_addr(pIPAddress), inet_addr(pNetMask), 0) == ERROR_SUCCESS)
bResult = TRUE;
FreeLibrary(hDhcpDll);
return bResult;
}
//-----------------------------------------------------------------
// 设置IP地址
// 如果只绑定一个IP,nIndex = 0,暂时未处理一个网卡绑定多个地址
//-----------------------------------------------------------------
BOOL SetIP(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate)
{
if(!RegSetIP(lpszAdapterName, nIndex, pIPAddress, pNetMask, pNetGate))
return FALSE;
if(!NotifyIPChange(lpszAdapterName, nIndex, pIPAddress, pNetMask))
return FALSE;
return TRUE;
}