CreateFileA Visit a windows file system driver returned INVALID_HANDLE_VALUE

Dan 1 Reputation point
2020-06-10T03:49:46.707+00:00

I writed a file system demo most likely fast fat ,but when I use CreateFileA to call my dirver ,it gives an INVALID_HANDLE_VALUE result . I have checked the driver `s IRP dispatches and found no DbgPrint setting in the function entries triggered .
This is the user mode code:

/***************This is the driver`s entry point code:*************************/

NTSTATUS
DriverEntry(
    _In_ PDRIVER_OBJECT DriverObject,
    _In_ PUNICODE_STRING RegistryPath
    )
{
    NTSTATUS Status;
    UNICODE_STRING UnicodeString;
    FS_FILTER_CALLBACKS FilterCallbacks;

    UNREFERENCED_PARAMETER( RegistryPath );
    DbgPrint("\nThis is HRFS Driver Entry\n");
    RtlInitUnicodeString(&UnicodeString, L"\\fastFatDemo");
    gSFilterDriverObject = DriverObject;

    Status = IoCreateDevice( DriverObject,
                             0,
                             &UnicodeString,
                             FILE_DEVICE_DISK_FILE_SYSTEM,
                             0,
                             FALSE,
                             &HrfsDiskFileSystemDeviceObject );

    if (!NT_SUCCESS( Status )) {
        return Status;
    }
    DbgPrint("HRFS device HRFS created\n ");
    DriverObject->DriverUnload = FatUnload;

    DriverObject->MajorFunction[IRP_MJ_CREATE]                   = (PDRIVER_DISPATCH)FatFsdCreate;
    DriverObject->MajorFunction[IRP_MJ_CLOSE]                    = (PDRIVER_DISPATCH)HrfsFsdClose;
    DriverObject->MajorFunction[IRP_MJ_READ]                     = (PDRIVER_DISPATCH)FatFsdRead;
    DriverObject->MajorFunction[IRP_MJ_WRITE]                    = (PDRIVER_DISPATCH)FatFsdWrite;
    DriverObject->MajorFunction[IRP_MJ_QUERY_INFORMATION]        = (PDRIVER_DISPATCH)FatFsdQueryInformation;
    DriverObject->MajorFunction[IRP_MJ_SET_INFORMATION]          = (PDRIVER_DISPATCH)FatFsdSetInformation;
    DriverObject->MajorFunction[IRP_MJ_QUERY_EA]                 = (PDRIVER_DISPATCH)FatFsdQueryEa;
    DriverObject->MajorFunction[IRP_MJ_SET_EA]                   = (PDRIVER_DISPATCH)FatFsdSetEa;
    DriverObject->MajorFunction[IRP_MJ_FLUSH_BUFFERS]            = (PDRIVER_DISPATCH)FatFsdFlushBuffers;
    DriverObject->MajorFunction[IRP_MJ_QUERY_VOLUME_INFORMATION] = (PDRIVER_DISPATCH)FatFsdQueryVolumeInformation;
    DriverObject->MajorFunction[IRP_MJ_SET_VOLUME_INFORMATION]   = (PDRIVER_DISPATCH)FatFsdSetVolumeInformation;
    DriverObject->MajorFunction[IRP_MJ_CLEANUP]                  = (PDRIVER_DISPATCH)FatFsdCleanup;
    DriverObject->MajorFunction[IRP_MJ_DIRECTORY_CONTROL]        = (PDRIVER_DISPATCH)FatFsdDirectoryControl;
    DriverObject->MajorFunction[IRP_MJ_FILE_SYSTEM_CONTROL]      = (PDRIVER_DISPATCH)FatFsdFileSystemControl;
    DriverObject->MajorFunction[IRP_MJ_LOCK_CONTROL]             = (PDRIVER_DISPATCH)FatFsdLockControl;
    DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL]           = (PDRIVER_DISPATCH)FatFsdDeviceControl;
    DriverObject->MajorFunction[IRP_MJ_SHUTDOWN]                 = (PDRIVER_DISPATCH)FatFsdShutdown;
    DriverObject->MajorFunction[IRP_MJ_PNP]                      = (PDRIVER_DISPATCH)FatFsdPnp;
    DbgPrint("HRFS device HRFS MajorFunction created\n ");
    DriverObject->FastIoDispatch = NULL;
    DbgPrint("HRFS device HRFS FatFastIoDispatch created\n ");
    RtlZeroMemory(&FatFastIoDispatch, sizeof(FatFastIoDispatch));

    FatFastIoDispatch.SizeOfFastIoDispatch =    sizeof(FAST_IO_DISPATCH);
    FatFastIoDispatch.FastIoCheckIfPossible =   FALSE;  //  CheckForFastIo
    FatFastIoDispatch.FastIoRead = FALSE;             //  Read
    FatFastIoDispatch.FastIoWrite = FALSE;            //  Write
    FatFastIoDispatch.FastIoQueryBasicInfo = FALSE;     //  QueryBasicInfo
    FatFastIoDispatch.FastIoQueryStandardInfo = FALSE;       //  QueryStandardInfo
    FatFastIoDispatch.FastIoLock = FALSE;               //  Lock
    FatFastIoDispatch.FastIoUnlockSingle = FALSE;       //  UnlockSingle
    FatFastIoDispatch.FastIoUnlockAll = FALSE;          //  UnlockAll
    FatFastIoDispatch.FastIoUnlockAllByKey = FALSE;     //  UnlockAllByKey
    FatFastIoDispatch.FastIoQueryNetworkOpenInfo = FALSE;
    FatFastIoDispatch.AcquireForCcFlush = FALSE;
    FatFastIoDispatch.ReleaseForCcFlush = FALSE;
    FatFastIoDispatch.MdlRead = FALSE;
    FatFastIoDispatch.MdlReadComplete = FALSE;
    FatFastIoDispatch.PrepareMdlWrite = FALSE;
    FatFastIoDispatch.MdlWriteComplete = FALSE;


    //FatFastIoDispatch.SizeOfFastIoDispatch = sizeof(FAST_IO_DISPATCH);
    //FatFastIoDispatch.FastIoCheckIfPossible = FatFastIoCheckIfPossible;  //  CheckForFastIo
    //FatFastIoDispatch.FastIoRead = FsRtlCopyRead;             //  Read
    //FatFastIoDispatch.FastIoWrite = FsRtlCopyWrite;            //  Write
    //FatFastIoDispatch.FastIoQueryBasicInfo = FatFastQueryBasicInfo;     //  QueryBasicInfo
    //FatFastIoDispatch.FastIoQueryStandardInfo = FatFastQueryStdInfo;       //  QueryStandardInfo
    //FatFastIoDispatch.FastIoLock = FatFastLock;               //  Lock
    //FatFastIoDispatch.FastIoUnlockSingle = FatFastUnlockSingle;       //  UnlockSingle
    //FatFastIoDispatch.FastIoUnlockAll = FatFastUnlockAll;          //  UnlockAll
    //FatFastIoDispatch.FastIoUnlockAllByKey = FatFastUnlockAllByKey;     //  UnlockAllByKey
    //FatFastIoDispatch.FastIoQueryNetworkOpenInfo = FatFastQueryNetworkOpenInfo;
    //FatFastIoDispatch.AcquireForCcFlush = HrfsAcquireForCcFlush;
    //FatFastIoDispatch.ReleaseForCcFlush = HrfsReleaseForCcFlush;
    //FatFastIoDispatch.MdlRead = FsRtlMdlReadDev;
    //FatFastIoDispatch.MdlReadComplete = FsRtlMdlReadCompleteDev;
    //FatFastIoDispatch.PrepareMdlWrite = FsRtlPrepareMdlWriteDev;
    //FatFastIoDispatch.MdlWriteComplete = FsRtlMdlWriteCompleteDev;





   /*  FsFilter通知回调例程在下层文件系统执行某些操作之前或之后调用。
       如果需要获取更多有关于FsFilter回调例程相关信息,可参见FsRtlRegisterFileSystemFilterCallbacks例程 
       为了注册FsFilter的通知回调例程必须分配并初始化FS_FILTER_CALLBACKS结构体,然后向该结构体中促出FsFilter回调例程,
       并将存储有Callbacks parameter到FsRtlRegisterFileSystemFilterCallbacks中。*/

    RtlZeroMemory( &FilterCallbacks,
                   sizeof(FS_FILTER_CALLBACKS) );

    FilterCallbacks.SizeOfFsFilterCallbacks = sizeof(FS_FILTER_CALLBACKS);
    FilterCallbacks.PreAcquireForSectionSynchronization = HrfsFilterCallbackAcquireForCreateSection;

    Status = FsRtlRegisterFileSystemFilterCallbacks( DriverObject,
                                                     &FilterCallbacks );

    if (!NT_SUCCESS( Status )) {

        IoDeleteDevice( HrfsDiskFileSystemDeviceObject );

        return Status;
    }
     DbgPrint("HRFS device HRFS FilterCallbacks registed\n ");


    RtlZeroMemory( &HrfsData, sizeof(FAT_DATA));

    HrfsData.NodeTypeCode = HRFS_NTC_DATA_HEADER;
    HrfsData.NodeByteSize = sizeof(FAT_DATA);

    InitializeListHead(&HrfsData.VcbQueue);

    pDriverObject = DriverObject;
    HrfsData.DiskFileSystemDeviceObject = HrfsDiskFileSystemDeviceObject;


    //
    //  This list head keeps track of closes yet to be done.
    //

    InitializeListHead( &HrfsData.AsyncCloseList );
    InitializeListHead( &HrfsData.DelayedCloseList );

    HrfsData.FatCloseItem = IoAllocateWorkItem( HrfsDiskFileSystemDeviceObject);

    if (HrfsData.FatCloseItem == NULL) {
        IoDeleteDevice (HrfsDiskFileSystemDeviceObject);

        return STATUS_INSUFFICIENT_RESOURCES;
    }
    DbgPrint("HRFS device HRFS workitem created\n ");
    //
    //  Allocate the zero page
    //

    HrfsData.ZeroPage = ExAllocatePoolWithTag( NonPagedPoolNx, PAGE_SIZE, 'ZtaF' );
    if (HrfsData.ZeroPage == NULL) {
        IoDeleteDevice (HrfsDiskFileSystemDeviceObject);

        return STATUS_INSUFFICIENT_RESOURCES;
    }
    RtlZeroMemory( HrfsData.ZeroPage, PAGE_SIZE );


    //
    //  Now initialize our general purpose spinlock (gag) and figure out how
    //  deep and wide we want our delayed lists (along with fooling ourselves
    //  about the lookaside depths).
    //

    KeInitializeSpinLock( &HrfsData.GeneralSpinLock );


    HrfsData.CacheManagerCallbacks.AcquireForLazyWrite  = &HrfsAcquireFcbForLazyWrite;
    HrfsData.CacheManagerCallbacks.ReleaseFromLazyWrite = &HrfsReleaseFcbFromLazyWrite;
    HrfsData.CacheManagerCallbacks.AcquireForReadAhead  = &HrfsAcquireFcbForReadAhead;
    HrfsData.CacheManagerCallbacks.ReleaseFromReadAhead = &HrfsReleaseFcbFromReadAhead;

    HrfsData.CacheManagerNoOpCallbacks.AcquireForLazyWrite  = &HrfsNoOpAcquire;
    HrfsData.CacheManagerNoOpCallbacks.ReleaseFromLazyWrite = &HrfsNoOpRelease;
    HrfsData.CacheManagerNoOpCallbacks.AcquireForReadAhead  = &HrfsNoOpAcquire;
    HrfsData.CacheManagerNoOpCallbacks.ReleaseFromReadAhead = &HrfsNoOpRelease;

    //
    //  Set up global pointer to our process.
    //

    HrfsData.OurProcess = PsGetCurrentProcess();
    DbgPrint("HRFS device HRFS our process getted\n ");
    // 
    //  Setup the number of processors we support for statistics as the current number 
    //  running.
    //

