Module: Win32::Pdh

Defined in:
lib/win32/pdh.rb,
lib/win32/pdh/query.rb,
lib/win32/pdh/counter.rb,
lib/win32/pdh/constants.rb

Defined Under Namespace

Modules: Constants, PdhFFI Classes: Counter, ItemEnum, PdhError, Query

Class Method Summary collapse

Class Method Details

.check_status(status) ⇒ Object

Simple convenience method that checks the status and raises an exception unless it’s a successful status.

Raises:



16
17
18
# File 'lib/win32/pdh.rb', line 16

def self.check_status(status)
  raise PdhError, status unless status == Constants::ERROR_SUCCESS
end

.enum_object_items(object:, source: nil, machine: nil, detail: :novice) ⇒ Object

Enumerates an object’s counter and instance names. Returns an ItemEnum with the results.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/win32/pdh.rb', line 133

def self.enum_object_items(object:, source: nil, machine: nil, detail: :novice)
  object = (object + "\0").encode('UTF-16LE')
  source =
    if source.nil?
      FFI::Pointer::NULL
    else
      (source + "\0").encode('UTF-16LE')
    end
  machine =
    if machine.nil?
      FFI::Pointer::NULL
    else
      (machine + "\0").encode('UTF-16LE')
    end
  detail =
    case detail
    when :wizard
      Constants::PERF_DETAIL_WIZARD
    when :expert
      Constants::PERF_DETAIL_EXPERT
    when :advanced
      Constants::PERF_DETAIL_ADVANCED
    else
      Constants::PERF_DETAIL_NOVICE
    end

  countersize = FFI::MemoryPointer.new(:uint)
  instancesize = FFI::MemoryPointer.new(:uint)
  countersize.write_uint(0)
  instancesize.write_uint(0)
  counterbuffer = FFI::Pointer::NULL
  instancebuffer = FFI::Pointer::NULL
  status = nil
  while status.nil? || status == Constants::PDH_MORE_DATA
    unless status.nil?
      counterbuffer = FFI::Buffer.new(:uint16, countersize.read_uint)
      instancebuffer = FFI::Buffer.new(:uint16, instancesize.read_uint)
    end
    status = PdhFFI.PdhEnumObjectItemsW(
      source,
      machine,
      object,
      counterbuffer,
      countersize,
      instancebuffer,
      instancesize,
      detail,
      0,
    )
  end

  Pdh.check_status status

  counterstring = counterbuffer.read_bytes(countersize.read_uint * 2).force_encoding('UTF-16LE').encode('UTF-8')
  instancestring = instancebuffer.read_bytes(instancesize.read_uint * 2).force_encoding('UTF-16LE').encode('UTF-8')

  enum = ItemEnum.new
  enum.counters = counterstring.split("\0").map(&:freeze).freeze
  enum.instances = instancestring.split("\0").map(&:freeze).freeze
  enum.freeze
end

.enum_objects(source: nil, machine: nil, detail: :novice) ⇒ Object

Uses PdhEnumObjects to enumerate objects at the given target. Returns the objects as a list.

Params:

source

The same as szDataSource

machine

The same as szMachineName

detail

Alias for dwDetailLevel, as a symbol. May be :novice, :advanced, :expert, or :wizard. Defaults to :novice.



78
79
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/win32/pdh.rb', line 78

def self.enum_objects(source: nil, machine: nil, detail: :novice)
  source =
    if source.nil?
      FFI::Pointer::NULL
    else
      (source + "\0").encode('UTF-16LE')
    end
  machine =
    if machine.nil?
      FFI::Pointer::NULL
    else
      (machine + "\0").encode('UTF-16LE')
    end
  detail =
    case detail
    when :wizard
      Constants::PERF_DETAIL_WIZARD
    when :expert
      Constants::PERF_DETAIL_EXPERT
    when :advanced
      Constants::PERF_DETAIL_ADVANCED
    else
      Constants::PERF_DETAIL_NOVICE
    end

  listsize = FFI::MemoryPointer.new(:uint)
  listsize.write_uint(0)
  listbuffer = FFI::Pointer::NULL
  status = nil
  while status.nil? || status == Constants::PDH_MORE_DATA
    listbuffer = FFI::Buffer.new(:uint16, listsize.read_uint) unless status.nil?

    status = PdhFFI.PdhEnumObjectsW(
      source,
      machine,
      listbuffer,
      listsize,
      detail,
      status.nil? ? :true : :false,
    )
  end

  Pdh.check_status status

  string = listbuffer.read_bytes(listsize.read_uint * 2).force_encoding('UTF-16LE').encode('UTF-8')

  # Split and return objects
  string.split("\0")
end

.expand_wildcards(path:, source: nil, expand_counters: true, expand_instances: true) ⇒ Object

Expands a wildcard path into all matching counter paths.



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/win32/pdh.rb', line 197

def self.expand_wildcards(path:, source: nil, expand_counters: true, expand_instances: true)
  path = (path + "\0").encode('UTF-16LE')
  source =
    if source.nil?
      FFI::Pointer::NULL
    else
      (source + "\0").encode('UTF-16LE')
    end

  flags = 0
  flags |= PDH_NOEXPANDCOUNTERS unless expand_counters
  flags |= PDH_NOEXPANDINSTANCES unless expand_instances

  listsize = FFI::MemoryPointer.new(:uint)
  listsize.write_uint(0)
  listbuffer = FFI::Pointer::NULL
  status = nil
  while status.nil? || status == Constants::PDH_MORE_DATA
    listbuffer = FFI::Buffer.new(:uint16, listsize.read_uint) unless status.nil?
    status = PdhFFI.PdhExpandWildCardPathW(
      source,
      path,
      listbuffer,
      listsize,
      flags,
    )
  end

  Pdh.check_status status

  liststring = listbuffer.read_bytes(listsize.read_uint * 2).force_encoding('UTF-16LE').encode('UTF-8')

  liststring.split("\0").map(&:freeze).freeze
end

.read_cwstr(pointer) ⇒ Object

Takes a pointer to null-terminated utf-16 data and reads it into a utf-8 encoded string.

If pointer is null, return nil instead of a string.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/win32/pdh.rb', line 24

def self.read_cwstr(pointer)
  return nil if pointer.null?
  array = []
  loop do
    char = pointer.read_uint16
    break if char == 0

    array << char
    # Need to proceed 2 bytes
    pointer += 2
  end

  array.pack('n*').force_encoding('UTF-16BE').encode('UTF-8')
end