我想监视一个服务的登陆帐户类型,如果发现不是localsystem就将其改回来,使用ChangeServiceConfig可以将帐户名改为其他帐户,但是无法改为.\System, 总是提示帐户名或密码不正确。
请高手指点
你试试LocalSystem行不行
调用ChangeServiceConfig函数时,应使用"LocalSystem"作为帐户名,而非".\System"
示范代码如下:
void OnSetAccount()
{
CString strServiceName="SampleService";//Service Name
CString strAccountName="LocalSystem";
CString strAccountPass="";
SC_LOCK sclLock = NULL;
SC_HANDLE schSCManager = OpenSCManager(
NULL, // local machine
NULL, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access rights
if (schSCManager == NULL)
{
// theApp.ReportLastError();
return;
}
SC_HANDLE schService = OpenService(
schSCManager, // SCManager database
strServiceName, // name of service
SERVICE_ALL_ACCESS);
if (schService == NULL)
{
// theApp.ReportLastError();
}
else
{
// Need to acquire the SCM database lock before changing the password.
sclLock = LockServiceDatabase(schSCManager);
if (sclLock == NULL)
{
// theApp.ReportLastError();
}
else
{
// Now set the account and password that the service uses at startup.
if (! ChangeServiceConfig(
schService, // Handle of service
SERVICE_NO_CHANGE, // Service type: no change
SERVICE_NO_CHANGE, // Change service start type
SERVICE_NO_CHANGE, // Error control: no change
NULL, // Binary path: no change
NULL, // Load order group: no change
NULL, // Tag ID: no change
NULL, // Dependencies: no change
strAccountName, // Account name: no change
strAccountPass, // New password
NULL) ) // Display name: no change
{
// theApp.ReportLastError();
}
}
}
if (sclLock)
UnlockServiceDatabase(sclLock);
if (schService)
CloseServiceHandle(schService);
if (schSCManager)
CloseServiceHandle(schSCManager);
}