⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 netcardr.c

📁 macaddrsdk的驱动程序 macaddrsdk的驱动程序
💻 C
字号:
/////////////////////////////////////////////////////////////////////////////
//// INCLUDE FILES

#include	<Windows.h>
#include	<Assert.h>
#include	<Stdio.h>
#include	<TChar.h>
#include	<RegStr.h>

#include "NetCardR.h"

// Copyright And Configuration Management ----------------------------------
//
//          Windis NetCard Registry Enumeration Module - NetCardR.C
//                                    For
//                         Windows NT And Windows 2000
//
//        Copyright (c) 2000 Printing Communications Associates, Inc.
//                                - PCAUSA -
//                         <http://www.pcausa.com>
//
//                             Thomas F. Divine
//                           4201 Brunswick Court
//                        Smyrna, Georgia 30080 USA
//                              (770) 432-4580
//
//                       <mailto:tdivine@pcausa.com>
//
// End ---------------------------------------------------------------------

/////////////////////////////////////////////////////////////////////////////
//// GLOBAL VARIABLES
//

static DWORD g_nNextEnumIndex = 0;

static BOOL    g_bIsWindowsNTChecked = FALSE;
static BOOL    g_bIsWindowsNT = FALSE;

static BOOL    g_bIsWindows95Checked = FALSE;
static BOOL    g_bIsWindows95 = FALSE;

static OSVERSIONINFO    g_OSVersionInfo;

#define MAII_REGSTR_PATH_NETCARDS   TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards")
#define MAII_REGSTR_PATH_CLASS_NET  TEXT( "System\\CurrentControlSet\\Services\\Class\\Net")

/////////////////////////////////////////////////////////////////////////////
//// MAII_OSGetPlatformVersion (Exported)
//
// Purpose
// Determine if the current OS platform matches specified nPlatformId and
// OS major version.
//
// Parameters
//
// Return Value
// Returns non-zero OS major version if current current platform matches
// specified nPlatformId.
//
// Remarks
//

DWORD MAII_OSGetPlatformVersion( DWORD nPlatformId )
{
   BOOLEAN         bResult;

   //
   // Get OS/Platform Version Information
   //
   g_OSVersionInfo.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );

   bResult = GetVersionEx( &g_OSVersionInfo );

   if( !bResult )
   {
      return( 0 );
   }

   if( g_OSVersionInfo.dwPlatformId == nPlatformId )
   {
      return( g_OSVersionInfo.dwMajorVersion );
   }

   return( 0 );
}

/////////////////////////////////////////////////////////////////////////////
//// MAII_IsWindowsNT (Exported)
//
// Purpose
// Determing if the current OS platform is Windows NT.
//
// Parameters
//
// Return Value
// Returns TRUE if current OS platform is Windows NT.
//
// Remarks
//

BOOLEAN MAII_IsWindowsNT( void )
{
   if( !g_bIsWindowsNTChecked )
   {
      g_bIsWindowsNT = (MAII_OSGetPlatformVersion( VER_PLATFORM_WIN32_NT ) > 0 );

      g_bIsWindowsNTChecked = TRUE;
   }

   return( g_bIsWindowsNT );
}


/////////////////////////////////////////////////////////////////////////////
//// MAII_IsWindows2000 (Exported)
//
// Purpose
// Determing if the current OS platform is Windows 2000.
//
// Parameters
//
// Return Value
// Returns TRUE if current OS platform is Windows 2000.
//
// Remarks
//

BOOLEAN MAII_IsWindows2000( void )
{
   if( MAII_IsWindowsNT() )
   {
      if( g_OSVersionInfo.dwMajorVersion == 5 )
      {
         return( TRUE );
      }
   }

   return( FALSE );
}


/////////////////////////////////////////////////////////////////////////////
//// MAII_IsWindows95 (Exported)
//
// Purpose
// Determing if the current OS platform is Windows 95.
//
// Parameters
//
// Return Value
// Returns TRUE if current OS platform is Windows 95.
//
// Remarks
//

BOOLEAN MAII_IsWindows95( void )
{
   if( !g_bIsWindows95Checked )
   {
      g_bIsWindows95 = (MAII_OSGetPlatformVersion( VER_PLATFORM_WIN32_WINDOWS ) > 0 );
      g_bIsWindows95Checked = TRUE;
   }

   return( g_bIsWindows95 );
}


////////////////////////////////////////////////////////////////////////////
//                          Registry Access Utilities                     //
////////////////////////////////////////////////////////////////////////////

TCHAR *MAII_GetNetCardRegistryPath( void )
{
   if( MAII_IsWindows95() )
   {
      return( MAII_REGSTR_PATH_CLASS_NET );
   }
   else if( MAII_IsWindows2000() )
   {
      return( MAII_REGSTR_PATH_NETCARDS );
   }
   else if( MAII_IsWindowsNT() )
   {
      return( MAII_REGSTR_PATH_NETCARDS );
   }

   return( TEXT( "Unknown" ) );
}

DWORD _MAII_GetAdapterRegistryInfo95(
   PMAII_ADAPTER_INFO pAdapterRegistryInfo,
   LPCTSTR pszSubkey
   )
{
	HKEY		hKeyClassNet;
	HKEY		hKeyAdapter;
	LONG		nResult;

   //
   // Save Subkey
   // -----------
   // On Windows 9X the subkey is the AdapterName
   //
   _tcscpy(
      pAdapterRegistryInfo->cServiceName,
      pszSubkey
      );

	nResult = RegOpenKeyEx(
					HKEY_LOCAL_MACHINE,
					MAII_REGSTR_PATH_CLASS_NET,
					0,							// Options (Reserved)
					KEY_READ,				// Security Access Mask
					&hKeyClassNet
					);

	if( nResult != ERROR_SUCCESS )
	{
		return( nResult );
	}

	nResult = RegOpenKeyEx(
					hKeyClassNet,	// Handle Of Key To Enumerate
					pszSubkey,	   // Subkey To Open
					0,					// Options, Must Be Zero
					KEY_READ,		// Security Access Mask
					&hKeyAdapter	// Address Of Handle Of Open Key
					);

	if( nResult == ERROR_SUCCESS )
	{
		DWORD	dwType, dwDataLen;

		/* Get Adapter Description
		-------------------------- */
		dwDataLen = sizeof( pAdapterRegistryInfo->cDescription );

		nResult = RegQueryValueEx(
						hKeyAdapter,			// Handle Of Key To Query
						_TEXT(REGSTR_VAL_DRVDESC),// Name Of Value To Query
						NULL,						// Reserved, Must Be NULL
						&dwType,					// Pointer To Value Type
						(PUCHAR )pAdapterRegistryInfo->cDescription,	// Pointer To Value Buffer
						&dwDataLen				// Pointer To Buffer Size
						);

		if( nResult != ERROR_SUCCESS || dwType != REG_SZ )
		{
		   dwDataLen = sizeof( pAdapterRegistryInfo->cDescription );

		   nResult = RegQueryValueEx(
						   hKeyAdapter,			// Handle Of Key To Query
						   _TEXT(REGSTR_VAL_DEVDESC),// Name Of Value To Query
						   NULL,						// Reserved, Must Be NULL
						   &dwType,					// Pointer To Value Type
						   (PUCHAR )pAdapterRegistryInfo->cDescription,	// Pointer To Value Buffer
						   &dwDataLen				// Pointer To Buffer Size
						   );
      }

		if( nResult != ERROR_SUCCESS || dwType != REG_SZ )
		{
         //
	      // Build Minimal Adapter Title
         //
			_tcscpy( pAdapterRegistryInfo->cDescription, _T("Unknown") );	// ATTENTION!!! Use LoadString
      }

		RegCloseKey( hKeyAdapter );
	}

	RegCloseKey( hKeyClassNet );

	return( ERROR_SUCCESS );
}

DWORD _MAII_GetAdapterRegistryInfoNT(
   PMAII_ADAPTER_INFO pAdapterRegistryInfo,
   LPCTSTR pszSubkey
   )
{
	HKEY		hKeyClassNet;
	HKEY		hKeyAdapter;
	LONG		nResult;

	nResult = RegOpenKeyEx(
					HKEY_LOCAL_MACHINE,
					MAII_REGSTR_PATH_NETCARDS,
					0,							// Options (Reserved)
					KEY_READ,				// Security Access Mask
					&hKeyClassNet
					);

	if( nResult != ERROR_SUCCESS )
	{
		return( nResult );
	}

	nResult = RegOpenKeyEx(
					hKeyClassNet,	// Handle Of Key To Enumerate
					pszSubkey,	   // Subkey To Open
					0,					// Options, Must Be Zero
					KEY_READ,		// Security Access Mask
					&hKeyAdapter	// Address Of Handle Of Open Key
					);

	if( nResult == ERROR_SUCCESS )
	{
		DWORD	dwType, dwDataLen;

		/* Get Adapter Service Name
		--------------------------- */
		dwDataLen = sizeof( pAdapterRegistryInfo->cServiceName );

		nResult = RegQueryValueEx(
						hKeyAdapter,			// Handle Of Key To Query
						_TEXT("ServiceName"),// Name Of Value To Query
						NULL,						// Reserved, Must Be NULL
						&dwType,					// Pointer To Value Type
						(PUCHAR )pAdapterRegistryInfo->cServiceName,	// Pointer To Value Buffer
						&dwDataLen				// Pointer To Buffer Size
						);

		if( nResult != ERROR_SUCCESS || dwType != REG_SZ )
		{
			_tcscpy( pAdapterRegistryInfo->cServiceName, _T("Unknown") );
		}

		/* Get Adapter Description
		-------------------------- */
		dwDataLen = sizeof( pAdapterRegistryInfo->cDescription );

		nResult = RegQueryValueEx(
						hKeyAdapter,			// Handle Of Key To Query
						_TEXT(REGSTR_VAL_DESCRIPTION),// Name Of Value To Query
						NULL,						// Reserved, Must Be NULL
						&dwType,					// Pointer To Value Type
						(PUCHAR )pAdapterRegistryInfo->cDescription,	// Pointer To Value Buffer
						&dwDataLen				// Pointer To Buffer Size
						);

		if( nResult != ERROR_SUCCESS || dwType != REG_SZ )
		{
			_tcscpy( pAdapterRegistryInfo->cDescription, _T("Unknown") );
		}

      //
		// Get Adapter Title
      // -----------------
      // Fetch the adapter Title, if it is present. The Title often provides
      // information that can be used to distinguish between multiple
      // instances of identical adapters. For example, the adapter
      // description may be:
      //
      //    Intel EtherExpress PRO Adapter
      //
      // If multiple instances of this adapter were installed on a machine
      // the Description would be the same for each. However, the Title
      // may be a little more usefull:
      //
      //   Intel EtherExpress PRO Adapter Bus 1 Slot 4
      //   Intel EtherExpress PRO Adapter Bus 1 Slot 5
      // 
      // The Title field is commonly found on Windows 9X and Windows NT 4.
      // However, it seems to be dropped from Windows 2000.
      //
      // If a Title can't be found, construct one from the InstanceNumber
      // and the Description. A constructed title would look like:
      //
      //   [4] Intel EtherExpress PRO Adapter
      //
		dwDataLen = sizeof( pAdapterRegistryInfo->cTitle );

		nResult = RegQueryValueEx(
						hKeyAdapter,			// Handle Of Key To Query
						_TEXT("Title"),		// Name Of Value To Query
						NULL,						// Reserved, Must Be NULL
						&dwType,					// Pointer To Value Type
						(PUCHAR )pAdapterRegistryInfo->cTitle,	// Pointer To Value Buffer
						&dwDataLen				// Pointer To Buffer Size
						);

		if( nResult != ERROR_SUCCESS || dwType != REG_SZ )
		{
         ULONG nInstanceNumber;

         //
         // Save Instance Number (Or CardNumber)
         //
         nInstanceNumber = atoi( pszSubkey );

         //
	      // Build Minimal Adapter Title From Description
         //
	      wsprintf( (PCHAR )pAdapterRegistryInfo->cTitle, "[%d] %s",
            nInstanceNumber,
            pAdapterRegistryInfo->cDescription
            );
      }

		RegCloseKey( hKeyAdapter );
	}

	RegCloseKey( hKeyClassNet );

	return( ERROR_SUCCESS );
}

