Solved

Get IP address when login on the net?

Posted on 1998-06-04
5
382 Views
Last Modified: 2008-02-01
I wan't my program found my IP Address if i'm login on the net. How to do that?
0
Comment
Question by:Acid_Buzz
  • 2
  • 2
5 Comments
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1462555
Use the GetAddressByName API:


 
The GetAddressByName function queries a name space, or a set of default name spaces, in order to obtain network address information for a specified network service. This process is known as service name resolution. A network service can also use the function to obtain local address information that it can use with the bind function.
INT GetAddressByName(
 

    DWORD dwNameSpace,      // name space to query for service address information
    LPGUID lpServiceType,      // the type of the service
    LPTSTR lpServiceName,      // the name of the service
    LPINT lpiProtocols,      // points to array of protocol identifiers
    DWORD dwResolution,      // set of bit flags that specify aspects of name resolution
    LPSERVICE_ASYNC_INFO lpServiceAsyncInfo,      // reserved for future use, must be NULL
    LPVOID lpCsaddrBuffer,      // points to buffer to receive address information
    LPDWORD lpdwBufferLength,      // points to variable with address buffer size information
    LPTSTR lpAliasBuffer,      // points to buffer to receive alias information
    LPDWORD lpdwAliasBufferLength       // points to variable with alias buffer size information
   );      
Parameters
dwNameSpace
Specifies the name space, or a set of default name spaces, that the operating system will query for network address information.
Use one of the following constants to specify a name space:
 
Value      Name Space

NS_DEFAULT      A set of default name spaces. The function queries each name space within this set. The set of default name spaces typically includes all the name spaces installed on the system. System administrators, however, can exclude particular name spaces from the set. This is the value that most applications should use for dwNameSpace.
NS_DNS      The Domain Name System used in the Internet for host name resolution.
NS_NETBT      The NetBIOS over TCP/IP layer. All Windows NT systems register their computer names with NetBIOS. This name space is used to convert a computer name to an IP address that uses this registration. Note that NS_NETBT may access a WINS server to perform the resolution.
NS_SAP      The Netware Service Advertising Protocol. This may access the Netware bindery if appropriate. NS_SAP is a dynamic name space that allows registration of services.
NS_TCPIP_HOSTS      Lookup value in the <systemroot>\system32\drivers\etc\hosts file.
NS_TCPIP_LOCAL      Local TCP/IP name resolution mechanisms, including comparisons against the local host name and looks up host names and IP addresses in cache of host to IP address mappings.
Most calls to GetAddressByName should use the special value NS_DEFAULT. This lets a client get by with no knowledge of which name spaces are available on an internetwork. The system administrator determines name space access. Name spaces can come and go without the client having to be aware of the changes.
lpServiceType
Points to a globally unique identifier (GUID) that specifies the type of the network service. The header file SVCGUID.H includes definitions of several GUID service types, and macros for working with them.  
lpServiceName
Points to a zero-terminated string that uniquely represents the service name. For example, "MY SNA SERVER".
Setting lpServiceName to NULL is the equivalent of setting dwResolution to RES_SERVICE. The function operates in its second mode, obtaining the local address to which a service of the specified type should bind. The function stores the local address within the LocalAddr member of the CSADDR_INFO structures stored into *lpCsaddrBuffer.
If dwResolution is set to RES_SERVICE, the function ignores the lpServiceName parameter.
If dwNameSpace is set to NS_DNS, *lpServiceName is the name of the host.  
lpiProtocols
Points to a zero-terminated array of protocol identifiers. The function restricts a name resolution attempt to name space providers that offer these protocols. This lets the caller limit the scope of the search.
If lpiProtocols is NULL, the function obtains information on all available protocols.
dwResolution
A set of bit flags that specify aspects of the service name resolution process. The following bit flags are defined:
 
Value      Meaning

