[英]Java JNA call to function “InitiateSystemShutdown” in dll Advapi32 don't work
[英]Change username and password for an existing Windows service in Java via Advapi32 (JNA API)
我一直在嘗試通過JNA API(特別是通過com.sun.jna.platform.win32.Advapi32
類)更改現有Windows Service的配置,但是找不到足夠的文檔來進行配置。
通過在Windows CMD中運行'sc'命令可以實現相同的目的:
sc config "MyServiceName" obj="domain\windowsuser" password= "userpassword"
到目前為止,這是我弄清楚Java代碼(正在進行的工作)的目的:
private static void printAllServices(String username, String password) {
boolean success = false;
SC_HANDLE serviceManager = openServiceControlManager(null, Winsvc.SC_MANAGER_ALL_ACCESS);
if (serviceManager != null) {
SC_HANDLE service = Advapi32.INSTANCE.OpenService(serviceManager, "MyServiceName",
WinNT.GENERIC_EXECUTE);
if (service != null) {
SERVICE_STATUS serviceStatus = new SERVICE_STATUS();
success = Advapi32.INSTANCE.ControlService(service, Winsvc.SERVICE_CHANGE_CONFIG, serviceStatus);
Advapi32.INSTANCE.CloseServiceHandle(service);
}
Advapi32.INSTANCE.CloseServiceHandle(serviceManager);
}
}
private static SC_HANDLE openServiceControlManager(String machine, int access) {
return Advapi32.INSTANCE.OpenSCManager(machine, null, access);
}
我的目標是通過Java API更改運行Windows服務的用戶名/密碼(但不能直接通過JNA API執行“ sc”命令)。
朝着正確方向(或參考文檔)的任何提示都將有所幫助。
由於Advapi32下似乎沒有JNA API,因此調用了script.exe(傳遞必需的參數),該腳本包含執行“ advapi32.dll”的DllImport的C#代碼並在以下導入下使用API
using System.Runtime.InteropServices;
using System.ComponentModel;
供參考: https : //www.morgantechspace.com/2015/03/csharp-change-service-account-username-and-password.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.