module Process

def uid(sid = false)


The Process.uid method in core Ruby always returns 0 on MS Windows.
--
numeric id is returned (the default).
If +sid+ is set to true, then a binary sid is returned. Otherwise, a

RID of the SID associated with the owner of the process.
Returns the uid of the current process. Specifically, it returns the
def uid(sid = false)
  token = FFI::MemoryPointer.new(:ulong)
  raise TypeError unless sid.is_a?(TrueClass) || sid.is_a?(FalseClass)
  unless OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, token)
    raise SystemCallError, FFI.errno, "OpenProcessToken"
  end
  token   = token.read_ulong
  rlength = FFI::MemoryPointer.new(:ulong)
  tuser   = 0.chr * 512
  bool = GetTokenInformation(
    token,
    TokenUser,
    tuser,
    tuser.size,
    rlength
  )
  unless bool
    raise SystemCallError, FFI.errno, "GetTokenInformation"
  end
  string_sid = tuser[FFI.type_size(:pointer) * 2, (rlength.read_ulong - FFI.type_size(:pointer) * 2)]
  if sid
    string_sid
  else
    psid = FFI::MemoryPointer.new(:uintptr_t)
    unless ConvertSidToStringSidA(string_sid, psid)
      raise SystemCallError, FFI.errno, "ConvertSidToStringSid"
    end
    psid.read_pointer.read_string.split("-").last.to_i
  end
end