diff --git a/src/Microsoft.PowerShell.Commands.Diagnostics/PdhHelper.cs b/src/Microsoft.PowerShell.Commands.Diagnostics/PdhHelper.cs index 97eff22db0d..ff2b7621403 100644 --- a/src/Microsoft.PowerShell.Commands.Diagnostics/PdhHelper.cs +++ b/src/Microsoft.PowerShell.Commands.Diagnostics/PdhHelper.cs @@ -7,7 +7,7 @@ using System.Diagnostics; using System.Globalization; using System.Runtime.InteropServices; - +using System.Text; using Microsoft.PowerShell.Commands.GetCounter; using Microsoft.Win32; @@ -531,20 +531,38 @@ public uint ConnectToDataSource(string dataSourceName) return res; } - public uint ConnectToDataSource(StringCollection blgFileNames) + public uint ConnectToDataSource(List blgFileNames) { if (blgFileNames.Count == 1) { return ConnectToDataSource(blgFileNames[0]); } - string doubleNullTerminated = string.Empty; - foreach (string fileName in blgFileNames) + int length = blgFileNames.Count + 1; + for (int i = 0; i < blgFileNames.Count; i++) { - doubleNullTerminated += fileName + '\0'; + length += blgFileNames[i].Length; } - doubleNullTerminated += '\0'; + string doubleNullTerminated = string.Create(length, blgFileNames, (buf, fileNames) => + { + int pos = 0; + for (int i = 0; i < fileNames.Count; i++) + { + string fileName = fileNames[i]; + int fileNameLength = fileName.Length; + fileName.AsSpan().CopyTo( + buf.Slice(pos, fileNameLength)); + pos += fileNameLength; + buf[pos] = '\0'; + pos++; + } + + buf[pos] = '\0'; + pos++; + + Debug.Assert(pos == length, "Error constructing double null terminated string."); + }); return ConnectToDataSource(doubleNullTerminated); }