Article View: borland.public.cppbuilder.nativeapi
Article #3855AV - Stack getting trashed - HELP!
From: Borland Lover
Date: Fri, 21 Apr 2006 22:57
Date: Fri, 21 Apr 2006 22:57
100 lines
4055 bytes
4055 bytes
I need to use the following code to get the MAC address on Win XP and 2k3.
It should work fine....
However, when execution gets to the lines (marked below) the stack gets
completely trashed. I have isolated the problem into a test app. You can
reproduce it by creating a new BCB project, drop a button on the form and
add this code.
Does it work on your system? Please help!
BCB6 Patched.
TIA
----------------
#include <winsock2.h>
#include "Iptypes.h"
#include "Iphlpapi.h"
typedef ULONG (*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 = EmptyStr;
if(NULL != hinst)
{
try
{
try
{
TGetAdaptersAddresses pGetAdaptersAddresses =
(TGetAdaptersAddresses)GetProcAddress(hinst, "GetAdaptersAddresses");
if(NULL != pGetAdaptersAddresses)
{
IP_ADAPTER_ADDRESSES * pAddresses = NULL;
try
{
ULONG outBufLen = 0;
// Make an initial call to GetAdaptersAddresses to
get the
// size needed into the outBufLen variable
if (pGetAdaptersAddresses(AF_INET, 0, NULL,
pAddresses, &outBufLen) == ERROR_BUFFER_OVERFLOW)
{
if (pAddresses)
GlobalFree(pAddresses);
pAddresses = (IP_ADAPTER_ADDRESSES*)new unsigned
char[outBufLen];
}
// Make a second call to GetAdapters Addresses to
get the actual data we want
if (pGetAdaptersAddresses(AF_INET, 0, NULL,
pAddresses, &outBufLen) == NO_ERROR)
{
// successful,
unsigned char * MACData =
pAddresses->PhysicalAddress;
////////<<---------------- As this next loop
executes - the stack gets trashed !!! WHY???
DWORD i;
for (i = 0; i <
pAddresses->PhysicalAddressLength; i++)
AMacAddress = AMacAddress + Format("%.2X:",
ARRAYOFCONST(( MACData[i] )));
AMacAddress =
AMacAddress.Delete(AMacAddress.Length(), 1); // take off the last colon
}
}
__finally
{
if (pAddresses)
delete [] pAddresses;
}
}
}
catch(...)
{
AMacAddress = "UNKNOWN MAC ADDR";
}
}
__finally
{
FreeLibrary(hinst);
}
}
MessageDlg(AMacAddress, mtWarning, TMsgDlgButtons() << mbOK, 0);
}
Technical Details
Message-ID:
<4449633f$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news3.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-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,aeca86a831696bee,start X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Borland Lover" <kdhf@kbdf.com> Newsgroups: borland.public.cppbuilder.nativeapi Subject: AV - Stack getting trashed - HELP! Date: Fri, 21 Apr 2006 15:57:03 -0700 Lines: 102 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2869 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2869 NNTP-Posting-Host: 207.61.87.125 Message-ID: <4449633f$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1145660223 207.61.87.125 (21 Apr 2006 15:57:03 -0700) Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news3.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