/** * Format a message from the value obtained from * {@link Kernel32#GetLastError()} or {@link Native#getLastError()}. * * @param code The error code * @return Formatted message. */ public static String formatMessage(int code) { PointerByReference buffer = new PointerByReference(); int nLen = Kernel32.INSTANCE.FormatMessage( WinBase.FORMAT_MESSAGE_ALLOCATE_BUFFER | WinBase.FORMAT_MESSAGE_FROM_SYSTEM | WinBase.FORMAT_MESSAGE_IGNORE_INSERTS, null, code, 0, // TODO: // MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT) buffer, 0, null); if (nLen == 0) { throw new LastErrorException(Native.getLastError()); } Pointer ptr = buffer.getValue(); try { String s = ptr.getWideString(0); return s.trim(); } finally { freeLocalMemory(ptr); } }
/** * Convert a security identifier (SID) to a string format suitable for * display, storage, or transmission. * * @param sid * SID bytes. * @return String SID. */ public static String convertSidToStringSid(PSID sid) { PointerByReference stringSid = new PointerByReference(); if (!Advapi32.INSTANCE.ConvertSidToStringSid(sid, stringSid)) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); } Pointer ptr = stringSid.getValue(); try { return ptr.getWideString(0); } finally { Kernel32Util.freeLocalMemory(ptr); } }
/** * Convert a string representation of a security identifier (SID) to a * binary format. * * @param sidString * String SID. * @return SID bytes. */ public static byte[] convertStringSidToSid(String sidString) { PSIDByReference pSID = new PSIDByReference(); if (!Advapi32.INSTANCE.ConvertStringSidToSid(sidString, pSID)) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); } PSID value = pSID.getValue(); try { return value.getBytes(); } finally { Kernel32Util.freeLocalMemory(value.getPointer()); } }
/** * Compares a SID to a well known SID and returns TRUE if they match. * * @param sidString * String representation of a SID. * @param wellKnownSidType * Member of the WELL_KNOWN_SID_TYPE enumeration to compare with * the SID at pSid. * @return True if the SID is of the well-known type, false otherwise. */ public static boolean isWellKnownSid(String sidString, int wellKnownSidType) { PSIDByReference pSID = new PSIDByReference(); if (!Advapi32.INSTANCE.ConvertStringSidToSid(sidString, pSID)) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); } PSID value = pSID.getValue(); try { return Advapi32.INSTANCE.IsWellKnownSid(value, wellKnownSidType); } finally { Kernel32Util.freeLocalMemory(value.getPointer()); } }
} finally { if (pDataProtected.pbData != null) { Kernel32Util.freeLocalMemory(pDataProtected.pbData);
if (pDataUnprotected.pbData != null) { try { Kernel32Util.freeLocalMemory(pDataUnprotected.pbData); } catch(Win32Exception e) { if (err == null) { Kernel32Util.freeLocalMemory(pDescription.getValue()); } catch(Win32Exception e) { if (err == null) {
return memory; } finally { Kernel32Util.freeLocalMemory(secValue);