Class: Win32::Pdh::Counter

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

Defined Under Namespace

Classes: PDH_COUNTER_INFO, PDH_COUNTER_PATH_ELEMENTS, PDH_FMT_COUNTERVALUE, PDH_FMT_COUNTERVALUE_VALUE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query:, path:) ⇒ Counter

Returns a new instance of Counter.



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/win32/pdh/counter.rb', line 55

def initialize(query:, path:)
  path = (path + "\0").encode('UTF-16LE')
  handle_pointer = FFI::MemoryPointer.new(:pointer)
  status = PdhFFI.PdhAddCounterW(
    query,
    path,
    FFI::Pointer::NULL,
    handle_pointer,
  )
  Pdh.check_status status
  @handle = handle_pointer.read_pointer
  load_info
end

Instance Attribute Details

#counter_nameObject

Returns the value of attribute counter_name.



53
54
55
# File 'lib/win32/pdh/counter.rb', line 53

def counter_name
  @counter_name
end

#default_scaleObject

Returns the value of attribute default_scale.



53
54
55
# File 'lib/win32/pdh/counter.rb', line 53

def default_scale
  @default_scale
end

#explain_textObject

Returns the value of attribute explain_text.



53
54
55
# File 'lib/win32/pdh/counter.rb', line 53

def explain_text
  @explain_text
end

#full_pathObject

Returns the value of attribute full_path.



53
54
55
# File 'lib/win32/pdh/counter.rb', line 53

def full_path
  @full_path
end

#instance_indexObject

Returns the value of attribute instance_index.



53
54
55
# File 'lib/win32/pdh/counter.rb', line 53

def instance_index
  @instance_index
end

#instance_nameObject

Returns the value of attribute instance_name.



53
54
55
# File 'lib/win32/pdh/counter.rb', line 53

def instance_name
  @instance_name
end

#machine_nameObject

Returns the value of attribute machine_name.



53
54
55
# File 'lib/win32/pdh/counter.rb', line 53

def machine_name
  @machine_name
end

#object_nameObject

Returns the value of attribute object_name.



53
54
55
# File 'lib/win32/pdh/counter.rb', line 53

def object_name
  @object_name
end

#scaleObject

Returns the value of attribute scale.



53
54
55
# File 'lib/win32/pdh/counter.rb', line 53

def scale
  @scale
end

#statusObject

Returns the value of attribute status.



53
54
55
# File 'lib/win32/pdh/counter.rb', line 53

def status
  @status
end

#typeObject

Returns the value of attribute type.



53
54
55
# File 'lib/win32/pdh/counter.rb', line 53

def type
  @type
end

#versionObject

Returns the value of attribute version.



53
54
55
# File 'lib/win32/pdh/counter.rb', line 53

def version
  @version
end

Instance Method Details

#get(format) ⇒ Object

Get the PDH_FMT_COUNTERVALUE_VALUE given the format, checking status and raising an exception if necessary



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/win32/pdh/counter.rb', line 114

def get(format)
  value = PDH_FMT_COUNTERVALUE.new
  status = PdhFFI.PdhGetFormattedCounterValue(
    @handle,
    format,
    FFI::Pointer::NULL,
    value,
  )
  Pdh.check_status status
  Pdh.check_status value[:CStatus]
  value[:value]
end

#get_doubleObject

Get value as a double



129
130
131
# File 'lib/win32/pdh/counter.rb', line 129

def get_double
  get(Constants::PDH_FMT_DOUBLE)[:doubleValue]
end

#get_largeObject

Get value as a 64-bit integer



135
136
137
# File 'lib/win32/pdh/counter.rb', line 135

def get_large
  get(Constants::PDH_FMT_LARGE)[:largeValue]
end

#get_longObject

Get value as a 32-bit integer



141
142
143
# File 'lib/win32/pdh/counter.rb', line 141

def get_long
  get(Constants::PDH_FMT_LONG)[:longValue]
end

#good?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/win32/pdh/counter.rb', line 108

def good?
  @status == Constants::ERROR_SUCCESS
end

#load_infoObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/win32/pdh/counter.rb', line 80

def load_info
  buffersize = FFI::MemoryPointer.new(:uint)
  buffersize.write_uint(0)
  buffer = FFI::Pointer::NULL
  status = nil
  while status.nil? || status == Constants::PDH_MORE_DATA
    buffer = FFI::Buffer.new(:uint16, buffersize.read_uint) unless status.nil?
    status = PdhFFI.PdhGetCounterInfoW(@handle, :false, buffersize, buffer)
  end
  Pdh.check_status status

  info = PDH_COUNTER_INFO.new(buffer)
  @type = info[:dwType]
  @version = info[:CVersion]
  @status = info[:CStatus]
  @scale = info[:lScale]
  @default_scale = info[:lDefaultScale]
  @full_path = Pdh.read_cwstr(info[:szFullPath]).freeze
  counter_path = info[:CounterPath]
  @machine_name = Pdh.read_cwstr(counter_path[:szMachineName]).freeze
  @object_name = Pdh.read_cwstr(counter_path[:szObjectName]).freeze
  @instance_name = Pdh.read_cwstr(counter_path[:szInstanceName]).freeze
  @instance_index = counter_path[:dwInstanceIndex]
  @counter_name = Pdh.read_cwstr(counter_path[:szCounterName]).freeze
  @explain_text = Pdh.read_cwstr(info[:szExplainText]).freeze
  Pdh.check_status @status
end

#removeObject Also known as: close



69
70
71
72
73
74
75
76
# File 'lib/win32/pdh/counter.rb', line 69

def remove
  # Only allow removing once
  unless @handle.nil?
    status = PdhFFI.PdhRemoveCounter(@handle) unless @handle.nil?
    Pdh.check_status status
    @handle = nil
  end
end