Я начал программировать драйвер. Я краснул много статей и разработал тестовый драйвер, который также работает. В начале это всего лишь Hello World Driver. В консоли я устанавливаю драйвер, запускаю, а затем останавливаю и удаляю. Все работает хорошо. Просто, когда я хочу установить и запустить его снова, он возвращает Error no 2 в функции «StartService». Throught Debug-Message. Я вижу, что во второй раз функция DriverEntry не вызывается. Но когда я перезаряжаю компьютер, я могу установить и запустить драйвер. 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);
}
Edit: Я попробовал то же самое с СБН создать и подкожно начать. Во второй раз он дает ошибку № 2 с дополнительной информацией, что он не может найти файл. Но, как я уже сказал, в первый раз он может найти файл и загрузить. Второй раз нет.
Спасибо за совет. Как я уже писал, я начал развиваться. Поэтому я проверю это. Большое спасибо! – michi101200
вызов IoDeleteDevice (pDeviceObject) в DriverUnload и исправление этого 'DriverObject-> Flags & = (~ DO_DEVICE_INITIALIZING);' как минимум – RbMm
Спасибо, это помогло !!!!! – michi101200