司机第二次无法启动

时间:2016-11-24 15:48:50

标签: c++ windows driver wdk

我开始编程驱动程序。我发了很多文章并开发了一个也在运行的测试驱动程序。在开始时它只是一个Hello World Driver。 在控制台中,我安装驱动程序,启动然后停止并删除。一切都运作良好。 就在我想要安装并再次启动时,它会在函数“StartService”中返回Error no 2。调试消息我可以看到第二次调用DriverEntry函数。 但是当我重新安装PC时,我可以安装并启动驱动程序。 Unfurtunaly我找不到任何关于这个问题的文章。只是返回2的其他问题,即未找到驱动程序。但这一切都有效。

我用以下代码安装驱动程序:

    SC_HANDLE hSCManager;
SC_HANDLE hService;
SERVICE_STATUS ss;

hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);

printf("Load Driver\n");

if(hSCManager)
{
    printf("Create Service\n");
    getchar();

    hService = CreateService(hSCManager, TEXT("Example"), TEXT("Example Driver"), SERVICE_START | DELETE | SERVICE_STOP, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE, TEXT("C:\\Windows\\System32\\drivers\\MyFirstDriver.sys"), NULL, NULL, NULL, NULL, NULL);
    //hService = CreateService(hSCManager, TEXT("Example"), TEXT("Example Driver"), SERVICE_START | DELETE | SERVICE_STOP, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE, TEXT("C:\\driver\\temp2\MyFirstDriver.sys"), NULL, NULL, NULL, NULL, NULL);

    if(!hService)
    {
        printf("OpenService Service\n");
        getchar();
        hService = OpenService(hSCManager, TEXT("Example"), SERVICE_START | DELETE | SERVICE_STOP);
    }

    if(hService)
    {
        printf("Start Service\n");
        getchar();
        if (StartService(hService, 0, NULL))
        {
            printf("Start war erfolgreich\n");
        }
        else
        {
            printf("Fehler beim starten\n");
            printf("StartService failed (%d)\n", GetLastError()); 

        }

        Sleep(5000);
        printf("Press Enter to close service\n");
        getchar();
        printf("beendet\n");
        getchar();
        ControlService(hService, SERVICE_CONTROL_STOP, &ss);



        if (DeleteService(hService)) {
            printf("Delete war erfolgreich\n");
        }
        else
        {
            printf("Fehler beim Delete\n");
            printf("Delete failed (%d)\n", GetLastError()); 
            //Error nr 6
        }

        if (CloseServiceHandle(hService)) {
            printf("Close war erfolgreich\n");
        }
        else
        {
            printf("Fehler beim Close\n");
            printf("Close failed (%d)\n", GetLastError()); 

        }
    }
    printf("Press Enter to close service\n");
        getchar();
    CloseServiceHandle(hSCManager);
}
return 0;

司机代码:

    #include "stdafx.h"
#include "functions.h"

