本文介绍了CreateService获取错误:ERROR_INVALID_ADDRESS(0x000001e7)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

plz帮我解决这个问题:

我用以下代码创建基本服务:

  #include"stdafx.h"PWSTR pszServiceName;PWSTR pszDisplayName;DWORD dwStartType;PWSTR pszDependencies;PWSTR pszAccount;PWSTR pszPassword;#定义MAX_PATH 100void __cdecl _tmain(int argc,TCHAR * argv []){wchar_t szPath [MAX_PATH];SC_HANDLE schSCManager = NULL;SC_HANDLE schService = NULL;如果(GetModuleFileName(NULL,szPath,ARRAYSIZE(szPath))== 0){wprintf(L"GetModuleFileName失败,错误0x%08lx \ n",GetLastError());转到清理;}//打开本地默认服务控制管理器数据库schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_CONNECT |SC_MANAGER_CREATE_SERVICE);如果(schSCManager == NULL){wprintf(L"OpenSCManager失败,错误0x%08lx \ n",GetLastError());转到清理;}//通过调用CreateService将服务安装到SCM中schService = CreateService(schSCManager,//SCManager数据库pszServiceName,//服务名称pszDisplayName,//要显示的名称SERVICE_QUERY_STATUS,//所需的访问权限SERVICE_WIN32_OWN_PROCESS,//服务类型dwStartType,//服务启动类型SERVICE_ERROR_NORMAL,//错误控制类型szPath,//服务的二进制文件NULL,//没有加载顺序组NULL,//没有标签标识符pszDependencies,//依赖关系pszAccount,//服务运行帐户pszPassword//帐户密码);如果(schService == NULL){wprintf(L"CreateService failed w/err 0x%08lx \ n",GetLastError());转到清理;}wprintf(L%s已安装.\ n",pszServiceName);清理://集中清理所有分配的资源.如果(schSCManager){CloseServiceHandle(schSCManager);schSCManager = NULL;}如果(schService){CloseServiceHandle(schService);schService = NULL;}} 

当我运行它时,出现错误:CreateService失败,错误0x000001e7(我只知道它是:ERROR_INVALID_ADDRESS)-但我不知道这是什么意思,以及如何解决.

任何人都可以帮助我.

解决方案

除了 schSCManager szPath 变量外, all 您传递给 CreateService()的其他变量尚未初始化,它们包含随机值.这对于 psz ... 变量尤其重要,因为它们是指针,因此您可以有效地将随机内存地址传递给 CreateService().这就是为什么出现 ERROR_INVALID_ADDRESS 错误的原因.

您需要初始化变量!

pszServiceName 需要指向一个以空值结尾的字符串,其中包含所需的服务名称.

pszDisplayName 需要指向一个以空值结尾的字符串,其中包含所需的服务显示名称.

dwStartType 必须包含有效的起始类型整数值.

pszDependencies 必须为NULL,或指向包含以空分隔的服务名称列表的双空终止字符串.

pszAccount 必须为NULL或指向一个以空终止的字符串,其中包含要在其中运行服务的所需用户帐户.

pszPassword 必须为NULL或指向包含以 pszAccount 帐户为密码的空终止字符串.

编辑:您最好简单地完全删除变量,然后将所需的值直接传递给 CreateService().试试这个:

  #include"stdafx.h"void __cdecl _tmain(int argc,TCHAR * argv []){wchar_t szPath [MAX_PATH + 1];SC_HANDLE schSCManager = NULL;SC_HANDLE schService = NULL;如果(GetModuleFileName(NULL,szPath,ARRAYSIZE(szPath))== 0){wprintf(L"GetModuleFileName失败,错误0x%08lx \ n",GetLastError());转到清理;}//打开本地默认服务控制管理器数据库schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE);如果(schSCManager == NULL){wprintf(L"OpenSCManager失败,错误0x%08lx \ n",GetLastError());转到清理;}//通过调用CreateService将服务安装到SCM中schService = CreateService(schSCManager,//SCManager数据库L"Win32_Service,//服务名称L我的服务,//要显示的名称SERVICE_QUERY_STATUS,//所需的访问权限SERVICE_WIN32_OWN_PROCESS,//服务类型SERVICE_DEMAND_START,//服务启动类型SERVICE_ERROR_NORMAL,//错误控制类型szPath,//服务的二进制文件NULL,//没有加载顺序组NULL,//没有标签标识符NULL,//没有依赖关系L"NT AUTHORITY \\ LocalService",//服务运行帐户NULL//没有帐户密码);如果(schService == NULL){wprintf(L"CreateService failed w/err 0x%08lx \ n",GetLastError());转到清理;}wprintf(L已安装服务.\ n");清理://集中清理所有分配的资源.如果(schService){CloseServiceHandle(schService);schService = NULL;}如果(schSCManager){CloseServiceHandle(schSCManager);schSCManager = NULL;}} 

plz help me this problem:

I create a basic service in this code:

#include "stdafx.h"

PWSTR pszServiceName;
PWSTR pszDisplayName;
DWORD dwStartType;
PWSTR pszDependencies;
PWSTR pszAccount;
PWSTR pszPassword;

#define MAX_PATH 100

void __cdecl _tmain(int argc, TCHAR *argv[])
{
    wchar_t szPath[MAX_PATH];
    SC_HANDLE schSCManager = NULL;
    SC_HANDLE schService = NULL;

    if (GetModuleFileName(NULL, szPath, ARRAYSIZE(szPath)) == 0)
    {
        wprintf(L"GetModuleFileName failed w/err 0x%08lx\n", GetLastError());
        goto Cleanup;
    }

    // Open the local default service control manager database
    schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT |
        SC_MANAGER_CREATE_SERVICE);
    if (schSCManager == NULL)
    {
        wprintf(L"OpenSCManager failed w/err 0x%08lx\n", GetLastError());
        goto Cleanup;
    }

    // Install the service into SCM by calling CreateService
    schService = CreateService(
        schSCManager,                   // SCManager database
        pszServiceName,                 // Name of service
        pszDisplayName,                 // Name to display
        SERVICE_QUERY_STATUS,           // Desired access
        SERVICE_WIN32_OWN_PROCESS,      // Service type
        dwStartType,                    // Service start type
        SERVICE_ERROR_NORMAL,           // Error control type
        szPath,                         // Service's binary
        NULL,                           // No load ordering group
        NULL,                           // No tag identifier
        pszDependencies,                // Dependencies
        pszAccount,                     // Service running account
        pszPassword                     // Password of the account
        );
    if (schService == NULL)
    {
        wprintf(L"CreateService failed w/err 0x%08lx\n", GetLastError());
        goto Cleanup;
    }

    wprintf(L"%s is installed.\n", pszServiceName);

Cleanup:
    // Centralized cleanup for all allocated resources.
    if (schSCManager)
    {
        CloseServiceHandle(schSCManager);
        schSCManager = NULL;
    }
    if (schService)
    {
        CloseServiceHandle(schService);
        schService = NULL;
    }
}

When I run it, I get error: CreateService failed w/err 0x000001e7 (I only know it is: ERROR_INVALID_ADDRESS) - but I don't known what is exactly mean, and how to fix.

Anyone plz help me.

解决方案

With the exception of the schSCManager and szPath variables, all of the other variables you are passing to CreateService() have not been initialized, they contain random values. That is especially important for the psz... variables, because they are pointers, so you are effectively passing random memory addresses to CreateService(). That is why you are getting an ERROR_INVALID_ADDRESS error.

You need to initialize your variables!

pszServiceName needs to point at a null-terminated string containing the desired service name.

pszDisplayName needs to point at a null-terminated string containing the desired service display name.

dwStartType needs to contain a valid start type integer value.

pszDependencies needs to either be NULL, or point at a double-null-terminated string containing a list of null-separated service names that your service depends on.

pszAccount needs to either be NULL or point at a null-terminated string containing the desired user account that the service runs under.

pszPassword needs to either be NULL or point at a null-terminated string containing the password for the pszAccount account.

Edit: You are better off simply getting rid of the variables altogether and pass the desired values directly to CreateService(). Try this:

#include "stdafx.h"

void __cdecl _tmain(int argc, TCHAR *argv[])
{
    wchar_t szPath[MAX_PATH+1];
    SC_HANDLE schSCManager = NULL;
    SC_HANDLE schService = NULL;

    if (GetModuleFileName(NULL, szPath, ARRAYSIZE(szPath)) == 0)
    {
        wprintf(L"GetModuleFileName failed w/err 0x%08lx\n", GetLastError());
        goto Cleanup;
    }

    // Open the local default service control manager database
    schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE);
    if (schSCManager == NULL)
    {
        wprintf(L"OpenSCManager failed w/err 0x%08lx\n", GetLastError());
        goto Cleanup;
    }

    // Install the service into SCM by calling CreateService
    schService = CreateService(
        schSCManager,                   // SCManager database
        L"Win32_Service,                // Name of service
        L"My Service,                   // Name to display
        SERVICE_QUERY_STATUS,           // Desired access
        SERVICE_WIN32_OWN_PROCESS,      // Service type
        SERVICE_DEMAND_START,           // Service start type
        SERVICE_ERROR_NORMAL,           // Error control type
        szPath,                         // Service's binary
        NULL,                           // No load ordering group
        NULL,                           // No tag identifier
        NULL,                           // No Dependencies
        L"NT AUTHORITY\\LocalService",  // Service running account
        NULL                            // No Password of the account
        );
    if (schService == NULL)
    {
        wprintf(L"CreateService failed w/err 0x%08lx\n", GetLastError());
        goto Cleanup;
    }

    wprintf(L"Service is installed.\n");

Cleanup:
    // Centralized cleanup for all allocated resources.
    if (schService)
    {
        CloseServiceHandle(schService);
        schService = NULL;
    }
    if (schSCManager)
    {
        CloseServiceHandle(schSCManager);
        schSCManager = NULL;
    }
}

这篇关于CreateService获取错误:ERROR_INVALID_ADDRESS(0x000001e7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 01:29