private static void initialize() { if (ApplicationManager.getApplication().isUnitTestMode()) { return; } Ole32 ole32 = Ole32.INSTANCE; ole32.CoInitializeEx(Pointer.NULL, 0); Guid.GUID CLSID_TaskbarList = Ole32Util.getGUIDFromString("{56FDF344-FD6D-11d0-958A-006097C9A090}"); Guid.GUID IID_ITaskbarList3 = Ole32Util.getGUIDFromString("{EA1AFB91-9E28-4B86-90E9-9E9F8A5EEFAF}"); PointerByReference p = new PointerByReference(); ole32.CoCreateInstance( CLSID_TaskbarList, Pointer.NULL, ObjBase.CLSCTX_ALL, IID_ITaskbarList3, p); myInterfacePointer = p.getValue(); Pointer vTablePointer = myInterfacePointer.getPointer(0); Pointer[] vTable = new Pointer[TaskBarList_Methods]; vTablePointer.read(0, vTable, 0, vTable.length); mySetProgressValue = Function.getFunction(vTable[TaskBarList_SetProgressValue], Function.ALT_CONVENTION); mySetProgressState = Function.getFunction(vTable[TaskBarList_SetProgressState], Function.ALT_CONVENTION); mySetOverlayIcon = Function.getFunction(vTable[TaskBarList_SetOverlayIcon], Function.ALT_CONVENTION); }
public void memoryMove(long src, long dest, long len) { final Pointer srcPointer = new Pointer(src); final Pointer destPointer = new Pointer(dest); if (memmove != null) memmove.invoke(Pointer.class, new Object[] {destPointer, srcPointer, new NativeLong(len)}); else if (bcopy != null) bcopy.invokeVoid(new Object[] {srcPointer, destPointer, new NativeLong(len)}); else { if (src > dest) for (long n = 0; n < len; n++) destPointer.setByte(n, srcPointer.getByte(n)); else for (long n = len - 1; n >= 0; n--) destPointer.setByte(n, srcPointer.getByte(n)); } }
static void setProgress(IdeFrame frame, double value, boolean isOk) { if (!isEnabled()) { return; } WinDef.HWND handle = getHandle(frame); mySetProgressState.invokeInt( new Object[] {myInterfacePointer, handle, isOk ? TBPF_NORMAL : TBPF_ERROR}); mySetProgressValue.invokeInt( new Object[] { myInterfacePointer, handle, new WinDef.ULONGLONG((long) (value * 100)), TOTAL_PROGRESS }); }
static void hideProgress(IdeFrame frame) { if (!isEnabled()) { return; } mySetProgressState.invokeInt( new Object[] {myInterfacePointer, getHandle(frame), TBPF_NOPROGRESS}); }
public Object invoke(Object proxy, Method method, Object[] inArgs) throws Throwable { // Intercept Object methods if (OBJECT_TOSTRING.equals(method)) { return "Proxy interface to " + nativeLibrary; } else if (OBJECT_HASHCODE.equals(method)) { return new Integer(hashCode()); } else if (OBJECT_EQUALS.equals(method)) { Object o = inArgs[0]; if (o != null && Proxy.isProxyClass(o.getClass())) { return Function.valueOf(Proxy.getInvocationHandler(o) == this); } return Boolean.FALSE; } FunctionInfo f = null; synchronized (functions) { f = (FunctionInfo) functions.get(method); if (f == null) { f = new FunctionInfo(); f.isVarArgs = Function.isVarArgs(method); if (invocationMapper != null) { f.handler = invocationMapper.getInvocationHandler(nativeLibrary, method); } if (f.handler == null) { // Find the function to invoke String methodName = functionMapper.getFunctionName(nativeLibrary, method); if (methodName == null) { // Just in case the function mapper screwed up methodName = method.getName(); } f.function = nativeLibrary.getFunction(methodName, method); f.options = new HashMap(this.options); f.options.put(Function.OPTION_INVOKING_METHOD, method); } functions.put(method, f); } } if (f.isVarArgs) { inArgs = Function.concatenateVarArgs(inArgs); } if (f.handler != null) { return f.handler.invoke(proxy, method, inArgs); } return f.function.invoke(method.getReturnType(), inArgs, f.options); }
public void testFunctionHoldsLibraryReference() throws Exception { NativeLibrary lib = NativeLibrary.getInstance("testlib"); WeakReference ref = new WeakReference(lib); Function f = lib.getFunction("callCount"); lib = null; System.gc(); long start = System.currentTimeMillis(); while (ref.get() != null && System.currentTimeMillis() - start < 2000) { Thread.sleep(10); } assertNotNull("Library GC'd when it should not be", ref.get()); f.invokeInt(new Object[0]); f = null; System.gc(); while (ref.get() != null && System.currentTimeMillis() - start < 5000) { Thread.sleep(10); } assertNull("Library not GC'd", ref.get()); }
static void setOverlayIcon(IdeFrame frame, Object icon, boolean dispose) { if (!isEnabled()) { return; } if (icon == null) { icon = Pointer.NULL; } mySetOverlayIcon.invokeInt( new Object[] {myInterfacePointer, getHandle(frame), icon, Pointer.NULL}); if (dispose) { User32.INSTANCE.DestroyIcon((WinDef.HICON) icon); } }
static { Function memmoveFc; try { memmoveFc = Function.getFunction(Platform.C_LIBRARY_NAME, "memmove"); } catch (UnsatisfiedLinkError linkError) { memmoveFc = null; } Function bcopyFc; try { bcopyFc = Function.getFunction(Platform.C_LIBRARY_NAME, "bcopy"); } catch (UnsatisfiedLinkError linkError) { bcopyFc = null; } memmove = memmoveFc; bcopy = bcopyFc; OLogManager.instance() .debug( CLibrary.class, "Following c library functions were found memmove : %s , bcopy : %s.", memmoveFc != null ? "yes" : "no", bcopyFc != null ? "yes" : "no"); }
public static int sched_getcpu() { final CLibrary lib = CLibrary.INSTANCE; try { final int ret = lib.sched_getcpu(); if (ret < 0) { throw new IllegalStateException("sched_getcpu() failed; errno=" + Native.getLastError()); } return ret; } catch (LastErrorException e) { throw new IllegalStateException("sched_getcpu() failed; errno=" + e.getErrorCode(), e); } catch (UnsatisfiedLinkError ule) { try { final IntByReference cpu = new IntByReference(); final IntByReference node = new IntByReference(); final int ret = lib.syscall(318, cpu, node, null); if (ret != 0) { throw new IllegalStateException("getcpu() failed; errno=" + Native.getLastError()); } return cpu.getValue(); } catch (LastErrorException lee) { if (lee.getErrorCode() == 38 && Platform.is64Bit()) { // unknown call final Pointer getcpuAddr = new Pointer((-10L << 20) + 1024L * 2L); final Function getcpu = Function.getFunction(getcpuAddr, Function.C_CONVENTION); final IntByReference cpu = new IntByReference(); if (getcpu.invokeInt(new Object[] {cpu, null, null}) < 0) { throw new IllegalStateException("getcpu() failed; errno=" + Native.getLastError()); } else { return cpu.getValue(); } } else { throw new IllegalStateException("getcpu() failed; errno=" + lee.getErrorCode(), lee); } } } }
public void testInterceptLastError() { if (!Platform.isWindows()) { return; } NativeLibrary kernel32 = (NativeLibrary) NativeLibrary.getInstance("kernel32", W32APIOptions.DEFAULT_OPTIONS); Function get = kernel32.getFunction("GetLastError"); Function set = kernel32.getFunction("SetLastError"); assertEquals("SetLastError should not be customized", Function.class, set.getClass()); assertTrue( "GetLastError should be a Function", Function.class.isAssignableFrom(get.getClass())); assertTrue("GetLastError should be a customized Function", get.getClass() != Function.class); final int EXPECTED = 42; set.invokeVoid(new Object[] {new Integer(EXPECTED)}); assertEquals("Wrong error", EXPECTED, get.invokeInt(null)); }