📄 ptuserio.cpp
字号:
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 ) )
/////////////////////////////////////////////////////////////////////////////
//// PtDisplayAdapterInfo
//
// Purpose
// Display some human-readable information about the specified adapter.
//
// Parameters
//
// Return Value
//
// Remarks
// Secondary purpose of this function is to illustrate calling
// PtQueryInformation.
//
//
DWORD
PtDisplayAdapterInfo( HANDLE hAdapter )
{
UCHAR OidData[4096];
DWORD nResult, ReturnedCount = 0;
NDIS_MEDIUM NdisMedium;
LPSTR strMedium;
ULONG nConnectedState;
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\n", 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;
}
//
// Query For Media Connect Status
//
nResult = PtQueryInformation(
hAdapter,
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"
);
}
printf( "\n" );
return( nResult );
}
/////////////////////////////////////////////////////////////////////////////
//// _tmain
//
// Purpose
// PTUserIo MFC console application MAIN entry point.
//
// Parameters
//
// Return Value
//
// Remarks
//
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
return nRetCode;
}
//
// Say "Hello"
//
CString strHello;
strHello.LoadString(IDS_HELLO);
cout << (LPCTSTR)strHello << endl;
strHello.LoadString(IDS_COPYRIGHT);
cout << (LPCTSTR)strHello << endl;
//
// Open A Handle On The PassThru Device
//
HANDLE PtHandle = PtOpenControlChannel();
if( PtHandle == INVALID_HANDLE_VALUE )
{
strHello.LoadString(IDS_DRIVER_OPEN_FAILED);
cout << (LPCTSTR)strHello << endl;
nRetCode = 1;
return nRetCode;
}
//
// 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 )
{
strHello.LoadString(IDS_EMPTY_ENUMERATION);
cout << (LPCTSTR)strHello << endl;
}
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".
//
_tprintf( " \042%ws\042\n", pWStr );
//
// Advance In Buffer
//
nWCHARsRead = wcslen( pWStr ) + 1;
nBytesUnread -= nWCHARsRead * sizeof( WCHAR );
if( nBytesUnread <= 0 )
{
pWStr = NULL;
}
else
{
pWStr += nWCHARsRead;
}
if( !( pWStr && *pWStr && nBytesUnread > 0 ) )
{
strHello.LoadString(IDS_UNEXPECTED_ENUMERATION_TERMINATION);
cout << (LPCTSTR)strHello << endl;
break;
}
_tprintf( " \042%ws\042\n", pWStr );
//
// Open A PassThru Handle Associated With The Lower Adapter
//
HANDLE hLowerAdapter;
hLowerAdapter = PtOpenAdapter( pWStr );
if( hLowerAdapter != INVALID_HANDLE_VALUE )
{
//
// Display Human-Readable Information
// ----------------------------------
// This function uses PtQueryInformation to fetch information from the
// lower miniport.
//
PtDisplayAdapterInfo( hLowerAdapter );
PtCloseAdapter( hLowerAdapter );
}
//
// Advance In Buffer
//
nWCHARsRead = wcslen( pWStr ) + 1;
nBytesUnread -= nWCHARsRead * sizeof( WCHAR );
if( nBytesUnread <= 0 )
{
pWStr = NULL;
}
else
{
pWStr += nWCHARsRead;
}
}
}
}
else
{
cout << endl << "PtEnumerateBindings Failed" << endl;
}
CloseHandle( PtHandle );
return nRetCode;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -