🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: AV - Stack getting trashed - HELP!

#3789
From: Remy Lebeau (TeamB)
Date: Mon, 24 Apr 2006 18:43
94 lines
4263 bytes
"Borland Lover" <kdhf@kbdf.com> wrote in message
news:4449633f$1@newsgroups.borland.com...

> However, when execution gets to the lines (marked below) the stack
> gets completely trashed.

That is because you are not managing the stack correctly in the first place.
See below.

> typedef ULONG (*TGetAdaptersAddresses)(ULONG Family, ULONG Flags, PVOID
> Reserved, PIP_ADAPTER_ADDRESSES pAdapterAddresses, PULONG pOutBufLen);

You are not specifying any calling convention in that declaration, so the
compiler's default conention is used.  Under C++, the default is __cdecl,
which is NOT the convention that GetAdaptersAddresses() actually uses.  If
you look at the documentation for GetAdatersAddresses(), it uses WINAPI for
the calling convention, which in turn usually resolves to __stdcall.  Win32
API functions always use the __stdcall convention (wsprintf() is about the
only API function that uses __cdecl instead).  __cdecl and _stdcall
manipulate the stack very differently.

Use this code instead:

    typedef ULONG (WINAPI *TGetAdaptersAddresses)(ULONG Family, ULONG Flags,
PVOID Reserved, PIP_ADAPTER_ADDRESSES pAdapterAddresses, PULONG pOutBufLen);

    void __fastcall TForm10::Button1Click(TObject *Sender)
    {
        HINSTANCE hinst = LoadLibrary("Iphlpapi.dll");
        AnsiString AMacAddress;

        if( NULL != hinst )
        {
            try
            {
                TGetAdaptersAddresses pGetAdaptersAddresses =
(TGetAdaptersAddresses) GetProcAddress(hinst, TEXT("GetAdaptersAddresses"));
                if( NULL != pGetAdaptersAddresses )
                {
                    IP_ADAPTER_ADDRESSES * pAddresses = NULL;
                    try
                    {
                        ULONG outBufLen = 0;

                        ULONG ulErr = pGetAdaptersAddresses(AF_INET, 0,
NULL, NULL, &outBufLen);
                        if( ERROR_BUFFER_OVERFLOW == ulErr )
                        {
                            pAddresses = (IP_ADAPTER_ADDRESSES*) new
BYTE[outBufLen];
                            ulErr = pGetAdaptersAddresses(AF_INET, 0, NULL,
pAddresses, &outBufLen);
                        }

                        if( (NO_ERROR == ulErr) && (0 !=
pAddresses->PhysicalAddressLength) )
                        {

AMacAddress.SetLength((pAddresses->PhysicalAddressLength * 3) - 1);
                            char *ptr = AMacAddress.c_str();

                            LPBYTE MACData = pAddresses->PhysicalAddress;

                            sprintf(ptr, "%02X", MACData[0]);
                            ptr += 2;

                            for(DWORD i = 1; i <
pAddresses->PhysicalAddressLength; ++i)
                            {
                                sprintf(ptr, ":%02X", MACData[i]);
                                ptr += 3;
                            }
                        }
                    }
                    __finally
                    {
                        delete [] pAddresses;
                    }
                }
            }
            catch(...)
            {
                AMacAddress = "UNKNOWN MAC ADDR";
            }

            FreeLibrary(hinst);
        }

        MessageDlg(AMacAddress, mtWarning, TMsgDlgButtons() << mbOK, 0);
    }


Gambit
Technical Details
Message-ID: <444d1d06$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <4449633f$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,aeca86a831696bee
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <4449633f$1@newsgroups.borland.com>
Subject: Re: AV - Stack getting trashed - HELP!
Date: Mon, 24 Apr 2006 11:43:57 -0700
Lines: 96
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 68.69.17.41
Message-ID: <444d1d06$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1145904390 68.69.17.41 (24 Apr 2006 11:46:30 -0700)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail