Query Service Status
From The CA Plex Wiki
Contents |
Query Service Status
The following source code can be used to query the status of a service, e.g the NT Dispatcher
Source Code Object
#include <aclapi.h>
#include<winsvc.h>
{
SC_HANDLE schManager = NULL;
SC_HANDLE schService = NULL;
BOOL bReturn = NULL;
TCHAR sMachine[MAX_COMPUTERNAME_LENGTH + 3];
TCHAR sService[256];
strcpy_s(sMachine, &(1:).strGetText());
strcpy_s(sService, &(2:).strGetText());
// Obtain a handle to the Service Controller.
schManager = OpenSCManager(sMachine, NULL, SC_MANAGER_CONNECT);
if (schManager == NULL) {
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL );
// Output the string.
&(4:).PutText((LPTSTR) lpMsgBuf);
// Free the buffer.
LocalFree( lpMsgBuf );
} else {
// Obtain a handle to the service.
schService = OpenService(schManager, sService, GENERIC_READ);
if (schService == NULL) {
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL );
// Output the string.
&(4:).PutText((LPTSTR) lpMsgBuf);
// Free the buffer.
LocalFree( lpMsgBuf );
} else {
// Query the service status
LPSERVICE_STATUS lpStatus;
lpStatus = (LPSERVICE_STATUS)calloc(1,sizeof(SERVICE_STATUS));
bReturn = QueryServiceStatus(schService, lpStatus);
if (bReturn == 0) {
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL );
// Output the string.
&(4:).PutText((LPTSTR) lpMsgBuf);
// Free the buffer.
LocalFree( lpMsgBuf );
} else {
DWORD dwStatus = lpStatus->dwCurrentState;
switch (dwStatus) {
case SERVICE_RUNNING:
&(3:).PutText("4");
break;
case SERVICE_STOPPED:
&(3:).PutText("1");
break;
case SERVICE_PAUSED:
&(3:).PutText("7");
break;
case SERVICE_START_PENDING:
&(3:).PutText("2");
break;
case SERVICE_STOP_PENDING:
&(3:).PutText("3");
break;
case SERVICE_CONTINUE_PENDING:
&(3:).PutText("5");
break;
case SERVICE_PAUSE_PENDING:
&(3:).PutText("6");
break;
default:
&(3:).PutText("0");
break;
}
}
//Close Service Handle
bReturn = CloseServiceHandle(schService);
if (bReturn == 0) {
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL );
// Output the string.
&(4:).PutText((LPTSTR) lpMsgBuf);
// Free the buffer.
LocalFree( lpMsgBuf );
}
}
//Close ServiceManager Handle
bReturn = CloseServiceHandle(schManager);
if (bReturn == 0) {
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL );
// Output the string.
&(4:).PutText((LPTSTR) lpMsgBuf);
// Free the buffer.
LocalFree( lpMsgBuf );
}
}
}
Parameters
- &(1:) - Name of the machine on which the service resides (FIELDS/VaryCharacter)
- &(2:) - Name of the service that you wish to check (FIELDS/VaryCharacter), e.g.
- Ob450dp
- Ob450bm
- MSSQLServer
- &(3:) - The returned status of the service (FIELDS/Status), with the following Values and Literals
- Stopped - 1
- Start Pending - 2
- Stop Pending - 3
- Running - 4
- Continue Pending - 5
- Pause Pending - 6
- Paused - 7
- Unknown - 0
- &(4:) - *Message text - the returned error message
Notes
The machine name must be in UNC format, i.e \\MyNTBox This source code can be useful in automated process where a Plex client function is run unattended. A client function which needs to call a WinNTC function, can check the status of the dispatcher prior to calling, thus avoiding run time dialog errors. To check the status of the System i dispatcher, a call can be made via the NT dispatcher, which returns an error should the system be unavailable.