Results 1 to 15 of 15

Thread: Simple: how to find I.P help!

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    5

    Simple: how to find I.P help!

    Introdution:
    hi there, im new to this forum so please be nice
    I've used the search option but i can't find what i am looking for, i have also tryed to google it but i still can't find it

    The problem:
    I am trying to get into Using V.B, i have downloaded "ms visual basics 2005 express edition" from the microsoft website.

    in college we have been set a task to code something in V.B, i chose to "find your i.p".
    i've ranted many books but they dont really show me what i need to do, im guessing its quite a simple code, all i want it to do is display an I.P (i know you can go to !www.whatismyip.com".)

    !! i would like a code to simpley go into command propt and type "ipconfig" and then the results would show up on the command prompt window !!

    please help,
    Sorry if its stupidly easy or something , im a noob to VB lol
    Last edited by scottaii; Jan 17th, 2007 at 10:07 AM.

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Simple: how to find I.P help!

    Look at the Shell API.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    5

    Re: Simple: how to find I.P help!

    whats that hehehe

  4. #4
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Simple: how to find I.P help!

    If this is .Net then I can't really help much.

    For people behind a router/hub, you cannot get your external IP locally. You have to get it remotely from a website like www.showmyip.com.

    You can get your network/LAN IP, but this is not the one visible to other people on the internet.

    For people who are not behind a router then you can get your IP locally.

    You can probably do this using the Sockets class (which is similar to Winsock in VB 6). Or there might be a better way in .Net but I don't know.

    I only use VB 6.

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Simple: how to find I.P help!

    Shell is an API that alloows you to run an external (to the VB program that's using it) program. So you could shell IPConfig, send its output to a text file, then input and parse the text to find "IP Address:" and the numbers that follow it are the IP.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  6. #6
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Simple: how to find I.P help!

    There's no point in shelling it to a textfile, reading the file, then getting the data, when it can be done using the system.net namespace

    VB Code:
    1. Dim hostName As String = System.Net.Dns.GetHostName()
    2.         Dim ipaddress As String = System.Net.Dns.GetHostByName(hostName).AddressList(0).ToString
    3.  
    4.         MessageBox.Show("My Local IP Address is: " & ipaddress)
    5.         MessageBox.Show("My Hostname is: " & hostName)
    Chris

  7. #7
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    958

    Re: Simple: how to find I.P help!

    This program will return the GateWay, you could change the Shell to return the User's IP
    Attached Files Attached Files

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    5

    Re: Simple: how to find I.P help!

    chears for the feedback guys,

    so when i go to design mode in my VB 2005, i put this code in

    Public Class Form1

    Dim hostName As String = System.Net.Dns.GetHostName()
    Dim ipaddress As String = System.Net.Dns.GetHostByName(hostName).AddressList(0).ToString

    MessageBox.Show("My Local IP Address is: " & ipaddress)
    MessageBox.Show("My Hostname is: " & hostName)

    End Class

    but when i go to "play" the option it says "decleration expected" and highlights the two "MessageBox" part? im confused

  9. #9
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Simple: how to find I.P help!

    you have to put the code in a button or something else that will trigger it. just put it in the click event of a button...
    Chris

  10. #10

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    5

    Re: Simple: how to find I.P help!

    I am now using the college VB 6.0,

    i have created a "button" and i insert the code
    __________________________________________________________
    Dim hostName As String = System.Net.Dns.GetHostName()
    Dim ipaddress As String = System.Net.Dns.GetHostByName(hostName).AddressList(0).ToString

    MessageBox.Show("My Local IP Address is: " & ipaddress)
    MessageBox.Show("My Hostname is: " & hostName)
    ___________________________________________________________

    i press "play" and it displays the .exe, it then comes with an error massage saying "compile error", "sytax error" and hightlights the 1st part of the code in red

    Dim hostName As String = System.Net.Dns.GetHostName()
    Dim ipaddress As String = System.Net.Dns.GetHostByName(hostName).AddressList(0).ToString


    pleeeease help, chears for everything so far!

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Simple: how to find I.P help!

    VB 2005 (all editions) are VB.Net, and VB6 is "Classic VB".

    Despite them both having Visual Basic in their name, they are completely different languages. Code that works in one is very unlikely to work in the other - and your code is a good example of that.

  12. #12
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Simple: how to find I.P help!

    Quote Originally Posted by scottaii
    The problem:
    I am trying to get into Using V.B, i have downloaded "ms visual basics 2005 express edition" from the microsoft website.
    This is NOT VB6.^^

    If you tell people your using vb2005 you will receive code specific to that version.

    Anyway, heres how to do it in vb6, open a new project, go to project>componants in the menubar, then scroll down the list and select and check Microsoft Internet Transfer Control, then click OK, drag an Inet onto the form from the toolbox, and put this code in a button:

    VB Code:
    1. MsgBox "Your IP is: " & Inet1.OpenURL("http://www.whatismyip.org")

    This wil tell you your external IP Address.
    Chris

  13. #13

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    5

    Re: Simple: how to find I.P help!

    Thank you ALL very much for replying with excelant advise, i now have a new problem however.

    there are the screen shots of what happens


    after this
    i press the play botton and i am left with this



    as you can see there is no button to press and no i.p has been displayed
    please help , thank you very much
    hope to hear from you soon

  14. #14
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Simple: how to find I.P help!

    After you have added the INet to the form, add a button to the form, then double click the button, and add the code:

    VB Code:
    1. MsgBox "Your IP is: " & Inet1.OpenURL("http://www.whatismyip.org")

    The code must go in the Buttons click event if you want it to work when you press the button.

    The problem you had is you put the code in the Inets StateChanged event, the code needs to go somewhere where you can trigger it for example a button_click or form_load
    Chris

  15. #15
    Lively Member Nerd-Man's Avatar
    Join Date
    Dec 2006
    Location
    India
    Posts
    119

    Re: Simple: how to find I.P help!

    you can also try this codes below:

    VB Code:
    1. Private Sub Form_Load()
    2.  Text1.Text = GetAddress()
    3. End Sub
    4.  
    5. Public Function GetAddress() As String
    6.    Dim sHostName As String * 256
    7.    Dim lpHost As Long
    8.    Dim HOST   As HOSTENT
    9.    Dim dwAddr  As Long
    10.    Dim tmpAddr() As Byte
    11.    Dim i   As Integer
    12.    Dim sAddr  As String
    13.    
    14.    If Not SocketsInitialize() Then
    15.    GetAddress = ""
    16.    Exit Function
    17.    End If
    18.    If gethostname(sHostName, 256) = Socket_Error Then
    19.    GetAddress = ""
    20.    MsgBox "Windows Sockets error " & Str$(WSAGetLastError()) & _
    21.      " has occurred. Unable to successfully get Host Name."
    22.    SocketsCleanup
    23.    Exit Function
    24.    End If
    25.    sHostName = Trim$(sHostName)
    26.    lpHost = gethostbyname(sHostName)
    27.  
    28.    If lpHost = 0 Then
    29.    GetAddress = ""
    30.    MsgBox "Windows Sockets are not responding. " & _
    31.      "Unable to successfully get Host Name."
    32.    SocketsCleanup
    33.    Exit Function
    34.    End If
    35.    CopyMemory HOST, lpHost, Len(HOST)
    36.    CopyMemory dwAddr, HOST.hAddrList, 4
    37.    ReDim tmpAddr(1 To HOST.hLen)
    38.    CopyMemory tmpAddr(1), dwAddr, HOST.hLen
    39.    For i = 1 To HOST.hLen
    40.    sAddr = sAddr & tmpAddr(i) & "-"
    41.    Next
    42.    GetAddress = Mid$(sAddr, 1, Len(sAddr) - 1)
    43.    SocketsCleanup
    44. End Function
    45.  
    46. Public Function HiByte(ByVal wParam As Integer)
    47.  HiByte = wParam \ &H100 And &HFF&
    48. End Function
    49.  
    50. Public Function LoByte(ByVal wParam As Integer)
    51.  LoByte = wParam And &HFF&
    52. End Function
    53.  
    54. Public Sub SocketsCleanup()
    55.  If WSACleanup() <> ERROR_SUCCESS Then
    56.   MsgBox "Socket error occurred in Cleanup."
    57.  End If
    58. End Sub
    59.  
    60. Public Function SocketsInitialize() As Boolean
    61.    Dim WSAD As WSADATA
    62.    Dim sLoByte As String
    63.    Dim sHiByte As String
    64.    If WSAStartup(WS_VERSION_REQD, WSAD) <> ERROR_SUCCESS Then
    65.    MsgBox "The 32-bit Windows Socket is not responding."
    66.    SocketsInitialize = False
    67.    Exit Function
    68.    End If
    69.    
    70.    If WSAD.wMaxSockets < MIN_SOCKETS_REQD Then
    71.   MsgBox "This application requires a minimum of " & _
    72.     CStr(MIN_SOCKETS_REQD) & " supported sockets."
    73.   SocketsInitialize = False
    74.   Exit Function
    75.    End If
    76.  
    77.    If LoByte(WSAD.wVersion) < WS_VERSION_MAJOR Or _
    78.   (LoByte(WSAD.wVersion) = WS_VERSION_MAJOR And _
    79.    HiByte(WSAD.wVersion) < WS_VERSION_MINOR) Then
    80.    
    81.    sHiByte = CStr(HiByte(WSAD.wVersion))
    82.    sLoByte = CStr(LoByte(WSAD.wVersion))
    83.    
    84.    MsgBox "Sockets version " & sLoByte & "." & sHiByte & _
    85.     " is not supported by 32-bit Windows Sockets."
    86.    SocketsInitialize = False
    87.    Exit Function
    88.  
    89.    End If
    90.  SocketsInitialize = True
    91. End Function

    this let you see what is your ip

    but there is a more easy code to do that

    VB Code:
    1. Private Sub Form_Load()
    2.  MsgBox "My IP is: " & Winsock1.LocalIP
    3.  'MsgBox "My Host Name is: " & Winsock1.LocalHostName
    4. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width