//驱动文件xx.c
#include <wdm.h>
#include <ntddk.h>
NTSTATUS DriverDispatch(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
{
Irp->IoStatus.Status = STATUS_SUCCESS;
IoCompleteRequest (Irp,IO_NO_INCREMENT);
return Irp->IoStatus.Status;
}
void DriverUnload(IN PDRIVER_OBJECT DriverObject)
{
DbgPrint("DriverUnload!\n");
}
NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
{
DbgPrint("DriverEntry!\n");
DriverObject->MajorFunction[IRP_MJ_CREATE] =
DriverObject->MajorFunction[IRP_MJ_CLOSE] = DriverDispatch;
DriverObject->DriverUnload = DriverUnload;
return STATUS_SUCCESS;
}
//加载测试代码:
BOOL CSvcCtl::Install(const char *svrname, const char *path)
{
BOOL ret=FALSE;
HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
if(hSCManager)
{
HANDLE hService = CreateService(hSCManager, svrname,
svrname,
SERVICE_START | DELETE | SERVICE_STOP,
SERVICE_KERNEL_DRIVER,
SERVICE_DEMAND_START,
SERVICE_ERROR_NORMAL,
path,
NULL, NULL, NULL, NULL, NULL);
if (hService) {
ret = TRUE;
CloseServiceHandle(hService);
}
else if (GetLastError() == ERROR_SERVICE_EXISTS) {
ret = TRUE;
}
CloseServiceHandle(hSCManager);
}
return ret;
}
BOOL CSvcCtl::Start(const char *svrname)
{
BOOL ret=FALSE;
HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
if(hSCManager)
{
HANDLE hService = OpenService(hSCManager, svrname,
SERVICE_START | DELETE | SERVICE_STOP);
if (hService) {
ret = StartService(hService, 0, NULL);
if (!ret && GetLastError() == ERROR_SERVICE_ALREADY_RUNNING) {
ret = TRUE;
}
CloseServiceHandle(hService);
}
CloseServiceHandle(hSCManager);
}
return ret;
}
//调用:
CSvcCtl::Install("xem", "E:\\xem.sys");
CSvcCtl::Start("xem");
//xem是随便起的名字,也换过其它,但无效。
//返回失败。错误代码1058:无法启动服务,原因可能是已被禁用或与其相关联的设备没有启动。
//而且在dbgview看到一加载就立刻Unload了,请教这代码哪里有问题?
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课