コード例 #1
0
  static UserPrincipal lookup(String name) throws IOException {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
      sm.checkPermission(new RuntimePermission("lookupUserInformation"));
    }

    // invoke LookupAccountName to get buffer size needed for SID
    int size = 0;
    try {
      size = LookupAccountName(name, 0L, 0);
    } catch (WindowsException x) {
      if (x.lastError() == ERROR_NONE_MAPPED) throw new UserPrincipalNotFoundException(name);
      throw new IOException(name + ": " + x.errorString());
    }
    assert size > 0;

    // allocate buffer and re-invoke LookupAccountName get SID
    NativeBuffer sidBuffer = NativeBuffers.getNativeBuffer(size);
    try {
      int newSize = LookupAccountName(name, sidBuffer.address(), size);
      if (newSize != size) {
        // can this happen?
        throw new AssertionError("SID change during lookup");
      }

      // return user principal
      return fromSid(sidBuffer.address());
    } catch (WindowsException x) {
      throw new IOException(name + ": " + x.errorString());
    } finally {
      sidBuffer.release();
    }
  }