|
JNative project : see http://jnative.sf.net |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.xvolks.jnative.JNative
public class JNative
JNative this is the main class for calling native functions.
$Id: JNative.java,v 1.8 2006/01/12 21:35:45 mdenty Exp $;
To do so you have to :
BOOL GetDiskFreeSpaceEx( LPCTSTR lpDirectoryName, PULARGE_INTEGER lpFreeBytesAvailable, PULARGE_INTEGER lpTotalNumberOfBytes, PULARGE_INTEGER lpTotalNumberOfFreeBytes );
public static final FreeDiskSpace getDiskFreeSpaceEx(String drive) throws NativeException, IllegalAccessException { if(drive == null) throw new NullPointerException("The drive name cannot be null !"); Pointer lpFreeBytesAvailable = new Pointer(24); int i = 0; JNative fs = new JNative("Kernel32.dll", "GetDiskFreeSpaceExA"); fs.setRetVal(Type.INT); fs.setParameter(i++, Type.STRING, drive); fs.setParameter(i++, lpFreeBytesAvailable.getPointer(), 8); fs.setParameter(i++, lpFreeBytesAvailable.getPointer() + 8, 8); fs.setParameter(i++, lpFreeBytesAvailable.getPointer() + 16 , 8); fs.invoke(); FreeDiskSpace dsp = new FreeDiskSpace(drive, lpFreeBytesAvailable); lpFreeBytesAvailable.dispose(); return dsp; }
Constructor Summary | |
---|---|
JNative(java.lang.String dllName,
java.lang.String functionName)
Constructor exact call of new JNative(dllName, functionName, false); Creates a function without debug output |
|
JNative(java.lang.String dllName,
java.lang.String functionName,
boolean nativeDebug)
Constructor |
Method Summary | |
---|---|
static int |
allocMemory(int size)
Method allocMemory allocates native block of memory : don't forget to free it with freeMemory(int) !!!! |
void |
dispose()
Method dispose free native pointers and memory internally used by the jnative dll No more calls to this dll can be done when dispose is called !! |
boolean |
equals(java.lang.Object obj)
Indicates whether some other object is "equal to" this one. |
protected void |
finalize()
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. |
static void |
freeMemory(int pointer)
Method freeMemory try to free the block of memory pointed by pointer No checks are done to see if the pointer is valid (TODO ?) |
static int |
getCurrentModule()
Method getCurrentModule |
static java.lang.String[] |
getDLLFileExports(java.lang.String dllFile)
Method getDLLFileExports gets all the names of the functions exported by a library |
java.lang.String |
getDLLName()
Method getDLLName |
java.lang.String |
getFunctionName()
Method getFunctionName |
static byte[] |
getMemory(int pointer,
int size)
Method getMemory |
static java.lang.String |
getMemoryAsString(int pointer,
int size)
Method getMemoryAsString |
java.lang.String |
getParameter(int pos)
Method getParameter |
java.lang.String |
getRetVal()
Method getRetVal gets the value returned by the function, should be verified to avoid invalid pointers when getting out values |
void |
invoke()
Method invoke calls the function |
static int |
registerWindowProc(int hwnd,
WindowProc proc)
Method registerWindowProc register a WindowProc for the Window hwnd |
static void |
setMemory(int pointer,
byte[] buffer)
Method setMemory fills the native memory with the content of buffer
Be aware that buffer overflows are no checked !!!! |
static void |
setMemory(int pointer,
byte[] buffer,
int offset,
int len)
Method setMemory fills the native memory with the content of buffer
Be aware that buffer overflows are no checked !!!! |
static void |
setMemory(int pointer,
java.lang.String buffer)
Method setMemory fills the native memory with the content of buffer
Be aware that buffer overflows are no checked !!!! |
void |
setParameter(int pos,
int pointer,
int size)
Method setParameter Sets the parameter at index pos |
void |
setParameter(int pos,
Pointer p)
Method setParameter Sets the parameter at index pos |
void |
setParameter(int pos,
Type type,
byte[] value)
Method setParameter Sets the parameter at index pos |
void |
setParameter(int pos,
Type type,
java.lang.String value)
Method setParameter Sets the parameter at index pos |
void |
setRetVal(Type type)
Method setRetVal fixes the return type of this function. |
Methods inherited from class java.lang.Object |
---|
clone, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public JNative(java.lang.String dllName, java.lang.String functionName) throws NativeException
dllName
- the name of library filefunctionName
- the decorated name of the function (MessageBoxA instead of MessageBox)
NativeException
- if the dll was not found, function name is incorrect...getDLLFileExports(String)
public JNative(java.lang.String dllName, java.lang.String functionName, boolean nativeDebug) throws NativeException
dllName
- a String the name of the library; that DLL must be in the library.pathfunctionName
- a String the name of the function this is the decorated name (@see org.xvolks.jnative.JNative#getDLLFileExports(String))nativeDebug
- a boolean if true the dll logs output on stdout (beware this is shared between all instances of JNative)
NativeException
- if the dll was not found, function name is incorrect...getDLLFileExports(String)
Method Detail |
---|
public void setParameter(int pos, Type type, java.lang.String value) throws NativeException, java.lang.IllegalAccessException
pos
pos
- the offset of the parametertype
- one of the enum entry (INT, STRING...)value
- the parameter in its String representation
NativeException
java.lang.IllegalAccessException
public void setParameter(int pos, Type type, byte[] value) throws NativeException, java.lang.IllegalAccessException
pos
pos
- the offset of the parametertype
- one of the enum entry (INT, STRING...)value
- the parameter in its byte[] representation
NativeException
java.lang.IllegalAccessException
public void setParameter(int pos, int pointer, int size) throws NativeException, java.lang.IllegalAccessException
pos
pos
- the offset of the parameterpointer
- the address of a pointersize
- the size of the reserved memory at address pointer
NativeException
java.lang.IllegalAccessException
- if dispose()
have already been called.public void setParameter(int pos, Pointer p) throws NativeException, java.lang.IllegalAccessException
pos
pos
- the offset of the parameterp
- a pointer object representing an address
NativeException
java.lang.IllegalAccessException
- if dispose()
have already been called.public void setRetVal(Type type) throws NativeException, java.lang.IllegalAccessException
type
- a Type generally VOID or INT, if VOID it is not necessary to call this function
NativeException
java.lang.IllegalAccessException
- if dispose()
have already been called.public java.lang.String getRetVal() throws NativeException, java.lang.IllegalAccessException
NativeException
java.lang.IllegalAccessException
- if dispose()
have already been called.public java.lang.String getParameter(int pos) throws NativeException, java.lang.IllegalAccessException
pos
- an int
pos
NativeException
java.lang.IllegalAccessException
- if dispose()
have already been called.public void invoke() throws NativeException, java.lang.IllegalAccessException
NativeException
java.lang.IllegalAccessException
- if dispose()
have already been called.public final void dispose() throws NativeException, java.lang.IllegalAccessException
No more calls to this dll can be done when dispose is called !!
NativeException
java.lang.IllegalAccessException
- if dispose()
have already been called.protected void finalize() throws java.lang.Throwable
finalize
method to dispose of
system resources or to perform other cleanup.
The general contract of finalize is that it is invoked if and when the JavaTM 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 finalize method may take any action, including making this object available again to other threads; the usual purpose of finalize, 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.
The finalize method of class Object performs no special action; it simply returns normally. Subclasses of Object may override this definition.
The Java programming language does not guarantee which thread will invoke the finalize 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.
After the finalize 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.
The finalize method is never invoked more than once by a Java virtual machine for any given object.
Any exception thrown by the finalize
method causes
the finalization of this object to be halted, but is otherwise
ignored.
This one calls dispose() but all Throwable are catched
finalize
in class java.lang.Object
java.lang.Throwable
- the Exception
raised by this methodpublic final java.lang.String getFunctionName()
public final java.lang.String getDLLName()
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public static int allocMemory(int size) throws NativeException
freeMemory(int)
!!!!
size
- the size of the memory block to reserve in bytes
NativeException
- if the memory cannot be reservedpublic static void freeMemory(int pointer) throws NativeException
pointer
pointer
- the address of a memory block to free
NativeException
- if the memory cannon be freedpublic static void setMemory(int pointer, java.lang.String buffer) throws NativeException
buffer
pointer
- the address of the native memorybuffer
- a String to memcpy
NativeException
public static void setMemory(int pointer, byte[] buffer) throws NativeException
buffer
pointer
- the address of the native memorybuffer
- a byte[] to memcpy
NativeException
public static void setMemory(int pointer, byte[] buffer, int offset, int len) throws NativeException
buffer
pointer
- the address of the native memorybuffer
- a byte[] to memcpyoffset
- the offset of the native sidelen
- the number of bytes to copy
NativeException
public static byte[] getMemory(int pointer, int size) throws NativeException
pointer
- the address of the native memorysize
- number of bytes to copy
pointer
NativeException
public static java.lang.String getMemoryAsString(int pointer, int size) throws NativeException
pointer
- the address of the native char*size
- number of bytes to copy
pointer
limited by a NULL terminator if the char* is lower than size characters
NativeException
public static int registerWindowProc(int hwnd, WindowProc proc) throws NativeException
hwnd
hwnd
- the HANDLE of the windowproc
- a WindowProc object that be called by native events
NativeException
- if the SetWindowLongPtr fails or somthing weird happens (can crash too ;) )...public static int getCurrentModule() throws NativeException
NativeException
public static java.lang.String[] getDLLFileExports(java.lang.String dllFile) throws NativeException, java.lang.IllegalAccessException
dllFile
- the name of a library
NativeException
java.lang.IllegalAccessException
|
JNative project : see http://jnative.sf.net |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |