org.safs.natives
Class NativeWrapper

java.lang.Object
  extended by org.safs.natives.NativeWrapper
Direct Known Subclasses:
MenuUtilities

public class NativeWrapper
extends java.lang.Object

This class is used to encapsulate platform independent calls to native operating systems through JNA, or other native platform technologies.

JAN is the Java Native Access library supplied via JNA Home

JNA provides Java programs easy access to native shared libraries on multiple operating systems without writing anything but Java codeĉ˘Ÿo JNI or native code is required. This functionality is comparable to Windows' Platform/Invoke and Python's ctypes. Access is dynamic at runtime without code generation.

SAFS is now delivered with the core JNA.ZIP(JAR). Other JNA support libraries may be added as needed.

Since:
2009.02.03

Field Summary
static int NO_RESULT
          -99
static java.lang.String RESULT_KEY
          "Result"
static java.lang.String VECTOR_KEY
          "Vector"
 
Constructor Summary
NativeWrapper()
           
 
Method Summary
protected static int _processLastError()
          Retrieves last error via GetLastError and also attempts to debug log the error code and any system message for the error code.
static boolean DoesRegistryKeyExist(java.lang.Object key, java.lang.Object valuename)
          True if the requested "registry key" already exists, false otherwise.
This is currently only supported on Windows.
static java.lang.Object[] EnumChildWindows(java.lang.Object parent)
          Platform independent entry-point to receive the ID or HANDLE of all child Windows of a parent.
static java.lang.Object[] EnumWindows()
          Platform independent entry-point to receive the ID or HANDLE of all top-level Windows.
static java.lang.Object GetDesktopWindow()
          Platform independent entry-point to receive the ID or HANDLE of the Desktop window.
static boolean getFileTime(java.lang.String filename, java.util.Date createTime, java.util.Date accessTime, java.util.Date writeTime)
           This method will get the file's created time, access time, and write time.
static java.lang.Object GetForegroundWindow()
          Platform independent entry-point to receive the ID or HANDLE of the current foreground window.
static java.lang.Object GetProcessFileName(java.lang.Object processID)
          Retrieve the name of the executable file for the specified process.
static java.lang.Object GetProcessUIResourceCount(java.lang.Object processID)
          Retrieve the count of UI elements used by the specified process.
static java.lang.Object GetRegistryKeyValue(java.lang.Object key, java.lang.Object valuename)
          Return the value of the specified key.
static java.lang.Object[] GetWindowThreadProcessId(java.lang.Object parent)
          The GetWindowThreadProcessId function retrieves the identifier of the thread that created the specified window and, optionally, the identifier of the process that created the window.
static java.lang.Object LaunchURLInDefaultWebBrowser(java.lang.String url)
          Attempt to launch a web URL via the system's default web browser.
static void main(java.lang.String[] args)
          Simple regression tests with output to System.out
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_RESULT

public static final int NO_RESULT
-99

See Also:
Constant Field Values

VECTOR_KEY

public static final java.lang.String VECTOR_KEY
"Vector"

See Also:
Constant Field Values

RESULT_KEY

public static final java.lang.String RESULT_KEY
"Result"

See Also:
Constant Field Values
Constructor Detail

NativeWrapper

public NativeWrapper()
Method Detail

DoesRegistryKeyExist

public static boolean DoesRegistryKeyExist(java.lang.Object key,
                                           java.lang.Object valuename)
True if the requested "registry key" already exists, false otherwise.
This is currently only supported on Windows.

The Windows version uses Reg.EXE which is supplied with WindowsXP. If this EXE is not present on the Windows system then this function will return false.

Parameters:
key - For Windows this is a String. Ex:"HKLM\Software\Rational Software\Rational Test\8\Options"
valuename - For Windows this is a String. Ex:"SpyHeapSize"
Returns:
true if the key and value exists, false if it does not.

GetRegistryKeyValue

public static java.lang.Object GetRegistryKeyValue(java.lang.Object key,
                                                   java.lang.Object valuename)
Return the value of the specified key. Null if the value does not exist.
This is currently only supported on Windows.

The Windows version uses Reg.EXE which is supplied with WindowsXP. If this EXE is not present on the Windows system then this function will always return false.

Parameters:
key - For Windows this is a String. Ex:"HKLM\Software\Rational Software\Rational Test\8\Options"
valuename - For Windows this is a String. Ex:"SpyHeapSize"
Returns:
Windows returns a String or null. Ex:"0x00200000". An empty string may be possible if no values are retrieved for a valid key. If valuename is null, then all the key values will be returned bracketed in the format: [key=val][key2=val2][key3=val3] etc...

_processLastError

protected static int _processLastError()
Retrieves last error via GetLastError and also attempts to debug log the error code and any system message for the error code.

Returns:
last error code encountered or 0

GetForegroundWindow

public static java.lang.Object GetForegroundWindow()
Platform independent entry-point to receive the ID or HANDLE of the current foreground window. The GetForegroundWindow function returns a "handle" to the foreground window--the window with which the user is currently working.

