The code, as shown, contains a potentially dangerous memory reference. The following addresses the problem by clearing the szHostName buffer, using an explicit buffer size instead of the strlen() function, and by checking the ptr for NULL before using it.
#define BUFFER_SIZE 200
CString strFullName = NetResource->lpRemoteName;
memset(szHostName, '\0', BUFFER_SIZE);
gethostname( szHostName, BUFFER_SIZE);
host = gethostbyname(strFullName);
if(host == NULL) continue;
ptr = (struct in_addr *) host->h_addr_list;
ASSERT(NULL != ptr);
if (NULL != ptr)
// Eg. 126.96.36.199 split up like this.
int a = ptr->S_un.S_un_b.s_b1; // 211
int b = ptr->S_un.S_un_b.s_b2; // 40
int c = ptr->S_un.S_un_b.s_b3; // 35
int d = ptr->S_un.S_un_b.s_b4; // 76
Many of you have problems with the size of the buffer. You can�t find all the Pcs of the total net and the solution you provide is to make bigger the size of the buffer. Ther is one more razonable metod to do this. You can do the same in a dinamic way like this:
DWORD BufferSize = sizeof(NETRESOURCE);
LPVOID Buffer = new char[BufferSize];
DWORD Count = 0xFFFFFFFF;
dwResult=WNetEnumResource( hEnum, &Count, Buffer, &BufferSize );
NetResource = (NETRESOURCE*)Buffer;
///Do here what you want with the netresource
You are working on the NetBios Host name.
What if you have the ip-address first.
How can you get that information.
Ok i know the function 'gethostbyaddr', but
what if the computer, you wanna retrieve the hostname from,
is shut down??
If anybody knows a answer or how to talk with a dns server
it would be great if you write me a mail :-).
5.12.2002 (if the date is older then one year,
you don't need to write me then ;) )