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
-
.check_status(status) ⇒ Object
Simple convenience method that checks the status and raises an exception unless it’s a successful status.
-
.enum_object_items(object:, source: nil, machine: nil, detail: :novice) ⇒ Object
Enumerates an object’s counter and instance names.
-
.enum_objects(source: nil, machine: nil, detail: :novice) ⇒ Object
Uses PdhEnumObjects to enumerate objects at the given target.
-
.expand_wildcards(path:, source: nil, expand_counters: true, expand_instances: true) ⇒ Object
Expands a wildcard path into all matching counter paths.
-
.read_cwstr(pointer) ⇒ Object
Takes a pointer to null-terminated utf-16 data and reads it into a utf-8 encoded string.
-
.strlen_cwstr(pointer) ⇒ Object
Gets the length of a cwstr (null-terminated UTF-16 string) in characters (16-bit units).
Class Method Details
.check_status(status) ⇒ Object
Simple convenience method that checks the status and raises an exception unless it’s a successful status.
19 20 21 |
# File 'lib/win32/pdh.rb', line 19 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.
Uses PdhEnumObjectItems: msdn.microsoft.com/en-us/library/windows/desktop/aa372595(v=vs.85).aspx
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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/win32/pdh.rb', line 159 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 an array of strings.
PdhEnumObjects: msdn.microsoft.com/en-us/library/windows/desktop/aa372600(v=vs.85).aspx
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.
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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/win32/pdh.rb', line 100 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.
Returns a frozen array of frozen strings.
Uses PdhExpandWildCardPath: msdn.microsoft.com/en-us/library/windows/desktop/aa372606(v=vs.85).aspx
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/win32/pdh.rb', line 227 def self.(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 flags |= PDH_NOEXPANDINSTANCES unless 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.
48 49 50 51 52 53 54 55 |
# File 'lib/win32/pdh.rb', line 48 def self.read_cwstr(pointer) return nil if pointer.null? # length in wchars length = strlen_cwstr(pointer) pointer.read_bytes(length * 2).force_encoding('UTF-16LE').encode('UTF-8') end |
.strlen_cwstr(pointer) ⇒ Object
Gets the length of a cwstr (null-terminated UTF-16 string) in characters (16-bit units).
Returns nil if the pointer is null
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/win32/pdh.rb', line 28 def self.strlen_cwstr(pointer) return nil if pointer.null? # Clone pointer, so we don't modify the original. pointer = FFI::Pointer.new(pointer) length = 0 until pointer.get_uint16(0) == 0 length += 1 # Need to proceed 2 bytes at a time; Ruby ffi gives no special pointer # arithmetic by type. pointer += 2 end length end |