How do I get the current IP address I am on using VB code?
Thanks
Printable View
How do I get the current IP address I am on using VB code?
Thanks
VB Code:
'This project requires the following components: ' - a form (Form1) with a textbox (Text1, Multiline=True) ' and a command button (Command1) ' - a module (Module1) 'in Form1: Private Sub Command1_Click() Module1.Start End Sub 'In Module1: '****************************************************************** 'Created By Verburgh Peter. ' 07-23-2001 ' [email][email protected][/email] '------------------------------------- 'With this small application , you can detect the IP's installed on your computer, 'including subnet mask , BroadcastAddr.. ' 'I've wrote this because i've a programm that uses the winsock control, but, 'if you have multiple ip's installed on your pc , you could get by using the Listen ' method the wrong ip ... 'Because Winsock.Localip => detects the default ip installed on your PC , ' and in most of the cases it could be the LAN (nic) not the WAN (nic) 'So then you have to use the Bind function ,to bind to your right ip.. 'but how do you know & find that ip ? 'you can find it now by this appl.. it check's in the api.. IP Table.. '****************************************************************** Const MAX_IP = 5 'To make a buffer... i dont think you have more than 5 ip on your pc.. Type IPINFO dwAddr As Long ' IP address dwIndex As Long ' interface index dwMask As Long ' subnet mask dwBCastAddr As Long ' broadcast address dwReasmSize As Long ' assembly size unused1 As Integer ' not currently used unused2 As Integer '; not currently used End Type Type MIB_IPADDRTABLE dEntrys As Long 'number of entries in the table mIPInfo(MAX_IP) As IPINFO 'array of IP address entries End Type Type IP_Array mBuffer As MIB_IPADDRTABLE BufferLen As Long End Type Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) Public Declare Function GetIpAddrTable Lib "IPHlpApi" (pIPAdrTable As Byte, pdwSize As Long, ByVal Sort As Long) As Long Sub main() Form1.Show End Sub 'converts a Long to a string Public Function ConvertAddressToString(longAddr As Long) As String Dim myByte(3) As Byte Dim Cnt As Long CopyMemory myByte(0), longAddr, 4 For Cnt = 0 To 3 ConvertAddressToString = ConvertAddressToString + CStr(myByte(Cnt)) + "." Next Cnt ConvertAddressToString = Left$(ConvertAddressToString, Len(ConvertAddressToString) - 1) End Function Public Sub Start() Dim Ret As Long, Tel As Long Dim bBytes() As Byte Dim Listing As MIB_IPADDRTABLE Form1.Text1 = "" On Error GoTo END1 GetIpAddrTable ByVal 0&, Ret, True If Ret <= 0 Then Exit Sub ReDim bBytes(0 To Ret - 1) As Byte 'retrieve the data GetIpAddrTable bBytes(0), Ret, False 'Get the first 4 bytes to get the entry's.. ip installed CopyMemory Listing.dEntrys, bBytes(0), 4 'MsgBox "IP's found : " & Listing.dEntrys => Founded ip installed on your PC.. Form1.Text1 = Listing.dEntrys & " IP addresses found on your PC !!" & vbCrLf Form1.Text1 = Form1.Text1 & "----------------------------------------" & vbCrLf For Tel = 0 To Listing.dEntrys - 1 'Copy whole structure to Listing.. ' MsgBox bBytes(tel) & "." CopyMemory Listing.mIPInfo(Tel), bBytes(4 + (Tel * Len(Listing.mIPInfo(0)))), Len(Listing.mIPInfo(Tel)) Form1.Text1 = Form1.Text1 & "IP address : " & ConvertAddressToString(Listing.mIPInfo(Tel).dwAddr) & vbCrLf Form1.Text1 = Form1.Text1 & "IP Subnetmask : " & ConvertAddressToString(Listing.mIPInfo(Tel).dwMask) & vbCrLf Form1.Text1 = Form1.Text1 & "BroadCast IP address : " & ConvertAddressToString(Listing.mIPInfo(Tel).dwBCastAddr) & vbCrLf Form1.Text1 = Form1.Text1 & "**************************************" & vbCrLf Next 'MsgBox ConvertAddressToString(Listing.mIPInfo(1).dwAddr) Exit Sub END1: MsgBox "ERROR" End Sub
Is there an easier way to do this?
All I want is the default IP address. I don't need the subnet mask or the default gateway.
Thanks
If you are looking at only getting the IP address then add the winsock control and then the local ip is
VB Code:
Private Sub Form_Load() strLocalIP = Winsock1.LocalIP Msgbox "Your Local IP is " & strLocalIP,vbInformation,"Local IP" End Sub
Go to ...Quote:
Originally posted by indydavid32
Is there an easier way to do this?
All I want is the default IP address. I don't need the subnet mask or the default gateway.
Thanks
http://www.mvps.org/vbnet/index.html...lipaddress.htm
My parameters have suddenly changed.
Now I need to bring back the current active IP address.
Here at work the IP address on laptops will be in one range. At home the ip address will be differant.
Is there a way to find the current active IP address?
Thanks for the help.
Based on kleinma's post, here you are....
To make it easier, you would only need this on the Form's code:
and the module I've provided.VB Code:
Private Sub Command1_Click() Dim Address() As String, SubNet() As String, BroadCast() As String GetIPs Address(), SubNet(), BroadCast() MsgBox "DefaultIP: " & Address(0) End Sub
That would mean that your app needs of the Winsock to be installed on the user's machine. I don't think this is a brightest idea.... unless you're already using this control on your project.Quote:
Originally posted by rdrouill
If you are looking at only getting the IP address then add the winsock control and then the local ip is
VB Code:
Private Sub Form_Load() strLocalIP = Winsock1.LocalIP Msgbox "Your Local IP is " & strLocalIP,vbInformation,"Local IP" End Sub
or you could just shell out to a command prompt saving yourself some coding :p ...
VB Code:
Private Sub Form_Load() MsgBox GetLocalIPAddress End Sub Private Function GetLocalIPAddress() As String Shell "Command.com /c IPConfig > c:\Output.txt" Open "c:\Output.txt" For Input As #1 GetLocalIPAddress = Input(LOF(1), 1) Close #1 GetLocalIPAddress = CStr(Mid(GetLocalIPAddress, _ InStr(GetLocalIPAddress, ". . : ") + 6, Len(GetLocalIPAddress))) GetLocalIPAddress = CStr(Trim(CStr(Left(GetLocalIPAddress, _ InStr(GetLocalIPAddress, Chr(13)) - 1)))) End Function
yup.. plus i think the winsock control uses the function i posted anyway.. it is just a wrapper around the winsock API functions..Quote:
Originally posted by Mc Brain
That would mean that your app needs of the Winsock to be installed on the user's machine. I don't think this is a brightest idea.... unless you're already using this control on your project.
Thanks for all the help everyone!