#if (NTDDI_VERSION >= NTDDI_VISTA)
    HrfsData.NumberProcessors = KeQueryActiveProcessorCount( NULL );
#else
    HrfsData.NumberProcessors = KeNumberProcessors;
#endif


    DbgPrint("HrfsData.NumberProcessors :%d", HrfsData.NumberProcessors);
    ExInitializeResourceLite( &HrfsData.Resource );
    IoRegisterFileSystem(HrfsDiskFileSystemDeviceObject);
    ObReferenceObject (HrfsDiskFileSystemDeviceObject);
    DbgPrint("Device HRFS HrfsDiskFileSystemDeviceObject registed\n ");


    return( STATUS_SUCCESS );
}

/**********This is the user mode testing code *******************/
public IntPtr LoadDriver(string lpFileName)
{
int error = 0;

                string openName = string.Format("\\\\.\\{0}", EXE_DRIVER_NAME);
                IntPtr hSCManager = WinAPI.OpenSCManager(null, null,
                WinAPI.SC_MANAGER_CREATE_SERVICE);
                if (IntPtr.Zero != hSCManager)
                {
                    IntPtr hService = WinAPI.CreateService(hSCManager, EXE_DRIVER_NAME,
                        DISPLAY_NAME, WinAPI.SERVICE_START,
                        WinAPI.SERVICE_KERNEL_DRIVER, WinAPI.SERVICE_DEMAND_START,
                        WinAPI.SERVICE_ERROR_IGNORE, lpFileName, null, IntPtr.Zero, null, null, null);

                    if (WinAPI.ERROR_SERVICE_EXISTS == WinAPI.GetLastError())
                    {
                        hService = WinAPI.OpenService(hSCManager, EXE_DRIVER_NAME, WinAPI.SERVICE_START);
                    }
                    error = WinAPI.GetLastError();
                    if(error!=0)
                    {
                        dumpErrorCode("OpenService failed ", error);

                    }

                    int startflag = WinAPI.StartService(hService, 0, 0);
                    if (startflag == 0)
                    {
                        error = WinAPI.GetLastError();
                        if (error != WinAPI.ERROR_SERVICE_ALREADY_RUNNING)  
                        {
                            dumpErrorCode("StartService failed", error);
                        }
                        else
                        {
                              MessageBox.Show("Server started");
                        }
                    }

                   WinAPI.CloseServiceHandle(hService);
                  WinAPI.CloseServiceHandle(hSCManager);
                    MessageBox.Show(openName);
                    try
                    {
                        hDriver = WinAPI.CreateFileA(openName, WinAPI.GENERIC_READ , 0, IntPtr.Zero, WinAPI.OPEN_EXISTING, null, IntPtr.Zero);


                        if (hDriver == (IntPtr)(-1))
                        {
                            dumpErrorCode("failed to get handle ", error);
                        }
                        else
                        {
                            MessageBox.Show("create device succeed");
                            //his.OpenDriverEvent();
                        }
                    }
                    catch (Exception exp)
                    {
                        MessageBox.Show(exp.Message);
                    }
                }
                return hDriver;
            }
Not Monitored
Not Monitored
Tag not monitored by Microsoft.
37,591 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Roy Li - MSFT 32,731 Reputation points Microsoft Vendor
    2020-06-10T05:41:03.823+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Currently, Microsoft Q&A supports the products listed over here: supported topics (more to be added later on).

    Your question about window driver is not supported yet now. You could ask about this in the Windows Hardware WDK and Driver Development forum.

    Thank you.

    0 comments No comments