NativeMethods.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using System.ComponentModel;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. namespace PaintDotNet.SystemLayer
  6. {
  7. internal static class NativeMethods
  8. {
  9. internal static bool SUCCEEDED(int hr)
  10. {
  11. return hr >= 0;
  12. }
  13. [DllImport("shell32.dll", CharSet = CharSet.Unicode, PreserveSig = false)]
  14. internal static extern void SHGetFolderPathW(
  15. IntPtr hwndOwner,
  16. int nFolder,
  17. IntPtr hToken,
  18. uint dwFlags,
  19. StringBuilder lpszPath);
  20. [DllImport("kernel32.dll", SetLastError = true)]
  21. [return: MarshalAs(UnmanagedType.Bool)]
  22. public static extern bool DeleteFileW(
  23. [MarshalAs(UnmanagedType.LPWStr)] string lpFileName);
  24. [DllImport("kernel32.dll", SetLastError = true)]
  25. [return: MarshalAs(UnmanagedType.Bool)]
  26. public static extern bool RemoveDirectoryW(
  27. [MarshalAs(UnmanagedType.LPWStr)] string lpPathName);
  28. [DllImport("user32.dll", SetLastError = true)]
  29. public static extern uint WaitForInputIdle(
  30. IntPtr hProcess,
  31. uint dwMilliseconds);
  32. [DllImport("user32.dll", SetLastError = true)]
  33. [return: MarshalAs(UnmanagedType.Bool)]
  34. public static extern bool EnumWindows(
  35. [MarshalAs(UnmanagedType.FunctionPtr)] NativeDelegates.EnumWindowsProc lpEnumFunc,
  36. IntPtr lParam);
  37. [DllImport("kernel32.dll", SetLastError = true)]
  38. public static extern IntPtr OpenProcess(
  39. uint dwDesiredAccess,
  40. [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle,
  41. uint dwProcessId);
  42. [DllImport("advapi32.dll", SetLastError = true)]
  43. [return: MarshalAs(UnmanagedType.Bool)]
  44. public static extern bool OpenProcessToken(
  45. IntPtr ProcessHandle,
  46. uint DesiredAccess,
  47. out IntPtr TokenHandle);
  48. [DllImport("advapi32.dll", SetLastError = true)]
  49. [return: MarshalAs(UnmanagedType.Bool)]
  50. public static extern bool DuplicateTokenEx(
  51. IntPtr hExistingToken,
  52. uint dwDesiredAccess,
  53. IntPtr lpTokenAttributes,
  54. NativeConstants.SECURITY_IMPERSONATION_LEVEL ImpersonationLevel,
  55. NativeConstants.TOKEN_TYPE TokenType,
  56. out IntPtr phNewToken);
  57. [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  58. [return: MarshalAs(UnmanagedType.Bool)]
  59. public static extern bool CreateProcessWithTokenW(
  60. IntPtr hToken,
  61. uint dwLogonFlags,
  62. IntPtr lpApplicationName,
  63. IntPtr lpCommandLine,
  64. uint dwCreationFlags,
  65. IntPtr lpEnvironment,
  66. IntPtr lpCurrentDirectory,
  67. IntPtr lpStartupInfo,
  68. out NativeStructs.PROCESS_INFORMATION lpProcessInfo);
  69. [DllImport("user32.dll", SetLastError = true)]
  70. [return: MarshalAs(UnmanagedType.Bool)]
  71. public static extern bool OpenClipboard(IntPtr hWndNewOwner);
  72. [DllImport("user32.dll", SetLastError = true)]
  73. [return: MarshalAs(UnmanagedType.Bool)]
  74. public static extern bool CloseClipboard();
  75. [DllImport("user32.dll", SetLastError = true)]
  76. public static extern IntPtr GetClipboardData(uint format);
  77. [DllImport("user32.dll", SetLastError = true)]
  78. [return: MarshalAs(UnmanagedType.Bool)]
  79. public static extern bool IsClipboardFormatAvailable(uint format);
  80. [DllImport("shell32.dll", CharSet = CharSet.Unicode, PreserveSig = false)]
  81. internal static extern void SHCreateItemFromParsingName(
  82. [MarshalAs(UnmanagedType.LPWStr)] string pszPath,
  83. IntPtr pbc,
  84. ref Guid riid,
  85. out IntPtr ppv);
  86. [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  87. [return: MarshalAs(UnmanagedType.Bool)]
  88. internal static extern bool VerifyVersionInfo(
  89. ref NativeStructs.OSVERSIONINFOEX lpVersionInfo,
  90. uint dwTypeMask,
  91. ulong dwlConditionMask);
  92. [DllImport("kernel32.dll")]
  93. internal static extern ulong VerSetConditionMask(
  94. ulong dwlConditionMask,
  95. uint dwTypeBitMask,
  96. byte dwConditionMask);
  97. [DllImport("kernel32.dll", SetLastError = true)]
  98. [return: MarshalAs(UnmanagedType.Bool)]
  99. internal static extern bool DeviceIoControl(
  100. IntPtr hDevice,
  101. uint dwIoControlCode,
  102. IntPtr lpInBuffer,
  103. uint nInBufferSize,
  104. IntPtr lpOutBuffer,
  105. uint nOutBufferSize,
  106. ref uint lpBytesReturned,
  107. IntPtr lpOverlapped);
  108. [DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  109. [return: MarshalAs(UnmanagedType.Bool)]
  110. internal static extern bool ShellExecuteExW(ref NativeStructs.SHELLEXECUTEINFO lpExecInfo);
  111. [DllImport("kernel32.dll", SetLastError = true)]
  112. [return: MarshalAs(UnmanagedType.Bool)]
  113. internal static extern bool GlobalMemoryStatusEx(ref NativeStructs.MEMORYSTATUSEX lpBuffer);
  114. [DllImport("shell32.dll", SetLastError = false)]
  115. internal static extern void SHAddToRecentDocs(uint uFlags, IntPtr pv);
  116. [DllImport("kernel32.dll", SetLastError = false)]
  117. internal static extern void GetSystemInfo(ref NativeStructs.SYSTEM_INFO lpSystemInfo);
  118. [DllImport("kernel32.dll", SetLastError = false)]
  119. internal static extern void GetNativeSystemInfo(ref NativeStructs.SYSTEM_INFO lpSystemInfo);
  120. [DllImport("Wintrust.dll", PreserveSig = true, SetLastError = false)]
  121. internal extern static unsafe int WinVerifyTrust(
  122. IntPtr hWnd,
  123. ref Guid pgActionID,
  124. ref NativeStructs.WINTRUST_DATA pWinTrustData
  125. );
  126. [DllImport("SetupApi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  127. internal static extern IntPtr SetupDiGetClassDevsW(
  128. ref Guid ClassGuid,
  129. [MarshalAs(UnmanagedType.LPWStr)] string Enumerator,
  130. IntPtr hwndParent,
  131. uint Flags);
  132. [DllImport("SetupApi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  133. [return: MarshalAs(UnmanagedType.Bool)]
  134. internal static extern bool SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);
  135. [DllImport("SetupApi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  136. [return: MarshalAs(UnmanagedType.Bool)]
  137. internal static extern bool SetupDiEnumDeviceInfo(
  138. IntPtr DeviceInfoSet,
  139. uint MemberIndex,
  140. ref NativeStructs.SP_DEVINFO_DATA DeviceInfoData);
  141. [DllImport("SetupApi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  142. [return: MarshalAs(UnmanagedType.Bool)]
  143. internal static extern bool SetupDiGetDeviceInstanceIdW(
  144. IntPtr DeviceInfoSet,
  145. ref NativeStructs.SP_DEVINFO_DATA DeviceInfoData,
  146. IntPtr DeviceInstanceId,
  147. uint DeviceInstanceIdSize,
  148. out uint RequiredSize);
  149. internal static void ThrowOnWin32Error(string message)
  150. {
  151. int lastWin32Error = Marshal.GetLastWin32Error();
  152. ThrowOnWin32Error(message, lastWin32Error);
  153. }
  154. internal static void ThrowOnWin32Error(string message, NativeErrors lastWin32Error)
  155. {
  156. ThrowOnWin32Error(message, (int)lastWin32Error);
  157. }
  158. internal static void ThrowOnWin32Error(string message, int lastWin32Error)
  159. {
  160. if (lastWin32Error != NativeConstants.ERROR_SUCCESS)
  161. {
  162. string exMessageFormat = "{0} ({1}, {2})";
  163. string exMessage = string.Format(exMessageFormat, message, lastWin32Error, ((NativeErrors)lastWin32Error).ToString());
  164. throw new Win32Exception(lastWin32Error, exMessage);
  165. }
  166. }
  167. }
  168. }