📄 driver.cpp
字号:
pDO->DeviceExtension;
// This will yield the symbolic link name
UNICODE_STRING pLinkName =
pDevExt->ustrSymLinkName;
// ... which can now be deleted
IoDeleteSymbolicLink(&pLinkName);
#if DBG>=1
DbgPrint("LODRIVER: Symbolic Link MPMP%d Deleted\n",
pDevExt->DeviceNumber+1);
#endif
// Delete the device
IoDeleteDevice( pDO );
// Put a remove device message in the Event Log
ReportEvent(
LOG_LEVEL_DEBUG,
MSG_REMOVING_DEVICE,
ERRORLOG_PNP,
(PVOID)pDO,
pIrp, // IRP
NULL, 0, // No dump data
NULL, 0 ); // No strings
pDevExt->state = Removed;
return PassDownPnP( pDO, pIrp );
}
//++
// Function: DriverUnload
//
// Description:
// For this driver, merely logs an event
//
// Arguments:
// pDriverObject - Passed from I/O Manager
//
// Return value:
// None
//--
VOID DriverUnload (
IN PDRIVER_OBJECT pDriverObject ) {
#if DBG>=1
DbgPrint("LODRIVER: DriverUnload\n");
#endif
// Put a shutdown message in the Event Log
ReportEvent(
LOG_LEVEL_DEBUG,
MSG_DRIVER_STOPPING,
ERRORLOG_UNLOAD,
(PVOID)pDriverObject,
NULL, // No IRP
NULL, 0, // No dump data
NULL, 0 ); // No strings
}
//++
// Function: DispatchCreate
//
// Description:
// Handles call from Win32 CreateFile request
// For this driver, does nothing
//
// Arguments:
// pDevObj - Passed from I/O Manager
// pIrp - Passed from I/O Manager
//
// Return value:
// NTSTATUS - success or failure code
//--
NTSTATUS DispatchCreate (
IN PDEVICE_OBJECT pDevObj,
IN PIRP pIrp ) {
#if DBG>=1
DbgPrint("LODRIVER: DispatchCreate\n");
#endif
// Put an open handle message in the Event Log
ReportEvent(
LOG_LEVEL_DEBUG,
MSG_OPENING_HANDLE,
ERRORLOG_DISPATCH,
(PVOID)pDevObj,
pIrp, // IRP
NULL, 0, // No dump data
NULL, 0 ); // No strings
PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)
pDevObj->DeviceExtension;
NTSTATUS status = STATUS_SUCCESS;
if (pDevExt->state != Started)
status = STATUS_DEVICE_REMOVED;
pIrp->IoStatus.Status = status;
pIrp->IoStatus.Information = 0; // no bytes xfered
IoCompleteRequest( pIrp, IO_NO_INCREMENT );
return status;
}
//++
// Function: DispatchClose
//
// Description:
// Handles call from Win32 CreateHandle request
// For this driver, does nothing
//
// Arguments:
// pDevObj - Passed from I/O Manager
// pIrp - Passed from I/O Manager
//
// Return value:
// NTSTATUS - success or failure code
//--
NTSTATUS DispatchClose (
IN PDEVICE_OBJECT pDevObj,
IN PIRP pIrp ) {
#if DBG>=1
DbgPrint("LODRIVER: DispatchClose\n");
#endif
// Put a close handle message in the Event Log
ReportEvent(
LOG_LEVEL_DEBUG,
MSG_CLOSING_HANDLE,
ERRORLOG_DISPATCH,
(PVOID)pDevObj,
pIrp, // IRP
NULL, 0, // No dump data
NULL, 0 ); // No strings
pIrp->IoStatus.Status = STATUS_SUCCESS;
pIrp->IoStatus.Information = 0; // no bytes xfered
IoCompleteRequest( pIrp, IO_NO_INCREMENT );
return STATUS_SUCCESS;
}
//++
// Function: DispatchCancel
//
// Description:
// Handles canceled IRP
//
// Arguments:
// pDevObj - Passed from I/O Manager
// pIrp - Passed from I/O Manager
//
// Return value:
// NTSTATUS - success or failuer code
//--
VOID DispatchCancel (
IN PDEVICE_OBJECT pDevObj,
IN PIRP pIrp ) {
#if DBG>=1
DbgPrint("LODRIVER: IRP Canceled\n");
#endif
// TODO: Perform IRP cleanup work
// Just complete the IRP
pIrp->IoStatus.Status = STATUS_CANCELLED;
pIrp->IoStatus.Information = 0; // bytes xfered
IoCompleteRequest( pIrp, IO_NO_INCREMENT );
IoStartNextPacket( pDevObj, TRUE );
}
//++
// Function: DispatchWrite
//
// Description:
// Handles call from Win32 WriteFile request
// For LODRIVER, puts an arbitray restriction
// on size of Write (16 - MAX_LO_TRANSFER_SIZE)
//
// Arguments:
// pDevObj - Passed from I/O Manager
// pIrp - Passed from I/O Manager
//
// Return value:
// NTSTATUS - success or failuer code
//--
NTSTATUS DispatchWrite (
IN PDEVICE_OBJECT pDevObj,
IN PIRP pIrp ) {
#if DBG>=1
DbgPrint("LODRIVER: Write Operation requested (DispatchWrite)");
#endif
NTSTATUS status = STATUS_SUCCESS;
PVOID userBuffer;
ULONG xferSize;
// The stack location contains the user buffer info
PIO_STACK_LOCATION pIrpStack =
IoGetCurrentIrpStackLocation( pIrp );
// Dig out the Device Extension from the Device object
PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)
pDevObj->DeviceExtension;
// Determine the length of the request
xferSize = pIrpStack->Parameters.Write.Length;
// Obtain user buffer pointer
userBuffer = pIrp->AssociatedIrp.SystemBuffer;
#if DBG>=1
DbgPrint("%d bytes\n", xferSize);
#endif
// Put a write request message in the Event Log
ReportEvent(
LOG_LEVEL_DEBUG,
MSG_WRITE_REQUEST,
ERRORLOG_DISPATCH,
(PVOID)pDevObj,
pIrp, // IRP
// Dump data is the data xfer request
&xferSize, 1,
NULL, 0 ); // No strings
if (xferSize > MAX_LO_TRANSFER_SIZE) {
xferSize = 0; // You get nothing if you ask for too much
status = STATUS_INVALID_BUFFER_SIZE;
}; // else - thank you very much
// Note that we don't actually keep the data...
// or do anything with it for that matter...
// Not really the point of this exercise...
pIrp->IoStatus.Status = status;
pIrp->IoStatus.Information = xferSize;
IoCompleteRequest( pIrp, IO_NO_INCREMENT );
return status;
}
//++
// Function: DispatchDeviceControl
//
// Description:
// Handles call from Win32 DeviceControl request
// For LODRIVER, puts an arbitray restriction
// on size of Write (16 - MAX_LO_TRANSFER_SIZE)
//
// Arguments:
// pDevObj - Passed from I/O Manager
// pIrp - Passed from I/O Manager
//
// Return value:
// NTSTATUS - success or failuer code
//--
NTSTATUS DispatchDeviceControl (
IN PDEVICE_OBJECT pDevObj,
IN PIRP pIrp ) {
#if DBG>=1
DbgPrint("LODRIVER: DeviceIoControl Operation requested (DispatchWrite)\n");
#endif
NTSTATUS status = STATUS_SUCCESS;
PIO_STACK_LOCATION pIrpStack =
IoGetCurrentIrpStackLocation( pIrp );
PBUFFER_SIZE_INFO pBufferInfo;
ULONG xferSize = 0;
// Here is the implementation
if (pIrpStack->Parameters.DeviceIoControl.IoControlCode
== IOCTL_GET_MAX_BUFFER_SIZE ) {
// The buffer passed by the user (by mutual
// agreement) is treated as BUFFER_SIZE_INFO type.
pBufferInfo = (PBUFFER_SIZE_INFO)
pIrp->AssociatedIrp.SystemBuffer;
pBufferInfo->MaxWriteLength = MAX_LO_TRANSFER_SIZE;
pBufferInfo->MaxReadLength = 0;
// Put a write request message in the Event Log
ReportEvent(
LOG_LEVEL_DEBUG,
MSG_DEVICE_CONTROL_REQUEST,
ERRORLOG_DISPATCH,
(PVOID)pDevObj,
pIrp, // IRP
// Dump data is the Buffer Info data
(ULONG*)pBufferInfo, 2,
NULL, 0 ); // No strings
// Complete the IRP by announcing the size of
// the returned BUFFER_SIZE_INFO information.
xferSize = sizeof(BUFFER_SIZE_INFO);
} else
// Not a recognized DeviceIoControl request
status = STATUS_INVALID_DEVICE_REQUEST;
pIrp->IoStatus.Information = xferSize;
pIrp->IoStatus.Status = status;
IoCompleteRequest( pIrp, IO_NO_INCREMENT );
return status;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -