View Single Post
  #38   Report Post  
Posted to microsoft.public.excel.programming
Andrey G Andrey G is offline
external usenet poster
 
Posts: 7
Default IP address to Hostname in Excel

On Wednesday, November 11, 2020 at 11:38:24 AM UTC, Peter T wrote:
"Auric__" wrote in message Andrey G wrote:

Firstly, thank you very much for a prompt reply - I was afraid that this
thread might be dead.


Best thing to do is to make a new thread, and mention the subject of the
thread you want to reference (e.g. "see also the thread from January 2009,
subject 'Why can't I?'"). Replying to a 3-year-old thread is not the way
to
go.

I will try to change the pointer to 8 bit


Argh! Bytes! 8 bytes! 64 bits.

Oh dear, yes indeed bytes not bits. Hope you noticed I did at get it right,
implicitly at least, with the var-name 'gcPtrBytes' I suggested:)

Also (Andrey), again perhaps slightly pedantic, what you will be changing is
the last argument for CopyMemory, namely the length or number of bytes to
copy, from 4 in x32 to 8 in x64.

Peter T





Hi Peter,

I've tried running code with the last argument for CopyMemory changed, but Excel still crashed. Please have a look below at the details of the problem:


Environment:
Windows 10 Enterprise, 64-bit
Office Professional Plus 2016, 64-bit


'Declarations

Private Declare PtrSafe Function gethostbyname Lib "wsock32.dll" _
(ByVal hostname As String) As Long

Private Declare PtrSafe Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(xDest As Any, _
xSource As Any, _
ByVal nbytes As LongPtr)

Private Declare PtrSafe Function lstrlenA Lib "kernel32" _
(lpString As Any) As Long

Private Declare PtrSafe Function WSAStartup Lib "wsock32.dll" _
(ByVal wVersionRequired As Long, _
lpWSADATA As WSADATA) As Long

Private Declare PtrSafe Function WSACleanup Lib "wsock32.dll" () As Long

Private Declare PtrSafe Function inet_ntoa Lib "wsock32.dll" _
(ByVal addr As Long) As Long

Private Declare PtrSafe Function lstrcpyA Lib "kernel32" _
(ByVal RetVal As String, _
ByVal Ptr As Long) As Long

Private Declare PtrSafe Function gethostname Lib "wsock32.dll" _
(ByVal szHost As String, _
ByVal dwHostLen As Long) As Long



' String sHostName is passed to GetIPFromHostName function correctly


Private Function GetIPFromHostName(ByVal sHostName As String) As String

'converts a host name to an IP address

Dim nbytes As LongPtr
Dim ptrHosent As LongPtr 'address of HOSENT structure
Dim ptrName As LongPtr 'address of name pointer
Dim ptrAddress As LongPtr 'address of address pointer
Dim ptrIPAddress As LongPtr
Dim ptrIPAddress2 As LongPtr

ptrHosent = gethostbyname(sHostName & vbNullChar)

If ptrHosent < 0 Then

'assign pointer addresses and offset

'Null-terminated list of addresses for the host.
'The Address is offset 12 bytes from the start of
'the HOSENT structure. Note: Here we are retrieving
'only the first address returned. To return more than
'one, define sAddress as a string array and loop through
'the 4-byte ptrIPAddress members returned. The last
'item is a terminating null. All addresses are returned
'in network byte order.
ptrAddress = ptrHosent + 12

'get the IP address
CopyMemory ptrAddress, ByVal ptrAddress, 8 '<----- Excel crashes on this line!
CopyMemory ptrIPAddress, ByVal ptrAddress, 8
CopyMemory ptrIPAddress2, ByVal ptrIPAddress, 8

GetIPFromHostName = GetInetStrFromPtr(ptrIPAddress2)

End If

End Function


Crash details:
Faulting application name: EXCEL.EXE, version: 16.0.5071.1000, time stamp: 0x5f629c9d Faulting module name: ntdll.dll, version: 10.0.18362.1139, time stamp: 0x468a1bf2 Exception code: 0xc0000028

KR
Andrey