Article View: borland.public.cppbuilder.nativeapi
Article #4599Re: all IP adresses of the local system
From: Remy Lebeau (TeamB)
Date: Thu, 22 Dec 2005 19:14
Date: Thu, 22 Dec 2005 19:14
90 lines
2850 bytes
2850 bytes
"EB" <elias.berelian@snet.net> wrote in message
news:43aad18b@newsgroups.borland.com...
> How do you do this if you don't want to use boost::?
You could write your own RAII class directly in your code:
template< typename T >
class TMemBuffer
{
private:
T* Data;
public:
TMemBuffer(DWORD Size)
{
Data = (T*) new BYTE[Size];
}
~TMemBuffer()
{
delete[] (BYTE) Data;
}
operator T*() { return Data; }
};
Then you can use it like this:
ULONG OutBufLen = 0;
if( GetAdaptersInfo(NULL, &OutBufLen) == ERROR_BUFFER_OVERFLOW )
{
TMemBuffer<IP_ADAPTER_INFO> Buffer(OutBufLen);
PIP_ADAPTER_INFO pAdapterInfo = Buffer;
if( GetAdaptersInfo(pAdapterInfo, &OutBufLen) == ERROR_SUCCESS )
{
// use pAdapterInfo as needed...
}
}
Alternatively, you can implement the class to not allow an exception if the
allocation fails:
template< typename T >
class TMemBuffer
{
private:
T* Data;
public:
TMemBuffer(DWORD Size)
{
Data = (T*) ::LocalAlloc(LMEM_FIXED, Size);
}
~TMemBuffer()
{
if( Data )
::LocalFree(Data);
}
operator T*() { return Data; }
bool operator !() { return (Data == NULL); }
};
ULONG OutBufLen = 0;
if( GetAdaptersInfo(NULL, &OutBufLen) == ERROR_BUFFER_OVERFLOW )
{
TMemBuffer<IP_ADAPTER_INFO> Buffer(OutBufLen);
if( !Buffer )
{
// handle the allocation failure ...
}
else
{
PIP_ADAPTER_INFO pAdapterInfo = Buffer;
if( GetAdaptersInfo(pAdapterInfo, &OutBufLen) == ERROR_SUCCESS )
{
// use pAdapterInfo as needed...
}
}
}
Gambit
Technical Details
Message-ID:
<43aafac0$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<43a832cb$1@newsgroups.borland.com> <43aad18b@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,744a3315ee7b00aa 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: <43a832cb$1@newsgroups.borland.com> <43aad18b@newsgroups.borland.com> Subject: Re: all IP adresses of the local system Date: Thu, 22 Dec 2005 11:14:40 -0800 Lines: 92 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: <43aafac0$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1135278784 68.69.17.41 (22 Dec 2005 11:13:04 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail