Top Description Fields Constructors Methods
com.sun.jna.platform.win32

public abstract Class Kernel32Util

extends Object
implements WinDef
Class Inheritance
All Implemented Interfaces
com.sun.jna.platform.win32.WinDef
Imports
java.io.File, .FileNotFoundException, java.nio.ByteOrder, java.util.ArrayList, .LinkedHashMap, .List, .Map, .TreeMap, com.sun.jna.LastErrorException, .Memory, .Native, .Pointer, com.sun.jna.platform.win32.WinNT.HANDLE, .WinNT.HANDLEByReference, .WinNT.HRESULT, .WinNT.LOGICAL_PROCESSOR_RELATIONSHIP, .WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX, com.sun.jna.ptr.IntByReference, .PointerByReference, com.sun.jna.win32.W32APITypeMapper

Kernel32 utility API.
Authors
dblock[at]dblock.org, markus[at]headcrashing[dot]eu, Andreas "PAX" Lück, onkelpax-git[at]yahoo.de

Field Summary

Modifier and TypeField and Description
private static final String[]
public static final String
public static final String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static void
public static void
closeHandle(WinNT.HANDLE
The handle to be closed - ignored if null
h
)

Invokes Kernel32#CloseHandle(WinNT.HANDLE) and checks the success code.

public static void
closeHandleRef(WinNT.HANDLEByReference
The handle reference - ignored if null
ref
)

Closes the handle in the reference

public static void
closeHandleRefs(WinNT.HANDLEByReference...
The references to close
refs
)

Closes all referenced handles.

public static void
closeHandles(WinNT.HANDLE...
The handles to be closed
handles
)

Invokes closeHandle(WinNT.HANDLE) on each handle.

public static void
deleteFile(String filename)

public static String

Returns:

the replaced string
expandEnvironmentStrings
(String
A string that contains one or more environment-variable strings in the form: %variableName%. For each such reference, the %variableName% portion is replaced with the current value of that environment variable.

Case is ignored when looking up the environment-variable name. If the name is not found, the %variableName% portion is left unexpanded.

Note that this function does not support all the features that Cmd.exe supports. For example, it does not support %variableName:str1=str2% or %variableName:~offset,length%.

input
)

Expands environment-variable strings and replaces them with the values defined for the current user.

public static final String

Returns:

The pure GUID value after stripping the "\\?\" prefix and removing the trailing backslash.
extractVolumeGUID
(String
The volume GUID path as returned by one of the above mentioned calls
volumeGUIDPath
)

Parses and returns the pure GUID value of a volume name obtained from Kernel32#FindFirstVolume(char[], int) or Kernel32#FindNextVolume calls

public static long

Returns:

The offset of the first '\0' in the data block starting at the specified offset - can be the start offset itself if empty string.
findEnvironmentStringBlockEntryEnd
(Pointer
The environment block as received from the GetEnvironmentStrings function
lpszEnvironmentBlock
,
long
Offset within the block to look for the entry
offset
,
boolean
If true then the block contains wchar_t instead of "plain old" chars
asWideChars
)

public static String

Returns:

Formatted message in the default locale.
formatMessage
(int
The error code
code
)

Format a message from a code.

public static String

Returns:

Formatted message in the specified locale.
formatMessage
(int
The error code
code
,
int
The primary language identifier
primaryLangId
,
int
The sublanguage identifier
sublangId
)

Format a message from the value obtained from Kernel32#GetLastError() or Native#getLastError().

public static String

Returns:

Formatted message in the default locale.
formatMessage
(WinNT.HRESULT
HRESULT
code
)

Format a message from an HRESULT.

public static String

Returns:

Formatted message in the specified locale.
formatMessage
(WinNT.HRESULT
HRESULT
code
,
int
The primary language identifier
primaryLangId
,
int
The primary language identifier
sublangId
)

Format a message from an HRESULT.

public static String

Returns:

Formatted message in the default locale.
formatMessageFromLastErrorCode
(int
Error code, typically a result of GetLastError.
code
)

Format a system message from an error code.

public static String

Returns:

Formatted message in the specified locale.
formatMessageFromLastErrorCode
(int
Error code, typically a result of GetLastError.
code
,
int
The primary language identifier
primaryLangId
,
int
The primary language identifier
sublangId
)

Format a system message from an error code.

public static void
freeGlobalMemory(Pointer
The Pointer to the memory to be released - ignored if NULL
ptr
)

Invokes Kernel32#GlobalFree(Pointer) and checks if it succeeded.

public static void
freeLocalMemory(Pointer
The Pointer to the memory to be released - ignored if NULL
ptr
)

Invokes Kernel32#LocalFree(Pointer) and checks if it succeeded.

public static String

Returns:

Netbios name.
getComputerName
()

Get current computer NetBIOS name.

public static WinDef.DWORD

Returns:

The priority class of the current process.
getCurrentProcessPriority
()

Gets the priority class of the current process.

public static int

Returns:

The priority value of the current thread.
getCurrentThreadPriority
()

Gets the priority value of the current thread.

public static int

Returns:

One of the WinBase.DRIVE_* constants.
getDriveType
(String
name of root drive
rootName
)

public static String

Returns:

Value of an environment variable.
getEnvironmentVariable
(String
Name of the environment variable.
name
)

Get the value of an environment variable.

public static Map<String, String>

Returns:

The current process environment as a Map.
getEnvironmentVariables
()

Uses the Kernel32#GetEnvironmentStrings() to retrieve and parse the current process environment

public static Map<String, String>

Returns:

A Map of the parsed name=value pairs

Note

if the environment block is null then null is returned instead of an empty map since we want to distinguish between the case that the data block is null and when there are no environment variables (as unlikely as it may be)

getEnvironmentVariables
(Pointer
The environment block as received from the GetEnvironmentStrings function
lpszEnvironmentBlock
,
long
Offset within the block to parse the data
offset
)

public static int

Returns:

The attributes of the specified file or directory.
getFileAttributes
(String
The name of the file or directory.
fileName
)

Retrieves file system attributes for a specified file or directory.

public static int

Returns:

file type
getFileType
(String
file name
fileName
)

Retrieves the result of GetFileType, provided the file exists.

public static String

Returns:

Obtains the human-readable error message text from the last error that occurred by invocating Kernel32.GetLastError() in the default locale.
getLastErrorMessage
()

public static String

Returns:

Obtains the human-readable error message text from the last error that occurred by invocating Kernel32.GetLastError() in the specified locale.
getLastErrorMessage
(int primaryLangId, int sublangId)

public static List<String>

Returns:

A List of valid drives.
getLogicalDriveStrings
()

Returns valid drives in the system.

public static final WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[]

Returns:

the array of processor information.
getLogicalProcessorInformation
()

Convenience method to get the processor information.

public static final WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX[]

Returns:

the array of processor information.
getLogicalProcessorInformationEx
(int relationshipType)

Convenience method to get the processor information.

public static List<Tlhelp32.MODULEENTRY32W>

Returns:

All the modules in the process.
getModules
(int
The process ID to get executable modules for
processID
)

Returns all the executable modules for a given process ID.

public static final int

Returns:

The retrieved integer, or the default if not found.
getPrivateProfileInt
(final String
The name of the section in the initialization file.
appName
,
final String
The name of the key whose value is to be retrieved. This value is in the form of a string; the Kernel32#GetPrivateProfileInt function converts the string into an integer and returns the integer.
keyName
,
final int
The default value to return if the key name cannot be found in the initialization file.
defaultValue
,
final String
The name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory.
fileName
)

Retrieves an integer associated with a key in the specified section of an initialization file.

public static final String[]

Returns:

The key name and value pairs associated with the named section.
getPrivateProfileSection
(final String
The name of the section in the initialization file.
appName
,
final String
The name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory.
fileName
)

Retrieves all the keys and values for the specified section of an initialization file.

public static final String[]

Returns:

the section names associated with the named file.
getPrivateProfileSectionNames
(final String
The name of the initialization file. If this parameter is NULL, the function searches the Win.ini file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory.
fileName
)

Retrieves the names of all sections in an initialization file.

public static final String

Returns:

If neither lpAppName nor lpKeyName is null and the destination buffer is too small to hold the requested string, the string is truncated.

If either lpAppName or lpKeyName is null and the destination buffer is too small to hold all the strings, the last string is truncated and followed by two null characters.

In the event the initialization file specified by lpFileName is not found, or contains invalid values, this function will set errorno with a value of '0x2' (File Not Found). To retrieve extended error information, call Kernel32#GetLastError.

getPrivateProfileString
(final String
The name of the section containing the key name. If this parameter is null, the Kernel32#GetPrivateProfileString function copies all section names in the file to the supplied buffer.
lpAppName
,
final String
The name of the key whose associated string is to be retrieved. If this parameter is null, all key names in the section specified by the lpAppName parameter are returned.
lpKeyName
,
final String
A default string. If the lpKeyName key cannot be found in the initialization file, Kernel32#GetPrivateProfileString returns the default. If this parameter is null, the default is an empty string, "".

Avoid specifying a default string with trailing blank characters. The function inserts a null character in the lpReturnedString buffer to strip any trailing blanks.

lpDefault
,
final String
The name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory.
lpFileName
)

Retrieves a string from the specified section in an initialization file.

public static WinDef.DWORD
getProcessPriority(final int
Identifier for the running process.
pid
)

Gets the priority class of the specified process.

public static byte[]

Returns:

The resource bytes, or null if no such resource exists.
getResource
(String
The path to the executable file
path
,
String
The type of the resource (either a type name or type ID is allowed)
type
,
String
The name or ID of the resource
name
)

Gets the specified resource out of the specified executable file

public static Map<String, List<String>>

Returns:

A map of resource type name/ID => resources.
A map key + a single list item + the path to the executable can be handed off to getResource() to actually get the resource.
getResourceNames
(String
The path to the executable file
path
)

Gets a list of all resources from the specified executable file

public static String

Returns:

Path.
getTempPath
()

Return the path designated for temporary files.

public static int
getThreadPriority(final int
Identifier for the running thread.
tid
)

Gets the priority value of the specified thread.

public static final List<String>

Returns:

The parsed result
getVolumePathNamesForVolumeName
(String
The volume name
lpszVolumeName
)

Invokes and parses the result of Kernel32#GetVolumePathNamesForVolumeName(String, char[], int, IntByReference)

public static boolean

Returns:

Returns true, if and only if the given priority value was valid
isValidPriorityClass
(final WinDef.DWORD
The priority value to test.
dwPriorityClass
)

Test whether the given priority class is valid.

public static boolean

Returns:

Returns true, if and only if the given priority value was valid
isValidThreadPriority
(final int
The priority value to test.
nPriority
)

Test whether the given thread priority is valid.

public static boolean

Returns:

true if the block contains wchar_t instead of "plain old" chars
isWideCharEnvironmentStringBlock
(Pointer
The environment block as received from the GetEnvironmentStrings function
lpszEnvironmentBlock
,
long
offset
offset
)

Attempts to determine whether the data block uses wchar_t instead of "plain old" chars.

private static boolean
isWideCharEnvironmentStringBlock(byte charsetIndicator)

public static final List<String>

Returns:

The parsed result
queryDosDevice
(String
The device name
lpszDeviceName
,
int
The work buffer size to use for the query
maxTargetSize
)

Invokes the Kernel32#QueryDosDevice(String, char[], int) method and parses the result

public static final String

Returns:

the full path of the process's executable file of null if failed. To get extended error information, call GetLastError.
QueryFullProcessImageName
(int
Identifier for the running process
pid
,
int
0 - The name should use the Win32 path format. 1(WinNT.PROCESS_NAME_NATIVE) - The name should use the native system path format.
dwFlags
)

This function retrieves the full path of the executable file of a given process identifier.

public static final String

Returns:

the full path of the process's executable file of null if failed. To get extended error information, call GetLastError.
QueryFullProcessImageName
(WinNT.HANDLE
Handle for the running process
hProcess
,
int
0 - The name should use the Win32 path format. 1(WinNT.PROCESS_NAME_NATIVE) - The name should use the native system path format.
dwFlags
)

This function retrieves the full path of the executable file of a given process.

public static String

Returns:

A String containing the name=value pair or empty if reached end of block
readEnvironmentStringBlockEntry
(Pointer
The environment block as received from the GetEnvironmentStrings function
lpszEnvironmentBlock
,
long
Offset within the block to look for the entry
offset
,
boolean
If true then the block contains wchar_t instead of "plain old" chars
asWideChars
)

public static void
setCurrentProcessBackgroundMode(final boolean
If true, enables "background" processing mode, otherwise disables it.
enable
)

Enables or disables "background" processing mode for the current process

public static void
setCurrentProcessPriority(final WinDef.DWORD
The priority class for the process.
dwPriorityClass
)

Sets the priority class for the current process.

public static void
setCurrentThreadBackgroundMode(final boolean
If true, enables "background" processing mode, otherwise disables it.
enable
)

Enables or disables "background" processing mode for the current thread

public static void
setCurrentThreadPriority(final int
The priority value for the thread.
nPriority
)

Sets the priority value for the current thread.

public static void
setProcessPriority(final int
Identifier for the running process.
pid
,
final WinDef.DWORD
The priority class for the process.
dwPriorityClass
)

Sets the priority class for the specified process.

public static void
setThreadPriority(final int
Identifier for the running thread.
tid
,
final int
The priority value for the thread.
nPriority
)

Sets the priority value for the specified thread.

public static final void
writePrivateProfileSection(final String
The name of the section in which data is written. This section name is typically the name of the calling application.
appName
,
final String[]
The new key names and associated values that are to be written to the named section. Each entry must be of the form key=value.
strings
,
final String
The name of the initialization file. If this parameter does not contain a full path for the file, the function searches the Windows directory for the file. If the file does not exist and lpFileName does not contain a full path, the function creates the file in the Windows directory.
fileName
)

public static final void
writePrivateProfileString(final String appName, final String keyName, final String string, final String fileName)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait