Windows内核开发 - 无法启动服务:错误1275

时间:2016-10-12 07:50:45

标签: windows service kernel driver

我刚刚使用Code :: Blocks编写了一个简单的内核模式驱动程序,因为我似乎无法成功加载它,因为错误1275似乎告诉我我正在尝试加载32位驱动程序在64位机器上。

ddk(驱动程序开发工具包)似乎是一个32位的。我无法找到64位的,因此我完全不知道如何将驱动程序重新创建为64位驱动程序。

*我使用的是Windows 8.1 64位

这是驱动程序代码:

#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;
}

这是load \ unload代码:

#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

1 个答案:

答案 0 :(得分:-1)

您无需重新创建只需下载WDK8.1的驱动程序,然后随visual studio2013一起安装。

在此之后,您需要使用nmake2msbuild实用程序nmake2msbuild

将项目转换为较新的WDK

,然后你只需在visual studio(.vcxproj)中打开你的项目,并将平台设置为 x64

在为windows8.1构建之后,您必须设置属性配置属性 - &gt;驱动程序设置 - &gt;常规 - &gt;目标Os版本= Windows8.1

现在构建项目并尝试安装驱动程序。如果您在构建项目时遇到任何错误,请将相应的SOURCES文件和MAKEFILE添加到您的项目中,然后尝试构建。