Class: Win32::Pdh::Query

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source = nil) ⇒ Query

Returns a new instance of Query.

Raises:



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

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)
  raise PdhError, status unless status == Constants::ERROR_SUCCESS
  @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.



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/win32/pdh/query.rb', line 36

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.



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

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

#closeObject



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

def close
  # Only allow closing once
  unless @handle.nil?
    status = PdhFFI.PdhCloseQuery(@handle)
    raise PdhError, status unless status == Constants::ERROR_SUCCESS
    @handle = nil
  end
end

#collect_query_dataObject



62
63
64
65
# File 'lib/win32/pdh/query.rb', line 62

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

#real_time?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/win32/pdh/query.rb', line 49

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