DWORD MAII_GetAdapterRegistryInfoBySubkey(
   PMAII_ADAPTER_INFO pAdapterRegistryInfo,
   LPCTSTR pszSubkey
   )
{
   DWORD nResult = ERROR_CALL_NOT_IMPLEMENTED;
   ULONG nInstanceNumber;

   //
   // Save Instance Number (Or CardNumber)
   //
   nInstanceNumber = atoi( pszSubkey );

   //
   // Setup Defaults
   //
   _tcscpy( pAdapterRegistryInfo->cTitle, _T("Unknown") );
   _tcscpy( pAdapterRegistryInfo->cDescription, _T("Unknown") );
   _tcscpy( pAdapterRegistryInfo->cServiceName, _T("Unknown") );

   //
   // Dispatch To Platform-Specific Handlers
   //
	if( MAII_IsWindows95() )
	{
		nResult = _MAII_GetAdapterRegistryInfo95( pAdapterRegistryInfo, pszSubkey );
	}
	else if( MAII_IsWindowsNT() )
	{
      nResult = _MAII_GetAdapterRegistryInfoNT( pAdapterRegistryInfo, pszSubkey );
	}

	return( nResult );
}

DWORD MAII_GetAdapterRegistryInfo(
   PMAII_ADAPTER_INFO pAdapterRegistryInfo,
   LPCTSTR pszEnumerator
   )
{
   return( MAII_GetAdapterRegistryInfoBySubkey( pAdapterRegistryInfo, pszEnumerator ) );
}

DWORD MAII_GetAdapterRegistryInfoBySubkeyIndex(
   PMAII_ADAPTER_INFO pAdapterRegistryInfo,
   DWORD dwIndex     // Subkey Index. Starts with zero (0).
   )
{
	HKEY		hKeyClassNet;
	LONG		nResult;
	CHAR		szName[ 16 ];
	DWORD		cbName = 16;

	nResult = RegOpenKeyEx(
					HKEY_LOCAL_MACHINE,
					MAII_GetNetCardRegistryPath(), // Address Of Name Of Subkey To Open
					0,							// Options (Reserved)
					KEY_READ,				// Security Access Mask
					&hKeyClassNet
					);

	if( nResult != ERROR_SUCCESS )
	{
      return( nResult );
   }

	nResult = RegEnumKeyEx(
					hKeyClassNet,	// Handle Of Key To Enumerate
					dwIndex,       // Index Of Subkey To Enumerate
					szName,			// Buffer For Subkey Name
					&cbName,			// Size Of Subkey Name
					NULL,				// Reserved, Must Be NULL
					NULL,				// Buffer For Class String
					NULL,				// Size Of Class Buffer
					NULL				// Time Key Last Written To
					);

   RegCloseKey( hKeyClassNet );

	if( nResult == ERROR_SUCCESS )
	{
      return( MAII_GetAdapterRegistryInfo( pAdapterRegistryInfo, szName ) );
   }

   return( nResult );
}

DWORD MAII_GetNextAdapterRegistryInfo(
   PMAII_ADAPTER_INFO pAdapterRegistryInfo
   )
{
   return( MAII_GetAdapterRegistryInfoBySubkeyIndex(
               pAdapterRegistryInfo,
               g_nNextEnumIndex++
               )
            );
}

DWORD MAII_GetFirstAdapterRegistryInfo(
   PMAII_ADAPTER_INFO pAdapterRegistryInfo
   )
{
   g_nNextEnumIndex = 0;

   return( MAII_GetNextAdapterRegistryInfo( pAdapterRegistryInfo ) );
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -