Search in sources :

Example 1 with Pointer

use of com.sun.jna.Pointer in project elasticsearch by elastic.

the class JNANatives method tryVirtualLock.

static void tryVirtualLock() {
    JNAKernel32Library kernel = JNAKernel32Library.getInstance();
    Pointer process = null;
    try {
        process = kernel.GetCurrentProcess();
        // By default, Windows limits the number of pages that can be locked.
        // Thus, we need to first increase the working set size of the JVM by
        // the amount of memory we wish to lock, plus a small overhead (1MB).
        SizeT size = new SizeT(JvmInfo.jvmInfo().getMem().getHeapInit().getBytes() + (1024 * 1024));
        if (!kernel.SetProcessWorkingSetSize(process, size, size)) {
            logger.warn("Unable to lock JVM memory. Failed to set working set size. Error code {}", Native.getLastError());
        } else {
            JNAKernel32Library.MemoryBasicInformation memInfo = new JNAKernel32Library.MemoryBasicInformation();
            long address = 0;
            while (kernel.VirtualQueryEx(process, new Pointer(address), memInfo, memInfo.size()) != 0) {
                boolean lockable = memInfo.State.longValue() == JNAKernel32Library.MEM_COMMIT && (memInfo.Protect.longValue() & JNAKernel32Library.PAGE_NOACCESS) != JNAKernel32Library.PAGE_NOACCESS && (memInfo.Protect.longValue() & JNAKernel32Library.PAGE_GUARD) != JNAKernel32Library.PAGE_GUARD;
                if (lockable) {
                    kernel.VirtualLock(memInfo.BaseAddress, new SizeT(memInfo.RegionSize.longValue()));
                }
                // Move to the next region
                address += memInfo.RegionSize.longValue();
            }
            LOCAL_MLOCKALL = true;
        }
    } catch (UnsatisfiedLinkError e) {
    // this will have already been logged by Kernel32Library, no need to repeat it
    } finally {
        if (process != null) {
            kernel.CloseHandle(process);
        }
    }
}
Also used : SizeT(org.elasticsearch.bootstrap.JNAKernel32Library.SizeT) Pointer(com.sun.jna.Pointer)

Example 2 with Pointer

use of com.sun.jna.Pointer in project elasticsearch by elastic.

the class SystemCallFilter method windowsImpl.

// windows impl via job ActiveProcessLimit
static void windowsImpl() {
    if (!Constants.WINDOWS) {
        throw new IllegalStateException("bug: should not be trying to initialize ActiveProcessLimit for an unsupported OS");
    }
    JNAKernel32Library lib = JNAKernel32Library.getInstance();
    // create a new Job
    Pointer job = lib.CreateJobObjectW(null, null);
    if (job == null) {
        throw new UnsupportedOperationException("CreateJobObject: " + Native.getLastError());
    }
    try {
        // retrieve the current basic limits of the job
        int clazz = JNAKernel32Library.JOBOBJECT_BASIC_LIMIT_INFORMATION_CLASS;
        JNAKernel32Library.JOBOBJECT_BASIC_LIMIT_INFORMATION limits = new JNAKernel32Library.JOBOBJECT_BASIC_LIMIT_INFORMATION();
        limits.write();
        if (!lib.QueryInformationJobObject(job, clazz, limits.getPointer(), limits.size(), null)) {
            throw new UnsupportedOperationException("QueryInformationJobObject: " + Native.getLastError());
        }
        limits.read();
        // modify the number of active processes to be 1 (exactly the one process we will add to the job).
        limits.ActiveProcessLimit = 1;
        limits.LimitFlags = JNAKernel32Library.JOB_OBJECT_LIMIT_ACTIVE_PROCESS;
        limits.write();
        if (!lib.SetInformationJobObject(job, clazz, limits.getPointer(), limits.size())) {
            throw new UnsupportedOperationException("SetInformationJobObject: " + Native.getLastError());
        }
        // assign ourselves to the job
        if (!lib.AssignProcessToJobObject(job, lib.GetCurrentProcess())) {
            throw new UnsupportedOperationException("AssignProcessToJobObject: " + Native.getLastError());
        }
    } finally {
        lib.CloseHandle(job);
    }
    logger.debug("Windows ActiveProcessLimit initialization successful");
}
Also used : Pointer(com.sun.jna.Pointer)

Example 3 with Pointer

use of com.sun.jna.Pointer in project jmonkeyengine by jMonkeyEngine.

the class OpenVR method getType.

@Override
public HmdType getType() {
    if (vrsystemFunctions != null) {
        Pointer str1 = new Memory(128);
        Pointer str2 = new Memory(128);
        String completeName = "";
        vrsystemFunctions.GetStringTrackedDeviceProperty.apply(JOpenVRLibrary.k_unTrackedDeviceIndex_Hmd, JOpenVRLibrary.ETrackedDeviceProperty.ETrackedDeviceProperty_Prop_ManufacturerName_String, str1, 128, hmdErrorStore);
        if (hmdErrorStore.getValue() == 0)
            completeName += str1.getString(0);
        vrsystemFunctions.GetStringTrackedDeviceProperty.apply(JOpenVRLibrary.k_unTrackedDeviceIndex_Hmd, JOpenVRLibrary.ETrackedDeviceProperty.ETrackedDeviceProperty_Prop_ModelNumber_String, str2, 128, hmdErrorStore);
        if (hmdErrorStore.getValue() == 0)
            completeName += " " + str2.getString(0);
        if (completeName.length() > 0) {
            completeName = completeName.toLowerCase(Locale.ENGLISH).trim();
            if (completeName.contains("htc") || completeName.contains("vive")) {
                return HmdType.HTC_VIVE;
            } else if (completeName.contains("osvr")) {
                return HmdType.OSVR;
            } else if (completeName.contains("oculus") || completeName.contains("rift") || completeName.contains("dk1") || completeName.contains("dk2") || completeName.contains("cv1")) {
                return HmdType.OCULUS_RIFT;
            } else if (completeName.contains("fove")) {
                return HmdType.FOVE;
            } else if (completeName.contains("game") && completeName.contains("face")) {
                return HmdType.GAMEFACE;
            } else if (completeName.contains("morpheus")) {
                return HmdType.MORPHEUS;
            } else if (completeName.contains("gear")) {
                return HmdType.GEARVR;
            } else if (completeName.contains("star")) {
                return HmdType.STARVR;
            } else if (completeName.contains("null")) {
                return HmdType.NULL;
            }
        }
    } else
        return HmdType.NONE;
    return HmdType.OTHER;
}
Also used : Memory(com.sun.jna.Memory) Pointer(com.sun.jna.Pointer)