void MyFirstDriverUnload(IN PDRIVER_OBJECT DriverObject);
NTSTATUS MyFirstDriverCreate(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
NTSTATUS MyFirstDriverClose(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
NTSTATUS MyFirstDriverDefaultHandler(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
NTSTATUS MyFirstDriverAddDevice(IN PDRIVER_OBJECT  DriverObject, IN PDEVICE_OBJECT  PhysicalDeviceObject);
NTSTATUS MyFirstDriverPnP(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
NTSTATUS MyFirstDriverRead(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
NTSTATUS MyFirstDriverUnsupportedFunc(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);


typedef struct _deviceExtension
{
PDEVICE_OBJECT DeviceObject;
PDEVICE_OBJECT TargetDeviceObject;
PDEVICE_OBJECT PhysicalDeviceObject;
UNICODE_STRING DeviceInterface;
} MyFirstDriver_DEVICE_EXTENSION, *PMyFirstDriver_DEVICE_EXTENSION;

// {282b517a-9a95-4fdc-8167-aec5f8463df3}
static const GUID GUID_MyFirstDriverInterface = {0x282B517A, 0x9a95, 0x4fdc, {0x81, 0x67, 0xae, 0xc5, 0xf8, 0x46, 0x3d, 0xf3 } };

#ifdef __cplusplus
extern "C" NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING  RegistryPath);
#endif

NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING  RegistryPath)
{

DbgPrint("DriverEntry01 Called \r\n");
NTSTATUS NtStatus = STATUS_SUCCESS;
    INT uiIndex = 0;
DbgPrint("DriverEntry02 Called \r\n");
    PDEVICE_OBJECT pDeviceObject = NULL;
DbgPrint("DriverEntry03 Called \r\n");
    UNICODE_STRING usDriverName, usDosDeviceName;

DbgPrint("DriverEntry3 Called \r\n");
DbgPrint("DriverEntry4 Called \r\n");
DbgPrint("DriverEntry5 Called \r\n");
    DbgPrint("DriverEntry6 Called \r\n");

    RtlInitUnicodeString(&usDriverName, L"\\Device\\Example");
DbgPrint("DriverEntry7 Called \r\n");
    RtlInitUnicodeString(&usDosDeviceName, L"\\DosDevices\\Example"); 
DbgPrint("DriverEntry8 Called \r\n");

    NtStatus = IoCreateDevice(DriverObject, 0, &usDriverName, FILE_DEVICE_UNKNOWN, FILE_DEVICE_SECURE_OPEN, FALSE, &pDeviceObject);
DbgPrint("DriverEntry9 Called \r\n");
if(NtStatus == STATUS_SUCCESS)
    {
    DbgPrint("DriverEntry10 Called \r\n");

    for(uiIndex = 0; uiIndex < IRP_MJ_MAXIMUM_FUNCTION; uiIndex++)
        DriverObject->MajorFunction[uiIndex] = MyFirstDriverUnsupportedFunc;

        DriverObject->MajorFunction[IRP_MJ_CLOSE]             = MyFirstDriverClose;
        DriverObject->MajorFunction[IRP_MJ_CREATE]            = MyFirstDriverCreate;
        DriverObject->MajorFunction[IRP_MJ_PNP]    = MyFirstDriverPnP;
        DriverObject->MajorFunction[IRP_MJ_READ]              = MyFirstDriverRead;

        DriverObject->DriverUnload =  MyFirstDriverUnload; 


        DriverObject->Flags &= (~DO_DEVICE_INITIALIZING);


        IoCreateSymbolicLink(&usDosDeviceName, &usDriverName);
    }


    return NtStatus;


}



void MyFirstDriverUnload(IN PDRIVER_OBJECT DriverObject)
{
DbgPrint("Goodbye from MyFirstDriver!\n");
}


NTSTATUS MyFirstDriverUnsupportedFunc(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
    NTSTATUS NtStatus = STATUS_SUCCESS;
    DbgPrint("MyFirstDriverUnsupportedFunc \r\n");

    return NtStatus;
}

NTSTATUS MyFirstDriverRead(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
    NTSTATUS NtStatus = STATUS_SUCCESS;


    DbgPrint("Example Read Called \r\n");

    return NtStatus;
}

NTSTATUS MyFirstDriverClose(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
NTSTATUS NtStatus = STATUS_SUCCESS;
    DbgPrint("Drive Close \r\n");

    return NtStatus;
}

NTSTATUS MyFirstDriverCreate(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
Irp->IoStatus.Status = STATUS_SUCCESS;
    Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return STATUS_SUCCESS;
}

NTSTATUS MyFirstDriverDefaultHandler(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{

PMyFirstDriver_DEVICE_EXTENSION deviceExtension = NULL;

IoSkipCurrentIrpStackLocation(Irp);
deviceExtension = (PMyFirstDriver_DEVICE_EXTENSION) DeviceObject->DeviceExtension;
return IoCallDriver(deviceExtension->TargetDeviceObject, Irp);

}

NTSTATUS MyFirstDriverAddDevice(IN PDRIVER_OBJECT  DriverObject, IN PDEVICE_OBJECT  PhysicalDeviceObject)
{
PDEVICE_OBJECT DeviceObject = NULL;
PMyFirstDriver_DEVICE_EXTENSION pExtension = NULL;
NTSTATUS status;

status = IoCreateDevice(DriverObject,
                        sizeof(MyFirstDriver_DEVICE_EXTENSION),
                        NULL,
                        FILE_DEVICE_UNKNOWN,
                        0,
                        0,
                        &DeviceObject);

if (!NT_SUCCESS(status))
    return status;

pExtension = (PMyFirstDriver_DEVICE_EXTENSION)DeviceObject->DeviceExtension;

pExtension->DeviceObject = DeviceObject;
pExtension->PhysicalDeviceObject = PhysicalDeviceObject;
pExtension->TargetDeviceObject = IoAttachDeviceToDeviceStack(DeviceObject, PhysicalDeviceObject);

status = IoRegisterDeviceInterface(PhysicalDeviceObject, &GUID_MyFirstDriverInterface, NULL, &pExtension->DeviceInterface);
ASSERT(NT_SUCCESS(status));

DeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
return STATUS_SUCCESS;
}


NTSTATUS MyFirstDriverIrpCompletion(
                  IN PDEVICE_OBJECT DeviceObject,
                  IN PIRP Irp,
                  IN PVOID Context
                  )
{
PKEVENT Event = (PKEVENT) Context;

UNREFERENCED_PARAMETER(DeviceObject);
UNREFERENCED_PARAMETER(Irp);

KeSetEvent(Event, IO_NO_INCREMENT, FALSE);

return(STATUS_MORE_PROCESSING_REQUIRED);
}

NTSTATUS MyFirstDriverForwardIrpSynchronous(
                          IN PDEVICE_OBJECT DeviceObject,
                          IN PIRP Irp
                          )
{
PMyFirstDriver_DEVICE_EXTENSION   deviceExtension;
KEVENT event;
NTSTATUS status;

KeInitializeEvent(&event, NotificationEvent, FALSE);
deviceExtension = (PMyFirstDriver_DEVICE_EXTENSION) DeviceObject->DeviceExtension;

IoCopyCurrentIrpStackLocationToNext(Irp);

IoSetCompletionRoutine(Irp, MyFirstDriverIrpCompletion, &event, TRUE, TRUE, TRUE);

status = IoCallDriver(deviceExtension->TargetDeviceObject, Irp);

if (status == STATUS_PENDING) {
    KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
    status = Irp->IoStatus.Status;
}
return status;
}

NTSTATUS MyFirstDriverPnP(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
PIO_STACK_LOCATION irpSp = IoGetCurrentIrpStackLocation(Irp);
PMyFirstDriver_DEVICE_EXTENSION pExt = ((PMyFirstDriver_DEVICE_EXTENSION)DeviceObject->DeviceExtension);
NTSTATUS status;

ASSERT(pExt);

switch (irpSp->MinorFunction)
{
case IRP_MN_START_DEVICE:
    IoSetDeviceInterfaceState(&pExt->DeviceInterface, TRUE);
    Irp->IoStatus.Status = STATUS_SUCCESS;
    IoCompleteRequest(Irp, IO_NO_INCREMENT);
    return STATUS_SUCCESS;

case IRP_MN_QUERY_REMOVE_DEVICE:
    Irp->IoStatus.Status = STATUS_SUCCESS;
    IoCompleteRequest(Irp, IO_NO_INCREMENT);
    return STATUS_SUCCESS;

case IRP_MN_REMOVE_DEVICE:
    IoSetDeviceInterfaceState(&pExt->DeviceInterface, FALSE);
    status = MyFirstDriverForwardIrpSynchronous(DeviceObject, Irp);
    IoDetachDevice(pExt->TargetDeviceObject);
    IoDeleteDevice(pExt->DeviceObject);
    RtlFreeUnicodeString(&pExt->DeviceInterface);
    Irp->IoStatus.Status = STATUS_SUCCESS;
    IoCompleteRequest(Irp, IO_NO_INCREMENT);
    return STATUS_SUCCESS;

case IRP_MN_QUERY_PNP_DEVICE_STATE:
    status = MyFirstDriverForwardIrpSynchronous(DeviceObject, Irp);
    Irp->IoStatus.Information = 0;
    IoCompleteRequest(Irp, IO_NO_INCREMENT);
    return status;
}
return MyFirstDriverDefaultHandler(DeviceObject, Irp);
}

编辑: 我用sc create和sc start尝试了同样的方法。在第二次,它给出错误no 2以及他找不到文件的附加信息。 但是,正如我所说,他第一次找到文件并加载。第二次没有。

2 个答案:

答案 0 :(得分:1)

您的驱动程序包含大量错误且无法卸载。您在"\\Device\\Example"中创建设备DriverEntry但不删除它MyFirstDriverUnload - 因此此设备并保留驱动程序卸载。您也尝试使用WDM驱动程序,但我确定您没有将驱动程序注册为任何deviceID的WDM,并且您的MyFirstDriverAddDevice从未调用过。 WDM驱动程序(在将自身设备连接到设备堆栈之后)根本无法通过启动/停止卸载,只能在停止/销毁堆栈后卸载。

DriverObject->Flags &= (~DO_DEVICE_INITIALIZING);

其他一个错误。关于IRP处理我已经不说了。但是再次 - 在这个具体的情况下,驱动程序没有被卸载,因为你没有在IoDeleteDevice中调用MyFirstDriverUnload

答案 1 :(得分:0)

我怀疑问题不在于第二次启动驱动程序。但我不确定:你没有检查ControlService(hService, SERVICE_CONTROL_STOP, &ss);的返回值。我的假设是the driver can't stop。在尝试再次启动之前,检查返回值以查看驱动程序是否已停止。