zoukankan      html  css  js  c++  java
  • 利用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());
    }
    }

  • 相关阅读:
    接口测试入门(5)----新框架重构,使用轻量级的HTTP开发库 Unirest
    接口测试入门(4)--接口自动化测试框架 / list和map用法 / 随机选取新闻 (随机数生成) / 接口相关id映射
    接口测试入门(3)--使用httpClient进行登录用例操作/set-cookies验证/ List<NameValuePair>设置post参数/json解析
    接口测试入门(2)--get和post初级请求/使用httpclient做一个获取信息list的请求(需要登录才可以)
    接口测试学习入门(1)--前期知识储备
    j2ee 使用db.properties连接mysql数据库
    com.mysql.jdbc.Driver 和 com.mysql.cj.jdbc.Driver的区别 serverTimezone设定
    mysql JDBC URL格式各个参数详解
    mysql新建数据库时的collation选择(转)
    SpringBoot MyBatis 配置多数据源 (静态多个)
  • 原文地址:https://www.cnblogs.com/liaomin416100569/p/9331586.html
Copyright © 2011-2022 走看看