www.pudn.com > QuickSYS_0_4_0.zip > cfile.c


/*++ 
 
Copyright (c)  
  
Module Name: 
  
    $(PROJECT_NAME).c 
 
Abstract: 
 
    This framework is generated by QuickSYS 0.4 
 
Author: 
 
	 
 
Environment: 
 
	Kernel mode only. 
 
Revision History: 
 
	N.B. 
	__except_handler4 is used by VC8 if BufferSecurityCheck(/GS) is on, supported by WDK. 
	Turn it off when using old DDK and VC8 will use __except_handler3. 
 
	If BufferSecurityCheck(/GS) is on, change entry point to GsDriverEntry@8 and add BufferOverflowK.lib. 
 
	For x64, change _X86_=1 to _AMD64=1, include directory to ddk\wnet, lib path to lib\wnet\amd64. 
 
--*/ 
 
#include "precomp.h" 
#include "$(PROJECT_NAME).h" 
 
// 
// A structure representing the instance information associated with 
// a particular device 
// 
 
typedef struct _DEVICE_EXTENSION 
{ 
    ULONG  StateVariable; 
 
} DEVICE_EXTENSION, *PDEVICE_EXTENSION; 
 
// 
// Device driver routine declarations. 
// 
 
NTSTATUS 
DriverEntry( 
	IN PDRIVER_OBJECT		DriverObject, 
	IN PUNICODE_STRING		RegistryPath 
	); 
 
NTSTATUS 
$(PROJECT_NAME_A)DispatchCreate( 
	IN PDEVICE_OBJECT		DeviceObject, 
	IN PIRP					Irp 
	); 
 
NTSTATUS 
$(PROJECT_NAME_A)DispatchClose( 
	IN PDEVICE_OBJECT		DeviceObject, 
	IN PIRP					Irp 
	); 
 
NTSTATUS 
$(PROJECT_NAME_A)DispatchDeviceControl( 
	IN PDEVICE_OBJECT		DeviceObject, 
	IN PIRP					Irp 
	); 
 
VOID 
$(PROJECT_NAME_A)Unload( 
	IN PDRIVER_OBJECT		DriverObject 
	); 
 
#ifdef ALLOC_PRAGMA 
#pragma alloc_text(INIT, DriverEntry) 
#pragma alloc_text(PAGE, $(PROJECT_NAME_A)DispatchCreate) 
#pragma alloc_text(PAGE, $(PROJECT_NAME_A)DispatchClose) 
#pragma alloc_text(PAGE, $(PROJECT_NAME_A)DispatchDeviceControl) 
#pragma alloc_text(PAGE, $(PROJECT_NAME_A)Unload) 
#endif // ALLOC_PRAGMA 
 
NTSTATUS 
DriverEntry( 
	IN PDRIVER_OBJECT		DriverObject, 
	IN PUNICODE_STRING		RegistryPath 
	) 
{ 
	NTSTATUS			status;     
    UNICODE_STRING		ntDeviceName; 
	UNICODE_STRING		dosDeviceName; 
    PDEVICE_EXTENSION	deviceExtension; 
	PDEVICE_OBJECT		deviceObject = NULL; 
	BOOLEAN				symbolicLink = FALSE; 
 
	//KdBreakPoint(); 
 
    dprintf("[$(PROJECT_NAME)] DriverEntry: %wZ\n", RegistryPath); 
 
    // 
    // TODO: 
    // 
    //     1. Report it's resources (IoReportResourceUsage) 
    // 
    //     2. Attempt to locate the device(s) it supports 
	// 
 
    RtlInitUnicodeString(&ntDeviceName, $(PROJECT_NAME_U)_DEVICE_NAME_W); 
 
    status = IoCreateDevice( 
		DriverObject, 
		sizeof(DEVICE_EXTENSION),		// DeviceExtensionSize 
		&ntDeviceName,					// DeviceName 
		FILE_DEVICE_$(PROJECT_NAME_U),	// DeviceType 
		0,								// DeviceCharacteristics 
		TRUE,							// Exclusive 
		&deviceObject					// [OUT] 
		); 
 
	if (!NT_SUCCESS(status)) 
	{ 
		dprintf("[$(PROJECT_NAME)] IoCreateDevice failed(0x%x).\n", status); 
		goto failed; 
	} 
 
	deviceExtension = (PDEVICE_EXTENSION)deviceObject->DeviceExtension; 
 
	// 
	// TODO: set up synchronization objects, state info,, etc. 
	// 
 
    RtlInitUnicodeString(&dosDeviceName, $(PROJECT_NAME_U)_DOS_DEVICE_NAME_W); 
 
    status = IoCreateSymbolicLink(&dosDeviceName, &ntDeviceName); 
 
    if (!NT_SUCCESS(status)) 
    { 
        dprintf("[$(PROJECT_NAME)] IoCreateSymbolicLink failed(0x%x).\n", status); 
		goto failed; 
    } 
 
	symbolicLink = TRUE; 
 
    DriverObject->MajorFunction[IRP_MJ_CREATE]         = $(PROJECT_NAME_A)DispatchCreate; 
    DriverObject->MajorFunction[IRP_MJ_CLOSE]          = $(PROJECT_NAME_A)DispatchClose; 
    DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = $(PROJECT_NAME_A)DispatchDeviceControl; 
    DriverObject->DriverUnload                         = $(PROJECT_NAME_A)Unload; 
 
    if (NT_SUCCESS(status)) 
	    return status; 
 
failed: 
 
	if (symbolicLink) 
		IoDeleteSymbolicLink(&dosDeviceName); 
 
	if (deviceObject) 
		IoDeleteDevice(deviceObject); 
 
	return status; 
} 
 
NTSTATUS 
$(PROJECT_NAME_A)DispatchCreate( 
	IN PDEVICE_OBJECT		DeviceObject, 
	IN PIRP					Irp 
	) 
{ 
	NTSTATUS status = STATUS_SUCCESS; 
 
    Irp->IoStatus.Information = 0; 
 
	dprintf("[$(PROJECT_NAME)] IRP_MJ_CREATE\n"); 
 
    Irp->IoStatus.Status = status; 
    IoCompleteRequest(Irp, IO_NO_INCREMENT); 
 
    return status; 
} 
 
NTSTATUS 
$(PROJECT_NAME_A)DispatchClose( 
	IN PDEVICE_OBJECT		DeviceObject, 
	IN PIRP					Irp 
	) 
{ 
	NTSTATUS status = STATUS_SUCCESS; 
 
    Irp->IoStatus.Information = 0; 
 
	dprintf("[$(PROJECT_NAME)] IRP_MJ_CLOSE\n"); 
 
    Irp->IoStatus.Status = status; 
    IoCompleteRequest(Irp, IO_NO_INCREMENT); 
 
    return status; 
} 
 
NTSTATUS 
$(PROJECT_NAME_A)DispatchDeviceControl( 
	IN PDEVICE_OBJECT		DeviceObject, 
	IN PIRP					Irp 
	) 
{ 
	NTSTATUS			status = STATUS_INVALID_PARAMETER; 
    PIO_STACK_LOCATION	irpSp; 
    PDEVICE_EXTENSION	deviceExtension; 
    PVOID				ioBuffer; 
    ULONG				inputBufferLength, outputBufferLength; 
	ULONG				ioControlCode; 
 
    irpSp = IoGetCurrentIrpStackLocation(Irp); 
    deviceExtension = (PDEVICE_EXTENSION)DeviceObject->DeviceExtension; 
 
    Irp->IoStatus.Information = 0; 
 
    // 
    // Get the pointer to the input/output buffer and it's length 
    // 
 
    ioBuffer			= Irp->AssociatedIrp.SystemBuffer; 
    inputBufferLength	= irpSp->Parameters.DeviceIoControl.InputBufferLength; 
    outputBufferLength	= irpSp->Parameters.DeviceIoControl.OutputBufferLength; 
    ioControlCode		= irpSp->Parameters.DeviceIoControl.IoControlCode; 
 
    switch (ioControlCode) 
    { 
	case IOCTL_$(PROJECT_NAME_U)_HELLO: 
		{ 
			// 
			// TODO: change the ioControlCode to appropriate code. 
			// 
 
			status = STATUS_SUCCESS; 
 
            break; 
		} 
 
    default: 
		dprintf("[$(PROJECT_NAME)] IRP_MJ_DEVICE_CONTROL: IoControlCode = 0x%x (%04x,%04x)\n", 
			ioControlCode, DEVICE_TYPE_FROM_CTL_CODE(ioControlCode), 
			IoGetFunctionCodeFromCtlCode(ioControlCode)); 
 
        break; 
	} 
 
	// 
	// TODO: if not pending, call IoCompleteRequest and Irp is freed. 
	// 
 
    Irp->IoStatus.Status = status; 
    IoCompleteRequest(Irp, IO_NO_INCREMENT); 
 
    return status; 
} 
 
VOID 
$(PROJECT_NAME_A)Unload( 
	IN PDRIVER_OBJECT		DriverObject 
	) 
{ 
    UNICODE_STRING dosDeviceName; 
 
	// 
    // Free any resources 
    // 
 
    // 
    // Delete the symbolic link 
    // 
 
    RtlInitUnicodeString(&dosDeviceName, $(PROJECT_NAME_U)_DOS_DEVICE_NAME_W); 
 
    IoDeleteSymbolicLink(&dosDeviceName); 
 
    // 
    // Delete the device object 
    // 
 
    IoDeleteDevice(DriverObject->DeviceObject); 
 
    dprintf("[$(PROJECT_NAME)] unloaded\n"); 
}