1.创建本地账号服务,实例代码
VOID Install(TCHAR* pPath, TCHAR* pName)
{
SC_HANDLE schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CREATE_SERVICE);
if (schSCManager==0)
{
long nError = GetLastError();
TCHAR pTemp[121];
swprintf(pTemp, _T("OpenSCManager failed, error code = %d"), nError);
WriteLog(pTemp);
}
else
{
SC_HANDLE schService = CreateService
(
schSCManager, /* SCManager database */
pName, /* name of service */
pName, /* service name to display */
SERVICE_ALL_ACCESS, /* desired access */
SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS , /* service type */
SERVICE_AUTO_START, /* start type */
SERVICE_ERROR_NORMAL, /* error control type */
pPath, /* service's binary */
NULL, /* no load ordering group */
NULL, /* no tag identifier */
NULL, /* no dependencies */
NULL, /* LocalSystem account */
NULL
); /* no password */
if (schService==0)
{
long nError = GetLastError();
TCHAR pTemp[121];
swprintf(pTemp, _T("Failed to create service %s, error code = %d"), pName, nError);
WriteLog(pTemp);
}
else
{
TCHAR pTemp[121];
swprintf(pTemp, _T("Service %s installed"), pName);
WriteLog(pTemp);
CloseServiceHandle(schService);
}
CloseServiceHandle(schSCManager);
}
}
2.创建本地系统服务
不能启用SERVICE_INTERACTIVE_PROCESS标识,实例代码
VOID Install(TCHAR* pPath, TCHAR* pName)
{
SC_HANDLE schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CREATE_SERVICE);
if (schSCManager==0)
{
long nError = GetLastError();
TCHAR pTemp[121];
swprintf(pTemp, _T("OpenSCManager failed, error code = %d"), nError);
WriteLog(pTemp);
}
else
{
TCHAR uac[] = _T("NT AUTHORITY\\LocalService");
SC_HANDLE schService = CreateService
(
schSCManager, /* SCManager database */
pName, /* name of service */
pName, /* service name to display */
SERVICE_ALL_ACCESS, /* desired access */
SERVICE_WIN32_OWN_PROCESS , /* service type */
SERVICE_AUTO_START, /* start type */
SERVICE_ERROR_NORMAL, /* error control type */
pPath, /* service's binary */
NULL, /* no load ordering group */
NULL, /* no tag identifier */
NULL, /* no dependencies */
uac,
NULL
); /* no password */
if (schService==0)
{
long nError = GetLastError();
TCHAR pTemp[121];
swprintf(pTemp, _T("Failed to create service %s, error code = %d"), pName, nError);
WriteLog(pTemp);
}
else
{
TCHAR pTemp[121];
swprintf(pTemp, _T("Service %s installed"), pName);
WriteLog(pTemp);
CloseServiceHandle(schService);
}
CloseServiceHandle(schSCManager);
}
}