/** * Get any error message provided by the platform (as in dlerror). If no error, then return null. * * <p>Note that calling this method clears the error state, so calling two times without calling * any other platform getFunction method (getInstance(), getFunction(), * getGlobalVariableAddress(), dispose()) will result in returning null. * * @return String (may be null) */ public static String errorStr() { if (DEBUG) { VM.println("Calling DLERROR"); } int result = VM.execSyncIO(ChannelConstants.DLERROR, 0, 0, 0, 0, 0, 0, null, null); Address r = Address.fromPrimitive(result); if (r.isZero()) { return null; } else { return Pointer.NativeUnsafeGetString(r); } }
/** * Look up a native libray named "name". Load if not loaded. Name can be a path or a libray name. * The Library will be looked up in a platform-dependant manor. * * @param name short library name, full file name, or path to the library file * @return NativeLibrary */ public static NativeLibrary getInstance(String name) { String nativeName = nativeLibraryName(name); Pointer name0 = Pointer.createStringBuffer(nativeName); if (DEBUG) { VM.print("Calling DLOPEN on "); VM.println(name); } int result = VM.execSyncIO( ChannelConstants.DLOPEN, name0.address().toUWord().toInt(), 0, 0, 0, 0, 0, null, null); Address r = Address.fromPrimitive(result); name0.free(); if (r.isZero()) { throw new RuntimeException("Can't open library " + name + ". OS Error: " + errorStr()); } return new NativeLibrary(name, r); }
/** * Close the library, as in dlclose. * * @throws RuntimeException if dlcose fails */ public void dispose() { if (closed || ptr.isZero()) { throw new RuntimeException("closed or RTLD_DEFAULT"); } if (DEBUG) { VM.print("Calling DLCLOSE on "); VM.println(name); } Pointer name0 = Pointer.createStringBuffer(name); int result = VM.execSyncIO( ChannelConstants.DLCLOSE, ptr.toUWord().toInt(), 0, 0, 0, 0, 0, 0, null, null); name0.free(); if (result != 0) { throw new RuntimeException("Error on dlclose: " + errorStr()); } closed = true; }
/** * getFunction a symbol's address by name (warpper around dlsym) * * @param name * @return Address of symbol */ private Address getSymbolAddress(String name) { if (closed) { throw new IllegalStateException("closed"); } if (DEBUG) { VM.print("Calling DLSYM on "); VM.println(name); } Pointer name0 = Pointer.createStringBuffer(name); int result = VM.execSyncIO( ChannelConstants.DLSYM, ptr.toUWord().toInt(), name0.address().toUWord().toInt(), 0, 0, 0, 0, null, null); name0.free(); return Address.fromPrimitive(result); }