我正在尝试使用Windows 7 64位中的CreateService API安装服务。当调用CreateService API时,它会失败,错误代码为1314,“客户端不会保留所需的权限。”。
我在管理员模式下运行Visual Studio。知道为什么在管理模式下运行的进程创建服务时它仍然失败。
此外,我尝试使用ACCESS_SYSTEM_SECURITY创建服务作为所需的访问标志之一.CreateService仅在传递ACCESS_SYSTEM_SECURITY时失败,否则其工作正常。
这是代码
LUID luidSecurityPriv;
HANDLE hTokenProcCur;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS | TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hTokenProcCur))
{
if (LookupPrivilegeValue(NULL, L"SeSecurityPrivilege", &luidSecurityPriv))
{
TOKEN_PRIVILEGES tp;
DWORD cbSinglePriv= sizeof(TOKEN_PRIVILEGES);
tp.PrivilegeCount= 1;
tp.Privileges[0].Luid= luidSecurityPriv;
tp.Privileges[0].Attributes= SE_PRIVILEGE_ENABLED;
if (AdjustTokenPrivileges(hTokenProcCur,
FALSE,
&tp,
cbSinglePriv,
NULL,
NULL))
{
// actually register the NanoService with the OS here
SC_HANDLE schService = CreateService(schSCManager,
_T(SERVICE_NAME),
(LPCTSTR)strServiceName,
SERVICE_QUERY_STATUS | SERVICE_CHANGE_CONFIG | SERVICE_START | READ_CONTROL | WRITE_DAC | ACCESS_SYSTEM_SECURITY, // desired access
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_AUTO_START, // start type
SERVICE_ERROR_NORMAL, // error control type
strServicePath, // service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // dependencies
NULL, // LocalSystem account
NULL); // no password
if (schService)
{
MessageBox(NULL,"CreateService Succeeded",L"",MB_OK);
}
else
MessageBox(NULL,"CreateService failed",L"",MB_OK);
}
}
}
答案 0 :(得分:1)
ACCESS_SYSTEM_SECURITY
的说明说明了此访问权限的要求:
获取此访问权限的正确方法是在调用方的当前访问令牌中启用SE_SECURITY_NAME权限,打开ACCESS_SYSTEM_SECURITY访问权限的句柄,然后禁用该权限。