-
Notifications
You must be signed in to change notification settings - Fork 977
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CA1838 Avoid 'StringBuilder' parameters for P/Invokes (#8113)
* Initial work to remove string builders from PInvoke * Fix build errors * changes from review * more changes from review * fix usings * Changes from review * changes from review * Convert GetWindowText usage * changes from review to avoid char[] allocation * changes from review * more changes from review * more changes from review * remove and sort usings * remove vb changes * changes from review
- Loading branch information
Showing
10 changed files
with
110 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/System.Windows.Forms.Primitives/src/Windows/Win32/PInvoke.GetModuleFileNameLongPath.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Buffers; | ||
using System.Diagnostics; | ||
|
||
namespace Windows.Win32 | ||
{ | ||
internal static partial class PInvoke | ||
{ | ||
public static unsafe string GetModuleFileNameLongPath(HINSTANCE hModule) | ||
{ | ||
Span<char> stackBuffer = stackalloc char[MAX_PATH]; | ||
uint pathLength; | ||
fixed (char* lpFilename = stackBuffer) | ||
{ | ||
pathLength = GetModuleFileName(hModule, lpFilename, (uint)stackBuffer.Length); | ||
} | ||
|
||
if (pathLength == 0) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
if (pathLength < stackBuffer.Length) | ||
{ | ||
return new string(stackBuffer[..(int)pathLength]); | ||
} | ||
|
||
char[] buffer; | ||
int bufferSize = 4096; | ||
// Allocate increasingly larger portions of memory until successful or we hit short.maxvalue. | ||
for (int i = 1; bufferSize <= short.MaxValue; i++, bufferSize = 4096 * i) | ||
{ | ||
buffer = ArrayPool<char>.Shared.Rent(bufferSize); | ||
fixed (char* lpFilename = buffer) | ||
{ | ||
pathLength = GetModuleFileName(hModule, lpFilename, (uint)buffer.Length); | ||
} | ||
|
||
if (pathLength == 0) | ||
{ | ||
ArrayPool<char>.Shared.Return(buffer); | ||
return string.Empty; | ||
} | ||
|
||
// If the length equals the buffer size we need to check to see if we were told the buffer was insufficient (it was trimmed) | ||
if (pathLength < buffer.Length) | ||
{ | ||
// Get return value and return buffer to array pool. | ||
string returnValue = new string(buffer, 0, (int)pathLength); | ||
ArrayPool<char>.Shared.Return(buffer); | ||
return returnValue; | ||
} | ||
|
||
// buffer was too small, return to array pool. | ||
ArrayPool<char>.Shared.Return(buffer); | ||
} | ||
|
||
Debug.Fail($"Module File Name is greater than {short.MaxValue}."); | ||
return string.Empty; | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/System.Windows.Forms.Primitives/src/Windows/Win32/PInvoke.MaxClassName.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
namespace Windows.Win32 | ||
{ | ||
internal static partial class PInvoke | ||
{ | ||
/// <summary> | ||
/// <para>The maximum length for lpszClassName is 256. If lpszClassName is greater than the maximum length, the RegisterClassEx function will fail.</para> | ||
/// <para><see href="https://learn.microsoft.com/windows/win32/api/winuser/ns-winuser-wndclassexw#members">Read more on docs.microsoft.com</see>.</para> | ||
/// </summary> | ||
public const int MaxClassName = 256; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters