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

📄 drivermain.cpp

📁 撰写windows驱动须知(Essentials Of Building Windows Drivers)
💻 CPP
📖 第 1 页 / 共 2 页
字号:
 pDevExt->DriverName = LclDrvName;                    // Copy ANSI-string descriptor.
   
 pMstrDevExt = pDevExt;   
   
 // Create a symbolic link to gain access to this driver/device, eg, '\DosDevices\Driver'.

 DeviceLinkUniString.Buffer =                         // Get space for symbolic link name.
   (PWSTR)ExAllocatePoolWithTag(           
                                PagedPool,
                                sizeof(constDeviceLink) +
                                  DriverName.Length,
                                'xxJA'
                               );

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

    goto done;
   }

 DeviceLinkUniString.Length = 0;                      // Show current length.
 DeviceLinkUniString.MaximumLength =                  // Show maximum length.
   sizeof(constDeviceLink) -
   sizeof(WCHAR)           +                          // Don't need size of first terminator (that is, terminator of constDeviceLink).
   DriverName.MaximumLength;

 RtlAppendUnicodeToString(                            // Initialize by appending L"\\DosDevices\\" to empty string.
                          &DeviceLinkUniString,
                          constDeviceLink
                         );

 RtlAppendUnicodeStringToString(                      // Append driver name.
                                &DeviceLinkUniString,
                                &DriverName
                               );
                                
 status = IoCreateSymbolicLink(
                               &DeviceLinkUniString,
                               &DeviceNameUniString
                              );

 if (FALSE==NT_SUCCESS(status))                       // A problem?
   {
    DbgPrint(("%s DriverEntry:  IoCreateSymbolicLink failed, rc = 0x%08X\n", LclDrvName.Buffer, status));

    goto done;
   }

 bHaveSymLink = TRUE;

 pDevExt->JAUniSymLinkName = DeviceLinkUniString;     // Copy descriptor.

 // Copy the registry path.   
   
 pDevExt->JARegPath.MaximumLength = pRegistryPath->Length + sizeof(UNICODE_NULL); 
 pDevExt->JARegPath.Length = pRegistryPath->Length; 
 pDevExt->JARegPath.Buffer = (PWSTR)ExAllocatePoolWithTag(PagedPool, pDevExt->JARegPath.MaximumLength, 'xxJA');   
   
 if (NULL==pDevExt->JARegPath.Buffer)                 // A problem?  
   {   
    DbgPrint(("%s DriverEntry:  Unable to allocate %d bytes for copy of registry key name\n",
              LclDrvName.Buffer,
              pRegistryPath->Length + sizeof(WCHAR)
             )
            );

    status = STATUS_INSUFFICIENT_RESOURCES;
    goto done;
   }   
   
 bHaveRegPath = TRUE;                                 // Remember storage allocated.
   
 RtlCopyUnicodeString(&pDevExt->JARegPath, pRegistryPath);  
   
 // Minimally support all types of IRP (taken from Walter Oney's "Programming the Microsoft Windows Driver Model,"
 // p 402).
   
 for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
   pDriverObject->MajorFunction[i] = DemoDrvDispatchAny;

 pDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DemoDrvDispatch2;

 pDriverObject->DriverUnload                           = DemoDrvUnload;

 DbgPrint(("%s DriverEntry:  Initialization complete\n", LclDrvName.Buffer));

done:

 if (NT_SUCCESS(status))                              // No problem?
   {                                                
    pMstrJADrvObj = pDriverObject;
    pJAMstrDevObj = pDevObj;
   }
 else
   {
    if (NULL!=LclDrvName.Buffer)                      // Free working storage for ansi driver name.
      RtlFreeAnsiString(&LclDrvName);

    if (TRUE==bHaveRegPath)                           // Copied registry path?
      ExFreePool(pDevExt->JARegPath.Buffer);

    if (TRUE==bHaveSymLink)                           // Symbolic link established?
      IoDeleteSymbolicLink(&DeviceLinkUniString);

    if (NULL!=DeviceLinkUniString.Buffer)             // Free working storage for symbolic link name.
      ExFreePool(DeviceLinkUniString.Buffer);

    if (NULL!=DeviceNameUniString.Buffer)             // Free working storage for device name.
      ExFreePool(DeviceNameUniString.Buffer);

    if (NULL!=pDevObj)                                // Was a device object created?
      {
       pPermDevObj = NULL;                            // Be neat.

       IoDeleteDevice(pDevObj);
      }
   }

 return status;
}                                                     // End DriverEntry().

/**************************************************************************************************/
/*                                                                                                */
/**************************************************************************************************/
NTSTATUS
DemoDrvDispatchAny(
                   IN PDEVICE_OBJECT pDevObj,
                   IN PIRP           pIrp
                  )
{
 NTSTATUS                status = STATUS_SUCCESS;
 PIO_STACK_LOCATION      pIrpStack;
 pDemoDrvExtension       pDevExt =                    // Address of device object extension.
                           (pDemoDrvExtension)pDevObj->DeviceExtension;

 DbgPrint(("%s DemoDrvDispatchAny:  Entered.\n", pMstrDevExt->DriverName.Buffer));

 pIrp->IoStatus.Status      = STATUS_SUCCESS;
 pIrp->IoStatus.Information = 0;

 pIrpStack = IoGetCurrentIrpStackLocation(pIrp);

 switch(pIrpStack->MajorFunction)
   {
    case 0:
    default:
      ;
   }

 IoCompleteRequest(pIrp, IO_NO_INCREMENT);

 return status;
}                                                     // End DemoDrvDispatchAny().

/**************************************************************************************************/
/*                                                                                                */
/**************************************************************************************************/
VOID
DemoDrvUnload(
              IN PDRIVER_OBJECT pDrvObj
             )
{
 pDemoDrvExtension       pDevExt =                    // Address of device object extension.    
                           (pDemoDrvExtension)pPermDevObj->DeviceExtension;                       

 DbgPrint(("%s DemoDrvUnload:  Unloading\n", pMstrDevExt->DriverName.Buffer));

 RtlFreeAnsiString(&pDevExt->DriverName);             // Free working storage for ansi driver name.

 ExFreePool(pDevExt->JARegPath.Buffer);

 IoDeleteSymbolicLink(&pDevExt->JAUniSymLinkName);    // Delete symbolic link.

 ExFreePool(pDevExt->JAUniSymLinkName.Buffer);

 ExFreePool(pDevExt->JAUniDeviceName.Buffer);

 IoDeleteDevice(pDrvObj->DeviceObject);               // Delete device object.
}                                                     // End DemoDrvUnload().

/**************************************************************************************************/
/*                                                                                                */
/**************************************************************************************************/
int
DemoDrvTechnique(
                 IN PDEVICE_OBJECT pDevObj,
                 IN PIRP           pIrp,
                 IN PVOID          pInBuffer,
                 IN ULONG          lenInBuffer,
                 IN PVOID          pOutBuffer,
                 IN ULONG          lenOutBuffer
                )
{
 ULONG                           rc = 0;

 return rc;
}                                                     // End DemoDrvTechnique().

⌨️ 快捷键说明

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