2011-11-14 322 views
1

我用下面的代碼從Java程序來控制Windows服務JNA:com.sun.jna.platform.win32.Win32Exception-訪問被拒絕

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

我正在使用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);