🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #11935

Re: GetAdaptersInfo problem

#11935
From: Remy Lebeau (TeamB)
Date: Wed, 07 Jan 2004 19:11
70 lines
3458 bytes
"Baz" <.@.com> wrote in message news:3ffbf6b6$1@newsgroups.borland.com...

>     pAdapterInfo = (IP_ADAPTER_INFO *) malloc ( sizeof(IP_ADAPTER_INFO) );

You should be calling GetAdaptersInfo() a first time to determine the actual
size, don't just hard-code a size at first.

>    if (GetAdaptersInfo( pAdapterInfo, &ulOutBufLen) !=
ERROR_BUFFER_OVERFLOW )

That check is wrong.  If the buffer is too small, then the function *will*
return ERROR_BUFFER_OVERFLOW.  You should be using the '==' operator instead
of the '!=' operator.

>    pAdapterInfo = (IP_ADAPTER_INFO *) malloc ( sizeof(ulOutBufLen));

You are using the wrong size.  You are using the size of the ulOutBufLen
variable itself, not the value that it contains.  The size will always be 4,
thus you are only allocating a buffer that is 4 bytes in size, but then
trying to stuff an entire array of data into it afterwards.  If anything,
GetAdaptersInfo() should have failed a second time because the buffer is too
small again.  That line should be this instead:

    pAdapterInfo = (PIP_ADAPTER_INFO) malloc ( ulOutBufLen );

>    while (pAdapter != NULL)

I do not see anywhere in your code where you are actually iterating the
adapters list at all.  You need to use the adapter's Next member to update
your pAdapter pointer at the end of each iteration of the loop.  Otherwise
you are simply looping over the same item each time indefinately.

With all of that said, you should be doing something more like the following
instead:

    PIP_ADAPTER_INFO pAdapterInfo = NULL;
    PIP_ADAPTER_INFO pAdapter = NULL;
    ULONG ulOutBufLen = 0;

    if( GetAdaptersInfo(NULL, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW )
    {
        pAdapterInfo = (PIP_ADAPTER_INFO) malloc(ulOutBufLen);
        if( pAdapterInfo )
        {
            if( GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == NO_ERROR )
            {
                pAdapter = pAdapterInfo;
                while( pAdapter )
                {
                    TListItem *newitem = lsvAdapters->Items->Add();
                    newitem->Caption = pAdapter->Description;

newitem->SubItems->Add(pAdapter->IpAddressList.IpAddress.String);
                    pAdapter = pAdapter->Next;
                }
            }
            else
                ShowMessage("Call to GetAdaptersInfo failed.");

            free(pAdapterInfo);
        }
        else
            ShowMessage("Call to malloc failed.");
    }
    else
        ShowMessage("Call to GetAdaptersInfo failed.");


Gambit
Technical Details
Message-ID: <3ffc588d$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <3ffbf6b6$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,d754bc29b79fd8f7
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2004-01-07 11:10:31 PST
From: "Remy Lebeau \(TeamB\)" <gambit47.no.spam@no.spam.yahoo.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <3ffbf6b6$1@newsgroups.borland.com>
Subject: Re: GetAdaptersInfo problem
Date: Wed, 7 Jan 2004 11:11:44 -0800
Lines: 72
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
NNTP-Posting-Host: 67.22.138.253
Message-ID: <3ffc588d$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1073502349 67.22.138.253 (7 Jan 2004 11:05:49 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail