func IsServiceRunning(serviceName string) (bool, error) { // This function requires fewer access rights than using the above classes and can be used to check if a service // is running without administrator access. scmhandle, err := wrappers.OpenSCManager( nil, syscall.StringToUTF16Ptr(wrappers.SERVICES_ACTIVE_DATABASE), wrappers.SC_MANAGER_CONNECT) if err != nil { return false, NewWindowsError("OpenSCManager", err) } defer wrappers.CloseServiceHandle(scmhandle) handle, err := wrappers.OpenService( scmhandle, syscall.StringToUTF16Ptr(serviceName), wrappers.SERVICE_QUERY_STATUS) if err == wrappers.ERROR_SERVICE_DOES_NOT_EXIST { return false, nil } else if err != nil { return false, NewWindowsError("OpenService", err) } defer wrappers.CloseServiceHandle(handle) var status wrappers.SERVICE_STATUS if err := wrappers.QueryServiceStatus(handle, &status); err != nil { return false, NewWindowsError("QueryServiceStatus", err) } return status.CurrentState == wrappers.SERVICE_RUNNING, nil }
func OpenLocalSCManager() (*SCManager, error) { handle, err := wrappers.OpenSCManager( nil, syscall.StringToUTF16Ptr(wrappers.SERVICES_ACTIVE_DATABASE), wrappers.SC_MANAGER_ALL_ACCESS) if err != nil { return nil, NewWindowsError("OpenSCManager", err) } return &SCManager{handle: handle}, nil }