WinDef.ATOM, WinDef.BOOL, WinDef.BOOLByReference, WinDef.BYTE, WinDef.CHAR, WinDef.CHARByReference, WinDef.DWORD, WinDef.DWORDByReference, WinDef.DWORDLONG, WinDef.HBITMAP, WinDef.HBRUSH, WinDef.HCURSOR, WinDef.HDC, WinDef.HFONT, WinDef.HGLRC, WinDef.HGLRCByReference, WinDef.HICON, WinDef.HINSTANCE, WinDef.HMENU, WinDef.HMODULE, WinDef.HPALETTE, WinDef.HPEN, WinDef.HRGN, WinDef.HRSRC, WinDef.HWND, WinDef.INT_PTR, WinDef.LCID, WinDef.LONG, WinDef.LONGByReference, WinDef.LONGLONG, WinDef.LONGLONGByReference, WinDef.LPARAM, WinDef.LPVOID, WinDef.LRESULT, WinDef.POINT, WinDef.PVOID, WinDef.RECT, WinDef.SCODE, WinDef.SCODEByReference, WinDef.SHORT, WinDef.UCHAR, WinDef.UINT, WinDef.UINT_PTR, WinDef.UINTByReference, WinDef.ULONG, WinDef.ULONGByReference, WinDef.ULONGLONG, WinDef.ULONGLONGByReference, WinDef.USHORT, WinDef.USHORTByReference, WinDef.WORD, WinDef.WORDByReference, WinDef.WPARAM| Modifier and Type | Field and Description | 
|---|---|
static String | 
VOLUME_GUID_PATH_PREFIX  | 
static String | 
VOLUME_GUID_PATH_SUFFIX  | 
| Constructor and Description | 
|---|
Kernel32Util()  | 
| Modifier and Type | Method and Description | 
|---|---|
static void | 
closeHandle(WinNT.HANDLE h)
Invokes  
Kernel32.CloseHandle(WinNT.HANDLE) and checks the success code. | 
static void | 
closeHandleRef(WinNT.HANDLEByReference ref)
Closes the handle in the reference 
 | 
static void | 
closeHandleRefs(WinNT.HANDLEByReference... refs)
Closes all referenced handles. 
 | 
static void | 
closeHandles(WinNT.HANDLE... handles)
Invokes  
closeHandle(WinNT.HANDLE) on each handle. | 
static void | 
deleteFile(String filename)  | 
static String | 
extractVolumeGUID(String volumeGUIDPath)
Parses and returns the pure GUID value of a volume name obtained
 from  
Kernel32.FindFirstVolume(char[], int) or
 Kernel32.FindNextVolume(com.sun.jna.platform.win32.WinNT.HANDLE, char[], int) calls | 
static long | 
findEnvironmentStringBlockEntryEnd(Pointer lpszEnvironmentBlock,
                                  long offset,
                                  boolean asWideChars)  | 
static String | 
formatMessage(int code)
Format a message from the value obtained from
  
Kernel32.GetLastError() or Native.getLastError(). | 
static String | 
formatMessage(WinNT.HRESULT code)
Format a message from an HRESULT. 
 | 
static String | 
formatMessageFromHR(WinNT.HRESULT code)
Deprecated. 
 
use  
formatMessage(WinNT.HRESULT) instead. | 
static String | 
formatMessageFromLastErrorCode(int code)
Format a system message from an error code. 
 | 
static void | 
freeGlobalMemory(Pointer ptr)
Invokes  
Kernel32.GlobalFree(Pointer) and checks if it succeeded. | 
static void | 
freeLocalMemory(Pointer ptr)
Invokes  
Kernel32.LocalFree(Pointer) and checks if it succeeded. | 
static String | 
getComputerName()
Get current computer NetBIOS name. 
 | 
static int | 
getDriveType(String rootName)  | 
static String | 
getEnvironmentVariable(String name)
Get the value of an environment variable. 
 | 
static Map<String,String> | 
getEnvironmentVariables()
Uses the  
Kernel32.GetEnvironmentStrings() to retrieve and
 parse the current process environment | 
static Map<String,String> | 
getEnvironmentVariables(Pointer lpszEnvironmentBlock,
                       long offset)  | 
static int | 
getFileAttributes(String fileName)
Retrieves file system attributes for a specified file or directory. 
 | 
static int | 
getFileType(String fileName)
Retrieves the result of GetFileType, provided the file exists. 
 | 
static String | 
getLastErrorMessage()  | 
static List<String> | 
getLogicalDriveStrings()
Returns valid drives in the system. 
 | 
static WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[] | 
getLogicalProcessorInformation()
Convenience method to get the processor information. 
 | 
static List<Tlhelp32.MODULEENTRY32W> | 
getModules(int processID)
Returns all the executable modules for a given process ID. 
 | 
static int | 
getPrivateProfileInt(String appName,
                    String keyName,
                    int defaultValue,
                    String fileName)
Retrieves an integer associated with a key in the specified section of an
 initialization file. 
 | 
static String[] | 
getPrivateProfileSection(String appName,
                        String fileName)
