使用JNA的启动/停止服务 [英] Start/Stop services using JNA
问题描述
我正在编写一个实用程序来启动和停止Windows服务.该程序将分布在具有不同级别用户权限的许多计算机上,因此我不想使用命令行.我已经尝试过使用JNA,
I am writing a utility to start and stop windows services. The program will be distributed across many computers with differing levels of user privileges so I don't want to use the command line. I've tried using JNA,
import com.sun.jna.platform.win32.W32Service;
import com.sun.jna.platform.win32.W32ServiceManager;
import com.sun.jna.platform.win32.Winsvc;
/**
*
* @author
*/
public class WindowsServices {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try
{
// TODO code application logic here
W32ServiceManager serviceManager = new W32ServiceManager();
W32Service service = serviceManager.openService("uvnc_service", Winsvc.SERVICE_ACCEPT_STOP);
service.stopService();
service.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
运行程序时出现以下错误
When I run the program I get the following error
com.sun.jna.platform.win32.Win32Exception:句柄无效. 在com.sun.jna.platform.win32.W32ServiceManager.openService(W32ServiceManager.java:77) 在windowsservices.WindowsServices.main(WindowsServices.java:26)
com.sun.jna.platform.win32.Win32Exception: The handle is invalid. at com.sun.jna.platform.win32.W32ServiceManager.openService(W32ServiceManager.java:77) at windowsservices.WindowsServices.main(WindowsServices.java:26)
任何建议都是最有帮助的.
Any suggestions would be most helpful.
推荐答案
感谢问题的作者提出的建议.
Thanks for the suggestion the author of the question found the error.
import com.sun.jna.platform.win32.W32Service;
import com.sun.jna.platform.win32.W32ServiceManager;
import com.sun.jna.platform.win32.Winsvc;
/**
*
* @author
*/
public class WindowsServices {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try
{
W32ServiceManager serviceManager = new W32ServiceManager();
serviceManager.open(Winsvc.SC_MANAGER_ALL_ACCESS);
W32Service service = serviceManager.openService("uvnc_service", Winsvc.SC_MANAGER_ALL_ACCESS);
service.startService();
service.close();
} catch (Exception ex)
{
ex.printStackTrace();
}
}
}
错误是代码没有打开服务控制管理器.我在查看MSDN时发现了我需要遵循的过程.我还碰巧看到了权限值,这也可能导致失败.
The error was that the code didn't open the Service Control Manager. I was looking on MSDN and found the process that I needed to follow. I also chanced the permission value, that might also of caused a failure.
这篇关于使用JNA的启动/停止服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!