protected Win32Exception(int code, HRESULT hr) { this(code, hr, Kernel32Util.formatMessage(hr)); }
/** * @return Obtains the human-readable error message text from the last error * that occurred by invocating {@code Kernel32.GetLastError()}. */ public static String getLastErrorMessage() { return Kernel32Util.formatMessageFromLastErrorCode(Kernel32.INSTANCE .GetLastError()); }
/** * Closes the handle in the reference * * @param ref The handle reference - ignored if {@code null} * @see #closeHandle(WinNT.HANDLE) */ public static void closeHandleRef(HANDLEByReference ref) { closeHandle((ref == null) ? null : ref.getValue()); }
/** * New Win32 exception from HRESULT. * @param hr * HRESULT */ public Win32Exception(HRESULT hr) { super(Kernel32Util.formatMessageFromHR(hr)); _hr = hr; }
/** * Closes all referenced handles. 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 thrown * * @param refs The references to close * @see #closeHandleRef(WinNT.HANDLEByReference) */ public static void closeHandleRefs(HANDLEByReference... refs) { Win32Exception err = null; for (HANDLEByReference r : refs) { try { closeHandleRef(r); } catch(Win32Exception e) { if (err == null) { err = e; } else { err.addSuppressedReflected(e); } } } if (err != null) { throw err; } } /**
long endOffset=findEnvironmentStringBlockEntryEnd(lpszEnvironmentBlock, offset, asWideChars); int dataLen=(int) (endOffset - offset); if (dataLen == 0) {
} finally { try { Kernel32Util.closeHandleRefs(openedAccessToken, duplicatedToken); } catch(Win32Exception e) { if (err == null) {
/** * Format a system message from an error code. * @param code * Error code, typically a result of GetLastError. * @return * Formatted message. */ public static String formatMessageFromLastErrorCode(int code) { return formatMessageFromHR(W32Errors.HRESULT_FROM_WIN32(code)); }
private static IOException toException(int error) { return new IOException(Kernel32Util.formatMessage(error)); }
WinProcess( int pid ) throws IOException { handle = Kernel32.INSTANCE .OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_SUSPEND_RESUME | PROCESS_TERMINATE | PROCESS_SYNCHRONIZE, false, pid ); if ( handle == null ) { throw new IOException( "OpenProcess failed: " + Kernel32Util.formatMessageFromLastErrorCode( Kernel32.INSTANCE.GetLastError() ) ); } this.pid = pid; }
/** * Invokes {@link #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 thrown * * @param handles The handles to be closed * @see Throwable#getSuppressed() */ public static void closeHandles(HANDLE... handles) { Win32Exception err = null; for (HANDLE h : handles) { try { closeHandle(h); } catch(Win32Exception e) { if (err == null) { err = e; } else { err.addSuppressedReflected(e); } } } if (err != null) { throw err; } }
private static IOException toException(int error) { return new IOException(Kernel32Util.formatMessage(error)); }
public void moveToTrash(File[] files) throws IOException { Shell32 shell = Shell32.INSTANCE; ShellAPI.SHFILEOPSTRUCT fileop = new ShellAPI.SHFILEOPSTRUCT(); fileop.wFunc = ShellAPI.FO_DELETE; String[] paths = new String[files.length]; for (int i=0;i < paths.length;i++) { paths[i] = files[i].getAbsolutePath(); } fileop.pFrom = fileop.encodePaths(paths); fileop.fFlags = ShellAPI.FOF_ALLOWUNDO|ShellAPI.FOF_NO_UI; int ret = shell.SHFileOperation(fileop); if (ret != 0) { throw new IOException("Move to trash failed: " + fileop.pFrom + ": " + Kernel32Util.formatMessageFromLastErrorCode(ret)); } if (fileop.fAnyOperationsAborted) { throw new IOException("Move to trash aborted"); } } }
} finally { try { closeHandle(hFile); } catch(Win32Exception e) { if (err == null) {
private static IOException toException(int error) { return new IOException(Kernel32Util.formatMessage(error)); }
public static void msync(RandomAccessFile raf, long addr, long length) throws IOException { int retry = 0; boolean success; int lastError = 0; // FlushViewOfFile can fail with ERROR_LOCK_VIOLATION if the memory system is writing dirty // pages to disk. As there is no way to synchronize the flushing then we retry a limited // number of times. do { success = KERNEL_32.FlushViewOfFile(new Pointer(addr), new SIZE_T(length)); if (success || (lastError = KERNEL_32.GetLastError()) != ERROR_LOCK_VIOLATION) break; retry++; } while (retry < 3); if (success) { // Finally calls FlushFileBuffers raf.getChannel().force(false); } else { throw new IOException(Kernel32Util.formatMessageFromLastErrorCode(lastError)); } }
} finally { try { closeHandle(snapshot); } catch(Win32Exception e) { if (we == null) {
/** * Format a system message from an error code. * * @param code * Error code, typically a result of GetLastError. * @return Formatted message. */ public static String formatMessageFromLastErrorCode(int code) { return formatMessage(W32Errors.HRESULT_FROM_WIN32(code)); }
public void moveToTrash(File[] files) throws IOException { Shell32 shell = Shell32.INSTANCE; ShellAPI.SHFILEOPSTRUCT fileop = new ShellAPI.SHFILEOPSTRUCT(); fileop.wFunc = ShellAPI.FO_DELETE; String[] paths = new String[files.length]; for (int i=0;i < paths.length;i++) { paths[i] = files[i].getAbsolutePath(); } fileop.pFrom = new WString(fileop.encodePaths(paths)); fileop.fFlags = ShellAPI.FOF_ALLOWUNDO|ShellAPI.FOF_NO_UI; int ret = shell.SHFileOperation(fileop); if (ret != 0) { throw new IOException("Move to trash failed: " + fileop.pFrom + ": " + Kernel32Util.formatMessageFromLastErrorCode(ret)); } if (fileop.fAnyOperationsAborted) { throw new IOException("Move to trash aborted"); } } }
if (!WinBase.INVALID_HANDLE_VALUE.equals(hToken)) { try { Kernel32Util.closeHandle(hToken); } catch(Win32Exception e) { if (err == null) {