📄 testioctl.cpp
字号:
// TestIOCTL.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "TestIOCTL.h"
// Copyright And Configuration Management ----------------------------------
//
// PassThruEx IP Blocking Control Application - TestIOCTL.cpp
//
// Companion Sample Code for the Article
//
// "Extending the Microsoft PassThru NDIS Intermediate Driver"
//
// Copyright (c) 2003 Printing Communications Associates, Inc. (PCAUSA)
// http://www.pcausa.com
//
// The right to use this code in your own derivative works is granted so long
// as 1.) your own derivative works include significant modifications of your
// own, 2.) you retain the above copyright notices and this paragraph in its
// entirety within sources derived from this code.
// This product includes software developed by PCAUSA. The name of PCAUSA
// may not be used to endorse or promote products derived from this software
// without specific prior written permission.
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// End ---------------------------------------------------------------------
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
using namespace std;
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 ) )
/////////////////////////////////////////////////////////////////////////////
//// DisplayAdapterInfo
//
// Purpose
// Display some human-readable information about the specified adapter.
//
// Parameters
//
// hAdapter - Handle returned from PtOpenAdapter.
//
// Return Value
//
// Remarks
//
//
DWORD
DisplayAdapterInfo( HANDLE hAdapter )
{
UCHAR OidData[4096];
DWORD nResult, ReturnedCount = 0;
NDIS_MEDIUM NdisMedium;
LPSTR strMedium;
WCHAR VendorDesc[ 256 ];
//
// Query For Vendor Description
//
nResult = PtQueryInformation(
hAdapter,
OID_GEN_VENDOR_DESCRIPTION,
VendorDesc,
sizeof( VendorDesc ),
&ReturnedCount
);
if( nResult != ERROR_SUCCESS )
{
printf( "Query For Vendor Description Failed; Error: 0x%8.8X\n", nResult );
return( nResult );
}
wprintf( L" Description: \042%*.*S\042\n",
ReturnedCount, ReturnedCount, (LPWSTR )VendorDesc );
//
// Query For Medium In Use
//
nResult = PtQueryInformation(
hAdapter,
OID_GEN_MEDIA_IN_USE,
OidData,
sizeof( OidData ),
&ReturnedCount
);
if( nResult != ERROR_SUCCESS )
{
printf( "Query For Media In Use Failed; Error: 0x%8.8X\n", nResult );
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; ", strMedium );
}
switch( NdisMedium )
{
case NdisMedium802_3:
//
// Query For 802.3 Current Address
//
nResult = PtQueryInformation(
hAdapter,
OID_802_3_CURRENT_ADDRESS,
OidData,
sizeof( OidData ),
&ReturnedCount
);
if( nResult != ERROR_SUCCESS )
{
printf( "Query For Current Address Failed; Error: 0x%8.8X\n", nResult );
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 = PtQueryInformation(
hAdapter,
OID_WAN_CURRENT_ADDRESS,
OidData,
sizeof( OidData ),
&ReturnedCount
);
if( nResult != ERROR_SUCCESS )
{
printf( "Query For Current Address Failed; Error: 0x%8.8X\n", nResult );
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;
}
printf( "\n" );
return( nResult );
}
/////////////////////////////////////////////////////////////////////////////
//// ShowBindings
//
// Purpose
// Enumerate all PassThru bindings and show some information about each.
//
// Parameters
//
// Return Value
//
// Remarks
//
//
void ShowBindings()
{
//
// Open A Handle On The PassThru Device
//
HANDLE PtHandle = PtOpenControlChannel();
if( PtHandle == INVALID_HANDLE_VALUE )
{
_tprintf( _T("Unable to open handle on PassThru device\n") );
return;
}
//
// Enumerate The PassThru Bindings
//
WCHAR BindingList[ 2048 ];
DWORD BufLength = sizeof( BindingList );
if( PtEnumerateBindings( PtHandle, (PCHAR )BindingList, &BufLength ) )
{
PWCHAR pWStr = BindingList;
UINT nWCHARsRead;
INT nBytesUnread = BufLength;
if( !BufLength )
{
_tprintf( _T("Enumeration is empty\n") );
}
else
{
cout << endl << "Driver Bindings:" << endl;
while( pWStr && *pWStr && nBytesUnread > 0 )
{
//
// Display Virtual Adapter Name
// ----------------------------
// This is the name passed to NdisIMInitializeDeviceInstanceEx.
// We can call this our "virtual adapter name".
//
printf( " \042%ws\042\n", pWStr );
//
// Advance In Buffer
//
nWCHARsRead = (ULONG )wcslen( pWStr ) + 1;
nBytesUnread -= nWCHARsRead * (ULONG )sizeof( WCHAR );
if( nBytesUnread <= 0 )
{
pWStr = NULL;
}
else
{
pWStr += nWCHARsRead;
}
if( !( pWStr && *pWStr && nBytesUnread > 0 ) )
{
_tprintf( _T("Unexpected enumeration termination\n") );
break;
}
//
// Display Lower Adapter Name
// --------------------------
// This is the name passed to NdisOpenAdapter. We call this the
// "lower adapte name".
//
printf( " \042%ws\042\n", pWStr );
//
// Open A PassThru Handle Associated With The Lower Adapter
//
HANDLE hLowerAdapter;
hLowerAdapter = PtOpenAdapterW( pWStr );
if( hLowerAdapter != INVALID_HANDLE_VALUE )
{
//
// Display Human-Readable Information
// ----------------------------------
// This function uses PtQueryInformation to fetch information from the
// lower miniport.
//
DisplayAdapterInfo( hLowerAdapter );
PtCloseAdapter( hLowerAdapter );
}
//
// Advance In Buffer
//
nWCHARsRead = (ULONG )wcslen( pWStr ) + 1;
nBytesUnread -= nWCHARsRead * (ULONG )sizeof( WCHAR );
if( nBytesUnread <= 0 )
{
pWStr = NULL;
}
else
{
pWStr += nWCHARsRead;
}
}
}
}
else
{
cout << endl << "PtEnumerateBindings Failed" << endl;
}
CloseHandle( PtHandle );
}
int IPAddressCompare( const void *pKey, const void *pElement )
{
ULONG a1 = *(PULONG )pKey;
ULONG a2 = *(PULONG )pElement;
if( a1 == a2 )
{
return( 0 );
}
if( a1 < a2 )
{
return( -1 );
}
return( 1 );
}
/////////////////////////////////////////////////////////////////////////////
//// ClearAllIPv4BlockingFilters
//
// Purpose
// Clear all IPv4 IP address blocking lists on all adapters.
//
// Parameters
//
// Return Value
//
// Remarks
//
//
void ClearAllIPv4BlockingFilters()
{
//
// Open A Handle On The PassThru Device
//
HANDLE PtHandle = PtOpenControlChannel();
if( PtHandle == INVALID_HANDLE_VALUE )
{
_tprintf( _T("Unable to open handle on PassThru device\n") );
return;
}
//
// Enumerate The PassThru Bindings
//
WCHAR BindingList[ 2048 ];
DWORD BufLength = sizeof( BindingList );
if( PtEnumerateBindings( PtHandle, (PCHAR )BindingList, &BufLength ) )
{
LPWSTR pWStr = BindingList;
UINT nWCHARsRead;
INT nBytesUnread = BufLength;
if( !BufLength )
{
_tprintf( _T("Enumeration is empty\n") );
}
else
{
cout << endl << "Driver Bindings:" << endl;
while( pWStr && *pWStr && nBytesUnread > 0 )
{
//
// Display Virtual Adapter Name
// ----------------------------
// This is the name passed to NdisIMInitializeDeviceInstanceEx.
// We can call this our "virtual adapter name".
//
printf( " \042%ws\042\n", pWStr );
//
// Advance In Buffer
//
nWCHARsRead = (ULONG )wcslen( pWStr ) + 1;
nBytesUnread -= nWCHARsRead * (ULONG )sizeof( WCHAR );
if( nBytesUnread <= 0 )
{
pWStr = NULL;
}
else
{
pWStr += nWCHARsRead;
}
if( !( pWStr && *pWStr && nBytesUnread > 0 ) )
{
_tprintf( _T("Unexpected enumeration termination\n") );
break;
}
//
// Display Lower Adapter Name
// --------------------------
// This is the name passed to NdisOpenAdapter. We call this the
// "lower adapte name".
//
printf( " \042%ws\042\n", pWStr );
//
// Open A PassThru Handle Associated With The Lower Adapter
//
HANDLE hLowerAdapter;
hLowerAdapter = PtOpenAdapterW( pWStr );
if( hLowerAdapter != INVALID_HANDLE_VALUE )
{
PtSetIPv4BlockingFilter( hLowerAdapter, NULL );
PtCloseAdapter( hLowerAdapter );
}
//
// Advance In Buffer
//
nWCHARsRead = (ULONG )wcslen( pWStr ) + 1;
nBytesUnread -= nWCHARsRead * sizeof( WCHAR );
if( nBytesUnread <= 0 )
{
pWStr = NULL;
}
else
{
pWStr += nWCHARsRead;
}
}
}
}
else
{
cout << endl << "PtEnumerateBindings Failed" << endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -