Query all Windows Services with JNA

耗尽温柔 提交于 2019-12-22 19:14:09

问题


Currently I'm trying to query all installed Windows Services from (remote) machine. I had a look at win32.Advapi32.

But here I can only "get" a defined (I have to give a "ServiceName") Windows Services. (Advapi32.INSTANCE.OpenSCManager, Advapi32.INSTANCE.OpenService, Advapi32.INSTANCE.QueryServiceStatusEx)

Do you know any API which allows to query all Windows Services from (remote) machine?

EDIT://

I tried it allready with the following code. But it aborts hardly with no error message!

public void getService(){
    IntByReference size = new IntByReference();
    IntByReference lppcbBytesneeded = new IntByReference();
    IntByReference retz = new IntByReference();
    SC_HANDLE scm = Advapi32.INSTANCE.OpenSCManager(null, null, Winsvc.SC_MANAGER_ENUMERATE_SERVICE);
    boolean ret = CustomAdvapi32.INSTANCE.EnumServicesStatusEx(scm, 0, 0x00000030, 0x0000000, null, lppcbBytesneeded, 
            retz, size, null);
    //CustomAdvapi32.INSTANCE.EnumServicesStatusEx(hSCManager, InfoLevel, dwServiceType, dwServiceState, 
    //cbBufSize, pcbBytesNeeded, lpServicesReturned, lpResumeHandle, pstzGroupName)
     int error = Native.getLastError();

       Memory buf = new Memory(lppcbBytesneeded.getValue());
       size.setValue(retz.getValue());
       ret = CustomAdvapi32.INSTANCE.EnumServicesStatusEx(scm, 0, 0x00000030, 0x0000000,
               buf, lppcbBytesneeded, retz, size, null);
       error = Native.getLastError();


       ENUM_SERVICE_STATUS_PROCESS serviceInfo = new ENUM_SERVICE_STATUS_PROCESS(buf);
       Structure[] serviceInfos = serviceInfo.toArray(retz.getValue());

       for(int i = 0; i < retz.getValue(); i++) {
         serviceInfo = (ENUM_SERVICE_STATUS_PROCESS) serviceInfos[i];
         System.out.println(serviceInfo.lpDisplayName + " / " + serviceInfo.lpServiceName);
       }
}

回答1:


You've incorrectly mapped EnumServicesStatusEx. The sixth argument needs to be the size of the buffer passed in (in your first call, this should be zero). The pointer to the required size would then come next.

Note that EnumServicesStatusEx requires 10 arguments and you've only mapped it with nine.



来源:https://stackoverflow.com/questions/20562528/query-all-windows-services-with-jna

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!