start working on Windows Service querying tool

This commit is contained in:
idk
2022-09-18 17:20:00 -04:00
parent 58ef493666
commit e09f05ce42

View File

@ -12,7 +12,8 @@ import java.io.InputStreamReader;
* see also:
* https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/sc-query
* https://learn.microsoft.com/en-us/dotnet/api/system.serviceprocess.servicecontrollerstatus?view=dotnet-plat-ext-6.0
* https://stackoverflow.com/questions/10604844/how-to-verify-whether-service-exists-in-services-msc C#, API ideas only
* https://stackoverflow.com/questions/10604844/how-to-verify-whether-service-exists-in-services-msc
* C#, API ideas only
* https://stackoverflow.com/questions/334471/need-a-way-to-check-status-of-windows-service-programmatically
* https://stackoverflow.com/questions/5388888/find-status-of-windows-service-from-java-application
* https://stackoverflow.com/questions/21566847/how-to-check-particular-windows-service-is-running-using
@ -48,16 +49,34 @@ public class WindowsServiceUtil {
}
return result;
}
private String getStatePrefix(String qResult) {
String statePrefix = "STATE : ";
// get the first occurrence of "STATE", then find the
// next occurrence of of ":" after that. Count the
// spaces between.
// numberOfSpaces = indexOf(":") - (indexOf(STATE)+5) +
int indexOfState = qResult.indexOf("STATE");
if (indexOfState >= 0) {
int indexOfColon = qResult.indexOf(":", indexOfState);
statePrefix = "STATE";
for (int f = 0; f == indexOfColon; f++) {
statePrefix += " ";
}
statePrefix += ": ";
}
return statePrefix;
}
public String getServiceState(String serviceName) {
String STATE_PREFIX = "STATE : ";
String s = queryService(serviceName);
// String statePrefix = "STATE : ";
String qResult = queryService(serviceName);
String statePrefix = getStatePrefix(qResult);
// check that the temp string contains the status prefix
int ix = s.indexOf(STATE_PREFIX);
int ix = qResult.indexOf(statePrefix);
String stateString = "uninstalled";
if (ix >= 0) {
// compare status number to one of the states
String stateStr = s.substring(ix + STATE_PREFIX.length(),
ix + STATE_PREFIX.length() + 1);
String stateStr = qResult.substring(ix + statePrefix.length(),
ix + statePrefix.length() + 1);
int state = Integer.parseInt(stateStr);
switch (state) {
case (1): // service stopped
@ -67,7 +86,7 @@ public class WindowsServiceUtil {
stateString = "starting";
break;
case (3): // don't know yet
//stateString = "started";
// stateString = "started";
break;
case (4): // service started
stateString = "started";