class Win32::Pdh::Query

Class representing a Query, and holding a query handle.

Public Class Methods

new(source=nil) click to toggle source
# 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
open(source=nil) { |query| ... } click to toggle source

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.

# 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

Public Instance Methods

add_counter(path) click to toggle source

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

# File lib/win32/pdh/query.rb, line 65
def add_counter(path)
  Counter.new(
    query: @handle,
    path: path,
  )
end
close() click to toggle source
# 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_data() click to toggle source

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.

# File lib/win32/pdh/query.rb, line 78
def collect_query_data
  status = PdhFFI.PdhCollectQueryData(@handle)
  Pdh.check_status status
end
real_time?() click to toggle source

Simple wrapper around PdhIsRealTimeQuery.

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

# File lib/win32/pdh/query.rb, line 57
def real_time?
  PdhFFI.PdhIsRealTimeQuery(@handle) == :true
end