Class: Win32::Pdh::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/win32/pdh/query.rb

Overview

Class representing a Query, and holding a query handle.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source = nil) ⇒ Query

Returns a new instance of Query.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/win32/pdh/query.rb', line 9

def initialize(source=nil)
  source =
    if source.nil?
      FFI::Pointer::NULL
    else
      (source + "\0").encode('UTF-16LE')
    end
  handle_pointer = FFI::MemoryPointer.new(:pointer)
  status = PdhFFI.PdhOpenQueryW(source, FFI::Pointer::NULL, handle_pointer)
  Pdh.check_status status
  @handle = handle_pointer.read_pointer
end

Class Method Details

.open(source = nil) ⇒ Object

Simple query opening function.

Uses the OpenQuery function and gets a query, passes it into the block, and then closes it afterward. If no block is given, it just returns the query, and you are responsible for closing it. The GC will not close this for you, so you can easily leak resources. It’s strongly recommended to use the block style if at all possible to ensure resource cleanup.



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/win32/pdh/query.rb', line 40

def self.open(source=nil)
  query = new source
  if block_given?
    begin
      return yield query
    ensure
      query.close
    end
  else
    query
  end
end

Instance Method Details

#add_counter(path) ⇒ Object

Adds a counter to this query and return it as a Counter object.

msdn.microsoft.com/en-us/library/windows/desktop/aa372204(v=vs.85).aspx



65
66
67
68
69
70
# File 'lib/win32/pdh/query.rb', line 65

def add_counter(path)
  Counter.new(
    query: @handle,
    path: path,
  )
end

#closeObject



22
23
24
25
26
27
28
29
# File 'lib/win32/pdh/query.rb', line 22

def close
  # Only allow closing once
  unless @handle.nil?
    status = PdhFFI.PdhCloseQuery(@handle)
    Pdh.check_status status
    @handle = nil
  end
end

#collect_query_dataObject

Calls PdhCollectQueryData. This is necessary to load counters with data before retreiving it.

Some counters may need this called twice before they can retreive the data. CPU counters are obvious examples.



78
79
80
81
# File 'lib/win32/pdh/query.rb', line 78

def collect_query_data
  status = PdhFFI.PdhCollectQueryData(@handle)
  Pdh.check_status status
end

#real_time?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/win32/pdh/query.rb', line 57

def real_time?
  PdhFFI.PdhIsRealTimeQuery(@handle) == :true
end