好友
阅读权限10
听众
最后登录1970-1-1
|
环境: win10 + vs2017
问题: 在第二次pIpAdapterInfo = (PIP_ADAPTER_INFO)new BYTE[stSize]; 有内存泄露
#include <Iphlpapi.h>
#pragma comment(lib,"Iphlpapi.lib") //需要添加Iphlpapi.lib库
void CTestRegeditOperationDlg::OnBnClickedButton5()
{
// TODO: 在此添加控件通知处理程序代码
PIP_ADAPTER_INFO pIpAdapterInfo = new IP_ADAPTER_INFO();
unsigned long stSize = sizeof(IP_ADAPTER_INFO);
int nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize);
if (ERROR_BUFFER_OVERFLOW == nRel)
{
delete pIpAdapterInfo;
pIpAdapterInfo = (PIP_ADAPTER_INFO)new BYTE[stSize];
//memset(pIpAdapterInfo, NULL, stSize);
nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize);
}
if (ERROR_SUCCESS == nRel) {
while (pIpAdapterInfo)
{
//根据描述过滤
//"wmware" "virtualbox"
char szDesc[MAX_PATH] = { 0 };
strcpy(szDesc, pIpAdapterInfo->Description);
_strlwr(szDesc);
if (strstr(szDesc, "vmware") != NULL || strstr(szDesc, "virtualbox") != NULL ||
strstr(szDesc, "VPN") != NULL || strstr(szDesc, "VMware") != NULL || strstr(szDesc, "Virtualbox") != NULL ||
strstr(szDesc, "VMnet") != NULL) {
pIpAdapterInfo = pIpAdapterInfo->Next;
continue;
}
//根据网卡类型过滤:网卡类型:MIB_IF_TYPE_ETHERNET:有线网卡 IF_TYPE_IEEE80211:无线网卡
if (pIpAdapterInfo->Type != MIB_IF_TYPE_ETHERNET && pIpAdapterInfo->Type != IF_TYPE_IEEE80211)
{
pIpAdapterInfo = pIpAdapterInfo->Next;
continue;
}
//可能网卡有多IP,因此通过循环去判断
IP_ADDR_STRING *pIpAddrString = &(pIpAdapterInfo->IpAddressList);
CString csIpString;
do {
csIpString = pIpAddrString->IpAddress.String;
if (strcmp("192.168.80.153", csIpString.GetBuffer(100)) == 0) {
char szMac[MAX_PATH] = { 0 };
for (DWORD i = 0; i < pIpAdapterInfo->AddressLength; i++)
{
if (i < pIpAdapterInfo->AddressLength - 1)
{
char szItem[MAX_PATH] = { 0 };
sprintf(szItem, "%02X:", pIpAdapterInfo->Address);
strcat(szMac, szItem);
}
else
{
char szItem[MAX_PATH] = { 0 };
sprintf(szItem, "%02X\n", pIpAdapterInfo->Address);
strcat(szMac, szItem);
}
}
MessageBox(szMac);
break;
}
pIpAddrString = pIpAddrString->Next;
} while (pIpAddrString);
pIpAdapterInfo = pIpAdapterInfo->Next;
}
}
else {
MessageBox("获取网卡信息失败!");
}
delete pIpAdapterInfo;
pIpAdapterInfo = NULL;
} |
-
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|