8000 Fix the definition of the `PDH_COUNTER_INFO` struct (#17779) · PowerShell/PowerShell@b780a34 · GitHub
[go: up one dir, main page]

Skip to content

Commit b780a34

Browse files
Fix the definition of the PDH_COUNTER_INFO struct (#17779)
* Correct the struct `PDH_COUNTER_INFO` Fixes #13829 * Make code factor happy
1 parent 1779175 commit b780a34

File tree

1 file changed

+60
-23
lines changed

1 file changed

+60
-23
lines changed

src/Microsoft.PowerShell.Commands.Diagnostics/PdhHelper.cs

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -267,28 +267,65 @@ private struct PDH_TIME_INFO
267267
// We only need dwType and lDefaultScale fields from this structure.
268268
// We access those fields directly. The struct is here for reference only.
269269
//
270-
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode)]
271-
private struct PDH_COUNTER_INFO
270+
[StructLayout(LayoutKind.Sequential)]
271+
private unsafe struct PDH_COUNTER_INFO
272272
{
273-
[FieldOffset(0)] public UInt32 dwLength;
274-
[FieldOffset(4)] public UInt32 dwType;
275-
[FieldOffset(8)] public UInt32 CVersion;
276-
[FieldOffset(12)] public UInt32 CStatus;
277-
[FieldOffset(16)] public UInt32 lScale;
278-
[FieldOffset(20)] public UInt32 lDefaultScale;
279-
[FieldOffset(24)] public IntPtr dwUserData;
280-
[FieldOffset(32)] public IntPtr dwQueryUserData;
281-
[FieldOffset(40)] public string szFullPath;
282-
283-
[FieldOffset(48)] public string szMachineName;
284-
[FieldOffset(56)] public string szObjectName;
285-
[FieldOffset(64)] public string szInstanceName;
286-
[FieldOffset(72)] public string szParentInstance;
287-
[FieldOffset(80)] public UInt32 dwInstanceIndex;
288-
[FieldOffset(88)] public string szCounterName;
289-
290-
[FieldOffset(96)] public string szExplainText;
291-
[FieldOffset(104)] public IntPtr DataBuffer;
273+
public uint Length;
274+
public uint Type;
275+
public uint CVersion;
276+
public uint CStatus;
277+
public int Scale;
278+
public int DefaultScale;
279+
public ulong UserData;
280+
public ulong QueryUserData;
281+
public ushort* FullPath;
282+
public _Anonymous_e__Union Anonymous;
283+
public ushort* ExplainText;
284+
public fixed uint DataBuffer[1];
285+
286+
[StructLayout(LayoutKind.Explicit)]
287+
internal struct _Anonymous_e__Union
288+
{
289+
[FieldOffset(0)]
290+
public PDH_DATA_ITEM_PATH_ELEMENTS_blittable DataItemPath;
291+
292+
[FieldOffset(0)]
293+
public PDH_COUNTER_PATH_ELEMENTS_blittable CounterPath;
294+
295+
[FieldOffset(0)]
296+
public _Anonymous_e__Struct Anonymous;
297+
298+
[StructLayout(LayoutKind.Sequential)]
299+
internal struct PDH_DATA_ITEM_PATH_ELEMENTS_blittable
300+
{
301+
public ushort* MachineName;
302+
public Guid ObjectGUID;
303+
public uint ItemId;
304+
public ushort* InstanceName;
305+
}
306+
307+
[StructLayout(LayoutKind.Sequential)]
308+
internal struct PDH_COUNTER_PATH_ELEMENTS_blittable
309+
{
310+
public ushort* MachineName;
311+
public ushort* ObjectName;
312+
public ushort* InstanceName;
313+
public ushort* ParentInstance;
314+
public uint InstanceIndex;
315+
public ushort* CounterName;
316+
}
317+
318+
[StructLayout(LayoutKind.Sequential)]
319+
internal struct _Anonymous_e__Struct
320+
{
321+
public ushort* MachineName;
322+
public ushort* ObjectName;
323+
public ushort* InstanceName;
324+
public ushort* ParentInstance;
325+
public uint InstanceIndex;
326+
public ushort* CounterName;
327+
}
328+
}
292329
}
293330

294331
[DllImport("pdh.dll", CharSet = CharSet.Unicode)]
@@ -476,8 +513,8 @@ private static uint GetCounterInfoPlus(IntPtr hCounter, out UInt32 counterType,
476513
if (res == PdhResults.PDH_CSTATUS_VALID_DATA && bufCounterInfo != IntPtr.Zero)
477514
{
478515
PDH_COUNTER_INFO pdhCounterInfo = (PDH_COUNTER_INFO)Marshal.PtrToStructure(bufCounterInfo, typeof(PDH_COUNTER_INFO));
479-
counterType = pdhCounterInfo.dwType;
480-
defaultScale = pdhCounterInfo.lDefaultScale;
516+
counterType = pdhCounterInfo.Type;
517+
defaultScale = (uint)pdhCounterInfo.DefaultScale;
481518
}
482519
}
483520
finally

0 commit comments

Comments
 (0)
0