2011年9月16日金曜日

VB6でIPアドレスを取得する方法

IPアドレスを取得するには下記の通りですある。

' 前処理----------------------------------------------------
Option Explicit

Private Type WSADATA
wVersion As Integer
wHighVersion As Integer
szDescription As String * 257
szSystemStatus As String * 129
iMaxSockets As Integer
iMaxUdpDg As Integer
lpVendorInfo As Long
End Type

Private Type hostent
h_name As Long
h_aliases As Long
h_addrtype As Integer
h_length As Integer
h_addr_list As Long
End Type

Private Declare Sub vbMemMove1 Lib "kernel32" Alias "RtlMoveMemory" & _
(dst As Any, ByVal src As Long, ByVal num As Long)
Private Declare Sub vbMemMove2 Lib "kernel32" Alias "RtlMoveMemory" & _
(dst As Long, ByVal src As Long, ByVal num As Long)

Private Declare Function gethostname Lib "wsock32.dll" & _
(ByVal Name As String, ByVal namelen As Long) As Long
Private Declare Function gethostbyname Lib "wsock32.dll" (ByVal Name As String) As Long
Private Declare Function WSAStartup Lib "wsock32.dll" & _
(ByVal wVersionRequested As Integer, lpWSAData As Any) As Long
Private Declare Function WSACleanup Lib "wsock32.dll" () As Long
Private Declare Function WSAGetLastError Lib "wsock32.dll" () As Integer
' 前処理 ここまで-------------------------------------------------


' IPアドレスを取得----------------------------------------------------
Public Function GetIpAddress() As String

Dim wsa As WSADATA
Dim sName As String * 65
Dim he As hostent
Dim p As Long
Dim b(3) As Byte

If WSAStartup(&H101, wsa) <> 0 Then Exit Function

If gethostname(sName, 64) = 0 Then
p = gethostbyname(sName)
If p <> 0 Then
vbMemMove1 he, p, 16
vbMemMove2 p, he.h_addr_list, 4
vbMemMove1 b(0), p, 4
GetIpAddress = b(0) & "." & b(1) & "." & b(2) & "." & b(3)
End If
End If

WSACleanup

End Function
'-----------------------------------------------------------------------
VB6 IPアドレス

0 件のコメント:

コメントを投稿