Check if process is installed/running as service on NT beofre trying t

o start it as a service (gets around an annoying pause when starting u
p as a non-service).
This commit is contained in:
Mark Valence 2000-06-15 02:21:01 +00:00
parent f770dc16ab
commit 9c7127cd81
2 changed files with 56 additions and 1 deletions

View File

@ -150,6 +150,57 @@ int srv_remove(LPCTSTR lpszServiceName, LPCTSTR lpszBinaryPathName)
}
DWORD
svc_installed (LPTSTR lpszServiceName, LPTSTR lpszBinaryPathName)
{
char buf[256];
HKEY key;
DWORD rc;
DWORD type;
long len;
strcpy(buf, TEXT("SYSTEM\\CurrentControlSet\\Services\\"));
strcat(buf, lpszServiceName);
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, buf, 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS)
return(-1);
rc = 0;
if (lpszBinaryPathName) {
len = sizeof(buf);
if (RegQueryValueEx(key, "ImagePath", NULL, &type, buf, &len) == ERROR_SUCCESS) {
if (strcmp(lpszBinaryPathName, buf))
rc = -1;
}
}
RegCloseKey(key);
return(rc);
}
DWORD
svc_running (LPTSTR lpszServiceName)
{
SC_HANDLE service;
SC_HANDLE scm;
DWORD rc;
SERVICE_STATUS ss;
if (!(scm = OpenSCManager(NULL, NULL, GENERIC_READ)))
return(GetLastError());
rc = 1;
service = OpenService(scm, lpszServiceName, SERVICE_QUERY_STATUS);
if (service) {
if (!QueryServiceStatus(service, &ss))
rc = GetLastError();
else if (ss.dwCurrentState != SERVICE_STOPPED)
rc = 0;
CloseServiceHandle(service);
}
CloseServiceHandle(scm);
return(rc);
}
static void *start_status_routine( void *ptr )
{

View File

@ -18,6 +18,8 @@ void WINAPI ServiceMain( DWORD argc, LPTSTR *argv );
int srv_install( char* service, char * displayName, char* filename,
BOOL auto_start );
int srv_remove ( char* service, char* filename );
DWORD svc_installed (LPTSTR lpszServiceName, LPTSTR lpszBinaryPathName);
DWORD svc_running (LPTSTR lpszServiceName);
int main( int argc, LPTSTR *argv )
{
@ -99,7 +101,9 @@ int main( int argc, LPTSTR *argv )
}
puts( "starting slapd..." );
if ( !StartServiceCtrlDispatcher(DispatchTable) )
if (svc_installed(SERVICE_NAME, NULL) != 0
|| svc_running(SERVICE_NAME) == 1
|| StartServiceCtrlDispatcher(DispatchTable) != 0 )
{
is_NT_Service = 0;
ServiceMain( argc, argv );