Accessing the IP address of multiple network adapters using the MS Winsock Control 
Author Message
 Accessing the IP address of multiple network adapters using the MS Winsock Control

Hello,
How can I access ALL the IP addresses for the different network adapters
I have installed? All I can currently access is the first one bound.

Thanks,
Gary



Sat, 28 Jul 2001 03:00:00 GMT  
 Accessing the IP address of multiple network adapters using the MS Winsock Control
Im not sure you can using the winsock control. (although someone else might
know of a very weird way to do it)
2 months ago or so windows developer journal had an article in c++ about
doing that sort of thing.
Quote:

>Hello,
>How can I access ALL the IP addresses for the different network adapters
>I have installed? All I can currently access is the first one bound.

>Thanks,
>Gary



Sun, 29 Jul 2001 03:00:00 GMT  
 Accessing the IP address of multiple network adapters using the MS Winsock Control
VC is my next step.....I'll use the Netbios API if need be, I just am
unable to find any documentation.

Thanks,
Gary


Quote:
> Im not sure you can using the winsock control. (although someone else might
> know of a very weird way to do it)
> 2 months ago or so windows developer journal had an article in c++ about
> doing that sort of thing.


> >Hello,
> >How can I access ALL the IP addresses for the different network adapters
> >I have installed? All I can currently access is the first one bound.

> >Thanks,
> >Gary



Sun, 29 Jul 2001 03:00:00 GMT  
 Accessing the IP address of multiple network adapters using the MS Winsock Control

If you're interested in using the MS INet control, I have a solution
--
Ricky

ICQ# 4730537             EGN# 16191
kharjes at dgi dot net
http://www.geocities.com/~howdy

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Please Reply To The Newsgroups

Quote:

>Hello,
>How can I access ALL the IP addresses for the different network adapters
>I have installed? All I can currently access is the first one bound.

>Thanks,
>Gary



Sun, 29 Jul 2001 03:00:00 GMT  
 Accessing the IP address of multiple network adapters using the MS Winsock Control
check out the catalyst control at
http://www.catalyst.com/forms/download-wrench.html
Quote:

>> >Hello,
>> >How can I access ALL the IP addresses for the different network adapters
>> >I have installed? All I can currently access is the first one bound.

>> >Thanks,
>> >Gary



Sun, 29 Jul 2001 03:00:00 GMT  
 Accessing the IP address of multiple network adapters using the MS Winsock Control

Quote:

>Im not sure you can using the winsock control. (although someone else might
>know of a very weird way to do it)


>>Hello,
>>How can I access ALL the IP addresses for the different network adapters
>>I have installed? All I can currently access is the first one bound.

I came across this article on Microsoft's web site:

  http://support.microsoft.com/support/kb/articles/q160/2/15.asp?FR=0

This article contains the VB comment: "get all of the IP address if
machine is  multi-homed".

I've pasted the article at the bottom in case you can't get to it.

By the way, have you noticed how MS tries to thwart us Netscape users
when they put a block of code on their support site? They use an
invalid HTML sequence "<PRE><PRE></PRE>", which apparently IE
interprets as preformatted text still in effect, but Netscape sees the
final </PRE> and turns it off. So if you view the page in Netscape,
the VB code is all run together without line breaks. The above
referenced page doesn't do this, but this page does:
http://support.microsoft.com/support/kb/articles/Q154/5/12.asp

In case you can't get to the article about multiple IP's, here it is:

SUMMARY

This article demonstrates how to obtain the host IP address of the
local machine by using the GetHostByName API of the Windows Sockets
dll, Wsock32.dll. To obtain the host IP address, you must use
GetHostByName in conjunction with GetHostName.

MORE INFORMATION

GetHostName returns the standard host name for the local machine. You
can use GetHostByName to resolve this name and return a pointer to the
HOSTENT structure. This structure contains the host address
information.

Step-by-Step Example

        1. Start a new project in Visual Basic.  Form1 is created by
default.

        2. Place a CommandButton, Command1, on Form1.

        3. Place the following code in the General Declarations
section of Form1.

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

Private Type HOSTENT

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

Private 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

   Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As
Long
   Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal _
   wVersionRequired&, lpWSAData As WSADATA) As Long
   Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
   Private Declare Function gethostname Lib "WSOCK32.DLL" (ByVal
hostname$, _
   ByVal HostLen as integer) as long

   Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal _
   hostname$) As Long

   Private Declare Sub RtlMoveMemory Lib "KERNEL32" (hpvDest As Any,
ByVal _
   hpvSource&, ByVal cbCopy&)

   Function hibyte(ByVal wParam As Integer)

       hibyte = wParam \ &H100 And &HFF&

   End Function

   Function lobyte(ByVal wParam As Integer)

       lobyte = wParam And &HFF&

   End Function

   Sub SocketsInitialize()
   Dim WSAD As WSADATA
   Dim iReturn As Integer
   Dim sLowByte As String, sHighByte As String, sMsg As String

       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

       'iMaxSockets is not used in winsock 2. So the following check
is only
       'necessary for winsock 1. If winsock 2 is requested,
       'the following check can be skipped.

       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

   Sub SocketsCleanup()
   Dim lReturn As Long

       lReturn = WSACleanup()

       If lReturn <> 0 Then
           MsgBox "Socket error " & Trim$(Str$(lReturn)) & " occurred
in Cleanup "
           End
       End If

   End Sub

   Sub Form_Load()

       SocketsInitialize

   End Sub

   Private Sub Form_Unload(Cancel As Integer)

       SocketsCleanup

   End Sub

   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

       MsgBox hostname

       'get all of the IP address if machine is  multi-homed

       Do
           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 ip_address

           ip_address = ""
           host.hAddrList = host.hAddrList + LenB(host.hAddrList)
           RtlMoveMemory hostip_addr, host.hAddrList, 4
        Loop While (hostip_addr <> 0)

   End Sub

        4. Press the F5 key to run the project.

        5. Click Command1 to obtain the host information.

REFERENCES

For additional information, please see the following article in the
Microsoft Knowledge Base:

   ARTICLE ID: HREF="/support/kb/articles/Q154/5/12.asp">Q154512
   TITLE     : SAMPLE: WINSOCK.EXE: Getting HostAddress Using Windows
               Sockets



Mon, 30 Jul 2001 03:00:00 GMT  
 Accessing the IP address of multiple network adapters using the MS Winsock Control


Quote:
> Hello,
> How can I access ALL the IP addresses for the different network adapters
> I have installed? All I can currently access is the first one bound.

these programs will work in c and you can call the api from vb, but you
will have to do some work to unravel the array of struct in_addr's.
second program requires winsock 2.2

#include <stdio.h>
#pragma warning(disable:4115 4201 4214 4514)
#include <winsock.h>
#pragma warning(default:4115 4201 4214 /*4514*/)
#define IPFAIL(x)               (x==SOCKET_ERROR)
#define BAD_SOCKET(x)   (x==INVALID_SOCKET)
static WSADATA WSAData;
void findIPAddresses(char *name)
        {
        struct hostent *host;
        struct in_addr *ip;
        printf("ip address(es) for %s: ",name);
        if((host=gethostbyname(name))==NULL)
                { WSACleanup(); fprintf(stderr,"gethostbyname fails");
exit(1); }
        while((ip=(struct in_addr *)*host->h_addr_list++)!=NULL)
                printf("%s ",inet_ntoa(*ip));
        printf("\n");
        }
int main(int argc,char *argv[])
        {
        char name[BUFSIZ],*hostName=name;
        int I;
        if(WSAStartup(0x0101,&WSAData))
                { WSACleanup(); fprintf(stderr,"WSAStartup fails");
exit(1); }
        if(argc==1)
                {
                if(IPFAIL(gethostname(name,BUFSIZ-1)))
                        { WSACleanup(); fprintf(stderr,"gethostname fails");
exit(1); }
                printf("gethostname returns: %s\n",name);
                findIPAddresses(name);
                }
                else for(I=1;I<argc;I++)
                        findIPAddresses(argv[I]);
        (void)getchar();
        return WSACleanup();
        }

#include <iostream.h>
#include <winsock2.h>
#include <ws2tcpip.h>

int doit()
{
    SOCKET sd = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0);
    if (sd == SOCKET_ERROR) {
        cerr << "Failed to get a socket.  Error " <<
                WSAGetLastError() << endl;
        return 1;
    }

    INTERFACE_INFO InterfaceList[20];
    unsigned long nBytesReturned;
    if (WSAIoctl(sd, SIO_GET_INTERFACE_LIST, 0, 0, &InterfaceList,
            sizeof(InterfaceList), &nBytesReturned, 0, 0) ==
            SOCKET_ERROR) {
        cerr << "Failed calling WSAIoctl: error " <<
                WSAGetLastError() << endl;
        return 1;
    }

    int nNumInterfaces = nBytesReturned / sizeof(INTERFACE_INFO);
    cout << "There are " << nNumInterfaces << " interfaces:" << endl;
    for (int I = 0; I < nNumInterfaces; ++I) {
        cout << endl;

        sockaddr_in* pAddress;
        pAddress = (sockaddr_in*)&(InterfaceList[I].iiAddress);
        cout << "    " << inet_ntoa(pAddress->sin_addr);

        pAddress = (sockaddr_in*)&(InterfaceList[I].iiBroadcastAddress);
        cout << " has bcast " << inet_ntoa(pAddress->sin_addr);

        pAddress = (sockaddr_in*)&(InterfaceList[I].iiNetmask);
        cout << " and netmask " << inet_ntoa(pAddress->sin_addr) << endl;

        cout << "    Iface is ";
        u_long nFlags = InterfaceList[I].iiFlags;
        if (nFlags & IFF_UP) cout << "up";
        else cout << "down";
        if (nFlags & IFF_POINTTOPOINT) cout << ", is point-to-point";
        if (nFlags & IFF_LOOPBACK) cout << ", is a loopback iface";
        cout << ", and can do: ";
        if (nFlags & IFF_BROADCAST) cout << "bcast ";
        if (nFlags & IFF_MULTICAST) cout << "multicast ";
        cout << endl;
    }

    return 0;

Quote:
}

int main()
{
    WSADATA WinsockData;
    if (WSAStartup(MAKEWORD(2, 2), &WinsockData) != 0) {
        cerr << "Failed to find Winsock 2.2!" << endl;
        return 2;
    }

    int nRetVal = doit();

    WSACleanup();

    return nRetVal;

Quote:
}

hth
--
Ray (will hack java for food) http://home.pacbell.net/rtayek/
hate Spam? http://www.blighty.com/products/spade/


Thu, 02 Aug 2001 03:00:00 GMT  
 Accessing the IP address of multiple network adapters using the MS Winsock Control
Thanks for the code...I'll have to dig out my C manuals and decode it!
LOL

Thanks again,
Gary



Quote:
> Subject: Re: Accessing the IP address of multiple network adapters using the MS Winsock Control

> Newsgroups: microsoft.public.vb.controls.internet, microsoft.public.vb.general.discussion, microsoft.public.vb.syntax, microsoft.public.vb.winapi, microsoft.public.vb.winapi.networks



> > Hello,
> > How can I access ALL the IP addresses for the different network adapters
> > I have installed? All I can currently access is the first one bound.

> these programs will work in c and you can call the api from vb, but you
> will have to do some work to unravel the array of struct in_addr's.
> second program requires winsock 2.2

> #include <stdio.h>
> #pragma warning(disable:4115 4201 4214 4514)
> #include <winsock.h>
> #pragma warning(default:4115 4201 4214 /*4514*/)
> #define IPFAIL(x)          (x==SOCKET_ERROR)
> #define BAD_SOCKET(x)      (x==INVALID_SOCKET)
> static WSADATA WSAData;
> void findIPAddresses(char *name)
>    {
>    struct hostent *host;
>    struct in_addr *ip;
>    printf("ip address(es) for %s: ",name);
>    if((host=gethostbyname(name))==NULL)
>            { WSACleanup(); fprintf(stderr,"gethostbyname fails");
> exit(1); }
>    while((ip=(struct in_addr *)*host->h_addr_list++)!=NULL)
>            printf("%s ",inet_ntoa(*ip));
>    printf("\n");
>    }
> int main(int argc,char *argv[])
>    {
>    char name[BUFSIZ],*hostName=name;
>    int I;
>    if(WSAStartup(0x0101,&WSAData))
>            { WSACleanup(); fprintf(stderr,"WSAStartup fails");
> exit(1); }
>    if(argc==1)
>            {
>            if(IPFAIL(gethostname(name,BUFSIZ-1)))
>                    { WSACleanup(); fprintf(stderr,"gethostname fails");
> exit(1); }
>            printf("gethostname returns: %s\n",name);
>            findIPAddresses(name);
>            }
>            else for(I=1;I<argc;I++)



Mon, 06 Aug 2001 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Accessing the IP address of multiple network adapters using the MS Winsock Control

2. Accessing the IP address of multiple network adapters using the MS Winsock Control

3. Accessing the IP address of multiple network adapters using the MS Winsock Control

4. Accessinf the IP address of multiple network adapters using the MS Winsock Control

5. Accessinf the IP address of multiple network adapters using the MS Winsock Control

6. Accessinf the IP address of multiple network adapters using the MS Winsock Control

7. Accessinf the IP address of multiple network adapters using the MS Winsock Control

8. Problems finding correct IP address using WINSOCK Control with 2 network adapters

9. Problems finding correct IP address using WINSOCK Control with 2 network adapters

10. Local IP address from multiple adapters through the Winsock control

11. TCP/IP IP address and multiple adapters

12. Network adapter question: how to put ip address and MAC addresses together

 

 
Powered by phpBB® Forum Software