【发布时间】:2017-02-20 22:35:21
【问题描述】:
我刚刚使用 Code::Blocks 编写了一个简单的内核模式驱动程序,并且由于错误 1275 似乎我无法成功加载它,这似乎告诉我我正在尝试在一个 32 位驱动程序上加载64位机器。
ddk(驱动程序开发工具包)似乎是 32 位的。我找不到 64 位的驱动程序,因此我完全不知道如何将驱动程序重新创建为 64 位驱动程序。
*我使用的是 windows 8.1 64bit
这是驱动代码:
#include "ddk/ntddk.h"
VOID __stdcall OnUnload( IN PDRIVER_OBJECT DriverObject )
{
DbgPrint("OnUnload called\n");
}
NTSTATUS __stdcall DriverEntry(IN PDRIVER_OBJECT theDriverObject, IN PUNICODE_STRING theRegistryPath)
{
DbgPrint("I loaded!\n");
theDriverObject->DriverUnload = OnUnload;
return STATUS_SUCCESS;
}
这是加载\卸载代码:
#include <stdio.h>
#include <windows.h>
#define false 0
#define true 1
int _util_unload_sysfile(char *driver)
{
char string[512] = "sc delete ";
strcat(string, driver); /* unsafe , fix later */
system(string);
return 1;
}
int _util_load_sysfile(char *theDriverName)
{
char aPath[1024];
char aCurrentDirectory[515];
SC_HANDLE sh = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if(!sh)
{
printf("Failed in OpenSCManager");
return false;
}
GetCurrentDirectory( 512, aCurrentDirectory);
_snprintf(aPath, 1022, "%s\\%s.sys", aCurrentDirectory, theDriverName);
printf("Loading %s\n", aPath);
SC_HANDLE rh = CreateService(sh, theDriverName, theDriverName, SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, aPath, NULL, NULL, NULL, NULL, NULL);
if(!rh)
{
printf("Failed to create service: ");
if (GetLastError() == ERROR_SERVICE_EXISTS)
{
// service exists
printf("Service already exists\n");
rh = OpenService(sh, theDriverName, SERVICE_ALL_ACCESS);
if(!rh)
{
CloseServiceHandle(sh);
return false;
}
}
else
{
printf("Unknown error\n");
CloseServiceHandle(sh);
return false;
}
}
// start the drivers
else
{
printf("Starting driver\n");
if(0 == StartService(rh, 0, NULL))
{
printf("Failed to start service\n");
printf("Last error: %d\n", GetLastError());
// if(ERROR_SERVICE_ALREADY_RUNNING == GetLastError())
// {
// printf("Service already running\n");
// // no real problem
// }
// else
// {
// CloseServiceHandle(sh);
// CloseServiceHandle(rh);
// return false;
// }
}
else
{
printf("Service started\n");
}
CloseServiceHandle(sh);
CloseServiceHandle(rh);
}
return true;
}
这是输出:
C:\Users\...\Desktop>a.exe load driver
Loading C:\Users\...\Desktop\driver.sys
Starting driver
Failed to start service
Last error: 1275
【问题讨论】:
-
尝试为 Windows 7 安装 WDK 并使用其
Build Environments之一编译驱动程序。有一个生产 64 位驱动程序的环境。您需要为您的驱动程序创建一个 MAKEFILE 和一个 SOURCES 文件。查看随 WDK 提供的示例,以便您了解如何处理它。或者,查看这个示例(来自我):jadro-windows.cz/download/hello.zip.
标签: windows service kernel driver