CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2008
    Location
    Pakistan
    Posts
    41

    Question how to get IP Address in VB6?

    i want to get MY computer's IP Address and computer's Name in vb6 form run time and return to database.how this can i do ?

  2. #2
    Join Date
    Aug 2007
    Posts
    445

    Re: how to get IP Address in VB6?

    Static IP Address or Dynamic IP Address?

    please rate the post if this is useful. And also never forget to mark the thread as [Resolved] when your problem is solved

  3. #3
    Join Date
    Apr 2008
    Location
    Pakistan
    Posts
    41

    Question Re: how to get IP Address in VB6?

    currently static ip but in near future i required dynamic ip ... please explane both
    thanks

  4. #4
    Join Date
    Aug 2007
    Posts
    445

    Re: how to get IP Address in VB6?

    this is for static IP



    add Microsoft Winsock Control 6.0 [i]MSWINSCK.ocx into component and use this code
    Code:
    Private Sub Command1_Click()
    MsgBox Winsock1.LocalIP
    End Sub

    please rate the post if this is useful. And also never forget to mark the thread as [Resolved] when your problem is solved

  5. #5
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: how to get IP Address in VB6?

    In case you don't want to use Winsock. You can try your hands on GetIpAddrTable API. Here is a sample code
    http://www.source-code.biz/snippets/vbasic/8.htm

  6. #6
    Join Date
    Aug 2007
    Posts
    445

    Re: how to get IP Address in VB6?

    here is also a VB Script example. paste it in notepad and save it as. FileName.vbs

    Code:
    '****************************************************************
    '  File:    getipname.vbs  (WSH for VBscript)
    '  Author:           M. Gallant    09/30/2001
    '
    '  Based on script by M. Harris & T. Lavedas:
    '  posted to: microsoft.public.scripting.vbscript  2000/07/21  
    
    '  Reads IP addresses via:
    '    ipconfig.exe  (NT4 and Win2000)
    '    winipcfg.exe  (Win95)
    '  For NT4, Win2000 resolves IP addresses to FQDN names via:
    '     nslookup.exe     (with default DNS server)
    ' 
    '****************************************************************
    'Modified 2/2/02 to just show the IP addresses - Doug Knox
    'Original script located at:  http://home.istar.ca/~neutron/wsh/IPInfo/getipname.html
    arAddresses = GetIPAddresses()
    
    info = ""
    
    for each ip in arAddresses
        info = info & ip & vbTab & GetFQDN(ip) & vbCR
    next
    
      WScript.echo info
    
    Function GetFQDN(ipaddress)
    '====
    ' Returns Fully Qualified Domain Name
    ' from reverse DNS lookup via nslookup.exe
    ' only implemented for NT4, 2000
    '====
      set sh = createobject("wscript.shell")
      set fso = createobject("scripting.filesystemobject")
      Set Env = sh.Environment("PROCESS")
    
      if Env("OS") = "Windows_NT" then
        workfile = fso.gettempname
        sh.run "%comspec% /c nslookup " & ipaddress & "  > " & workfile,0,true
       set sh = nothing
       set ts = fso.opentextfile(workfile)
       data = split(ts.readall,vbcr)
       ts.close
       set ts = nothing
       fso.deletefile workfile
       set fso = nothing
      for n = 0 to ubound(data)
        if instr(data(n),"Name") then
          parts = split(data(n),":")
            hostname= trim(cstr(parts(1)))
           Exit For
        end if
        hostname = "could not resolve IP address"
      next
        GetFQDN = hostname
      else
       set sh = nothing
       set fso = nothing
       GetFQDN = ""
      end if
    End Function
    
    
    Function GetIPAddresses()
    '=====
    ' Returns array of IP Addresses as output
    ' by ipconfig or winipcfg...
    '
    ' Win98/WinNT have ipconfig (Win95 doesn't)
    ' Win98/Win95 have winipcfg (WinNt doesn't)
    '
    ' Note: The PPP Adapter (Dial Up Adapter) is
    ' excluded if not connected (IP address will be 0.0.0.0)
    ' and included if it is connected.
    '=====
      set sh = createobject("wscript.shell")
      set fso = createobject("scripting.filesystemobject")
    
      Set Env = sh.Environment("PROCESS")
      if Env("OS") = "Windows_NT" then
        workfile = fso.gettempname
        sh.run "%comspec% /c ipconfig > " & workfile,0,true
      else
        'winipcfg in batch mode sends output to
        'filename winipcfg.out
        workfile = "winipcfg.out"
        sh.run "winipcfg /batch" ,0,true
      end if
      set sh = nothing
      set ts = fso.opentextfile(workfile)
      data = split(ts.readall,vbcr)
      ts.close
      set ts = nothing
      fso.deletefile workfile
      set fso = nothing
      arIPAddress = array()
      index = -1
      for n = 0 to ubound(data)
        if instr(data(n),"IP Address") then
          parts = split(data(n),":")
          if trim(parts(1)) <> "0.0.0.0" then
            index = index + 1
            ReDim Preserve arIPAddress(index)
            arIPAddress(index)= trim(cstr(parts(1)))
          end if
        end if
      next
      GetIPAddresses = arIPAddress
    End Function
    but Ali gave you the much better option for using API

    please rate the post if this is useful. And also never forget to mark the thread as [Resolved] when your problem is solved

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: how to get IP Address in VB6?

    Much simpler to use this:

    Code:
    Option Explicit
    ' Add Microsoft Internet Transfer Control
    
    Dim pubIPA As String, pos1 As Long, pos2 As Long, str As String
          
    Private Sub Form_Load()
        str = Inet1.OpenURL("http://vbnet.mvps.org/resources/tools/getpublicip.shtml", icString)
        pos1 = InStr(str, "var ip =")
        pos1 = InStr(pos1 + 1, str, "'", vbTextCompare) + 1
        pos2 = InStr(pos1 + 1, str, "'", vbTextCompare)
        pubIPA = Mid$(str, pos1, pos2 - pos1)
        MsgBox pubIPA, vbInformation
        Unload Me
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Apr 2008
    Location
    Pakistan
    Posts
    41

    Thumbs up Re: how to get IP Address in VB6?

    thank you v much dear friends
    i have done my work from your kindness

    now plzzz tel me how i get my computer name through code. ...

  9. #9
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: how to get IP Address in VB6?

    Hi there..

    You should always check google first:
    http://www.google.com/search?q=compu...x=&startPage=1

    first on the list.
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

  10. #10
    Join Date
    Aug 2007
    Posts
    445

    Re: how to get IP Address in VB6?

    code taken from this thread
    http://www.codeguru.com/forum/showthread.php?t=317191

    Add a reference to Active DS Type Library and then use this code:

    Code:
    Public Function DomainName() As String
        On Error Resume Next
        Dim oSysInfo As New ActiveDs.WinNTSystemInfo
        DomainName = oSysInfo.DomainName
        Set oSysInfo = Nothing
    End Function

    please rate the post if this is useful. And also never forget to mark the thread as [Resolved] when your problem is solved

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

Featured