For windows NOTE: WIN32: we will return a Long representing the Handle (HWND), or null on error. NOTE: Support for other Platforms will be added as needed.

Returns:
See Also:
User32.GetForegroundWindow()

GetDesktopWindow

public static java.lang.Object GetDesktopWindow()
Platform independent entry-point to receive the ID or HANDLE of the Desktop window. The GetDesktopWindow function returns a "handle" to the window on which all other windows are painted.

For windows NOTE: WIN32: we will return a Long representing the Handle (HWND), or null on error. NOTE: Support for other Platforms will be added as needed.

Returns:
See Also:
User32.GetDesktopWindow()

EnumWindows

public static java.lang.Object[] EnumWindows()
Platform independent entry-point to receive the ID or HANDLE of all top-level Windows. The EnumWindows function returns an array of "handles" to these top-level windows.

For windows NOTE: WIN32: we will return an array of Longs representing the Handles (HWND), or null on error. NOTE: Support for other Platforms will be added as needed.

Returns:
Object[] of Longs or Object[0].
See Also:
User32.EnumWindows(WNDENUMPROC,Pointer)

EnumChildWindows

public static java.lang.Object[] EnumChildWindows(java.lang.Object parent)
Platform independent entry-point to receive the ID or HANDLE of all child Windows of a parent. The EnumChildWindows function returns an array of "handles" to these child windows.

For windows NOTE: WIN32: we will return an array of Longs representing the Handles (HWND), or null on error. NOTE: Support for other Platforms will be added as needed.

Parameters:
handle - to the parent. For WIN32, this is a Long.
Returns:
Object[] of Longs or Object[0].
See Also:
User32.EnumWindows(WNDENUMPROC,Pointer)

GetWindowThreadProcessId

public static java.lang.Object[] GetWindowThreadProcessId(java.lang.Object parent)
The GetWindowThreadProcessId function retrieves the identifier of the thread that created the specified window and, optionally, the identifier of the process that created the window.

Parameters:
parent - - handle to the parent. For WIN32, this is a Long.
Returns:
Object[2] [0]=ThreadID, [1]=ProcessID. For WIN32 these are both Integers. Array values must be <> 0 to be considered valid.

GetProcessUIResourceCount

public static java.lang.Object GetProcessUIResourceCount(java.lang.Object processID)
Retrieve the count of UI elements used by the specified process. If the process does not have any UI elements, then we should get zero. NOTE: Support for non-WIN32 platforms will be added when able. NOTE: Will not work on "protected processes" on Windows Vista.

Parameters:
processID - -- the id of the process to query. For WIN32 this is an Integer.
Returns:
Object -- the number of UI elements used by the process, or 0. For WIN32 this is an Integer.
See Also:
org.safs.natives.win32.Kernel32#GetGuiResources(Pointer, int)

GetProcessFileName

public static java.lang.Object GetProcessFileName(java.lang.Object processID)
Retrieve the name of the executable file for the specified process. The returned value should not include path information to the executable filename. NOTE: Support for non-WIN32 platforms will be added when able.

Parameters:
processID - -- the id of the process to query. For WIN32 this is an Integer.
Returns:
Object -- the name of the process or NULL. For WIN32 this is a String.
See Also:
Kernel32.GetProcessImageFileNameA(Pointer, Pointer, int), Psapi.GetProcessImageFileNameA(Pointer, Pointer, int)

LaunchURLInDefaultWebBrowser

public static java.lang.Object LaunchURLInDefaultWebBrowser(java.lang.String url)
Attempt to launch a web URL via the system's default web browser.

On Windows this uses ShellExecute's "open" operation to open the URL document using the default app.

Parameters:
url - -- User should include the 'protocol' portion of the url (Ex: 'http://')
Returns:
Object -- For WIN32 this is typically an Integer(42). NULL on execution failures.
See Also:
Shell32.ShellExecuteA(NativeLong, String, String, String, String, long)

getFileTime

public static boolean getFileTime(java.lang.String filename,
                                  java.util.Date createTime,
                                  java.util.Date accessTime,
                                  java.util.Date writeTime)
 This method will get the file's created time, access time, and write time.
 Dor in parameter filename: it MUST be an absolute file path.
 For out parameters: createTime, accessTime and writeTime, if you don't want
 some of them, you can just pass a null value; If you want some of them, you
 MUST pass an instance of java.util.Date as value.
 

Parameters:
filename - In The absolute file path
createTime - Out The java.util.Date object contains the file created time
accessTime - Out The java.util.Date object contains the file last accessed time
writeTime - Out The java.util.Date object contains the file last modified time
Returns:
A boolean to indicate if this method get the file time successfully

main

public static void main(java.lang.String[] args)
Simple regression tests with output to System.out

java org.safs.natives.NativeWrapper > outputFile.txt

Parameters:
args - --accepts none