RES_SERVICE      If this flag is set, the function obtains the address to which a service of the specified type should bind. This is the equivalent of setting lpServiceName to NULL.If this flag is clear, normal name resolution occurs.
RES_FIND_MULTIPLE      If this flag is set, the operating system performs an extensive search of all name spaces for the service. It will ask every appropriate name space to resolve the service name. If this flag is clear, the operating system stops looking for service addresses as soon as one is found.  
RES_SOFT_SEARCH      This flag is valid if the name space supports multiple levels of searching.If this flag is valid and set, the operating system performs a simple and quick search of the name space. This is useful if an application only needs to obtain easy-to-find addresses for the service.If this flag is valid and clear, the operating system performs a more extensive search of the name space.
lplpServiceAsyncInfo
Reserved for future use; must be set to NULL.  
lpCsaddrBuffer
Points to a buffer to receive one or more CSADDR_INFO data structures. The number of structures written to the buffer depends on the amount of information found in the resolution attempt. You should assume that multiple structures will be written, although in many cases there will only be one.  
lpdwBufferLength
Points to a variable that, upon input, specifies the size, in bytes, of the buffer pointed to by lpCsaddrBuffer.  
Upon output, this variable contains the total number of bytes required to store the array of CSADDR_INFO structures. If this value is less than or equal to the input value of *lpdwBufferLength, and the function is successful, this is the number of bytes actually stored in the buffer. If this value is greater than the input value of *lpdwBufferLength, the buffer was too small, and the output value of *lpdwBufferLength is the minimal required buffer size.
lpAliasBuffer
Points to a buffer to receive alias information for the network service.
If a name space supports aliases, the function stores an array of zero-terminated name strings into the buffer pointed to by lpAliasBuffer. There is a double zero-terminator at the end of the list. The first name in the array is the service's primary name. Names that follow are aliases. An example of a name space that supports aliases is DNS.
If a name space does not support aliases, it stores a double zero-terminator into the buffer.
This parameter is optional, and can be set to NULL.
lpdwAliasBufferLength
Points to a variable that, upon input, specifies the size, in bytes, of the buffer pointed to by lpAliasBuffer.  
Upon output, this variable contains the total number of bytes required to store the array of name strings. If this value is less than or equal to the input value of *lpdwAliasBufferLength, and the function is successful, this is the number of bytes actually stored in the buffer. If this value is greater than the input value of *lpdwAliasBufferLength, the buffer was too small, and the output value of *lpdwAliasBufferLength is the minimal required buffer size.
If lpAliasBuffer is NULL, lpdwAliasBufferLength is meaningless and can also be NULL.
Return Value
If the function succeeds, the return value is the number of CSADDR_INFO data structures written to the buffer pointed to by lpCsaddrBuffer.
If the function fails, the return value is SOCKET_ERROR( - 1). To get extended error information, call GetLastError. GetLastError may return the following extended error value:
 
Value      Meaning

ERROR_INSUFFICIENT_BUFFER      The buffer pointed to by lpCsaddrBuffer was too small to receive all of the relevant CSADDR_INFO structures. Call the function with a buffer at least as large as the value returned in *lpdwBufferLength.
Remarks
This function is a more powerful version of the Windows Sockets function gethostbyname The GetAddressByName function works with multiple name services.
The GetAddressByName function lets a client obtain a Windows Sockets address for a network service. The client specifies the service of interest by its service type and service name.
Many name services support a default prefix or suffix that the name service provider considers when resolving service names. For example, in the DNS name space, if a domain is named "nt.microsoft.com", and "ftp millikan" is provided as input, the DNS software fails to resolve "millikan", but successfully resolves "millikan.nt.microsoft.com".
Note that the GetAddressByName function can search for a service address in two ways: within a particular name space, or within a set of default name spaces. Using a default name space, an administrator can specify that certain name spaces will be searched for service addresses only if specified by name. An administrator or name space setup program can also control the ordering of name space searches.
See Also
gethostbyname, CSADDR_INFO

 


0
 

Author Comment

by:Acid_Buzz
ID: 1462556
Sorry MikeP but ask my question in VB I verry appreciate if i have an anwser in vb to... Hall contents of your inswer, I can found it in the help file hi have.. but I can't convert it from C++ if you can convert it I give you point. So for the momment I reopen question.

Thank anyway.. Sorry
0
 
LVL 4

Accepted Solution

by:
yowkee earned 70 total points
ID: 1462557
 Here's an example to get IP address. Place all declarations on module, initialize and check winsock version in Form_Load. And show the IP address when click on button command1.
  I got the code from newsgroup. If any further question, please ask. :)

-----' Module
Public Const WS_VERSION_REQD = &H101
Public Const WS_VERSION_MAJOR = WS_VERSION_REQD \ &H100 And &HFF&
Public Const WS_VERSION_MINOR = WS_VERSION_REQD And &HFF&
Public Const MIN_SOCKETS_REQD = 1
Public Const SOCKET_ERROR = -1
Public Const WSADescription_Len = 256
Public Const WSASYS_Status_Len = 128

Public Type HOSTENT
    hName As Long
    hAliases As Long
    hAddrType As Integer
    hLength As Integer
    hAddrList As Long
