Search in sources :

Example 31 with PointerByReference

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

the class VersionTest method testGetFileVersion.

public void testGetFileVersion() {
    String systemRoot = System.getenv("SystemRoot");
    assertNotNull("Missing system root environment variable", systemRoot);
    File file = new File(systemRoot + File.separator + "regedit.exe");
    if (!file.exists()) {
        fail("Can't obtain file version, file " + file + " is missing");
    }
    String filePath = file.getAbsolutePath();
    int size = Version.INSTANCE.GetFileVersionInfoSize(filePath, null);
    assertTrue("GetFileVersionInfoSize(" + filePath + ")", size > 0);
    Pointer buffer = Kernel32.INSTANCE.LocalAlloc(WinBase.LMEM_ZEROINIT, size);
    assertTrue("LocalAlloc(" + size + ")", !buffer.equals(Pointer.NULL));
    try {
        assertTrue("GetFileVersionInfo(" + filePath + ")", Version.INSTANCE.GetFileVersionInfo(filePath, 0, size, buffer));
        IntByReference outputSize = new IntByReference();
        PointerByReference pointer = new PointerByReference();
        assertTrue("VerQueryValue", Version.INSTANCE.VerQueryValue(buffer, "\\", pointer, outputSize));
        VerRsrc.VS_FIXEDFILEINFO fixedFileInfo = new VerRsrc.VS_FIXEDFILEINFO(pointer.getValue());
        assertTrue("dwFileVersionLS", fixedFileInfo.dwFileVersionLS.longValue() > 0);
        assertTrue("dwFileVersionMS", fixedFileInfo.dwFileVersionMS.longValue() > 0);
    } finally {
        Kernel32Util.freeGlobalMemory(buffer);
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) Pointer(com.sun.jna.Pointer) File(java.io.File)

Example 32 with PointerByReference

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

the class OleAutoTest method testLoadRegTypeLib.

public void testLoadRegTypeLib() {
    CLSID.ByReference clsid = new CLSID.ByReference();
    // get CLSID from string, Microsoft Scripting Engine
    HRESULT hr = Ole32.INSTANCE.CLSIDFromString(new WString("{420B2830-E718-11CF-893D-00A0C9054228}"), clsid);
    COMUtils.checkRC(hr);
    assertEquals(0, hr.intValue());
    // get user default lcid
    LCID lcid = Kernel32.INSTANCE.GetUserDefaultLCID();
    PointerByReference pWordTypeLib = new PointerByReference();
    // get typelib version 1.0
    hr = OleAuto.INSTANCE.LoadRegTypeLib(clsid, 1, 0, lcid, pWordTypeLib);
    COMUtils.checkRC(hr);
    assertEquals(0, hr.intValue());
}
Also used : WString(com.sun.jna.WString) LCID(com.sun.jna.platform.win32.WinDef.LCID) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) CLSID(com.sun.jna.platform.win32.Guid.CLSID) PointerByReference(com.sun.jna.ptr.PointerByReference) PointerByReference(com.sun.jna.ptr.PointerByReference)

Example 33 with PointerByReference

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

the class CallbacksTest method testDLLCallback.

public void testDLLCallback() throws Exception {
    if (!Platform.HAS_DLL_CALLBACKS) {
        return;
    }
    final boolean[] called = { false };
    class TestCallback implements TestLibrary.VoidCallback, com.sun.jna.win32.DLLCallback {

        @Override
        public void callback() {
            called[0] = true;
        }
    }
    TestCallback cb = new TestCallback();
    lib.callVoidCallback(cb);
    assertTrue("Callback not called", called[0]);
    // Check module information
    Pointer fp = CallbackReference.getFunctionPointer(cb);
    NativeLibrary kernel32 = NativeLibrary.getInstance("kernel32", W32APIOptions.DEFAULT_OPTIONS);
    Function f = kernel32.getFunction("GetModuleHandleExW");
    final int GET_MODULE_HANDLE_FROM_ADDRESS = 0x4;
    PointerByReference pref = new PointerByReference();
    int result = f.invokeInt(new Object[] { Integer.valueOf(GET_MODULE_HANDLE_FROM_ADDRESS), fp, pref });
    assertTrue("GetModuleHandleEx(fptr) failed: " + Native.getLastError(), result != 0);
    f = kernel32.getFunction("GetModuleFileNameW");
    char[] buf = new char[1024];
    result = f.invokeInt(new Object[] { pref.getValue(), buf, buf.length });
    assertTrue("GetModuleFileName(fptr) failed: " + Native.getLastError(), result != 0);
    f = kernel32.getFunction("GetModuleHandleW");
    Pointer handle = f.invokePointer(new Object[] { Native.jnidispatchPath != null ? Native.jnidispatchPath : "jnidispatch" });
    assertNotNull("GetModuleHandle(\"jnidispatch\") failed: " + Native.getLastError(), handle);
    assertEquals("Wrong module HANDLE for DLL function pointer", handle, pref.getValue());
    // Check slot re-use
    Map<Callback, CallbackReference> refs = new WeakHashMap<Callback, CallbackReference>(callbackCache());
    assertTrue("Callback not cached", refs.containsKey(cb));
    CallbackReference ref = refs.get(cb);
    refs = callbackCache();
    Pointer cbstruct = ref.cbstruct;
    Pointer first_fptr = cbstruct.getPointer(0);
    cb = null;
    System.gc();
    for (int i = 0; i < 100 && (ref.get() != null || refs.containsValue(ref)); ++i) {
        // Give the GC a chance to run
        Thread.sleep(10);
        System.gc();
    }
    assertNull("Callback not GC'd", ref.get());
    assertFalse("Callback still in map", refs.containsValue(ref));
    ref = null;
    System.gc();
    for (int i = 0; i < 100 && (cbstruct.peer != 0 || refs.size() > 0); ++i) {
        // Flush weak hash map
        refs.size();
        // Give the GC a chance to run
        Thread.sleep(10);
        System.gc();
    }
    assertEquals("Callback trampoline not freed", 0, cbstruct.peer);
    // Next allocation should be at same place
    called[0] = false;
    cb = new TestCallback();
    lib.callVoidCallback(cb);
    ref = refs.get(cb);
    cbstruct = ref.cbstruct;
    assertTrue("Callback not called", called[0]);
    assertEquals("Same (in-DLL) address should be re-used for DLL callbacks after callback is GCd", first_fptr, cbstruct.getPointer(0));
}
Also used : CbCallback(com.sun.jna.CallbacksTest.TestLibrary.CbCallback) PointerByReference(com.sun.jna.ptr.PointerByReference) WeakHashMap(java.util.WeakHashMap)

Example 34 with PointerByReference

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

the class ByReferenceArgumentsTest method testPointerByReference.

public void testPointerByReference() {
    PointerByReference pref = new PointerByReference();
    assertNull("Default pointer should be null", pref.getValue());
    pref = new PointerByReference(new Memory(16));
    assertNotNull("Explicit pointer should not be null", pref.getValue());
    lib.setPointerByReferenceNull(pref);
    assertNull("Default pointer should be NULL after call", pref.getValue());
}
Also used : PointerByReference(com.sun.jna.ptr.PointerByReference)

Example 35 with PointerByReference

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

the class Moniker method GetDisplayName.

@Override
public String GetDisplayName(Pointer pbc, Pointer pmkToLeft) {
    final int vTableId = vTableIdStart + 13;
    PointerByReference ppszDisplayNameRef = new PointerByReference();
    WinNT.HRESULT hr = (WinNT.HRESULT) this._invokeNativeObject(vTableId, new Object[] { this.getPointer(), pbc, pmkToLeft, ppszDisplayNameRef }, WinNT.HRESULT.class);
    COMUtils.checkRC(hr);
    Pointer ppszDisplayName = ppszDisplayNameRef.getValue();
    if (ppszDisplayName == null) {
        return null;
    }
    WTypes.LPOLESTR oleStr = new WTypes.LPOLESTR(ppszDisplayName);
    String name = oleStr.getValue();
    Ole32.INSTANCE.CoTaskMemFree(ppszDisplayName);
    return name;
}
Also used : PointerByReference(com.sun.jna.ptr.PointerByReference) WinNT(com.sun.jna.platform.win32.WinNT) Pointer(com.sun.jna.Pointer) WTypes(com.sun.jna.platform.win32.WTypes)

Aggregations

PointerByReference (com.sun.jna.ptr.PointerByReference)128 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)54 IntByReference (com.sun.jna.ptr.IntByReference)35 Pointer (com.sun.jna.Pointer)20 Test (org.junit.Test)19 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)18 REFIID (com.sun.jna.platform.win32.Guid.REFIID)13 File (java.io.File)13 ULONG (com.sun.jna.platform.win32.WinDef.ULONG)10 PSID (com.sun.jna.platform.win32.WinNT.PSID)10 Memory (com.sun.jna.Memory)8 WString (com.sun.jna.WString)8 Dispatch (com.sun.jna.platform.win32.COM.Dispatch)7 ULONGByReference (com.sun.jna.platform.win32.WinDef.ULONGByReference)7 WinNT (com.sun.jna.platform.win32.WinNT)7 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)7 IID (com.sun.jna.platform.win32.Guid.IID)6 ArrayList (java.util.ArrayList)6 IDispatch (com.sun.jna.platform.win32.COM.IDispatch)5 UINT (com.sun.jna.platform.win32.WinDef.UINT)5