View Single Post
  #24   Report Post  
Posted to microsoft.public.excel.programming
Peter T[_7_] Peter T[_7_] is offline
external usenet poster
 
Posts: 162
Default IP address to Hostname in Excel


wrote in message
...
Sorry - I should have pointed out that the "CopyMemory" problem occurs on
both 32 and 64 bit Office IF running on 64-bit Windows. I'm familiar with
the PtrSafe construct but I've tried adjusting the data types (LongPtr,
LongLong, etc) without success. I also emailed Randy who said he'd never
tried to make it work on 64-bit OS. Another source said that anything using
winsock32.dll on 64 bit OS won't work reliably, but I don't know if that is
true. You can get a Name from an IP using WMI but if the remote computer
isn't listening on WMI the code waits a long-time before giving up, which is
no good if its along list of computers, and they may not all be running
Windows!

I can only reiterate Randy's original works fine for me in Win10 + Excel64
the with the usual adaptations for the APIs. Without knowing what you've
changed hard to suggest why yours crashes.

All the APIs should be declared with PtrSafe.
The gethostbyaddr function should return a LongPtr and its hAddr argument
should be a LongPtr

In Randy's GetHostNameFromIP function ptrHosent and hAddress should be
LongPtr

If you need to cater for Office 2007 or earlier you'll need to include the
differences for both APIs and declared variables (in your own functions)
under
#IfVBA7
ptrSafe LongPtr version..
#Else
original code.
#End If

In passing don't declare as LongLong unless you're coding exclusively for
Office64 or under #Win64.Under VBA7 LongPtr is a Long in x32 and a LongLong
in x64.

Another tip, look for arguments in APIs starting with h (or hwnd) or include
ptr. Handles and pointers are LongPtr (ie LongLong in x64) and variables
that pass or return such values should be declared as LongPtr. APIs that
return similar will return a LongPtr, and should be returned to
appropriately declared variables.

Head your module Option Explicit and do Debug / compile in both x32 and x64

In 32 bit Office Randy's original should work fine as-is even in 64bit
Windows. Make sure you copy it directly from his site. All you need to do is
change the two text-box references to strings.

Peter T