far*_*dve 5 c windows winapi wdk
出于我的目的,我需要为Windows编写内核模式驱动程序.目前我正在尝试使其在Windows 7 x64下运行.
我在Visual Studio 2012中创建了一个简单项目,其中包含KMDF驱动程序的默认代码.我通过测试签名编译了代码.驱动程序已编译并签名.我也启用了Test-Signing ON,清晰地显示在桌面的左下角.
在尝试启动驱动程序作为服务时,我总是得到一个错误代码6:无效句柄错误.(我已经简化了代码,只是尝试启动它但仍然无效;默认代码也不起作用)
基本上,我遇到的问题与这里提出的问题相同
不幸的是他从来没有回答过.我尝试了提供的解决方案,但它也没有帮助.
我试图启动驱动程序的代码是
int _cdecl main(void)
{
HANDLE hSCManager;
HANDLE hService;
SERVICE_STATUS ss;
hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
printf("Load Driver\n");
if(hSCManager)
{
printf("Create Service\n");
hService = CreateService(hSCManager, "Example",
"Example Driver",
SERVICE_ALL_ACCESS | SERVICE_START | DELETE | SERVICE_STOP ,
SERVICE_KERNEL_DRIVER,
SERVICE_DEMAND_START,
SERVICE_ERROR_IGNORE,
"\\path\\to\\driver\\KMDFDriver1.sys",
NULL, NULL, NULL, NULL, NULL);
if(!hService)
{
hService = OpenService(hSCManager, "Example",
SERVICE_ALL_ACCESS | SERVICE_START | DELETE | SERVICE_STOP);
if(!hService)
{
// If initial startup of the driver failed, it will fail here.
process_error();
return 0;
}
}
if(hService)
{
printf("Start Service\n");
if(StartService(hService, 0, NULL) == 0)
{
// Start service ALWAYS returns 0. Only when executed for the first time. Next time it fails on OpenService.
process_error();
printf("Did not start!\n");
}
printf("Press Enter to close service\r\n");
getchar();
ControlService(hService, SERVICE_CONTROL_STOP, &ss);
DeleteService(hService);
CloseServiceHandle(hService);
}
CloseServiceHandle(hSCManager);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是驱动程序代码
DRIVER_INITIALIZE DriverEntry;
#ifdef ALLOC_PRAGMA
#pragma alloc_text (INIT, DriverEntry)
#endif
NTSTATUS
DriverEntry(
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING RegistryPath
)
{
WDF_DRIVER_CONFIG config;
NTSTATUS status;
DbgPrint("Hello World!\n");
WDF_DRIVER_CONFIG_INIT(&config,
NULL
);
config.DriverInitFlags = WdfDriverInitNonPnpDriver;
status = WdfDriverCreate(DriverObject,
RegistryPath,
WDF_NO_OBJECT_ATTRIBUTES,
&config,
WDF_NO_HANDLE
);
if (!NT_SUCCESS(status)) {
KdPrint( ("WdfDriverCreate failed with "
"status 0x%x\n", status));
}
return status;
}
Run Code Online (Sandbox Code Playgroud)
该函数process_error()是一个包装器GetLastError(),除了提供数值之外,还显示错误代码的文本版本.我已经用尽了解决这个问题的所有选项.谷歌搜索只发现了这个问题,并在这里提出了问题.
问题是什么?
额外说明:驱动程序是使用Visual Studio 2012 Ultimate编译的,而我的启动代码是使用MinGW-W64编译的(使用GCC).但启动代码应该与驱动程序无关.
额外注意事项2:经过长时间的疑惑后,我开始考虑是否是测试签名证书,因为我尝试了从MSDN提供的驱动程序源代码,并且在成功编译后,我仍然得到了ERROR_INVALID_HANDLE(错误代码6)试图开始它.我还没有找到解决方案.
小智 9
我将其跟踪到驱动程序的项目设置.该项目缺少KMDF版本.
调整以下内容(在"驱动程序模型设置"下):
- KMDF版本主要= 1
- KMDF版本次要= 9
点击"确定",重新编译并重新安装.为我工作!
Rem*_*eau -1
您的调用OpenSCManager()只是请求SC_MANAGER_CREATE_SERVICE其本身的许可,这不足以OpenService()成功StartService()。