JNA:com.sun.jna.platform.win32.Win32Exception-访问被拒绝

时间:2011-11-14 11:21:17

标签: java windows winapi jna

我使用以下代码从java程序

控制Windows服务
public class PostgresService2
{
    public static void main(String[] args)
    {
        try
            {
                W32ServiceManager serviceManager = new W32ServiceManager();
                serviceManager.open(Winsvc.SERVICE_STOP);
                W32Service service = serviceManager.openService("DBService",
                        Winsvc.SERVICE_ACCEPT_STOP);
                service.stopService();
                service.close();
             }
             catch (Exception ex)
             {
                 ex.printStackTrace();
             }
    }
}

这会在windows7(64位)计算机上出错

com.sun.jna.platform.win32.Win32Exception: Access is denied.
        at com.sun.jna.platform.win32.W32Service.queryStatus(W32Service.java
        at com.sun.jna.platform.win32.W32Service.waitForNonPendingState(W32S
        at com.sun.jna.platform.win32.W32Service.stopService(W32Service.java
        at chs.capitalmigrate.ui.PostgresService2.main(PostgresService2.java

运行命令的shell具有管理权限。我如何提供完全访问权限?

1 个答案:

答案 0 :(得分:1)

我正在使用SC_MANAGER_ALL_ACCESS,这有效。不确定这是否有帮助。

 W32ServiceManager serviceManager = new W32ServiceManager();
               serviceManager.open(Winsvc.SC_MANAGER_ALL_ACCESS);
               W32Service service = serviceManager.openService("servicename", Winsvc.SC_MANAGER_ALL_ACCESS);