请帮我这个问题:
我使用以下代码创建基本服务:
#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;
}
}
当我运行它时,出现错误:CreateService失败,错误0x000001e7(我只知道是:ERROR_INVALID_ADDRESS)-但我不知道这是什么意思,以及如何解决。
有人请帮助我。
最佳答案
除了schSCManager
和szPath
变量外,您传递给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;
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;
}
}