⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jnative.java

📁 jnative java 调用动态库需要的包和dll
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	 * @exception NativeException
	 * 
	 */
	public void setParameter(int pos, Type type, byte[] value)
			throws /* NativeException, */IllegalAccessException {
		throwClosed();
        
    	if (parameters.size() <= pos) {
			// Fills the vector with null and espect that the call will give
			// right values later
			int i = parameters.size();
			while (i++ <= pos) {
				parameters.add(new byte[4]);
				parameterTypes.add(Type.INT.getNativeType());
			}
		}
		parameters.set(pos, value);
		parameterTypes.set(pos, type.getNativeType());

		// ---------------------------------------DEBUG CODE -
		// final String pouet;
		// if(parameterTypes.get(pos) == Type.PSTRUCT.getNativeType()) {
		// int val=StructConverter.bytesIntoInt(parameters.get(pos), 0);
		// pouet = "0x"+Integer.toHexString(val)+ " soit "+val+" dec";
		// } else {
		// pouet = new String(parameters.get(pos));
		// }
		// getLogger().log(SEVERITY.DEBUG, "Adding parameter "+pos+", value = "+pouet);
		// ---------------------------------------DEBUG CODE -

	}

	/**
	 * Method setParameter <br>
	 * Sets the parameter at index <code>pos</code>
	 * 
	 * @param pos
	 *            the offset of the parameter
	 * @param p
	 *            a pointer object representing an address
	 * 
	 * @exception NativeException
	 * @exception IllegalAccessException
	 *                if <code>dispose()</code> have already been called.
	 * 
	 */
	public void setParameter(int pos, Pointer p) throws NativeException,
			IllegalAccessException {
		throwClosed();
		if (p == null || p.getPointer() == 0) {
			setParameter(pos, 0);
		} else {
			byte[] buf = new byte[4];
			StructConverter.intIntoBytes(p.getPointer(), buf, 0);
			setParameter(pos, Type.PSTRUCT, buf);
			// nSetPointer(mJNativePointer, pos, p.getPointer(), p.getSize());
		}
	}

	/**
	 * Method setRetVal fixes the return type of this function.
	 * 
	 * @param type
	 *            a Type generally VOID or INT, if VOID it is not necessary to
	 *            call this function
	 * 
	 * @exception NativeException
	 * @exception IllegalAccessException
	 *                if <code>dispose()</code> have already been called.
	 * 
	 */
	public void setRetVal(Type type) throws NativeException,
			IllegalAccessException {
		throwClosed();
		mRetType = type.getNativeType();
	}

	/**
	 * Method getRetVal gets the value returned by the function, should be
	 * verified to avoid invalid pointers when getting out values
	 * 
	 * @return the value returned by this function in its String representation<br>
	 *         BOOL functions return int values
	 * 
	 * @exception IllegalAccessException
	 *                if <code>dispose()</code> have already been called.
	 * 
	 */
	public String getRetVal() throws IllegalAccessException {
		throwClosed();
		return mRetValue;
	}

	/**
	 * Method getRetValAsInt gets the value returned by the function, should be
	 * verified to avoid invalid pointers when getting out values
	 * 
	 * @return the value returned by this function in its Integer representation<br>
	 *         BOOL functions return int values
	 * 
	 * @exception IllegalAccessException
	 *                if <code>dispose()</code> have already been called.
	 * 
	 */
	public int getRetValAsInt() throws IllegalAccessException /*, ParseException*/ {
		throwClosed();
		return Integer.parseInt(mRetValue);
	}

	/**
	 * Method getParameter
	 * 
	 * @param pos
	 *            an int
	 * 
	 * @return the parameter at index <code>pos</code>
	 * 
	 * @exception NativeException
	 * @exception IllegalAccessException
	 *                if <code>dispose()</code> have already been called.
	 * 
	 */
	public String getParameter(int pos) throws NativeException,
			IllegalAccessException {
		throwClosed();
		return nGetParameter(mJNativePointer, pos);
	}

	/**
	 * Method invoke calls the function
	 * 
	 * @exception NativeException
	 * @exception IllegalAccessException
	 *                if <code>dispose()</code> have already been called.
	 * 
	 */
	public void invoke() throws NativeException, IllegalAccessException {
		throwClosed();
		nInvoke(mJNativePointer);
	}

	/**
	 * Method dispose free native pointers and memory internally used by the
	 * jnative dll
	 * <p>
	 * No more calls to this dll can be done when dispose is called !!
	 * </p>
	 * 
	 * @exception NativeException
	 * @exception IllegalAccessException
	 *                if <code>dispose()</code> have already been called.
	 * 
	 */
	public final void dispose() throws NativeException, IllegalAccessException {
		throwClosed();
		synchronized (mLibs) {
			LibDesc libDesc = getLibDesc(mDllName);
			libDesc.numHolders--;
			if(libDesc.numHolders == 0) {
				nDispose(mJNativeHModule);
				isClosed = true;
				mLibs.remove(mDllName);
			}
		}
	}

	/**
	 * Called by the garbage collector on an object when garbage collection
	 * determines that there are no more references to the object. A subclass
	 * overrides the <code>finalize</code> method to dispose of system
	 * resources or to perform other cleanup.
	 * <p>
	 * The general contract of <tt>finalize</tt> is that it is invoked if and
	 * when the Java<font size="-2"><sup>TM</sup></font> virtual machine has
	 * determined that there is no longer any means by which this object can be
	 * accessed by any thread that has not yet died, except as a result of an
	 * action taken by the finalization of some other object or class which is
	 * ready to be finalized. The <tt>finalize</tt> method may take any
	 * action, including making this object available again to other threads;
	 * the usual purpose of <tt>finalize</tt>, however, is to perform cleanup
	 * actions before the object is irrevocably discarded. For example, the
	 * finalize method for an object that represents an input/output connection
	 * might perform explicit I/O transactions to break the connection before
	 * the object is permanently discarded.
	 * <p>
	 * The <tt>finalize</tt> method of class <tt>Object</tt> performs no
	 * special action; it simply returns normally. Subclasses of <tt>Object</tt>
	 * may override this definition.
	 * <p>
	 * The Java programming language does not guarantee which thread will invoke
	 * the <tt>finalize</tt> method for any given object. It is guaranteed,
	 * however, that the thread that invokes finalize will not be holding any
	 * user-visible synchronization locks when finalize is invoked. If an
	 * uncaught exception is thrown by the finalize method, the exception is
	 * ignored and finalization of that object terminates.
	 * <p>
	 * After the <tt>finalize</tt> method has been invoked for an object, no
	 * further action is taken until the Java virtual machine has again
	 * determined that there is no longer any means by which this object can be
	 * accessed by any thread that has not yet died, including possible actions
	 * by other objects or classes which are ready to be finalized, at which
	 * point the object may be discarded.
	 * <p>
	 * The <tt>finalize</tt> method is never invoked more than once by a Java
	 * virtual machine for any given object.
	 * <p>
	 * Any exception thrown by the <code>finalize</code> method causes the
	 * finalization of this object to be halted, but is otherwise ignored.
	 * 
	 * <p>
	 * <b>This one calls dispose() but all Throwable are catched</b>
	 * </p>
	 * 
	 * @throws Throwable
	 *             the <code>Exception</code> raised by this method
	 */
	@Override
	protected void finalize() throws Throwable {
		try {
			dispose();
		} catch (Throwable e) {
			// getLogger().log(SEVERITY.ERROR, e);
		}
	}

	/**
	 * Method getFunctionName
	 * 
	 * @return the name of this function
	 * 
	 */
	public final String getFunctionName() {
		return mFunctionName;
	}

	/**
	 * Method getDLLName
	 * 
	 * @return the name of this DLL
	 * 
	 */
	public final String getDLLName() {
		return mDllName;
	}

	/**
	 * 
	 * @return the convention of call (CDECL, STDCALL)
	 */
	public Convention getStyle() {
		return Convention.fromInt(convention);
	}
	
	/**
	 * Indicates whether some other object is "equal to" this one.
	 * 
	 * @returns true if the dll and the funtionName are equals
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj == null || !(obj instanceof JNative)) {
			return false;
		} else {
			JNative n = (JNative) obj;
			return mDllName.equals(n.getDLLName())
					&& mFunctionName.equals(n.getFunctionName());
		}
	}

	private void throwClosed() throws IllegalAccessException {
		if (isClosed)
			throw new IllegalAccessException("This function ("
					+ getFunctionName() + " in " + getDLLName()
					+ ") is already closed");
	}

	/**
	 * Method allocMemory allocates native block of memory : don't forget to
	 * free it with <code>freeMemory(int)</code>!!!!
	 * 
	 * @param size
	 *            the size of the memory block to reserve in bytes
	 * 
	 * @return the address of the reserved memory (a pointer)
	 * 
	 * @exception NativeException
	 *                if the memory cannot be reserved
	 * 
	 */
	public static int allocMemory(int size) throws NativeException {
		return nMalloc(size);
	}

	/**
	 * Method freeMemory try to free the block of memory pointed by
	 * <code>pointer</code><br>
	 * No checks are done to see if the pointer is valid (TODO ?)
	 * 
	 * @param pointer
	 *            the address of a memory block to free
	 * 
	 * @exception NativeException
	 *                if the memory cannon be freed
	 * 
	 */
	public static void freeMemory(int pointer) throws NativeException {
		nFree(pointer);
	}

	/**
	 * Method setMemory fills the native memory with the content of
	 * <code>buffer</code> <br>
	 * <b>Be aware that buffer overflows are no checked !!!!</b>
	 * 
	 * @param pointer
	 *            the address of the native memory
	 * @param buffer
	 *            a String to memcpy
	 * 
	 * @exception NativeException
	 * 
	 */
	public static void setMemory(int pointer, String buffer)
			throws NativeException {
		setMemory(pointer, buffer.getBytes());
	}

	/**
	 * Method setMemory fills the native memory with the content of
	 * <code>buffer</code> <br>
	 * <b>Be aware that buffer overflows are no checked !!!!</b>
	 * 
	 * @param pointer
	 *            the address of the native memory
	 * @param buffer
	 *            a byte[] to memcpy
	 * 
	 * @exception NativeException
	 * 
	 */
	public static void setMemory(int pointer, byte[] buffer)
			throws NativeException {
		setMemory(pointer, buffer, 0, buffer.length);
	}

	/**
	 * Method setMemory fills the native memory with the content of
	 * <code>buffer</code> <br>
	 * <b>Be aware that buffer overflows are no checked !!!!</b>
	 * 
	 * @param pointer
	 *            the address of the native memory
	 * @param buffer
	 *            a byte[] to memcpy
	 * @param offset
	 *            the offset of the native side
	 * @param len
	 *            the number of bytes to copy
	 * 
	 * @exception NativeException
	 * 
	 */
	public static void setMemory(int pointer, byte[] buffer, int offset, int len)
			throws NativeException {
		nSetMemory(pointer, buffer, offset, len);
	}

	/**
	 * Method getMemory
	 * 
	 * @param pointer
	 *            the address of the native memory
	 * @param size
	 *            number of bytes to copy
	 * 
	 * @return a copy of the memory at address <code>pointer</code>
	 * 
	 * @exception NativeException
	 * 
	 */
	public static byte[] getMemory(int pointer, int size)
			throws NativeException {
		return nGetMemory(pointer, size);
	}

	/**
	 * Method getMemoryAsString
	 * 
	 * @param pointer
	 *            the address of the native char*
	 * @param size
	 *            number of bytes to copy
	 * 
	 * @return a String copy of the memory at address <code>pointer</code>
	 *         limited by a NULL terminator if the char* is lower than </code>size</code>
	 *         characters
	 * 
	 * @exception NativeException
	 * 
	 */
	public static String getMemoryAsString(int pointer, int size)
			throws NativeException {
		byte[] buf = nGetMemory(pointer, size);
		for (int i = 0; i < buf.length; i++) {
			if (buf[i] == 0) {
				return new String(buf, 0, i);
			}
		}
		return new String(buf);
	}

	/**
	 * Method registerWindowProc register a WindowProc for the Window
	 * <code>hwnd</code>
	 * 
	 * @param hwnd
	 *            the HANDLE of the window
	 * @param proc
	 *            a WindowProc object that be called by native events
	 * 
	 * @return the previous function pointer used as a WindowProc for this
	 *         window.
	 * 
	 * @exception NativeException
	 *                if the SetWindowLongPtr fails or somthing weird happens
	 *                (can crash too ;) )...
	 * 
	 */
	public static int registerWindowProc(int hwnd, WindowProc proc)
			throws NativeException {
		return nRegisterWindowProc(hwnd, proc, false);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -