以下代码显示DNS客户端中缓存的域名。当它执行到int stat = DnsGetCacheDataTable(pEntry);这一行时,能否有人帮我找出内存泄漏的原因?
PS:编译代码时请使用DNSAPI.lib。
PS:编译代码时请使用DNSAPI.lib。
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <WinDNS.h>
#include <stdarg.h>
typedef struct _DNS_CACHE_ENTRY {
struct _DNS_CACHE_ENTRY* pNext; // Pointer to next entry
PWSTR pszName; // DNS Record Name
unsigned short wType; // DNS Record Type
unsigned short wDataLength; // Not referenced
unsigned long dwFlags; // DNS Record FlagsB
} DNSCACHEENTRY, *PDNSCACHEENTRY;
typedef int(WINAPI *DNS_GET_CACHE_DATA_TABLE)(PDNSCACHEENTRY);
void UpdateDNS(void)
{
PDNSCACHEENTRY pEntry = (PDNSCACHEENTRY) malloc(sizeof(DNSCACHEENTRY));
// Loading DLL
HINSTANCE hLib = LoadLibrary(TEXT("DNSAPI.dll"));
// Get function address
DNS_GET_CACHE_DATA_TABLE DnsGetCacheDataTable = (DNS_GET_CACHE_DATA_TABLE) GetProcAddress(hLib, "DnsGetCacheDataTable");
int stat = DnsGetCacheDataTable(pEntry);
printf("stat = %d\n", stat);
pEntry = pEntry->pNext;
while (pEntry) {
wprintf(L"%s : %d \n", (pEntry->pszName), (pEntry->wType));
pEntry = pEntry->pNext;
}
free(pEntry);
}
int main(int argc, char **argv) {
while (TRUE)
{
Sleep(100);
UpdateDNS();
}
return 0;
}
PDNSCACHEENTRY
是DNSCACHEENTRY *
... 你能告诉我*
的意思,以及为什么要添加P
来表示它吗? - autistic