public class NativeWrapper
extends java.lang.Object
JNA 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 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.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
ARG_FILE_FOR_GETTIME
"-fileforgettime" followed by a full path file name, for testing
getFileTime(String, Date, Date, Date) |
static java.lang.String |
ARG_HOSTNAME
"-hostname" followed by a hostname, for testing
getHostIPByPing(String) |
static java.lang.String |
COMMAND_HOSTNMAE
command 'hostname'
|
static java.lang.String |
COMMAND_IFCONFIG
command 'ifconfig'
|
static java.lang.String |
COMMAND_IPCONFIG
command 'ipconfig'
|
static java.lang.String |
COMMAND_PING
command 'ping'
|
static java.lang.String |
DIRECTORY_SBIN
directory 'sbin'
|
static int |
NO_RESULT
-99
|
static java.lang.String |
PARAM_HOSTNMAE_f
parameter '-f' for command 'hostname', to get full qualified domain name
|
static java.lang.String |
REG_BINARY |
static java.lang.String |
REG_DWORD |
static java.lang.String |
REG_EXPAND_SZ |
static java.lang.String |
REG_MULTI_SZ |
static java.lang.String |
REG_NONE |
static java.lang.String |
REG_QWORD |
static java.lang.String |
REG_SZ |
static java.lang.String |
RESULT_KEY
"Result"
|
static java.lang.String |
VECTOR_KEY
"Vector"
|
Constructor and Description |
---|
NativeWrapper() |
Modifier and Type | Method and Description |
---|---|
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.util.List<java.lang.String> |
execute(java.lang.String command,
java.lang.String... parameters)
Return the result of execution of a command as a List of String.
Note: The command MUST end with limit result, otherwise this method will block. |
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.String |
getHostIPByPing(java.lang.String hostname)
Get the host's IP Address by command
COMMAND_PING . |
static java.lang.String |
getLocalHostIPByConfig()
Execute
COMMAND_IPCONFIG or COMMAND_IFCONFIG to parse the local IP address. |
static java.lang.String |
getLocalHostName() |
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.String |
GetSystemEnvironmentVariable(java.lang.Object key)
Get a System Environment Variable.
|
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.util.List<java.lang.String> |
ifconfig(java.lang.String... parameters)
Return the ifconfig's result as a List of String.
|
static java.util.List<java.lang.String> |
ipconfig(java.lang.String... parameters)
Return the ipconfig's result as a List of String.
|
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)
Test some implementations of this class.
Simple regression tests with output to System.out |
static java.util.List<java.lang.String> |
ping(java.lang.String hostname,
java.lang.String... parameters)
Return the ping's result as a List of String.
|
static boolean |
RemoveRegistryKeyValue(java.lang.Object key,
java.lang.Object valuename)
Remove a registry key, and/or key value.
|
static boolean |
RemoveSystemEnvironmentVariable(java.lang.Object key)
Delete/Remove a System Environment Variable.
|
static boolean |
runAsynchBatchProcess(java.lang.String workdir,
java.lang.String... batchAndargs)
Runs an asynch batch process and immediately returns.
|
static boolean |
runAsynchExec(java.lang.String fullpath)
Runs an asynch exec and immediately returns.
|
static java.util.Hashtable |
runShortProcessAndWait(java.lang.String proc,
java.lang.String... args)
Runs a short process and waits for it to complete.
|
static boolean |
SetForegroundWindow(java.lang.String regex_title)
Set ForeGroundWindow active by title.
|
static boolean |
SetRegistryKeyValue(java.lang.Object key,
java.lang.Object valuename,
java.lang.String strtype,
java.lang.Object... vals)
Set/Create a registry key, and/or key value.
|
static boolean |
SetSystemEnvironmentVariable(java.lang.Object key,
java.lang.Object value)
Set/Create a System Environment Variable.
|
public static final int NO_RESULT
public static final java.lang.String VECTOR_KEY
public static final java.lang.String RESULT_KEY
public static final java.lang.String REG_SZ
public static final java.lang.String REG_EXPAND_SZ
public static final java.lang.String REG_MULTI_SZ
public static final java.lang.String REG_DWORD
public static final java.lang.String REG_QWORD
public static final java.lang.String REG_BINARY
public static final java.lang.String REG_NONE
public static final java.lang.String COMMAND_PING
public static final java.lang.String COMMAND_IPCONFIG
public static final java.lang.String COMMAND_IFCONFIG
public static final java.lang.String COMMAND_HOSTNMAE
public static final java.lang.String PARAM_HOSTNMAE_f
public static final java.lang.String DIRECTORY_SBIN
public static final java.lang.String ARG_FILE_FOR_GETTIME
getFileTime(String, Date, Date, Date)
public static final java.lang.String ARG_HOSTNAME
getHostIPByPing(String)
public static boolean DoesRegistryKeyExist(java.lang.Object key, java.lang.Object valuename)
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.
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"public static java.util.Hashtable runShortProcessAndWait(java.lang.String proc, java.lang.String... args) throws java.io.IOException
proc
- Ex: "myProg" -- the caller must do any necessary quoting if the proc path contains spaces that
would interfere with finding and executing the process.
args
- Ex: "-u" or String[]{"-u", "arg2", "arg3"}. The caller must do any necessary quoting of individual
arguments if spaces in the arguments will interfere with proper interpretation of the arguments by the process.
java.io.IOException
- if the process was not executed on the system.public static boolean runAsynchBatchProcess(java.lang.String workdir, java.lang.String... batchAndargs) throws java.io.IOException
workdir
- The fullpath to a valid directory to be used as the batch working directory.batchAndargs
- the program and arguments array. The first item in the array is the batch program to execute.
java.io.IOException
- if the process path was not valid or was not executed on the system.Console
public static boolean runAsynchExec(java.lang.String fullpath) throws java.io.IOException
Full
- path of the file.
java.io.IOException
- if the process path was not valid or was not executed on the system.public static boolean SetRegistryKeyValue(java.lang.Object key, java.lang.Object valuename, java.lang.String strtype, java.lang.Object... vals)
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.
strkey
- Ex:"HKLM\Software\Rational Software\Rational Test\8\Options"strname
- (Optional) Ex: The Name of a value to put under the key.strtype
- (Optional) Ex: REG_SZ or REG_MULTI_SZ, etc. (REG_SZ is the default)strval[]
- (Optional) One or more values to place in the strname value. Multiple
values should only be used for REG_MULTI_SZ types.public static boolean RemoveRegistryKeyValue(java.lang.Object key, java.lang.Object valuename)
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.
strkey
- Ex:"HKLM\Software\Rational Software\Rational Test\8\Options"strname
- (Optional) Ex: The Name of a value to put under the key.public static boolean SetSystemEnvironmentVariable(java.lang.Object key, java.lang.Object value)
The Windows version uses SetX.EXE Vista and later. If this EXE is not present on the Windows system then will try to set the value through the Registry.
Because the system combines LOCAL variables and USER variables on any get, when we go to SET we do NOT want to have USER variable values mixed in. They will be duplicated if they are mixed in. So, on any SET, we must try to make sure we remove USER variables from the value we are setting.
strkey
- Ex:"MY_VAR_NAME"strval
- Ex:"My variable value."public static boolean RemoveSystemEnvironmentVariable(java.lang.Object key)
The Windows version uses REG.EXE Vista and later. If this EXE is not present on the Windows system then we will exit with failure.
strkey
- Ex:"SAFSDIR"public static java.lang.String GetSystemEnvironmentVariable(java.lang.Object key)
The Windows version uses ECHO.
Note: variables set or changed after JVM start are NOT available to the currently running JVM through System.getEnv(). This is because the JVM does not refresh its Environment variable space after launch. Use GetSystemEnvironmentVariable to get the latest "refreshed" value of any System Environment Variable.
strkey
- Ex:"MY_VAR_NAME"public static java.lang.Object GetRegistryKeyValue(java.lang.Object key, java.lang.Object valuename)
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.
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"protected static int _processLastError()
public static java.lang.Object GetForegroundWindow()
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.
User32.GetForegroundWindow()
public static boolean SetForegroundWindow(java.lang.String regex_title)
title
- - Title of the window; Regular expression allowed.User32.ShowWindow(NativeLong, int)
public static java.lang.Object GetDesktopWindow()
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.
User32.GetDesktopWindow()
public static java.lang.Object[] EnumWindows()
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.
User32.EnumWindows(WNDENUMPROC,Pointer)
public static java.lang.Object[] EnumChildWindows(java.lang.Object parent)
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.
handle
- to the parent. For WIN32, this is a Long.User32.EnumWindows(WNDENUMPROC,Pointer)
public static java.lang.Object[] GetWindowThreadProcessId(java.lang.Object parent)
parent
- - handle to the parent. For WIN32, this is a Long.public static java.lang.Object GetProcessUIResourceCount(java.lang.Object processID)
processID
- -- the id of the process to query. For WIN32 this is an Integer.org.safs.natives.win32.Kernel32#GetGuiResources(Pointer, int)
public static java.lang.Object GetProcessFileName(java.lang.Object processID)
processID
- -- the id of the process to query. For WIN32 this is an Integer.Kernel32.GetProcessImageFileNameA(Pointer, Pointer, int)
,
Psapi.GetProcessImageFileNameA(Pointer, Pointer, int)
public static java.lang.Object LaunchURLInDefaultWebBrowser(java.lang.String url)
On Windows this uses ShellExecute's "open" operation to open the URL document using the default app.
url
- -- User should include the 'protocol' portion of the url (Ex: 'http://')Shell32.ShellExecuteA(NativeLong, String, String, String, String, long)
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.
filename
- In The absolute file pathcreateTime
- Out The java.util.Date object contains the file created timeaccessTime
- Out The java.util.Date object contains the file last accessed timewriteTime
- Out The java.util.Date object contains the file last modified timepublic static java.util.List<java.lang.String> ipconfig(java.lang.String... parameters)
parameters
- String[], the parameters for command ping.public static java.util.List<java.lang.String> ifconfig(java.lang.String... parameters)
parameters
- String[], the parameters for command ping.public static java.util.List<java.lang.String> ping(java.lang.String hostname, java.lang.String... parameters)
hostname
- String, the name of the host to pingparameters
- String[], the parameters for command ping.public static java.util.List<java.lang.String> execute(java.lang.String command, java.lang.String... parameters)
parameters
- String[], the parameters for command.public static java.lang.String getHostIPByPing(java.lang.String hostname)
COMMAND_PING
.#getHostIPByName(String)
public static java.lang.String getLocalHostIPByConfig()
COMMAND_IPCONFIG
or COMMAND_IFCONFIG
to parse the local IP address.public static java.lang.String getLocalHostName()
public static void main(java.lang.String[] args)
java org.safs.natives.NativeWrapper > outputFile.txt
To test getFileTime(String, Date, Date, Date)
, call as following:
java java org.safs.natives.NativeWrapper -fileforgettime fullPathFileName > outputFile.txt
args
- String[], IndependantLog.ARG_DEBUG
is present, then show debug message on consoleARG_FILE_FOR_GETTIME
followed by a full path file name such as "c:\temp\myfile.txt", then test_getFileTime(String[])
ARG_HOSTNAME
followed by host name such as machine.domain.com, then test_getHostIp(String[])
Copyright © SAS Institute. All Rights Reserved.