I need a code to know my own IP address. Is this possible ??
Printable View
I need a code to know my own IP address. Is this possible ??
This method isn't 100% always acurate...but:
Hope that helps,Code:'Add a winsock control and a textbox, and add this code
Text1.Text = Winsock1.LocalIP
D!m
Is there any particular reason it isn't 100% effective.
Aside from the fact that you add the overhead of the control to your project, it's slick.
if ur using a router,
it will show an Internal IP:
192.168.*.*
Instead of your External IP:
62.31.*.*
The only "Real" way would be to go to a website, that gets your REAL IP, and then get it from its contents or something :shrugs:
: Such as a PHP's
PHP Code:<?php
$remoteadd = GetEnv("Remote_Addr");
echo "$remoteadd";
?>
U can get the contents from that then,,
But u need a webserver to read it from ETC :@
Would be more "Wise" then the "Winsock1".
But, If their using a Proxy, it will return dat :\
If you are using a router, The IP given by the website would be the address of the router. The IP of the local computer IS 192.168....
Z.
yeh, but im guessing that he mite want his "External" IP ;)
If it is internal IP Address's...
: Looks for Code :
Theres this API:
Call it like:
VB Code:
Module1.Start
In a module Called "Module1"
VB Code:
'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 = 6 '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() frmIPCheck.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 Dim StaredLine As String StaredLine = vbCrLf & "**********************************************************" frmIPCheck.txtIPCheck = "" 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 frmIPCheck.lblIPsFound = Listing.dEntrys & " IP addresses found on your PC!" & vbCrLf frmIPCheck.txtIPCheck = StaredLine & vbCrLf & 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)) frmIPCheck.txtIPCheck = frmIPCheck.txtIPCheck & "IP address: " & ConvertAddressToString(Listing.mIPInfo(Tel).dwAddr) & vbCrLf frmIPCheck.txtIPCheck = frmIPCheck.txtIPCheck & "IP Subnetmask: " & ConvertAddressToString(Listing.mIPInfo(Tel).dwMask) & vbCrLf frmIPCheck.txtIPCheck = frmIPCheck.txtIPCheck & "BroadCast IP address: " & ConvertAddressToString(Listing.mIPInfo(Tel).dwBCastAddr) & vbCrLf frmIPCheck.txtIPCheck = frmIPCheck.txtIPCheck & StaredLine & vbCrLf & vbCrLf Next 'MsgBox ConvertAddressToString(Listing.mIPInfo(1).dwAddr) Exit Sub END1: MsgBox "ERROR" End Sub
BTW::
frmIPCheck.txtIPCheck
This needs to be changed to:
MYForm.MyTextBox
Text box needs to be multi lined,,,
Will return 6 IP Addresses//
VB Code:
frmIPCheck.txtIPCheck = frmIPCheck.txtIPCheck & "IP address: " & ConvertAddressToString(Listing.mIPInfo(Tel).dwAddr) & vbCrLf frmIPCheck.txtIPCheck = frmIPCheck.txtIPCheck & "IP Subnetmask: " & ConvertAddressToString(Listing.mIPInfo(Tel).dwMask) & vbCrLf frmIPCheck.txtIPCheck = frmIPCheck.txtIPCheck & "BroadCast IP address: " & ConvertAddressToString(Listing.mIPInfo(Tel).dwBCastAddr) & vbCrLf
That will return IP | Subnet Mask | Broadcast Address
VB Code:
' Change to this for just IP: frmIPCheck.txtIPCheck = frmIPCheck.txtIPCheck & "IP address: " & ConvertAddressToString(Listing.mIPInfo(Tel).dwAddr) & vbCrLf
To add it to a combo box, Change to:
VB Code:
' Change to this for just IP: frmIPCheck.MyCombo.AddItem ConvertAddressToString(Listing.mIPInfo(Tel).dwAddr)
Its all really cool, but this only returns LOCAL ADDRESSES. (No WinSock or that needed,
U Can return however many you need too as well :),
VB Code:
Const MAX_IP = 6
Alter this to make it less or more...
:)