Search in sources :

Example 76 with Advapi32

use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.

the class Advapi32Test method testQueryServiceStatusEx.

public void testQueryServiceStatusEx() {
    SC_HANDLE scmHandle = Advapi32.INSTANCE.OpenSCManager(null, null, Winsvc.SC_MANAGER_CONNECT);
    assertNotNull(scmHandle);
    SC_HANDLE serviceHandle = Advapi32.INSTANCE.OpenService(scmHandle, "eventlog", Winsvc.SERVICE_QUERY_STATUS);
    assertNotNull(serviceHandle);
    IntByReference pcbBytesNeeded = new IntByReference();
    assertFalse(Advapi32.INSTANCE.QueryServiceStatusEx(serviceHandle, SC_STATUS_TYPE.SC_STATUS_PROCESS_INFO, null, 0, pcbBytesNeeded));
    assertEquals(W32Errors.ERROR_INSUFFICIENT_BUFFER, Kernel32.INSTANCE.GetLastError());
    assertTrue(pcbBytesNeeded.getValue() > 0);
    SERVICE_STATUS_PROCESS status = new SERVICE_STATUS_PROCESS(pcbBytesNeeded.getValue());
    assertTrue(Advapi32.INSTANCE.QueryServiceStatusEx(serviceHandle, SC_STATUS_TYPE.SC_STATUS_PROCESS_INFO, status, status.size(), pcbBytesNeeded));
    assertTrue(status.dwCurrentState == Winsvc.SERVICE_STOPPED || status.dwCurrentState == Winsvc.SERVICE_RUNNING);
    assertTrue(Advapi32.INSTANCE.CloseServiceHandle(serviceHandle));
    assertTrue(Advapi32.INSTANCE.CloseServiceHandle(scmHandle));
}
Also used : SERVICE_STATUS_PROCESS(com.sun.jna.platform.win32.Winsvc.SERVICE_STATUS_PROCESS) IntByReference(com.sun.jna.ptr.IntByReference) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE)

Example 77 with Advapi32

use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.

the class Advapi32Test method testInitializeAcl.

public void testInitializeAcl() throws IOException {
    ACL pAcl;
    int cbAcl = 0;
    PSID pSid = new PSID(WinNT.SECURITY_MAX_SID_SIZE);
    IntByReference cbSid = new IntByReference(WinNT.SECURITY_MAX_SID_SIZE);
    assertTrue("Failed to create well-known SID", Advapi32.INSTANCE.CreateWellKnownSid(WELL_KNOWN_SID_TYPE.WinBuiltinAdministratorsSid, null, pSid, cbSid));
    int sidLength = Advapi32.INSTANCE.GetLengthSid(pSid);
    cbAcl = Native.getNativeSize(ACL.class, null);
    cbAcl += Native.getNativeSize(ACCESS_ALLOWED_ACE.class, null);
    cbAcl += (sidLength - DWORD.SIZE);
    cbAcl = Advapi32Util.alignOnDWORD(cbAcl);
    pAcl = new ACL(cbAcl);
    assertTrue(Advapi32.INSTANCE.InitializeAcl(pAcl, cbAcl, WinNT.ACL_REVISION));
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) ACCESS_ALLOWED_ACE(com.sun.jna.platform.win32.WinNT.ACCESS_ALLOWED_ACE) ACL(com.sun.jna.platform.win32.WinNT.ACL) PSID(com.sun.jna.platform.win32.WinNT.PSID)

Example 78 with Advapi32

use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.

the class Advapi32Test method testGetTokenOwnerInformation.

public void testGetTokenOwnerInformation() {
    HANDLEByReference phToken = new HANDLEByReference();
    try {
        HANDLE processHandle = Kernel32.INSTANCE.GetCurrentProcess();
        assertTrue(Advapi32.INSTANCE.OpenProcessToken(processHandle, WinNT.TOKEN_DUPLICATE | WinNT.TOKEN_QUERY, phToken));
        IntByReference tokenInformationLength = new IntByReference();
        assertFalse(Advapi32.INSTANCE.GetTokenInformation(phToken.getValue(), WinNT.TOKEN_INFORMATION_CLASS.TokenOwner, null, 0, tokenInformationLength));
        assertEquals(W32Errors.ERROR_INSUFFICIENT_BUFFER, Kernel32.INSTANCE.GetLastError());
        WinNT.TOKEN_OWNER owner = new WinNT.TOKEN_OWNER(tokenInformationLength.getValue());
        assertTrue(Advapi32.INSTANCE.GetTokenInformation(phToken.getValue(), WinNT.TOKEN_INFORMATION_CLASS.TokenOwner, owner, tokenInformationLength.getValue(), tokenInformationLength));
        assertTrue(tokenInformationLength.getValue() > 0);
        assertTrue(Advapi32.INSTANCE.IsValidSid(owner.Owner));
        int sidLength = Advapi32.INSTANCE.GetLengthSid(owner.Owner);
        assertTrue(sidLength < tokenInformationLength.getValue());
        assertTrue(sidLength > 0);
    // System.out.println(Advapi32Util.convertSidToStringSid(owner.Owner));
    } finally {
        Kernel32Util.closeHandleRef(phToken);
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 79 with Advapi32

use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.

the class Advapi32Test method testRegOpenKeyEx.

public void testRegOpenKeyEx() {
    HKEYByReference phKey = new HKEYByReference();
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegOpenKeyEx(WinReg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft", 0, WinNT.KEY_READ, phKey));
    assertTrue(WinBase.INVALID_HANDLE_VALUE != phKey.getValue());
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(phKey.getValue()));
}
Also used : HKEYByReference(com.sun.jna.platform.win32.WinReg.HKEYByReference)

Example 80 with Advapi32

use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.

the class Win32Service method reportStatus.

/**
   * Report service status to the ServiceControlManager.
   *
   * @param status status
   * @param win32ExitCode exit code 
   * @param waitHint time to wait
   */
private void reportStatus(int status, int win32ExitCode, int waitHint) {
    Advapi32 advapi32;
    SERVICE_STATUS serviceStatus;
    advapi32 = Advapi32.INSTANCE;
    serviceStatus = new SERVICE_STATUS();
    serviceStatus.dwServiceType = WinNT.SERVICE_WIN32_OWN_PROCESS;
    serviceStatus.dwControlsAccepted = Winsvc.SERVICE_ACCEPT_STOP | Winsvc.SERVICE_ACCEPT_SHUTDOWN;
    serviceStatus.dwWin32ExitCode = win32ExitCode;
    serviceStatus.dwWaitHint = waitHint;
    serviceStatus.dwCurrentState = status;
    advapi32.SetServiceStatus(serviceStatusHandle, serviceStatus);
}
Also used : SERVICE_STATUS(com.sun.jna.platform.win32.Winsvc.SERVICE_STATUS)

Aggregations

IntByReference (com.sun.jna.ptr.IntByReference)51 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)39 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)31 HKEYByReference (com.sun.jna.platform.win32.WinReg.HKEYByReference)31 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)23 PSID (com.sun.jna.platform.win32.WinNT.PSID)20 PointerByReference (com.sun.jna.ptr.PointerByReference)20 Advapi32 (com.sun.jna.platform.win32.Advapi32)15 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)15 File (java.io.File)15 Memory (com.sun.jna.Memory)13 HKEY (com.sun.jna.platform.win32.WinReg.HKEY)13 PSIDByReference (com.sun.jna.platform.win32.WinNT.PSIDByReference)10 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)9 ACL (com.sun.jna.platform.win32.WinNT.ACL)9 SECURITY_DESCRIPTOR (com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR)7 Pointer (com.sun.jna.Pointer)6 ACCESS_ALLOWED_ACE (com.sun.jna.platform.win32.WinNT.ACCESS_ALLOWED_ACE)6 GENERIC_MAPPING (com.sun.jna.platform.win32.WinNT.GENERIC_MAPPING)6 BOOLByReference (com.sun.jna.platform.win32.WinDef.BOOLByReference)5