state = 0;
service = advapi32.OpenService(serviceManager, name, WINNT.GENERIC_READ);
if (service != null)
{
IntByReference pcbBytesNeeded = new IntByReference();
state |= Service.STATE_INSTALLED;
// get size required
if (!advapi32.QueryServiceConfig(service, null, 0, pcbBytesNeeded))
{
// now get the data
int cbBufSize = pcbBytesNeeded.getValue();
Memory buffer = new Memory(cbBufSize);
buffer.clear();
if (advapi32.QueryServiceConfig(service, buffer, cbBufSize, pcbBytesNeeded))
{
QUERY_SERVICE_CONFIG lpServiceConfig = new QUERY_SERVICE_CONFIG();
lpServiceConfig.init(buffer);
if (lpServiceConfig.dwStartType == Advapi32.SERVICE_DISABLED)
state |= Service.STATE_DISABLED;
if (lpServiceConfig.dwStartType == Advapi32.SERVICE_BOOT_START | lpServiceConfig.dwStartType == Advapi32.SERVICE_SYSTEM_START
| lpServiceConfig.dwStartType == Advapi32.SERVICE_AUTO_START)
state |= Service.STATE_AUTOMATIC;
if (lpServiceConfig.dwStartType == Advapi32.SERVICE_DEMAND_START)
state |= Service.STATE_MANUAL;
if ((lpServiceConfig.dwServiceType & Advapi32.SERVICE_INTERACTIVE_PROCESS) != 0)
state |= Service.STATE_INTERACTIVE;
result.setAccount(lpServiceConfig.lpServiceStartName);
result.setCommand(lpServiceConfig.lpBinaryPathName);
result.setDependencies(lpServiceConfig.getDependencies());
result.setDisplayName(lpServiceConfig.lpDisplayName);
}
else
{
state |= Service.STATE_UNKNOWN;
System.out.println("Error in QueryServiceConfig: " + Native.getLastError());
}
}
else
{
state |= Service.STATE_UNKNOWN;
System.out.println("Error in QueryServiceConfig: " + Native.getLastError());
}
if (!advapi32.QueryServiceStatusEx(service, (byte) advapi32.SC_STATUS_PROCESS_INFO, null, 0, pcbBytesNeeded))
{
// now get the data
int cbBufSize = pcbBytesNeeded.getValue();
Memory buffer = new Memory(cbBufSize);
buffer.clear();
if (advapi32.QueryServiceStatusEx(service, (byte) advapi32.SC_STATUS_PROCESS_INFO, buffer, cbBufSize, pcbBytesNeeded))
{
SERVICE_STATUS_PROCESS lpBuffer = new SERVICE_STATUS_PROCESS();
lpBuffer.init(buffer);
if (lpBuffer.dwCurrentState == advapi32.SERVICE_RUNNING)
state |= Service.STATE_RUNNING;
if (lpBuffer.dwCurrentState == advapi32.SERVICE_PAUSED)
state |= Service.STATE_PAUSED;
if (lpBuffer.dwCurrentState == advapi32.SERVICE_START_PENDING)
state |= Service.STATE_STARTING;
if (lpBuffer.dwCurrentState == advapi32.SERVICE_STOP_PENDING)
state |= Service.STATE_STOPPING;
result.setPid(lpBuffer.dwProcessId);
}
else
{
state |= Service.STATE_UNKNOWN;
System.out.println("Error in QueryServiceStatusEx: " + Native.getLastError());
}
}
if (!advapi32.QueryServiceConfig2(service, (byte) advapi32.SERVICE_CONFIG_DESCRIPTION, null, 0, pcbBytesNeeded))
{
// now get the data
int cbBufSize = pcbBytesNeeded.getValue();
Memory buffer = new Memory(cbBufSize);
buffer.clear();
if (advapi32.QueryServiceConfig2(service, (byte) advapi32.SERVICE_CONFIG_DESCRIPTION, buffer, cbBufSize, pcbBytesNeeded))
{
SERVICE_DESCRIPTION lpBuffer = new SERVICE_DESCRIPTION();