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

📄 drivermain.cpp

📁 撰写windows驱动须知(Essentials Of Building Windows Drivers)
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//**************************************************************************************************//
//*                                                                                                *//
//* Copyright (C) 2003, James Antognini, antognini@mindspring.com.                                 *//
//*                                                                                                *//
//**************************************************************************************************//

#define JADriverVersion "1.16"

#ifdef __cplusplus  // C++ conversion
extern "C"
{
#endif

#include <ntddk.h>

#define  JADriverKernelMode 1                         // Ensure kernel pieces available.
#define  _DemoDriverPart1   1                         // Master device object pointer, etc.
#include "DemoDriver.h"

pDemoDrvExtension pMstrDevExt = NULL;

#ifdef __cplusplus  // C++ conversion
}
#endif

//***************************************************************************//
//                                                                           //
// Globals.                                                                  //
//                                                                           //
//***************************************************************************//

#define CompDateTimeStr "dd mmm yyyy hh:mm:ss"  
                                                
char DrvCompileInfo[sizeof(CompDateTimeStr)+1]; 

PDEVICE_OBJECT  pPermDevObj = NULL;

//**************************************************************************************************//
//*                                                                                                *//
//* DriverEntry.                                                                                   *//
//*                                                                                                *//
//**************************************************************************************************//
extern "C"
NTSTATUS
DriverEntry(
            IN PDRIVER_OBJECT  pDriverObject,
            IN PUNICODE_STRING pRegistryPath
           )
{
 NTSTATUS                        status = STATUS_SUCCESS;
 UNICODE_STRING                  DeviceNameUniString,
                                 DeviceLinkUniString,
                                 DriverName,
                                 WkUniStr;
 PDEVICE_OBJECT                  pDevObj = NULL;
 pDemoDrvExtension               pDevExt;
 ULONG                           i;
 BOOLEAN                         bHaveSymLink = FALSE,
                                 bHaveRegPath = FALSE;
 WCHAR                     const backslash = L'\\';   // Delimiter in Unicode.          
 WCHAR                     const constDeviceName[] = L"\\Device\\",                 
                                 constDeviceLink[] = L"\\DosDevices\\";   
 PWSTR                           pIdx;
 ANSI_STRING                     LclDrvName;
  
 char                      const DateCompiledBase[] = __DATE__,
                                 TimeCompiledBase[] = " "__TIME__;
 char                            DateCompiled[] =     // Build date in preferred (dd mmm yyyy) format.
                                   {DateCompiledBase[4], DateCompiledBase[5], DateCompiledBase[6],
                                    DateCompiledBase[0], DateCompiledBase[1], DateCompiledBase[2], DateCompiledBase[3],
                                    DateCompiledBase[7], DateCompiledBase[8], DateCompiledBase[9], DateCompiledBase[10],
                                    0x0
                                   };

 if (' '==DateCompiled[0])
  strcpy(DrvCompileInfo, DateCompiled+1);
 else
  strcpy(DrvCompileInfo, DateCompiled+0);

 strcat(DrvCompileInfo, TimeCompiledBase);

 DbgPrint((JADrvNm " v" JADriverVersion " (compiled %s)\n", DrvCompileInfo));

 // Invoke an attached debugger.  If there's none, continue.

 _try                                                 // Per Mark Roddy, comp.os.ms-windows.programmer.nt.kernel-mode, 27 Apr 2001.
    {
//   DbgBreakPoint();
    }
     _except(EXCEPTION_EXECUTE_HANDLER)
      {
      }

 RtlZeroMemory(&LclDrvName, sizeof(LclDrvName));      // Clear ANSI string descriptor, ensure safe exit.

 DeviceNameUniString.Buffer = NULL;                   // Ensure safe exit by branch to label 'done'.
 DeviceLinkUniString.Buffer = NULL;
 LclDrvName.Buffer = NULL;
  
 // Find the driver name in the registry path information.  The following assumes the string to be searched ends with a
 // backslash followed by 1 or more WCHARs that are the Unicode name.
  
 for (                                                // Find last delimiter by working back from end.       
      pIdx =                                          // Point to last WCHAR in buffer.                      
        pRegistryPath->Buffer + (pRegistryPath->Length / sizeof(WCHAR)) - 1;
      ;
      pIdx--                                          // Decrement by WCHAR width.                           
     )                                                                                                       
    if (backslash==*pIdx)                             // Is current WCHAR a delimiter?                       
      break;                                                                                                 

 // Build Unicode descriptor of driver name.
  
 pIdx++;                                              // Point to just after backslash.
  
 DriverName.Length =                                  // Calculate length.              
   pRegistryPath->Length -                                     
   ((pIdx - pRegistryPath->Buffer) * sizeof(WCHAR)); 
  
 DriverName.MaximumLength = DriverName.Length +       // Figure in length of terminator.
                            sizeof(WCHAR);
 DriverName.Buffer = pIdx;                            // Point to driver name.

 status =                                             // Get driver name in ansi.
   RtlUnicodeStringToAnsiString(
                                &LclDrvName,          // Descriptor.
                                &DriverName,          // Unicode string to convert.
                                TRUE                  // Allocate space (which must be freed later).
                               );

 if (!NT_SUCCESS(status))                             // A problem?
   {
    DbgPrint((JADrvNm " DriverEntry:  Failed to convert driver name!\n"));
    goto done;
   }

 DeviceNameUniString.Length = 0;                      // Show current length of device name.

 DeviceNameUniString.MaximumLength =                  // Show maximum length of device name.
   sizeof(constDeviceName) -
   sizeof(WCHAR)           +                          // Don't need size of first terminator (that is, terminator of constDeviceName).
   DriverName.MaximumLength;

 DeviceNameUniString.Buffer =                         // Get space for device name and for ASCII driver name.
   (PWSTR)ExAllocatePoolWithTag(
                                PagedPool,
                                DeviceNameUniString.MaximumLength,
                                'xxJA'
                               );

 if (NULL==DeviceNameUniString.Buffer)                // Failed to get storage? 
   {
    DbgPrint(("%s DriverEntry:  Failed to get storage for device name!\n", LclDrvName.Buffer));
    goto done;
   }

 // Now build proper device name, eg, '\Device\Driver'.

 RtlAppendUnicodeToString(                            // Initialize by appending L"\\Device\\" to empty string.
                          &DeviceNameUniString,
                          constDeviceName
                         );

 RtlAppendUnicodeStringToString(                      // Append driver name.
                                &DeviceNameUniString,
                                &DriverName
                               );
                                
 // Set up the device.

 status = IoCreateDevice(
                         pDriverObject,
                         sizeof(DemoDrvExtension),
                         &DeviceNameUniString,
                         FILE_DEVICE_UNKNOWN,         // Per Walt Oney, p 403.
                         0,
                         FALSE,                       // Not exclusive.
                         &pDevObj
                        );

 if (FALSE==NT_SUCCESS(status))                       // A problem?
   {
    DbgPrint(("%s DriverEntry:  Failed to create the device, rc = 0x%08X!\n", LclDrvName.Buffer, status));
    goto done;
   }

 DbgPrint(("%s DriverEntry:  Device at 0x%08X\n", LclDrvName.Buffer, pDevObj));

 pPermDevObj = pDevObj;                               // Save device address in module's global storage.

 // Extension will have been zeroed by OS.

 pDevExt = (pDemoDrvExtension)pDevObj->DeviceExtension;

 pDevExt->JAUniDeviceName.Length =                    // Save length of Unicode driver name.
   DeviceNameUniString.Length;                                                            
 pDevExt->JAUniDeviceName.MaximumLength =             // Save maximum length of Unicode driver name.
   DeviceNameUniString.MaximumLength;                                                            
 pDevExt->JAUniDeviceName.Buffer =                    // Save address of Unicode driver name.
   DeviceNameUniString.Buffer;                                                                   
   

⌨️ 快捷键说明

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