lib/win32/security.rb in win32-security-0.4.0 vs lib/win32/security.rb in win32-security-0.4.1

- old
+ new

@@ -1,12 +1,12 @@ # This file allows users to require all security related classes from # a single file, instead of having to require individual files. +require 'ffi/win32/extensions' require_relative 'security/windows/constants' require_relative 'security/windows/structs' require_relative 'security/windows/functions' -require_relative 'security/windows/helper' # The Win32 module serves as a namespace only. module Win32 # The Security class encapsulates security aspects of MS Windows. @@ -19,11 +19,11 @@ include Windows::Security::Constants include Windows::Security::Structs extend Windows::Security::Functions # The version of the win32-security library - VERSION = '0.4.0' + VERSION = '0.4.1' # Used by OpenProcessToken TOKEN_QUERY = 8 # Returns whether or not the owner of the current process is running @@ -39,31 +39,44 @@ ptr_type = :uintptr_t end FFI::MemoryPointer.new(ptr_type) do |token| unless OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, token) - raise SystemCallError.new("OpenProcessToken", FFI.errno) + FFI.raise_windows_error('OpenProcessToken') end begin token = token.read_pointer.to_i # Since the TokenElevation struct only has 1 member, we use a pointer. - te = FFI::MemoryPointer.new(:ulong) + te = FFI::MemoryPointer.new(:pointer) rl = FFI::MemoryPointer.new(:ulong) bool = GetTokenInformation( token, :TokenElevation, te, te.size, rl ) - raise SystemCallError.new("GetTokenInformation", FFI.errno) unless bool + te.free + te = FFI::MemoryPointer.new(rl.read_ulong) + rl.clear - result = te.read_ulong != 0 + bool = GetTokenInformation( + token, + :TokenElevation, + te, + te.size, + rl + ) + + FFI.raise_windows_error('GetTokenInformation') unless bool + + token_info = rl.read_ulong == 4 ? te.read_uint : te.read_ulong + result = token_info != 0 ensure CloseHandle(token) te.free rl.free end @@ -77,10 +90,10 @@ def self.windows_version ver = OSVERSIONINFO.new ver[:dwOSVersionInfoSize] = ver.size unless GetVersionExA(ver) - raise SystemCallError.new("GetVersionEx", FFI.errno) + FFI.raise_windows_error('GetVersionEx') end ver[:dwMajorVersion] end end