简体   繁体   中英

Query all Windows Services with JNA

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);
       }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM