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