利用java控制window服务
下载 JInvoke 工具包
package com;
import com.jinvoke.JInvoke;
import com.jinvoke.NativeImport;
import com.jinvoke.Util;
import com.jinvoke.win32.Advapi32;
import com.jinvoke.win32.Kernel32;
import com.jinvoke.win32.WinConstants;
import com.jinvoke.win32.structs.EnumServiceStatus;
import com.jinvoke.win32.structs.ServiceStatus;
public class ServiceTool {
@NativeImport(library = "Advapi32")
public static native boolean StartService(int hService,
int dwNumServiceArgs, String lpServiceArgVectors);
public static native boolean ControlService(int hService, int dwControl,
ServiceStatus lpServiceStatus);
public static native boolean DeleteService(int hService);
public static native int CreateService(int hSCManager,
String lpServiceName, String lpDisplayName, int dwDesiredAccess,
int dwServiceType, int dwStartType, int dwErrorControl,
String lpBinaryPathName, String lpLoadOrderGroup, String lpdwTagId,
String lpDependencies, String lpServiceStartName, String lpPassword);
public static void main(String[] args) {
// createService();
// deleteService();
// startService("lanmanserver");
queryService();
}
public static void stopService(String serviceName) {
int point = Advapi32
.OpenSCManager(null, null, WinConstants.GENERIC_ALL);
int s = Advapi32.OpenService(point, serviceName,
WinConstants.SERVICE_STOP);
JInvoke.initialize();
ServiceStatus status = new ServiceStatus();
ControlService(s, WinConstants.SERVICE_CONTROL_STOP, status);
System.out.println(status.dwCurrentState);
}
public static void startService(String serviceName) {
int point = Advapi32
.OpenSCManager(null, null, WinConstants.GENERIC_ALL);
int s = Advapi32.OpenService(point, serviceName,
WinConstants.SERVICE_START);
JInvoke.initialize();
StartService(s, 0, null);
}
public static void createService() {
int point = Advapi32
.OpenSCManager(null, null, WinConstants.GENERIC_ALL);
JInvoke.initialize();
int result = CreateService(point, "bakserver", "bakserver",
WinConstants.GENERIC_ALL,
WinConstants.SERVICE_WIN32_SHARE_PROCESS,
WinConstants.SERVICE_AUTO_START,
WinConstants.SERVICE_ERROR_NORMAL, "d:/bakserver.exe", null,
null, "", null, null);
Advapi32.CloseServiceHandle(result);
Advapi32.CloseServiceHandle(point);
System.out.println(Kernel32.GetLastError());
}
public static void deleteService() {
// ��ȡscm����ָ̨�� ��������ָʾ��������̨���Ȩ��
int point = Advapi32
.OpenSCManager(null, null, WinConstants.GENERIC_ALL);
// ��ȡ����ָ�� ���������ʾ�Է�������ЩȨ��
int s = Advapi32.OpenService(point, "bakserver", WinConstants.DELETE);
JInvoke.initialize();
System.out.println("ɾ��service:" + DeleteService(s));
System.out.println("�ر�scm��ݿ�:" + Advapi32.CloseServiceHandle(s));
System.out.println("�رջ�ȡ����ָ��:" + Advapi32.CloseServiceHandle(point));
System.out.println("��ȡ�������:" + Kernel32.GetLastError());
}
public static void queryService() {
// ��ȡscm����ָ̨�� ��������ָʾ��������̨���Ȩ��
int point = Advapi32
.OpenSCManager(null, null, WinConstants.GENERIC_ALL);
// ���ȱ����ȡ����ĸ���
int buff = 0;
// ����������Ϊ���صĽ�� java��int �������ô��� ֻ�������飬��ʵֻ���˵�һ��λ��
// ��ʾҪ��ȡ���з�����Ҫռ�õ��ֽڿռ�
int[] bytesNeeded = { 0 };
// ��ʾ���صķ������ ��Ϊ���ÿռ��Сbuff=0���Կ�������ȡ���ظ���
int[] servicesReturned = { 0 };
int[] resumeHandle = { 0 };
int size = Util.getStructSize(EnumServiceStatus.class);
boolean result = Advapi32.EnumServicesStatus(point,
WinConstants.SERVICE_WIN32, WinConstants.SERVICE_STATE_ALL,
buff, size, bytesNeeded, servicesReturned, resumeHandle);
System.out.println("��Ҫ���ֽڿռ�:" + bytesNeeded[0]);
// һ�����ռ��size�ռ� bytesNeeded[0]Ϊռ���ֽ��� (bytesNeeded[0]/size)Ϊ�������
System.out.println("�������ĸ���:" + (bytesNeeded[0] / size + 1));
int numberOfServices = (bytesNeeded[0] / Util
.getStructSize(EnumServiceStatus.class)) + 1;
EnumServiceStatus[] servicesInfo = new EnumServiceStatus[numberOfServices];
// ���ٸ���� ʵ����ٸ�������
for (int i = 0; i < numberOfServices; i++) {
servicesInfo[i] = new EnumServiceStatus();
}
byte[] by = new byte[bytesNeeded[0]];
for (int i = 0; i < by.length; i++) {
by[i] = 0;
}
int bufferSize = Util.byteArrayToPtr(by);
result = Advapi32.EnumServicesStatus(point, WinConstants.SERVICE_WIN32,
WinConstants.SERVICE_STATE_ALL, bufferSize, bytesNeeded[0],
bytesNeeded, servicesReturned, resumeHandle);
System.out.println("�������" + servicesReturned[0]);
for (int i = 0; i < servicesReturned[0]; i++) {
EnumServiceStatus ess = Util.ptrToStruct(bufferSize,
EnumServiceStatus.class);
System.out.print("ess[" + i + "].lpDisplayName = "
+ ess.lpServiceName + " (" + ess.lpDisplayName
+ ") ");
int hService = Advapi32.OpenService(point, ess.lpServiceName
.toString(), WinConstants.GENERIC_READ);
ServiceStatus serviceStatus = new ServiceStatus();
Advapi32.QueryServiceStatus(hService, serviceStatus);
String serviceState = null;
switch (ess.dwCurrentState) {
case WinConstants.SERVICE_STOPPED:
serviceState = "Stopped";
break;
case WinConstants.SERVICE_START_PENDING:
serviceState = "Starting";
break;
case WinConstants.SERVICE_STOP_PENDING:
serviceState = "Shutting down";
break;
case WinConstants.SERVICE_RUNNING:
serviceState = "Running";
break;
case WinConstants.SERVICE_CONTINUE_PENDING:
serviceState = "Continuing";
break;
case WinConstants.SERVICE_PAUSE_PENDING:
serviceState = "Pausing";
break;
case WinConstants.SERVICE_PAUSED:
serviceState = "Paused";
break;
}
System.out.println(serviceState);
bufferSize += Util.getStructSize(ess);
}
System.out.println("��ȡ�������:" + Kernel32.GetLastError());
}
}