Retrieves all the keys and values for the specified section of an initialization file. 
 | 
static String[] | 
getPrivateProfileSectionNames(String fileName)
Retrieves the names of all sections in an initialization file. 
 | 
static String | 
getPrivateProfileString(String lpAppName,
                       String lpKeyName,
                       String lpDefault,
                       String lpFileName)
Retrieves a string from the specified section in an initialization file. 
 | 
static byte[] | 
getResource(String path,
           String type,
           String name)
Gets the specified resource out of the specified executable file 
 | 
static Map<String,List<String>> | 
getResourceNames(String path)
Gets a list of all resources from the specified executable file 
 | 
static String | 
getTempPath()
Return the path designated for temporary files. 
 | 
static List<String> | 
getVolumePathNamesForVolumeName(String lpszVolumeName)
Invokes and parses the result of  
Kernel32.GetVolumePathNamesForVolumeName(String, char[], int, IntByReference) | 
static boolean | 
isWideCharEnvironmentStringBlock(Pointer lpszEnvironmentBlock,
                                long offset)
Attempts to determine whether the data block uses  
wchar_t
 instead of "plain old" chars. | 
static List<String> | 
queryDosDevice(String lpszDeviceName,
              int maxTargetSize)
Invokes the  
Kernel32.QueryDosDevice(String, char[], int) method
 and parses the result | 
static String | 
QueryFullProcessImageName(WinNT.HANDLE hProcess,
                         int dwFlags)
This function retrieves the full path of the executable file of a given process. 
 | 
static String | 
readEnvironmentStringBlockEntry(Pointer lpszEnvironmentBlock,
                               long offset,
                               boolean asWideChars)  | 
static void | 
writePrivateProfileSection(String appName,
                          String[] strings,
                          String fileName)  | 
static void | 
writePrivateProfileString(String appName,
                         String keyName,
                         String string,
                         String fileName)  | 
public static final String VOLUME_GUID_PATH_PREFIX
public static final String VOLUME_GUID_PATH_SUFFIX
public static String getComputerName()
public static void freeLocalMemory(Pointer ptr)
Kernel32.LocalFree(Pointer) and checks if it succeeded.ptr - The Pointer to the memory to be released - ignored if NULLWin32Exception - if non-ERROR_SUCCESS code reportedpublic static void freeGlobalMemory(Pointer ptr)
Kernel32.GlobalFree(Pointer) and checks if it succeeded.ptr - The Pointer to the memory to be released - ignored if NULLWin32Exception - if non-ERROR_SUCCESS code reportedpublic static void closeHandleRefs(WinNT.HANDLEByReference... refs)
refs - The references to closecloseHandleRef(WinNT.HANDLEByReference)public static void closeHandleRef(WinNT.HANDLEByReference ref)
ref - The handle reference - ignored if nullcloseHandle(WinNT.HANDLE)public static void closeHandles(WinNT.HANDLE... handles)
closeHandle(WinNT.HANDLE) on each handle. If an exception
 is thrown for a specific handle, then it is accumulated until all
 handles have been closed. If more than one exception occurs, then it
 is added as a suppressed exception to the first one. Once closed all
 handles, the accumulated exception (if any) is thrownhandles - The handles to be closedThrowable.getSuppressed()public static void closeHandle(WinNT.HANDLE h)
Kernel32.CloseHandle(WinNT.HANDLE) and checks the success code.
 If not successful, then throws a Win32Exception with the
 GetLastError valueh - The handle to be closed - ignored if nullpublic static String formatMessage(int code)
Kernel32.GetLastError() or Native.getLastError().code - The error codepublic static String formatMessage(WinNT.HRESULT code)
code - HRESULT@Deprecated public static String formatMessageFromHR(WinNT.HRESULT code)
formatMessage(WinNT.HRESULT) instead.code - error codepublic static String formatMessageFromLastErrorCode(int code)
code - Error code, typically a result of GetLastError.public static String getLastErrorMessage()
Kernel32.GetLastError().public static String getTempPath()
public static void deleteFile(String filename)
public static List<String> getLogicalDriveStrings()
List of valid drives.public static int getFileAttributes(String fileName)
fileName - The name of the file or directory.public static int getFileType(String fileName) throws FileNotFoundException
fileName - file nameFileNotFoundException - if file not foundpublic static int getDriveType(String rootName)
rootName - name of root drivepublic static String getEnvironmentVariable(String name)
name - Name of the environment variable.public static Map<String,String> getEnvironmentVariables()
Kernel32.GetEnvironmentStrings() to retrieve and
 parse the current process environmentMap.LastErrorException - if failed to get or free the environment
 data blockgetEnvironmentVariables(Pointer, long)public static Map<String,String> getEnvironmentVariables(Pointer lpszEnvironmentBlock, long offset)
lpszEnvironmentBlock - The environment block as received from the
 GetEnvironmentStrings
 functionoffset - Offset within the block to parse the dataMap 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)public static String readEnvironmentStringBlockEntry(Pointer lpszEnvironmentBlock, long offset, boolean asWideChars)
lpszEnvironmentBlock - The environment block as received from the
 GetEnvironmentStrings
 functionoffset - Offset within the block to look for the entryasWideChars - If true then the block contains wchar_t
 instead of "plain old" charsString containing the name=value pair or
 empty if reached end of blockisWideCharEnvironmentStringBlock(com.sun.jna.Pointer, long), 
findEnvironmentStringBlockEntryEnd(com.sun.jna.Pointer, long, boolean)public static long findEnvironmentStringBlockEntryEnd(Pointer lpszEnvironmentBlock, long offset, boolean asWideChars)
lpszEnvironmentBlock - The environment block as received from the
 GetEnvironmentStrings
 functionoffset - Offset within the block to look for the entryasWideChars - If true then the block contains wchar_t
 instead of "plain old" chars'\0' in the data block
 starting at the specified offset - can be the start offset itself if empty
 string.isWideCharEnvironmentStringBlock(com.sun.jna.Pointer, long)public static boolean isWideCharEnvironmentStringBlock(Pointer lpszEnvironmentBlock, long offset)
Attempts to determine whether the data block uses wchar_t
 instead of "plain old" chars. It does that by reading
 2 bytes from the specified offset - the character value and its charset
 indicator - and examining them as follows:
chars data block. Note:
      the assumption is that the environment variable name (at
      least) is ASCII.
      wchar_t
      ByteOrder even though
 only ByteOrder.LITTLE_ENDIAN is the likely onelpszEnvironmentBlock - The environment block as received from the
 GetEnvironmentStrings
 functionoffset - offsettrue if the block contains wchar_t instead of
 "plain old" charspublic static final int getPrivateProfileInt(String appName, String keyName, int defaultValue, String fileName)
appName - The name of the section in the initialization file.keyName - The name of the key whose value is to be retrieved. This value
            is in the form of a string; the
            Kernel32.GetPrivateProfileInt(java.lang.String, java.lang.String, int, java.lang.String) function converts the
            string into an integer and returns the integer.defaultValue - The default value to return if the key name cannot be found in
            the initialization file.fileName - 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.public static final String getPrivateProfileString(String lpAppName, String lpKeyName, String lpDefault, String lpFileName)
lpAppName - The name of the section containing the key name. If this
            parameter is null, the
            Kernel32.GetPrivateProfileString(java.lang.String, java.lang.String, java.lang.String, char[], com.sun.jna.platform.win32.WinDef.DWORD, java.lang.String) function copies all
            section names in the file to the supplied buffer.lpKeyName - 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.lpDefault - A default string. If the lpKeyName key cannot be found
            in the initialization file,
            Kernel32.GetPrivateProfileString(java.lang.String, java.lang.String, java.lang.String, char[], com.sun.jna.platform.win32.WinDef.DWORD, java.lang.String) 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.
            
lpFileName - 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.
         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().
         
public static final void writePrivateProfileString(String appName, String keyName, String string, String fileName)
public static final WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[] getLogicalProcessorInformation()
public static final String[] getPrivateProfileSection(String appName, String fileName)
 Each string has the following format: key=string.
 
This operation is atomic; no updates to the specified initialization file are allowed while this method is executed.
appName - The name of the section in the initialization file.fileName - 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.public static final String[] getPrivateProfileSectionNames(String fileName)
This operation is atomic; no updates to the initialization file are allowed while this method is executed.
fileName - 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.public static final void writePrivateProfileSection(String appName, String[] strings, String fileName)
appName - The name of the section in which data is written. This section name is typically the name of the calling application.strings - 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.fileName - 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.public static final List<String> queryDosDevice(String lpszDeviceName, int maxTargetSize)
Kernel32.QueryDosDevice(String, char[], int) method
 and parses the resultlpszDeviceName - The device namemaxTargetSize - The work buffer size to use for the querypublic static final List<String> getVolumePathNamesForVolumeName(String lpszVolumeName)
Kernel32.GetVolumePathNamesForVolumeName(String, char[], int, IntByReference)lpszVolumeName - The volume nameWin32Exception - If failed to retrieve the required informationpublic static final String extractVolumeGUID(String volumeGUIDPath)
Kernel32.FindFirstVolume(char[], int) or
 Kernel32.FindNextVolume(com.sun.jna.platform.win32.WinNT.HANDLE, char[], int) callsvolumeGUIDPath - The volume GUID path as returned by one of the above mentioned callsIllegalArgumentException - if bad format encounteredpublic static final String QueryFullProcessImageName(WinNT.HANDLE hProcess, int dwFlags)
hProcess - Handle for the running processdwFlags - 0 - The name should use the Win32 path format.
          1(WinNT.PROCESS_NAME_NATIVE) - The name should use the native system path format.public static byte[] getResource(String path, String type, String name)
path - The path to the executable filetype - The type of the resource (either a type name or type ID is
            allowed)name - The name or ID of the resourceIllegalStateException - if the call to LockResource failspublic static Map<String,List<String>> getResourceNames(String path)
path - The path to the executable filepublic static List<Tlhelp32.MODULEENTRY32W> getModules(int processID)
processID - The process ID to get executable modules for