Search in sources :

Example 31 with HANDLEByReference

use of com.sun.jna.platform.win32.WinNT.HANDLEByReference 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 32 with HANDLEByReference

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

the class WinspoolTest method testOpenPrinter.

public void testOpenPrinter() {
    HANDLEByReference hbr = new HANDLEByReference();
    boolean result = Winspool.INSTANCE.OpenPrinter("1234567890A123", hbr, null);
    assertFalse("OpenPrinter should return false on failure.", result);
    assertNull("The pointer-to-a-printer-handle should be null on failure.", hbr.getValue());
    assertEquals("GetLastError() should return ERROR_INVALID_PRINTER_NAME", WinError.ERROR_INVALID_PRINTER_NAME, Native.getLastError());
}
Also used : HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference)

Example 33 with HANDLEByReference

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

the class PdhTest method testQueryMultipleCounters.

@Test
public void testQueryMultipleCounters() {
    Collection<String> names = new LinkedList<String>();
    PDH_COUNTER_PATH_ELEMENTS elems = new PDH_COUNTER_PATH_ELEMENTS();
    elems.szObjectName = "Processor";
    elems.szInstanceName = "_Total";
    for (String n : new String[] { "% Processor Time", "% Idle Time", "% User Time" }) {
        elems.szCounterName = n;
        String counterName = makeCounterPath(pdh, elems);
        names.add(counterName);
    }
    HANDLEByReference ref = new HANDLEByReference();
    assertErrorSuccess("PdhOpenQuery", pdh.PdhOpenQuery(null, null, ref), true);
    HANDLE hQuery = ref.getValue();
    try {
        Map<String, HANDLE> handlesMap = new HashMap<String, HANDLE>(names.size());
        try {
            for (String counterName : names) {
                ref.setValue(null);
                assertErrorSuccess("PdhAddCounter[" + counterName + "]", pdh.PdhAddEnglishCounter(hQuery, counterName, null, ref), true);
                HANDLE hCounter = ref.getValue();
                handlesMap.put(counterName, hCounter);
            }
            assertErrorSuccess("PdhCollectQueryData", pdh.PdhCollectQueryData(hQuery), true);
            for (Map.Entry<String, HANDLE> ch : handlesMap.entrySet()) {
                String counterName = ch.getKey();
                HANDLE hCounter = ch.getValue();
                PDH_RAW_COUNTER rawCounter = new PDH_RAW_COUNTER();
                DWORDByReference lpdwType = new DWORDByReference();
                assertErrorSuccess("PdhGetRawCounterValue[" + counterName + "]", pdh.PdhGetRawCounterValue(hCounter, lpdwType, rawCounter), true);
                assertEquals("Bad counter data status for " + counterName, PdhMsg.PDH_CSTATUS_VALID_DATA, rawCounter.CStatus);
                showRawCounterData(System.out, counterName, rawCounter);
            }
        } finally {
            names.clear();
            for (Map.Entry<String, HANDLE> ch : handlesMap.entrySet()) {
                String name = ch.getKey();
                HANDLE hCounter = ch.getValue();
                int status = pdh.PdhRemoveCounter(hCounter);
                if (status != WinError.ERROR_SUCCESS) {
                    names.add(name);
                }
            }
            if (names.size() > 0) {
                fail("Failed to remove counters: " + names);
            }
        }
    } finally {
        assertErrorSuccess("PdhCloseQuery", pdh.PdhCloseQuery(hQuery), true);
    }
}
Also used : PDH_COUNTER_PATH_ELEMENTS(com.sun.jna.platform.win32.Pdh.PDH_COUNTER_PATH_ELEMENTS) HashMap(java.util.HashMap) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) PDH_RAW_COUNTER(com.sun.jna.platform.win32.Pdh.PDH_RAW_COUNTER) LinkedList(java.util.LinkedList) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 34 with HANDLEByReference

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

the class PdhTest method testQueryOneCounter.

@Test
public void testQueryOneCounter() {
    PDH_COUNTER_PATH_ELEMENTS elems = new PDH_COUNTER_PATH_ELEMENTS();
    elems.szObjectName = "Processor";
    elems.szInstanceName = "_Total";
    elems.szCounterName = "% Processor Time";
    String counterName = makeCounterPath(pdh, elems);
    HANDLEByReference ref = new HANDLEByReference();
    assertErrorSuccess("PdhOpenQuery", pdh.PdhOpenQuery(null, null, ref), true);
    HANDLE hQuery = ref.getValue();
    try {
        ref.setValue(null);
        assertErrorSuccess("PdhAddEnglishCounter", pdh.PdhAddEnglishCounter(hQuery, counterName, null, ref), true);
        HANDLE hCounter = ref.getValue();
        try {
            assertErrorSuccess("PdhCollectQueryData", pdh.PdhCollectQueryData(hQuery), true);
            DWORDByReference lpdwType = new DWORDByReference();
            PDH_RAW_COUNTER rawCounter = new PDH_RAW_COUNTER();
            assertErrorSuccess("PdhGetRawCounterValue", pdh.PdhGetRawCounterValue(hCounter, lpdwType, rawCounter), true);
            assertEquals("Bad counter data status", PdhMsg.PDH_CSTATUS_VALID_DATA, rawCounter.CStatus);
            DWORD dwType = lpdwType.getValue();
            int typeValue = dwType.intValue();
            // see https://technet.microsoft.com/en-us/library/cc786359(v=ws.10).aspx
            assertEquals("Mismatched counter type", WinPerf.PERF_100NSEC_TIMER_INV, typeValue);
            showRawCounterData(System.out, counterName, rawCounter);
        } finally {
            assertErrorSuccess("PdhRemoveCounter", pdh.PdhRemoveCounter(hCounter), true);
        }
    } finally {
        assertErrorSuccess("PdhCloseQuery", pdh.PdhCloseQuery(hQuery), true);
    }
}
Also used : PDH_COUNTER_PATH_ELEMENTS(com.sun.jna.platform.win32.Pdh.PDH_COUNTER_PATH_ELEMENTS) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) PDH_RAW_COUNTER(com.sun.jna.platform.win32.Pdh.PDH_RAW_COUNTER) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) Test(org.junit.Test)

Aggregations

HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)33 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)22 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)15 IntByReference (com.sun.jna.ptr.IntByReference)11 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)7 TOKEN_PRIVILEGES (com.sun.jna.platform.win32.WinNT.TOKEN_PRIVILEGES)5 File (java.io.File)5 USER_INFO_1 (com.sun.jna.platform.win32.LMAccess.USER_INFO_1)3 BOOLByReference (com.sun.jna.platform.win32.WinDef.BOOLByReference)3 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)3 PointerByReference (com.sun.jna.ptr.PointerByReference)3 Memory (com.sun.jna.Memory)2 Account (com.sun.jna.platform.win32.Advapi32Util.Account)2 PDH_COUNTER_PATH_ELEMENTS (com.sun.jna.platform.win32.Pdh.PDH_COUNTER_PATH_ELEMENTS)2 PDH_RAW_COUNTER (com.sun.jna.platform.win32.Pdh.PDH_RAW_COUNTER)2 RASCREDENTIALS (com.sun.jna.platform.win32.WinRas.RASCREDENTIALS)2 RASDIALPARAMS (com.sun.jna.platform.win32.WinRas.RASDIALPARAMS)2 Test (org.junit.Test)2 CredHandle (com.sun.jna.platform.win32.Sspi.CredHandle)1 CtxtHandle (com.sun.jna.platform.win32.Sspi.CtxtHandle)1