Module: Six::Uac

Includes:
Win32
Defined in:
lib/six/uac.rb

Constant Summary collapse

ShellExecute =
API.new("ShellExecute", "LPPPPL", "L", "shell32")

Class Method Summary collapse

Class Method Details

.check_elevatedObject

Raises:

  • (Error)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/six/uac.rb', line 16

def check_elevated
  token = 0.chr * 4
  raise Error, get_last_error unless OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, token)

  token   = token.unpack('V')[0]
  rlength = 0.chr * 4
  televation   = 0.chr * 4

  bool = GetTokenInformation(
    token,
    TokenElevation,
    televation,
    televation.size,
    rlength
  )

  raise Error, get_last_error unless bool

  televation.unpack('V')[0]
end

.restart_app_elevated(interpreter = nil) ⇒ Object



51
52
53
54
55
# File 'lib/six/uac.rb', line 51

def restart_app_elevated(interpreter = nil)
  ar = [$0.clone, ARGV.join(" "), Dir.pwd, interpreter]
  p ar
  run_elevated(*ar)
end

.restart_app_if_not_elevated(interpreter = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/six/uac.rb', line 57

def restart_app_if_not_elevated(interpreter = nil)
  begin
    elevation = check_elevated
    if elevation.nil?
      puts "Could not verify if you are running as administrator."
      return nil
    end
    if elevation == 1
      # TODO: If Verbose?

      #puts "You're running as administrator"

      return false
    else
      puts "You're not running as administrator!"
      restart_app_elevated(interpreter)
      return true
    end
  rescue => e
    puts "Could not verify if you are running as administrator:\n#{[e.class, e.message].join(": ")}"
    return nil
  end
end

.run_elevated(app, params = nil, path = nil, interpreter = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/six/uac.rb', line 37

def run_elevated(app, params = nil, path = nil, interpreter = nil)
  if interpreter
    params = "#{app} #{params}"
    app = interpreter
  end
  app = app.clone
  app.gsub!("/", "\\")
  if path
    path = path.clone
    path.gsub!("/", "\\")
  end
  ShellExecute.call( nil, "runas", app, params, path, SW_SHOWNORMAL );
end