📄 macaddr2.c
字号:
/////////////////////////////////////////////////////////////////////////////
//// INCLUDE FILES
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <tchar.h>
#include <ntddndis.h>
#include "IoctlNQGS.h"
#include "NetCardR.h"
// Copyright And Configuration Management ----------------------------------
//
// Win32 NDIS Framework (WinDis 32)
// For
// Windows 95 And Windows NT
// Adapter Registry Information Test Application - RTPlus.c
//
// Copyright (c) 1999-2000 Printing Communications Associates, Inc.
// - PCAUSA -
//
// Thomas F. Divine
// 4201 Brunswick Court
// Smyrna, Georgia 30080 USA
// (770) 432-4580
// tdivine@pcausa.com
//
// End ---------------------------------------------------------------------
//
// About MACADDR II
// ----------------
// The Windows NT DDK includes a sample application called MACADDR. The
// MACADDR sample illustrated the use of Win32 DeviceIoControl calls to
// access NDIS MAC drivers using the IOCTL_NDIS_QUERY_GLOBAL_STATS Ioctl.
//
// The technique illustrated in the MACADDR sample actually works on Windows
// 2000. However, the MACADDR sample is omitted from the Windows 2000 DDK.
//
// The PCAUSA MACADDR II sample illustrates the MACADDR techniques for both
// Windows NT and Windows 2000.
//
// Using MACADDR II
// ----------------
// MACADDR II is a Win32 Console Application. It is meant to be run from
// within a Command Prompt window. To run MACADDR II, first change to the
// directory containing the MacAddr2.exe file. Then enter the command:
//
// D:>macaddr2
//
// The application will run and output to the console.
//
// MACADDR II enumerates known MAC adapters in the NetCards registry key.
// It displays 1.) "Title", 2.) Description and 3.) Adapter Name (name
// passed to NdisOpenAdapter) from the registry information.
//
// For each MAC device found the program attempts to use the
// IOCTL_NDIS_QUERY_GLOBAL_STATS DeviceIoControl call to fetch the
// adapters OID_GEN_MEDIA_IN_USE and OID_XXX_CURRENT_ADDRESS.
//
// Other information of interest to programmers is displayed as the
// MACADDR II application runs.
//
// Sample MACADDR II Output
// ------------------------
// See NT.LOG and W2K.LOG for sample output.
//
// Terms of Use
// ------------
// This software is provided "as is", without any guarantee made
// as to its suitability or fitness for any particular use. It may
// contain bugs, so use of this tool is at your own risk. PCAUSA takes
// no responsilbity for any damage that may unintentionally be caused
// through its use.
//
// You may use MacAddr II source code in your own product, either free or
// commercial, without requesting permission of PCAUSA. However, informing
// PCAUSA of products which benefit from PCAUSA code is encouraged.
//
// You may NOT redistribute MacAddr II sources or executables or any of the
// components MacAddr components that you downloaded without express written
// permission of PCAUSA. If someone wants these items, then they must
// download them from the PCAUSA website.
//
/////////////////////////////////////////////////////////////////////////////
//// GLOBAL VARIABLES
//
#define APP_VERSION TEXT("5.00.13.51")
static LPSTR MediumText[ ] =
{
"802.3",
"802.5",
"FDDI",
"WAN",
"LocalTalk",
"DIX", // defined for convenience, not a real medium
"Arcnet Raw",
"Arcnet 878.2",
"ATM",
"Wireless WAN",
"Irda",
"Bpc",
"CoWan",
"IEEE 1394 (Firewire)",
"Unknown"
};
#define MaxMediumText (sizeof( MediumText )/sizeof( LPSTR ) )
DWORD
IoctlNdisQueryTest( LPCTSTR lpAdapterName )
{
UCHAR OidData[4096];
DWORD nResult, ReturnedCount = 0;
NDIS_MEDIUM NdisMedium;
LPSTR strMedium;
ULONG nConnectedState;
#ifdef DO_OID_GEN_SUPPORTED_LIST_QUERY
//
// Query For List Of Supported OIDs
//
nResult = MAII_IoctlNdisQueryGlobalStats(
lpAdapterName,
OID_GEN_SUPPORTED_LIST,
OidData,
sizeof( OidData ),
&ReturnedCount
);
if( nResult == ERROR_SUCCESS )
{
PULONG pSupportedOid;
pSupportedOid = (PULONG )OidData;
printf( "Supported OID List:\n" );
while( ReturnedCount >= sizeof( ULONG ) )
{
printf( " Oid: 0x%8.8X\n", *pSupportedOid );
if( ReturnedCount >= sizeof( ULONG ) )
{
ReturnedCount -= sizeof( ULONG );
++pSupportedOid;
}
else
{
ReturnedCount = 0;
}
}
}
#endif // DO_OID_GEN_SUPPORTED_LIST_QUERY
//
// Query For Medium In Use
//
nResult = MAII_IoctlNdisQueryGlobalStats(
lpAdapterName,
OID_GEN_MEDIA_IN_USE,
OidData,
sizeof( OidData ),
&ReturnedCount
);
if( nResult != ERROR_SUCCESS )
{
return( nResult );
}
NdisMedium = *((PNDIS_MEDIUM )OidData);
if( ReturnedCount >= sizeof( NDIS_MEDIUM ) )
{
if( NdisMedium < MaxMediumText )
{
strMedium = MediumText[ NdisMedium ];
}
else
{
strMedium = MediumText[ MaxMediumText - 1 ];
}
printf( "Medium: %s\n", strMedium );
}
switch( NdisMedium )
{
case NdisMedium802_3:
//
// Query For 802.3 Current Address
//
nResult = MAII_IoctlNdisQueryGlobalStats(
lpAdapterName,
OID_802_3_CURRENT_ADDRESS,
OidData,
sizeof( OidData ),
&ReturnedCount
);
if( nResult != ERROR_SUCCESS )
{
return( nResult );
}
if (ReturnedCount == 6)
{
printf(
"Mac address = %02.2X-%02.2X-%02.2X-%02.2X-%02.2X-%02.2X\n",
OidData[0], OidData[1], OidData[2], OidData[3],
OidData[4], OidData[5], OidData[6], OidData[7]
);
}
else
{
printf(
"DeviceIoControl returned an invalid count = %d\n",
ReturnedCount
);
}
break;
case NdisMediumWan:
//
// Query For WAN Current Address
//
nResult = MAII_IoctlNdisQueryGlobalStats(
lpAdapterName,
OID_WAN_CURRENT_ADDRESS,
OidData,
sizeof( OidData ),
&ReturnedCount
);
if( nResult != ERROR_SUCCESS )
{
return( nResult );
}
if (ReturnedCount == 6)
{
printf(
"Mac address = %02.2X-%02.2X-%02.2X-%02.2X-%02.2X-%02.2X\n",
OidData[0], OidData[1], OidData[2], OidData[3],
OidData[4], OidData[5], OidData[6], OidData[7]
);
}
else
{
printf(
"DeviceIoControl returned an invalid count = %d\n",
ReturnedCount
);
}
break;
default:
printf( "Mac address: Query Not Supported By Application...\n" );
break;
}
//
// Query For Medium In Use
//
nResult = MAII_IoctlNdisQueryGlobalStats(
lpAdapterName,
OID_GEN_MEDIA_CONNECT_STATUS,
&nConnectedState,
sizeof( nConnectedState ),
&ReturnedCount
);
if( nResult != ERROR_SUCCESS )
{
printf( "Media Connect Status: UNKNOWN\n" );
}
else
{
printf( "Media Connect Status: %s\n",
nConnectedState == NdisMediaStateConnected ? "Connected" : "Disconnected"
);
}
return( nResult );
}
/////////////////////////////////////////////////////////////////////////////
//// main
//
// Purpose
// Console application MAIN entry point.
//
// Parameters
//
// Return Value
//
// Remarks
//
int main( int argc, char **argv )
{
DWORD nResult;
MAII_ADAPTER_INFO AdapterInfo;
//
// Say Hello
//
printf( "MACADDR II Sample For Windows NT And Windows 2000 V%s.\n", APP_VERSION );
printf( "Copyright (c) 2000 Printing Communications Associates, Inc.\n" );
printf( "<http://www.pcausa.com>\n" );
printf( "All rights reserved.\n\n" );
if( MAII_IsWindows95() )
{
printf( "Running On Windows 9X\n\n" );
}
else if( MAII_IsWindows2000() )
{
printf( "Running On Windows 2000\n\n" );
}
else if( MAII_IsWindowsNT() )
{
printf( "Running On Windows NT\n\n" );
}
//
// Get First Adapter's Registry Information
//
nResult = MAII_GetFirstAdapterRegistryInfo( &AdapterInfo );
while( !nResult )
{
printf( "+++++++++++++++++++++++++++++++++++\n" );
//
// Display The Title Field
//
_tprintf( "%s\n", AdapterInfo.cTitle );
//
// Display The Adapter Description Field
//
_tprintf( " %s\n", AdapterInfo.cDescription );
//
// Display The AdapterName
// -----------------------
// The AdapterName is the string that is passed to MAII_OpenAdapter
// to open a specified adapter.
//
if( MAII_IsWindows95() )
{
//
// Windows 9X Example : "0001"
//
_tprintf( " %s\n", AdapterInfo.cServiceName );
}
else
{
//
// Windows NT Example : "\Device\El90x1"
// Windows 2000 Example: "\Device\{...GUID...}"
//
_tprintf( " \\Device\\%s\n", AdapterInfo.cServiceName );
}
if( MAII_IsWindowsNT() )
{
IoctlNdisQueryTest( AdapterInfo.cServiceName );
}
printf( "\n" );
//
// Get Next Adapter's Registry Information
//
nResult = MAII_GetNextAdapterRegistryInfo( &AdapterInfo );
}
return( 0 ); // No Complaints
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -