// GetIfEntry.cpp : Defines the entry point for the console application.
//=============== Created by Stoic Joker: Tuesday, 02/01/2011 @ 6:23:11pm
#include "stdafx.h" //--------------------------------------------+++-->
//================================================================================================
//-------------+++--> Strip \DEVICE\TCPIP_ Off of wszName to Get the Adapter's GUID For Comparison:
void StripOutGUID(WCHAR *szGUID, WCHAR *wszName) { //---------------------------------------+++-->
WCHAR *p; // Walk the string to find the GUID opening '{' character. Sure There
int i=0; // Are Easier Ways to do This ... But This Way I Avoid Language Issues.
p = wszName;
while(*p != '{') {
*p++; i++;
}
wsprintf(szGUID, L"%s", p);
}
//================================================================================================
//------------------------+++--> Get Lst of Network Interfaces Available on Machine for Comparison:
BOOL GetAdapters(WCHAR *szTarg) { //-----------( Much of This is From the MSDN )------------+++-->
PMIB_IFTABLE ifTable; // Declare and initialize variables.
PMIB_IFROW pMibIfRow;
DWORD dwSize = 0;
DWORD dwRetVal = 0;
// Allocate memory for our pointers.
ifTable = (MIB_IFTABLE*) malloc(sizeof(MIB_IFTABLE));
pMibIfRow = (MIB_IFROW*) malloc(sizeof(MIB_IFROW));
// Before calling GetIfEntry, we call GetIfTable to make sure there are entries to get.
// So... First, make an initial call to GetIfTable to get the necessary size into dwSize
if(GetIfTable(ifTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) {
GlobalFree(ifTable);
ifTable = (MIB_IFTABLE *) malloc (dwSize);
}
// Then make a second call to GetIfTable to get the actual data we want.
if((dwRetVal = GetIfTable(ifTable, &dwSize, 0)) == NO_ERROR) {
if(ifTable->dwNumEntries > 0) {
for(UINT i=1; i<= ifTable->dwNumEntries; i++) {
pMibIfRow->dwIndex = i;
if((dwRetVal = GetIfEntry(pMibIfRow)) == NO_ERROR) {
if(pMibIfRow->dwType == MIB_IF_TYPE_ETHERNET &&
pMibIfRow->dwAdminStatus == MIB_IF_ADMIN_STATUS_UP) {
WCHAR szGUID[GEN_BUFF] = {0};
printf("\tDescription: %s\n", pMibIfRow->bDescr);
printf("\twszName: %S\n", pMibIfRow->wszName);
StripOutGUID(szGUID, pMibIfRow->wszName);
if(wcscmp(szGUID, szTarg) == MATCH) {
printf("\tGUID: %S <--+++--> MATCHES REQUESTED ACTIVE TARGET ADAPTER!!!\n", szGUID);
}else{
printf("\tGUID: %S <-+-> Is Active, But Not the One We're After.\n", szGUID);
}
printf("\tReceived %d, Sent %d\n", pMibIfRow->dwInOctets, pMibIfRow->dwOutOctets);
printf("\t\tNext...\n\n");
}
}
}
}
GlobalFree(ifTable);
return TRUE;
}
return FALSE;
}
//================================================================================================
//--------------------------------------//---------------------------+++--> Console Program Main():
int _tmain(int argc, _TCHAR* argv[]) { //---------------------------------------------------+++-->
if(argc < 2) {
printf("\n\n\tERROR: No GUID Specified, Exiting!\n\n");
return 0;
}else{
printf("\n\n\tSearching for GUID: %S\n\n", argv[1]);
}
if(!GetAdapters(argv[1])) printf("\tGetIfTable failed.\n");
return 0;
}
// ============================================= STDAFX.H <-> Header File Below This Line!
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later.
#define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows.
#endif
#define GEN_BUFF 128
#define MATCH 0
#include <stdio.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
#include <Windows.h> //--+++--> Also Required by GetIfEntry():
#include <Iphlpapi.h> //---+++--> Required by GetIfEntry():
#pragma comment(lib, "Iphlpapi.lib") //-++-> ^See Above^: