3 《Undocumented Windows 2000 Secrets》翻译 --- 第三章( 四 )


return hService;
}
// -----------------------------------------------------------------
BOOL WINAPI w2kServiceClose (SC_HANDLE hService)
{
return (hService != NULL) && CloseServiceHandle (hService);
}
// -----------------------------------------------------------------
BOOL WINAPI w2kServiceAdd (SC_HANDLE hManager,
PWORD pwName,
PWORD pwInfo,
PWORD pwPath)
{
SC_HANDLE hManager1, hService;
PWORD pwFile;
WORD awPath [MAX_PATH];
DWORD n;
BOOL fOk = FALSE;
w2kServiceManager (hManager, &hManager1, TRUE);
if ((hManager1 != NULL) && (pwName != NULL) &&
(pwInfo != NULL) && (pwPath != NULL) &&
(n = GetFullPathName (pwPath, MAX_PATH, awPath, &pwFile)) &&
(n < MAX_PATH))
{
if ((hService = CreateService (hManager1, pwName, pwInfo,
SERVICE_ALL_ACCESS,
SERVICE_KERNEL_DRIVER,
SERVICE_DEMAND_START,
SERVICE_ERROR_NORMAL,
awPath, NULL, NULL,
NULL, NULL, NULL))
!= NULL)
{
w2kServiceClose (hService);
fOk = TRUE;
}
else
{
fOk = (GetLastError () ==
ERROR_SERVICE_EXISTS);
}
}
w2kServiceManager (hManager, &hManager1, FALSE);
return fOk;
}
// -----------------------------------------------------------------
BOOL WINAPI w2kServiceRemove (SC_HANDLE hManager,
PWORD pwName)
{
SC_HANDLE hService;
BOOL fOk = FALSE;
if ((hService = w2kServiceOpen (hManager, pwName)) != NULL)
{
if (DeleteService (hService))
{
fOk = TRUE;
}
else
{
fOk = (GetLastError () ==
ERROR_SERVICE_MARKED_FOR_DELETE);
}
w2kServiceClose (hService);
}
return fOk;
}
// -----------------------------------------------------------------
BOOL WINAPI w2kServiceStart (SC_HANDLE hManager,
PWORD pwName)
{
SC_HANDLE hService;
BOOL fOk = FALSE;
if ((hService = w2kServiceOpen (hManager, pwName)) != NULL)
{
if (StartService (hService, 1, &pwName))
{
fOk = TRUE;
}
else
{
fOk = (GetLastError () ==
ERROR_SERVICE_ALREADY_RUNNING);
}
w2kServiceClose (hService);
}
return fOk;
}
// -----------------------------------------------------------------
BOOL WINAPI w2kServiceControl (SC_HANDLE hManager,
PWORD pwName,
DWORD dControl)
{
SC_HANDLE hService;
SERVICE_STATUS ServiceStatus;
BOOL fOk = FALSE;
if ((hService = w2kServiceOpen (hManager, pwName)) != NULL)
{
if (QueryServiceStatus (hService, &ServiceStatus))
{
switch (ServiceStatus.dwCurrentState)
{
case SERVICE_STOP_PENDING:
case SERVICE_STOPPED:
{
fOk = (dControl == SERVICE_CONTROL_STOP);
break;
}
case SERVICE_PAUSE_PENDING:
case SERVICE_PAUSED:
{
fOk = (dControl == SERVICE_CONTROL_PAUSE);
break;
}
case SERVICE_START_PENDING:
case SERVICE_CONTINUE_PENDING:
case SERVICE_RUNNING:
{
fOk = (dControl == SERVICE_CONTROL_CONTINUE);
break;
}
}
}
fOk = fOk ||
ControlService (hService, dControl, &ServiceStatus);
w2kServiceClose (hService);
}
return fOk;
}
// -----------------------------------------------------------------
BOOL WINAPI w2kServiceStop (SC_HANDLE hManager,
PWORD pwName)
{
return w2kServiceControl (hManager, pwName,
SERVICE_CONTROL_STOP);
}
// -----------------------------------------------------------------
BOOL WINAPI w2kServicePause (SC_HANDLE hManager,
PWORD pwName)
{
return w2kServiceControl (hManager, pwName,
SERVICE_CONTROL_PAUSE);
}
// -----------------------------------------------------------------

推荐阅读