Example 4 with Pointer

use of com.sun.jna.Pointer in project jna by java-native-access.

the class Advapi32Test method testCreateWellKnownSid.

public void testCreateWellKnownSid() {
    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.WinWorldSid, null, pSid, cbSid));
    assertTrue("Not recognized as well-known SID", Advapi32.INSTANCE.IsWellKnownSid(pSid, WELL_KNOWN_SID_TYPE.WinWorldSid));
    assertTrue("Invalid SID size", cbSid.getValue() <= WinNT.SECURITY_MAX_SID_SIZE);
    PointerByReference convertedSidStringPtr = new PointerByReference();
    assertTrue("Failed to convert SID", Advapi32.INSTANCE.ConvertSidToStringSid(pSid, convertedSidStringPtr));
    Pointer conv = convertedSidStringPtr.getValue();
    try {
        String convertedSidString = conv.getWideString(0);
        assertEquals("Mismatched SID string", EVERYONE, convertedSidString);
    } finally {
        Kernel32Util.freeLocalMemory(conv);
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) Pointer(com.sun.jna.Pointer) PSID(com.sun.jna.platform.win32.WinNT.PSID)

Example 5 with Pointer

use of com.sun.jna.Pointer in project jna by java-native-access.

the class Advapi32Test method testReadEventLogEntries.

public void testReadEventLogEntries() {
    HANDLE h = Advapi32.INSTANCE.OpenEventLog(null, "Application");
    IntByReference pnBytesRead = new IntByReference();
    IntByReference pnMinNumberOfBytesNeeded = new IntByReference();
    Memory buffer = new Memory(1024 * 64);
    // shorten test, avoid iterating through all events
    int maxReads = 3;
    int rc = 0;
    while (true) {
        if (maxReads-- <= 0)
            break;
        if (!Advapi32.INSTANCE.ReadEventLog(h, WinNT.EVENTLOG_SEQUENTIAL_READ | WinNT.EVENTLOG_FORWARDS_READ, 0, buffer, (int) buffer.size(), pnBytesRead, pnMinNumberOfBytesNeeded)) {
            rc = Kernel32.INSTANCE.GetLastError();
            if (rc == W32Errors.ERROR_INSUFFICIENT_BUFFER) {
                buffer = new Memory(pnMinNumberOfBytesNeeded.getValue());
                rc = 0;
                continue;
            }
            break;
        }
        int dwRead = pnBytesRead.getValue();
        Pointer pevlr = buffer;
        int maxRecords = 3;
        while (dwRead > 0 && maxRecords-- > 0) {
            EVENTLOGRECORD record = new EVENTLOGRECORD(pevlr);
            /*
                  System.out.println(record.RecordNumber.intValue()
                  + " Event ID: " + record.EventID.intValue()
                  + " Event Type: " + record.EventType.intValue()
                  + " Event Source: " + pevlr.getString(record.size(), true));
                */
            dwRead -= record.Length.intValue();
            pevlr = pevlr.share(record.Length.intValue());
        }
    }
    assertTrue("Unexpected error after reading event log: " + new Win32Exception(rc), rc == W32Errors.ERROR_HANDLE_EOF || rc == 0);
    assertTrue("Error closing event log", Advapi32.INSTANCE.CloseEventLog(h));
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) Memory(com.sun.jna.Memory) Pointer(com.sun.jna.Pointer) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) EVENTLOGRECORD(com.sun.jna.platform.win32.WinNT.EVENTLOGRECORD)

Aggregations

Pointer (com.sun.jna.Pointer)152 PointerByReference (com.sun.jna.ptr.PointerByReference)31 Test (org.junit.Test)31 Memory (com.sun.jna.Memory)23 File (java.io.File)21 ByteBuffer (java.nio.ByteBuffer)18 NativeLong (com.sun.jna.NativeLong)17 IntByReference (com.sun.jna.ptr.IntByReference)17 BufferedImage (java.awt.image.BufferedImage)15 FileInputStream (java.io.FileInputStream)11 HDDEDATA (com.sun.jna.platform.win32.Ddeml.HDDEDATA)8 HSZ (com.sun.jna.platform.win32.Ddeml.HSZ)8 DirectBuffer (sun.nio.ch.DirectBuffer)8 HCONV (com.sun.jna.platform.win32.Ddeml.HCONV)7 ConnectHandler (com.sun.jna.platform.win32.DdemlUtil.ConnectHandler)7 IDdeConnection (com.sun.jna.platform.win32.DdemlUtil.IDdeConnection)7 StandaloneDdeClient (com.sun.jna.platform.win32.DdemlUtil.StandaloneDdeClient)7 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)7 IOException (java.io.IOException)7 CountDownLatch (java.util.concurrent.CountDownLatch)7