/src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/UnsafeNativeMethods.cs

https://github.com/dotnet/winforms · C# · 59 lines · 45 code · 9 blank · 5 comment · 5 complexity · aa09bc267638377b8223d34d384f4559 MD5 · raw file

  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using static Interop;
  7. namespace System.Windows.Forms
  8. {
  9. internal static class UnsafeNativeMethods
  10. {
  11. [DllImport(Libraries.User32)]
  12. #pragma warning disable CA1838 // Avoid 'StringBuilder' parameters for P/Invokes
  13. public static extern int GetClassName(HandleRef hwnd, StringBuilder lpClassName, int nMaxCount);
  14. #pragma warning restore CA1838 // Avoid 'StringBuilder' parameters for P/Invokes
  15. [DllImport(Libraries.Comdlg32, SetLastError = true, CharSet = CharSet.Auto)]
  16. public static extern HRESULT PrintDlgEx([In, Out] NativeMethods.PRINTDLGEX lppdex);
  17. [DllImport(Libraries.Comdlg32, SetLastError = true, CharSet = CharSet.Auto)]
  18. public static extern bool GetOpenFileName([In, Out] NativeMethods.OPENFILENAME_I ofn);
  19. [DllImport(Libraries.Kernel32, CharSet = CharSet.Auto, SetLastError = true)]
  20. #pragma warning disable CA1838 // Avoid 'StringBuilder' parameters for P/Invokes
  21. public static extern int GetModuleFileName(HandleRef hModule, StringBuilder buffer, int length);
  22. #pragma warning restore CA1838 // Avoid 'StringBuilder' parameters for P/Invokes
  23. public static StringBuilder GetModuleFileNameLongPath(HandleRef hModule)
  24. {
  25. StringBuilder buffer = new StringBuilder(Kernel32.MAX_PATH);
  26. int noOfTimes = 1;
  27. int length = 0;
  28. // Iterating by allocating chunk of memory each time we find the length is not sufficient.
  29. // Performance should not be an issue for current MAX_PATH length due to this change.
  30. while (((length = GetModuleFileName(hModule, buffer, buffer.Capacity)) == buffer.Capacity)
  31. && Marshal.GetLastWin32Error() == ERROR.INSUFFICIENT_BUFFER
  32. && buffer.Capacity < Kernel32.MAX_UNICODESTRING_LEN)
  33. {
  34. noOfTimes += 2; // Increasing buffer size by 520 in each iteration.
  35. int capacity = noOfTimes * Kernel32.MAX_PATH < Kernel32.MAX_UNICODESTRING_LEN ? noOfTimes * Kernel32.MAX_PATH : Kernel32.MAX_UNICODESTRING_LEN;
  36. buffer.EnsureCapacity(capacity);
  37. }
  38. buffer.Length = length;
  39. return buffer;
  40. }
  41. [DllImport(Libraries.Comdlg32, SetLastError = true, CharSet = CharSet.Auto)]
  42. public static extern bool GetSaveFileName([In, Out] NativeMethods.OPENFILENAME_I ofn);
  43. [DllImport(Libraries.Kernel32, CharSet = CharSet.Auto)]
  44. #pragma warning disable CA1838 // Avoid 'StringBuilder' parameters for P/Invokes
  45. public static extern void GetTempFileName(string tempDirName, string prefixName, int unique, StringBuilder sb);
  46. #pragma warning restore CA1838 // Avoid 'StringBuilder' parameters for P/Invokes
  47. [DllImport(Libraries.Oleacc, ExactSpelling = true, CharSet = CharSet.Auto)]
  48. public static extern int CreateStdAccessibleObject(HandleRef hWnd, int objID, ref Guid refiid, [In, Out, MarshalAs(UnmanagedType.Interface)] ref object? pAcc);
  49. }
  50. }