End Type

Public Type WSADATA
    wversion As Integer
    wHighVersion As Integer
    szDescription(0 To WSADescription_Len) As Byte
    szSystemStatus(0 To WSASYS_Status_Len) As Byte
    iMaxSockets As Integer
    iMaxUdpDg As Integer
    lpszVendorInfo As Long
End Type

Public Declare Function WSAGetLastError Lib _
    "WSOCK32.DLL" () As Long
Public Declare Function WSAStartup Lib _
    "WSOCK32.DLL" (ByVal wVersionRequired&, _
    lpWSAData As WSADATA) As Long
Public Declare Function WSACleanup Lib _
    "WSOCK32.DLL" () As Long
Public Declare Function gethostname Lib _
    "WSOCK32.DLL" (ByVal hostname$, HostLen&) As Long
Public Declare Function gethostbyname Lib _
    "WSOCK32.DLL" (ByVal hostname$) As Long
Public Declare Sub RtlMoveMemory Lib _
    "KERNEL32" (hpvDest As Any, ByVal hpvSource&, ByVal cbCopy&)

-----' Form
Private Sub Form_Load()
    'Sockets Initialize
    Dim WSAD As WSADATA
    Dim iReturn As Integer
    Dim sLowByte As String, sHighByte As String, sMsg As String
    'Winsock startup
    iReturn = WSAStartup(WS_VERSION_REQD, WSAD)
   
    If iReturn <> 0 Then
        MsgBox "Winsock.dll is not responding."
        End
    End If

    If lobyte(WSAD.wversion) < WS_VERSION_MAJOR _
       Or (lobyte(WSAD.wversion) = WS_VERSION_MAJOR _
       And hibyte(WSAD.wversion) < WS_VERSION_MINOR) Then
        sHighByte = Trim$(Str$(hibyte(WSAD.wversion)))
        sLowByte = Trim$(Str$(lobyte(WSAD.wversion)))
        sMsg = "Windows Sockets version " & sLowByte & "." & sHighByte
        sMsg = sMsg & " is not supported by winsock.dll "
        MsgBox sMsg
        End
    End If
       
    If WSAD.iMaxSockets < MIN_SOCKETS_REQD Then
        sMsg = "This application requires a minimum of "
        sMsg = sMsg & Trim$(Str$(MIN_SOCKETS_REQD)) _
        & " supported sockets."
        MsgBox sMsg
        End
    End If
End Sub

Public Function hibyte(ByVal wParam As Integer)
       hibyte = wParam \ &H100 And &HFF&
End Function

Public Function lobyte(ByVal wParam As Integer)
       lobyte = wParam And &HFF&
End Function

Private Sub Command1_click()

    Dim hostname As String * 256
    Dim hostent_addr As Long
    Dim host As HOSTENT
    Dim hostip_addr As Long
    Dim temp_ip_address() As Byte
    Dim i As Integer
    Dim ip_address As String

    If gethostname(hostname, 256) = SOCKET_ERROR Then
        MsgBox "Windows Sockets error " & Str(WSAGetLastError())
        Exit Sub
    Else
        hostname = Trim$(hostname)
    End If

    hostent_addr = gethostbyname(hostname)
    If hostent_addr = 0 Then
        MsgBox "Winsock.dll is not responding."
        Exit Sub
    End If

    RtlMoveMemory host, hostent_addr, LenB(host)
    RtlMoveMemory hostip_addr, host.hAddrList, 4
    ReDim temp_ip_address(1 To host.hLength)
    RtlMoveMemory temp_ip_address(1), hostip_addr, host.hLength

    For i = 1 To host.hLength
        ip_address = ip_address & temp_ip_address(i) & "."
    Next

    ip_address = Mid$(ip_address, 1, Len(ip_address) - 1)
    MsgBox hostname + vbNewLine + ip_address

End Sub

------

Regards.

0
 

Author Comment

by:Acid_Buzz
ID: 1462558
Exellent answer.. but juste change your trim$(hosname) because trim function not removed 0 ascii carater.. so you have to use

    dim pos as integer
    dim Hostresult as string

        pos = instr(hostname,chr$(0))
        hostResult = left(hostname,pos-1)

        msgbox hostresult & vbcrlf & ip_Adress..

thanks.. for your answer.. and if i have another question don't worry i post it.. thank..

0
 
LVL 4

Expert Comment

by:yowkee
ID: 1462559
Acid_Buzz,

  You are right. Sorry for my careless. I notice that but didn't change it since I was in a hurry then(go to bed) :)...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now