最近在尝试自己写个驱动程序,但是在用户态调用CreateFile()时总是失败,返回0xffffffff,请各路高手帮我找找问题所在,谢谢了。 // 驱动代码代码如下
C/C++ code
//
// HelloWorldDrv.c
//
#include "ntstatus.h"
#include "ntddk.h"
#include <stdio.h>
#include <stdlib.h>
#define true 1
#define false 0
// const variables
const WCHAR deviceNameBuffer [] = L"\\Device\\HelloWorldDevice";
const WCHAR dosDeviceNameBuffer [] = L"\\DosDevice\\HelloWorldDevice";
// global pointer to our device object
PDEVICE_OBJECT g_helloWorldDevice;
// This is our unload function
void OnUnload ( IN PDRIVER_OBJECT driverObject )
{
UNICODE_STRING strDosDeviceNameUnicodeString;
DbgPrint ( "OnUnload() called!!!!!!\n" );
RtlInitUnicodeString ( &strDosDeviceNameUnicodeString, dosDeviceNameBuffer );
// delete symbolic link
IoDeleteSymbolicLink ( &strDosDeviceNameUnicodeString );
// delete device
IoDeleteDevice ( driverObject->DeviceObject );
}NTSTATUS MyOpen ( IN PDEVICE_OBJECT pDeviceObject, IN PIRP pIRP )
{
DbgPrint ( "MyOpen() called!!!\n" );
return STATUS_SUCCESS;
} NTSTATUS MyClose ( IN PDEVICE_OBJECT pDeviceObject, IN PIRP pIRP )
{
DbgPrint ( "MyClose() called!!!\n" );
return STATUS_SUCCESS;
}NTSTATUS MyRead ( IN PDEVICE_OBJECT pDeviceObject, IN PIRP pIRP )
{
NTSTATUS ntStatus = STATUS_SUCCESS;
DbgPrint ( "MyRead() called!!!!!!\n" );
return ntStatus;
}NTSTATUS MyWrite ( IN PDEVICE_OBJECT pDeviceObject, IN PIRP pIRP )
{
NTSTATUS ntStatus = STATUS_SUCCESS;
DbgPrint ( "MyWrite() called!!!!!!\n" );
return ntStatus;
}// Entry function
NTSTATUS DriverEntry ( IN PDRIVER_OBJECT pDriverObject,
IN PUNICODE_STRING theRegistryPath )
{
//NTSTATUS ntStatus;
UNICODE_STRING strDeviceNameUnicodeString;
UNICODE_STRING strDosDeviceNameUnicodeString;
NTSTATUS ntStatus;
DbgPrint ( "DriverEntry() called!!!!!!\n" );
// set up our name
RtlInitUnicodeString ( &strDeviceNameUnicodeString, deviceNameBuffer );
RtlInitUnicodeString ( &strDosDeviceNameUnicodeString, dosDeviceNameBuffer );
// Initialize the pointer to the unload function in the driverObject
pDriverObject->DriverUnload = OnUnload;
// set functions
pDriverObject->MajorFunction [ IRP_MJ_CREATE ] = MyOpen;
pDriverObject->MajorFunction [ IRP_MJ_CLOSE ] = MyClose;
pDriverObject->MajorFunction [ IRP_MJ_READ ] = MyRead;
pDriverObject->MajorFunction [ IRP_MJ_WRITE ] = MyWrite;
// create device
ntStatus = IoCreateDevice ( pDriverObject, 0,// for driver extension &strDeviceNameUnicodeString, FILE_DEVICE_UNKNOWN, FILE_DEVICE_SECURE_OPEN, false,
&g_helloWorldDevice );
if ( NT_SUCCESS( ntStatus ) )
{
// create a symbolic link to the device
IoCreateSymbolicLink ( & strDosDeviceNameUnicodeString, strDeviceNameUnicodeString );
}
return STATUS_SUCCESS;
}
//
// SOURCES
//
TARGETNAME=HelloWorldDrv
TARGETPATH=OBJ
TARGETTYPE=DRIVER
SOURCES=HelloWorldDrv.c
//
// MAKEFILE
//
!INCLUDE $(NTMAKEENV)\makefile.def
//
// HelloWorldApp.c
//
#include <stdio.h>
#include <windows.h>int main()
{ HANDLE hFile;
DWORD dwReturn; hFile = CreateFile("\\\\.\\HelloWorldDevice",
GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL); if( hFile == INVALID_HANDLE_VALUE )
{
printf("create file failed!!!\n");
}
else
{
CloseHandle(hFile);
}
return 0;
}
C/C++ code
//
// HelloWorldDrv.c
//
#include "ntstatus.h"
#include "ntddk.h"
#include <stdio.h>
#include <stdlib.h>
#define true 1
#define false 0
// const variables
const WCHAR deviceNameBuffer [] = L"\\Device\\HelloWorldDevice";
const WCHAR dosDeviceNameBuffer [] = L"\\DosDevice\\HelloWorldDevice";
// global pointer to our device object
PDEVICE_OBJECT g_helloWorldDevice;
// This is our unload function
void OnUnload ( IN PDRIVER_OBJECT driverObject )
{
UNICODE_STRING strDosDeviceNameUnicodeString;
DbgPrint ( "OnUnload() called!!!!!!\n" );
RtlInitUnicodeString ( &strDosDeviceNameUnicodeString, dosDeviceNameBuffer );
// delete symbolic link
IoDeleteSymbolicLink ( &strDosDeviceNameUnicodeString );
// delete device
IoDeleteDevice ( driverObject->DeviceObject );
}NTSTATUS MyOpen ( IN PDEVICE_OBJECT pDeviceObject, IN PIRP pIRP )
{
DbgPrint ( "MyOpen() called!!!\n" );
return STATUS_SUCCESS;
} NTSTATUS MyClose ( IN PDEVICE_OBJECT pDeviceObject, IN PIRP pIRP )
{
DbgPrint ( "MyClose() called!!!\n" );
return STATUS_SUCCESS;
}NTSTATUS MyRead ( IN PDEVICE_OBJECT pDeviceObject, IN PIRP pIRP )
{
NTSTATUS ntStatus = STATUS_SUCCESS;
DbgPrint ( "MyRead() called!!!!!!\n" );
return ntStatus;
}NTSTATUS MyWrite ( IN PDEVICE_OBJECT pDeviceObject, IN PIRP pIRP )
{
NTSTATUS ntStatus = STATUS_SUCCESS;
DbgPrint ( "MyWrite() called!!!!!!\n" );
return ntStatus;
}// Entry function
NTSTATUS DriverEntry ( IN PDRIVER_OBJECT pDriverObject,
IN PUNICODE_STRING theRegistryPath )
{
//NTSTATUS ntStatus;
UNICODE_STRING strDeviceNameUnicodeString;
UNICODE_STRING strDosDeviceNameUnicodeString;
NTSTATUS ntStatus;
DbgPrint ( "DriverEntry() called!!!!!!\n" );
// set up our name
RtlInitUnicodeString ( &strDeviceNameUnicodeString, deviceNameBuffer );
RtlInitUnicodeString ( &strDosDeviceNameUnicodeString, dosDeviceNameBuffer );
// Initialize the pointer to the unload function in the driverObject
pDriverObject->DriverUnload = OnUnload;
// set functions
pDriverObject->MajorFunction [ IRP_MJ_CREATE ] = MyOpen;
pDriverObject->MajorFunction [ IRP_MJ_CLOSE ] = MyClose;
pDriverObject->MajorFunction [ IRP_MJ_READ ] = MyRead;
pDriverObject->MajorFunction [ IRP_MJ_WRITE ] = MyWrite;
// create device
ntStatus = IoCreateDevice ( pDriverObject, 0,// for driver extension &strDeviceNameUnicodeString, FILE_DEVICE_UNKNOWN, FILE_DEVICE_SECURE_OPEN, false,
&g_helloWorldDevice );
if ( NT_SUCCESS( ntStatus ) )
{
// create a symbolic link to the device
IoCreateSymbolicLink ( & strDosDeviceNameUnicodeString, strDeviceNameUnicodeString );
}
return STATUS_SUCCESS;
}
//
// SOURCES
//
TARGETNAME=HelloWorldDrv
TARGETPATH=OBJ
TARGETTYPE=DRIVER
SOURCES=HelloWorldDrv.c
//
// MAKEFILE
//
!INCLUDE $(NTMAKEENV)\makefile.def
//
// HelloWorldApp.c
//
#include <stdio.h>
#include <windows.h>int main()
{ HANDLE hFile;
DWORD dwReturn; hFile = CreateFile("\\\\.\\HelloWorldDevice",
GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL); if( hFile == INVALID_HANDLE_VALUE )
{
printf("create file failed!!!\n");
}
else
{
CloseHandle(hFile);
}
return 0;
}
楼主,你在MyOpen 例程中返回之前加入
IoCompleteRequest(pIrp,IO_NO_INCREMENT);
试一下.