본문 바로가기
프로그래밍/라이브러리

[Win32 API] 네트웍 상태 알기

by 써드아이 2008. 2. 6.

#define ROUND_UP_COUNT( Count, Pow2 )   (((Count)+(Pow2)-1)&(~(((LONG)(Pow2))-1)))
#define ALIGN_WORST        8
#define BIG_TABLE        30
// ------------------------------------------------------------------------------------------------------
//
PMIB_IFTABLE myGetIfTable( BOOL bOrder )
{
 PMIB_IFTABLE pIfTable = NULL;
 DWORD dwActualSize, status = NO_ERROR;
 dwActualSize = ROUND_UP_COUNT( sizeof( MIB_IFTABLE ), ALIGN_WORST ) + BIG_TABLE * sizeof( MIB_IFROW );
 pIfTable = (PMIB_IFTABLE) HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, dwActualSize );
 if ( pIfTable == NULL )
 {
  return NULL;
 }
 pIfTable->dwNumEntries = BIG_TABLE;
 status = GetIfTable( pIfTable, &dwActualSize, bOrder );

 if ( status != NO_ERROR )
 {
  HeapFree( GetProcessHeap(), NULL, pIfTable );
  pIfTable = (PMIB_IFTABLE) HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, dwActualSize );
  if ( pIfTable == NULL )
  {
   return NULL;
  }
  status = GetIfTable( pIfTable, &dwActualSize, bOrder );
  if ( status != NO_ERROR )
  {
   HeapFree( GetProcessHeap(), NULL, pIfTable );
   pIfTable = NULL;
  }
 }
 return pIfTable;
}

// ------------------------------------------------------------------------------------------------------
//
BOOL GetNetworkConnection( PBOOL pbConnect )
{
 UINT i = 0;
 MIB_IFROW IfEntry;
 DWORD status = NO_ERROR;
 BOOL bSuccess = TRUE;
 PMIB_IFTABLE pIfTable = NULL;
RETAILMSG( DEBUG_TYPE, (_T( "GetNetworkConnection() -  %d\r\n" ), __LINE__ ));
 pIfTable = myGetIfTable( FALSE );
 if ( pIfTable == NULL )
 {
  return FALSE;
 }
  for ( i = 0; i < pIfTable->dwNumEntries; i++ )
 {
  IfEntry.dwIndex = pIfTable->table[i].dwIndex;
  status = GetIfEntry( &IfEntry );
  if ( status != NO_ERROR )
  {
   bSuccess = FALSE;
  }
  else
   IfEntry.dwOperStatus ==    // 연결됨 : 5, 연결안됨 0
 //  *pbConnect = IfEntry.dwOperStatus ? TRUE : FALSE;
 }
 HeapFree( GetProcessHeap(), NULL, pIfTable );
 return bSuccess;
}


GetIfTable(), GetIfEntry() - iphlpapi.h / lib
MIB_IFTABLE - Iprtrmib.h