当前位置: 代码迷 >> 驱动开发 >> 自己写的驱动,加载时总是蓝屏,win7系统,望高手解决。该如何解决
  详细解决方案

自己写的驱动,加载时总是蓝屏,win7系统,望高手解决。该如何解决

热度:97   发布时间:2016-04-28 11:05:23.0
自己写的驱动,加载时总是蓝屏,win7系统,望高手解决。
////////////////////////////////////////////driver.h////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
#pragma once

#ifdef _cplusplus
extern "C"
{
#endif
#include <ntddk.h>
#ifdef _cplusplus
}
#endif

#define PAGEDCODE code_seg("PAGE")
#define LOCKEDCODE code_seg()
#define INITCODE code_seg("INIT")

#define PAGEDDATE date_seg("PAGE")
#define LOCKEDDATE date_seg()
#define INITDATE date_seg("INIT")

typedef struct _DEVICE_EXTENSION
{
UNICODE_STRING ustrDevName;
UNICODE_STRING ustrSymName;
PDEVICE_OBJECT pdevObj;
} DEVICE_EXTENSION,*PDEVICE_EXTENSION;

NTSTATUS CreateDevice(IN PDRIVER_OBJECT pDriverObject);
NTSTATUS HelloDDKWriteRoutine(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp);
NTSTATUS HelloDDKReadRoutine(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp);
NTSTATUS HelloDDKCloseRoutine(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp);
NTSTATUS HelloDDKCreateRoutine(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp);
void UnLoadRoutine(IN PDRIVER_OBJECT pDriverObject);


//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////driver.cpp/////////////////////////////////////////////////////
#include "driver.h"


#pragma INITCODE
extern "C"
NTSTATUS DriverEntry(IN PDRIVER_OBJECT pDriverObject,IN UNICODE_STRING ustrRegisterPath)
{
NTSTATUS status = STATUS_SUCCESS;

pDriverObject->DriverUnload = UnLoadRoutine;
pDriverObject->MajorFunction[IRP_MJ_CLOSE] = HelloDDKCloseRoutine;
pDriverObject->MajorFunction[IRP_MJ_READ] = HelloDDKReadRoutine;
pDriverObject->MajorFunction[IRP_MJ_WRITE] = HelloDDKWriteRoutine;
pDriverObject->MajorFunction[IRP_MJ_CREATE] = HelloDDKCreateRoutine;

status = CreateDevice(pDriverObject);


return status;
}

#pragma INITCODE
NTSTATUS CreateDevice(IN PDRIVER_OBJECT pDriverObject)
{
NTSTATUS status;
PDEVICE_OBJECT pDevObj;
PDEVICE_EXTENSION pDevExt;
UNICODE_STRING devName;

RtlInitUnicodeString(&devName,L"\\DEVICE\\MyDevice");

status = IoCreateDevice(pDriverObject,sizeof(DEVICE_EXTENSION),&(UNICODE_STRING)devName,FILE_DEVICE_UNKNOWN,0,true,&pDevObj);

if(!NT_SUCCESS(status))
{
return status;
}

// pDevObj->NextDevice = NULL;
pDevObj->Flags |= DO_BUFFERED_IO;

pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension;
pDevExt->pdevObj = pDevObj;
pDevExt->ustrDevName = devName;

UNICODE_STRING symName;
RtlInitUnicodeString(&symName,L"\\??\\MyOwnDevice");
pDevExt->ustrSymName = symName;
status = IoCreateSymbolicLink(&symName,&devName);
if(!NT_SUCCESS(status))
{
IoDeleteDevice(pDevObj);
return status;
}

return STATUS_SUCCESS;

}

#pragma PAGEDCODE
NTSTATUS HelloDDKWriteRoutine(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp)
{
NTSTATUS status = STATUS_SUCCESS;

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

IoCompleteRequest(pIrp,IO_NO_INCREMENT);

return status;
}

#pragma PAGEDCODE
NTSTATUS HelloDDKReadRoutine(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp)
{
NTSTATUS status = STATUS_SUCCESS;

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

IoCompleteRequest(pIrp,IO_NO_INCREMENT);

return status;
}

#pragma PAGEDCODE
  相关解决方案