NativeMethods.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. using System;
  2. using System.ComponentModel;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Runtime.ConstrainedExecution;
  5. using System.Runtime.InteropServices;
  6. using System.Runtime.InteropServices.ComTypes;
  7. using System.Text;
  8. namespace Standard;
  9. internal static class NativeMethods
  10. {
  11. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  12. [DllImport("user32.dll", EntryPoint = "AdjustWindowRectEx", SetLastError = true)]
  13. [return: MarshalAs(UnmanagedType.Bool)]
  14. private static extern bool _AdjustWindowRectEx(ref RECT lpRect, WS dwStyle, [MarshalAs(UnmanagedType.Bool)] bool bMenu, WS_EX dwExStyle);
  15. public static RECT AdjustWindowRectEx(RECT lpRect, WS dwStyle, bool bMenu, WS_EX dwExStyle)
  16. {
  17. if (!NativeMethods._AdjustWindowRectEx(ref lpRect, dwStyle, bMenu, dwExStyle))
  18. {
  19. HRESULT.ThrowLastError();
  20. }
  21. return lpRect;
  22. }
  23. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  24. [DllImport("user32.dll", EntryPoint = "ChangeWindowMessageFilter", SetLastError = true)]
  25. [return: MarshalAs(UnmanagedType.Bool)]
  26. private static extern bool _ChangeWindowMessageFilter(WM message, MSGFLT dwFlag);
  27. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  28. [DllImport("user32.dll", EntryPoint = "ChangeWindowMessageFilterEx", SetLastError = true)]
  29. [return: MarshalAs(UnmanagedType.Bool)]
  30. private static extern bool _ChangeWindowMessageFilterEx(IntPtr hwnd, WM message, MSGFLT action, [In][Out] ref CHANGEFILTERSTRUCT pChangeFilterStruct);
  31. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  32. public static HRESULT ChangeWindowMessageFilterEx(IntPtr hwnd, WM message, MSGFLT action, out MSGFLTINFO filterInfo)
  33. {
  34. filterInfo = MSGFLTINFO.NONE;
  35. if (!Utility.IsOSVistaOrNewer)
  36. {
  37. return HRESULT.S_FALSE;
  38. }
  39. if (!Utility.IsOSWindows7OrNewer)
  40. {
  41. if (!NativeMethods._ChangeWindowMessageFilter(message, action))
  42. {
  43. return (HRESULT) Win32Error.GetLastError();
  44. }
  45. return HRESULT.S_OK;
  46. }
  47. else
  48. {
  49. CHANGEFILTERSTRUCT changefilterstruct = new CHANGEFILTERSTRUCT
  50. {
  51. cbSize = (uint) Marshal.SizeOf(typeof(CHANGEFILTERSTRUCT))
  52. };
  53. if (!NativeMethods._ChangeWindowMessageFilterEx(hwnd, message, action, ref changefilterstruct))
  54. {
  55. return (HRESULT) Win32Error.GetLastError();
  56. }
  57. filterInfo = changefilterstruct.ExtStatus;
  58. return HRESULT.S_OK;
  59. }
  60. }
  61. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  62. [DllImport("gdi32.dll")]
  63. public static extern CombineRgnResult CombineRgn(IntPtr hrgnDest, IntPtr hrgnSrc1, IntPtr hrgnSrc2, RGN fnCombineMode);
  64. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  65. [DllImport("shell32.dll", CharSet = CharSet.Unicode, EntryPoint = "CommandLineToArgvW")]
  66. private static extern IntPtr _CommandLineToArgvW([MarshalAs(UnmanagedType.LPWStr)] string cmdLine, out int numArgs);
  67. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  68. public static string[] CommandLineToArgvW(string cmdLine)
  69. {
  70. IntPtr intPtr = IntPtr.Zero;
  71. string[] result;
  72. try
  73. {
  74. int num = 0;
  75. intPtr = NativeMethods._CommandLineToArgvW(cmdLine, out num);
  76. if (intPtr == IntPtr.Zero)
  77. {
  78. throw new Win32Exception();
  79. }
  80. string[] array = new string[num];
  81. for (int i = 0; i < num; i++)
  82. {
  83. IntPtr ptr = Marshal.ReadIntPtr(intPtr, i * Marshal.SizeOf(typeof(IntPtr)));
  84. array[i] = Marshal.PtrToStringUni(ptr);
  85. }
  86. result = array;
  87. }
  88. finally
  89. {
  90. NativeMethods._LocalFree(intPtr);
  91. }
  92. return result;
  93. }
  94. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  95. [DllImport("gdi32.dll", EntryPoint = "CreateDIBSection", SetLastError = true)]
  96. private static extern SafeHBITMAP _CreateDIBSection(SafeDC hdc, [In] ref BITMAPINFO bitmapInfo, int iUsage, out IntPtr ppvBits, IntPtr hSection, int dwOffset);
  97. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  98. [DllImport("gdi32.dll", EntryPoint = "CreateDIBSection", SetLastError = true)]
  99. private static extern SafeHBITMAP _CreateDIBSectionIntPtr(IntPtr hdc, [In] ref BITMAPINFO bitmapInfo, int iUsage, out IntPtr ppvBits, IntPtr hSection, int dwOffset);
  100. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  101. public static SafeHBITMAP CreateDIBSection(SafeDC hdc, ref BITMAPINFO bitmapInfo, out IntPtr ppvBits, IntPtr hSection, int dwOffset)
  102. {
  103. SafeHBITMAP safeHBITMAP;
  104. if (hdc == null)
  105. {
  106. safeHBITMAP = NativeMethods._CreateDIBSectionIntPtr(IntPtr.Zero, ref bitmapInfo, 0, out ppvBits, hSection, dwOffset);
  107. }
  108. else
  109. {
  110. safeHBITMAP = NativeMethods._CreateDIBSection(hdc, ref bitmapInfo, 0, out ppvBits, hSection, dwOffset);
  111. }
  112. if (safeHBITMAP.IsInvalid)
  113. {
  114. HRESULT.ThrowLastError();
  115. }
  116. return safeHBITMAP;
  117. }
  118. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  119. [DllImport("gdi32.dll", EntryPoint = "CreateRoundRectRgn", SetLastError = true)]
  120. private static extern IntPtr _CreateRoundRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);
  121. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  122. public static IntPtr CreateRoundRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse)
  123. {
  124. IntPtr intPtr = NativeMethods._CreateRoundRectRgn(nLeftRect, nTopRect, nRightRect, nBottomRect, nWidthEllipse, nHeightEllipse);
  125. if (IntPtr.Zero == intPtr)
  126. {
  127. throw new Win32Exception();
  128. }
  129. return intPtr;
  130. }
  131. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  132. [DllImport("gdi32.dll", EntryPoint = "CreateRectRgn", SetLastError = true)]
  133. private static extern IntPtr _CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
  134. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  135. public static IntPtr CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect)
  136. {
  137. IntPtr intPtr = NativeMethods._CreateRectRgn(nLeftRect, nTopRect, nRightRect, nBottomRect);
  138. if (IntPtr.Zero == intPtr)
  139. {
  140. throw new Win32Exception();
  141. }
  142. return intPtr;
  143. }
  144. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  145. [DllImport("gdi32.dll", EntryPoint = "CreateRectRgnIndirect", SetLastError = true)]
  146. private static extern IntPtr _CreateRectRgnIndirect([In] ref RECT lprc);
  147. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  148. public static IntPtr CreateRectRgnIndirect(RECT lprc)
  149. {
  150. IntPtr intPtr = NativeMethods._CreateRectRgnIndirect(ref lprc);
  151. if (IntPtr.Zero == intPtr)
  152. {
  153. throw new Win32Exception();
  154. }
  155. return intPtr;
  156. }
  157. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  158. [DllImport("gdi32.dll")]
  159. public static extern IntPtr CreateSolidBrush(int crColor);
  160. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  161. [DllImport("user32.dll", CharSet = CharSet.Unicode, EntryPoint = "CreateWindowExW", SetLastError = true)]
  162. private static extern IntPtr _CreateWindowEx(WS_EX dwExStyle, [MarshalAs(UnmanagedType.LPWStr)] string lpClassName, [MarshalAs(UnmanagedType.LPWStr)] string lpWindowName, WS dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);
  163. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  164. public static IntPtr CreateWindowEx(WS_EX dwExStyle, string lpClassName, string lpWindowName, WS dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam)
  165. {
  166. IntPtr intPtr = NativeMethods._CreateWindowEx(dwExStyle, lpClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
  167. if (IntPtr.Zero == intPtr)
  168. {
  169. HRESULT.ThrowLastError();
  170. }
  171. return intPtr;
  172. }
  173. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  174. [DllImport("user32.dll", CharSet = CharSet.Unicode, EntryPoint = "DefWindowProcW")]
  175. public static extern IntPtr DefWindowProc(IntPtr hWnd, WM Msg, IntPtr wParam, IntPtr lParam);
  176. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  177. [DllImport("gdi32.dll")]
  178. [return: MarshalAs(UnmanagedType.Bool)]
  179. public static extern bool DeleteObject(IntPtr hObject);
  180. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  181. [DllImport("user32.dll")]
  182. [return: MarshalAs(UnmanagedType.Bool)]
  183. public static extern bool DestroyIcon(IntPtr handle);
  184. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  185. [DllImport("user32.dll", SetLastError = true)]
  186. [return: MarshalAs(UnmanagedType.Bool)]
  187. public static extern bool DestroyWindow(IntPtr hwnd);
  188. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  189. [DllImport("user32.dll")]
  190. [return: MarshalAs(UnmanagedType.Bool)]
  191. public static extern bool IsWindow(IntPtr hwnd);
  192. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  193. [DllImport("dwmapi.dll", PreserveSig = false)]
  194. public static extern void DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS pMarInset);
  195. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  196. [DllImport("dwmapi.dll", EntryPoint = "DwmIsCompositionEnabled", PreserveSig = false)]
  197. [return: MarshalAs(UnmanagedType.Bool)]
  198. private static extern bool _DwmIsCompositionEnabled();
  199. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  200. [DllImport("dwmapi.dll", EntryPoint = "DwmGetColorizationColor")]
  201. private static extern HRESULT _DwmGetColorizationColor(out uint pcrColorization, [MarshalAs(UnmanagedType.Bool)] out bool pfOpaqueBlend);
  202. public static bool DwmGetColorizationColor(out uint pcrColorization, out bool pfOpaqueBlend)
  203. {
  204. if (Utility.IsOSVistaOrNewer && NativeMethods.IsThemeActive() && NativeMethods._DwmGetColorizationColor(out pcrColorization, out pfOpaqueBlend).Succeeded)
  205. {
  206. return true;
  207. }
  208. pcrColorization = 4278190080u;
  209. pfOpaqueBlend = true;
  210. return false;
  211. }
  212. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  213. public static bool DwmIsCompositionEnabled()
  214. {
  215. return Utility.IsOSVistaOrNewer && NativeMethods._DwmIsCompositionEnabled();
  216. }
  217. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  218. [DllImport("dwmapi.dll")]
  219. [return: MarshalAs(UnmanagedType.Bool)]
  220. public static extern bool DwmDefWindowProc(IntPtr hwnd, WM msg, IntPtr wParam, IntPtr lParam, out IntPtr plResult);
  221. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  222. [DllImport("dwmapi.dll", EntryPoint = "DwmSetWindowAttribute")]
  223. private static extern void _DwmSetWindowAttribute(IntPtr hwnd, DWMWA dwAttribute, ref int pvAttribute, int cbAttribute);
  224. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  225. public static void DwmSetWindowAttributeFlip3DPolicy(IntPtr hwnd, DWMFLIP3D flip3dPolicy)
  226. {
  227. int num = (int) flip3dPolicy;
  228. NativeMethods._DwmSetWindowAttribute(hwnd, DWMWA.FLIP3D_POLICY, ref num, 4);
  229. }
  230. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  231. public static void DwmSetWindowAttributeDisallowPeek(IntPtr hwnd, bool disallowPeek)
  232. {
  233. int num = disallowPeek ? 1 : 0;
  234. NativeMethods._DwmSetWindowAttribute(hwnd, DWMWA.DISALLOW_PEEK, ref num, 4);
  235. }
  236. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  237. [DllImport("user32.dll", EntryPoint = "EnableMenuItem")]
  238. private static extern int _EnableMenuItem(IntPtr hMenu, SC uIDEnableItem, MF uEnable);
  239. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  240. public static MF EnableMenuItem(IntPtr hMenu, SC uIDEnableItem, MF uEnable)
  241. {
  242. return (MF) NativeMethods._EnableMenuItem(hMenu, uIDEnableItem, uEnable);
  243. }
  244. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  245. [DllImport("user32.dll", EntryPoint = "RemoveMenu", SetLastError = true)]
  246. [return: MarshalAs(UnmanagedType.Bool)]
  247. private static extern bool _RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);
  248. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  249. public static void RemoveMenu(IntPtr hMenu, SC uPosition, MF uFlags)
  250. {
  251. if (!NativeMethods._RemoveMenu(hMenu, (uint) uPosition, (uint) uFlags))
  252. {
  253. throw new Win32Exception();
  254. }
  255. }
  256. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  257. [DllImport("user32.dll", EntryPoint = "DrawMenuBar", SetLastError = true)]
  258. [return: MarshalAs(UnmanagedType.Bool)]
  259. private static extern bool _DrawMenuBar(IntPtr hWnd);
  260. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  261. public static void DrawMenuBar(IntPtr hWnd)
  262. {
  263. if (!NativeMethods._DrawMenuBar(hWnd))
  264. {
  265. throw new Win32Exception();
  266. }
  267. }
  268. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  269. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  270. [DllImport("kernel32.dll")]
  271. [return: MarshalAs(UnmanagedType.Bool)]
  272. public static extern bool FindClose(IntPtr handle);
  273. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  274. [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  275. public static extern SafeFindHandle FindFirstFileW(string lpFileName, [MarshalAs(UnmanagedType.LPStruct)][In][Out] WIN32_FIND_DATAW lpFindFileData);
  276. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  277. [DllImport("kernel32.dll", SetLastError = true)]
  278. [return: MarshalAs(UnmanagedType.Bool)]
  279. public static extern bool FindNextFileW(SafeFindHandle hndFindFile, [MarshalAs(UnmanagedType.LPStruct)][In][Out] WIN32_FIND_DATAW lpFindFileData);
  280. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  281. [DllImport("user32.dll", EntryPoint = "GetClientRect", SetLastError = true)]
  282. [return: MarshalAs(UnmanagedType.Bool)]
  283. private static extern bool _GetClientRect(IntPtr hwnd, out RECT lpRect);
  284. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  285. public static RECT GetClientRect(IntPtr hwnd)
  286. {
  287. RECT result;
  288. if (!NativeMethods._GetClientRect(hwnd, out result))
  289. {
  290. HRESULT.ThrowLastError();
  291. }
  292. return result;
  293. }
  294. [DllImport("uxtheme.dll", CharSet = CharSet.Unicode, EntryPoint = "GetCurrentThemeName")]
  295. private static extern HRESULT _GetCurrentThemeName(StringBuilder pszThemeFileName, int dwMaxNameChars, StringBuilder pszColorBuff, int cchMaxColorChars, StringBuilder pszSizeBuff, int cchMaxSizeChars);
  296. public static void GetCurrentThemeName(out string themeFileName, out string color, out string size)
  297. {
  298. StringBuilder stringBuilder = new StringBuilder(260);
  299. StringBuilder stringBuilder2 = new StringBuilder(260);
  300. StringBuilder stringBuilder3 = new StringBuilder(260);
  301. NativeMethods._GetCurrentThemeName(stringBuilder, stringBuilder.Capacity, stringBuilder2, stringBuilder2.Capacity, stringBuilder3, stringBuilder3.Capacity).ThrowIfFailed();
  302. themeFileName = stringBuilder.ToString();
  303. color = stringBuilder2.ToString();
  304. size = stringBuilder3.ToString();
  305. }
  306. [DllImport("uxtheme.dll")]
  307. [return: MarshalAs(UnmanagedType.Bool)]
  308. public static extern bool IsThemeActive();
  309. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  310. [Obsolete("Use SafeDC.GetDC instead.", true)]
  311. public static void GetDC()
  312. {
  313. }
  314. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  315. [DllImport("gdi32.dll")]
  316. public static extern int GetDeviceCaps(SafeDC hdc, DeviceCap nIndex);
  317. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  318. [DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "GetModuleFileName", SetLastError = true)]
  319. private static extern int _GetModuleFileName(IntPtr hModule, StringBuilder lpFilename, int nSize);
  320. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  321. public static string GetModuleFileName(IntPtr hModule)
  322. {
  323. StringBuilder stringBuilder = new StringBuilder(260);
  324. for (; ; )
  325. {
  326. int num = NativeMethods._GetModuleFileName(hModule, stringBuilder, stringBuilder.Capacity);
  327. if (num == 0)
  328. {
  329. HRESULT.ThrowLastError();
  330. }
  331. if (num != stringBuilder.Capacity)
  332. {
  333. break;
  334. }
  335. stringBuilder.EnsureCapacity(stringBuilder.Capacity * 2);
  336. }
  337. return stringBuilder.ToString();
  338. }
  339. [DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "GetModuleHandleW", SetLastError = true)]
  340. private static extern IntPtr _GetModuleHandle([MarshalAs(UnmanagedType.LPWStr)] string lpModuleName);
  341. public static IntPtr GetModuleHandle(string lpModuleName)
  342. {
  343. IntPtr intPtr = NativeMethods._GetModuleHandle(lpModuleName);
  344. if (intPtr == IntPtr.Zero)
  345. {
  346. HRESULT.ThrowLastError();
  347. }
  348. return intPtr;
  349. }
  350. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  351. [DllImport("user32.dll", EntryPoint = "GetMonitorInfo", SetLastError = true)]
  352. [return: MarshalAs(UnmanagedType.Bool)]
  353. private static extern bool _GetMonitorInfo(IntPtr hMonitor, [In][Out] MONITORINFO lpmi);
  354. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  355. public static MONITORINFO GetMonitorInfo(IntPtr hMonitor)
  356. {
  357. MONITORINFO monitorinfo = new MONITORINFO();
  358. if (!NativeMethods._GetMonitorInfo(hMonitor, monitorinfo))
  359. {
  360. throw new Win32Exception();
  361. }
  362. return monitorinfo;
  363. }
  364. [DllImport("gdi32.dll", EntryPoint = "GetStockObject", SetLastError = true)]
  365. private static extern IntPtr _GetStockObject(StockObject fnObject);
  366. public static IntPtr GetStockObject(StockObject fnObject)
  367. {
  368. return NativeMethods._GetStockObject(fnObject);
  369. }
  370. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  371. [DllImport("user32.dll")]
  372. public static extern IntPtr GetSystemMenu(IntPtr hWnd, [MarshalAs(UnmanagedType.Bool)] bool bRevert);
  373. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  374. [DllImport("user32.dll")]
  375. public static extern int GetSystemMetrics(SM nIndex);
  376. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  377. public static IntPtr GetWindowLongPtr(IntPtr hwnd, GWL nIndex)
  378. {
  379. IntPtr intPtr = IntPtr.Zero;
  380. if (8 == IntPtr.Size)
  381. {
  382. intPtr = NativeMethods.GetWindowLongPtr64(hwnd, nIndex);
  383. }
  384. else
  385. {
  386. intPtr = new IntPtr(NativeMethods.GetWindowLongPtr32(hwnd, nIndex));
  387. }
  388. if (IntPtr.Zero == intPtr)
  389. {
  390. throw new Win32Exception();
  391. }
  392. return intPtr;
  393. }
  394. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  395. [DllImport("uxtheme.dll", PreserveSig = false)]
  396. public static extern void SetWindowThemeAttribute([In] IntPtr hwnd, [In] WINDOWTHEMEATTRIBUTETYPE eAttribute, [In] ref WTA_OPTIONS pvAttribute, [In] uint cbAttribute);
  397. [SuppressMessage("Microsoft.Interoperability", "CA1400:PInvokeEntryPointsShouldExist")]
  398. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  399. [DllImport("user32.dll", EntryPoint = "GetWindowLong", SetLastError = true)]
  400. private static extern int GetWindowLongPtr32(IntPtr hWnd, GWL nIndex);
  401. [SuppressMessage("Microsoft.Interoperability", "CA1400:PInvokeEntryPointsShouldExist")]
  402. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  403. [DllImport("user32.dll", EntryPoint = "GetWindowLongPtr", SetLastError = true)]
  404. private static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, GWL nIndex);
  405. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  406. [DllImport("user32.dll", SetLastError = true)]
  407. [return: MarshalAs(UnmanagedType.Bool)]
  408. private static extern bool GetWindowPlacement(IntPtr hwnd, WINDOWPLACEMENT lpwndpl);
  409. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  410. public static WINDOWPLACEMENT GetWindowPlacement(IntPtr hwnd)
  411. {
  412. WINDOWPLACEMENT windowplacement = new WINDOWPLACEMENT();
  413. if (NativeMethods.GetWindowPlacement(hwnd, windowplacement))
  414. {
  415. return windowplacement;
  416. }
  417. throw new Win32Exception();
  418. }
  419. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  420. [DllImport("user32.dll", EntryPoint = "GetWindowRect", SetLastError = true)]
  421. [return: MarshalAs(UnmanagedType.Bool)]
  422. private static extern bool _GetWindowRect(IntPtr hWnd, out RECT lpRect);
  423. public static RECT GetWindowRect(IntPtr hwnd)
  424. {
  425. RECT result;
  426. if (!NativeMethods._GetWindowRect(hwnd, out result))
  427. {
  428. HRESULT.ThrowLastError();
  429. }
  430. return result;
  431. }
  432. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  433. [DllImport("gdiplus.dll")]
  434. public static extern Status GdipCreateBitmapFromStream(IStream stream, out IntPtr bitmap);
  435. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  436. [DllImport("gdiplus.dll")]
  437. public static extern Status GdipCreateHBITMAPFromBitmap(IntPtr bitmap, out IntPtr hbmReturn, int background);
  438. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  439. [DllImport("gdiplus.dll")]
  440. public static extern Status GdipCreateHICONFromBitmap(IntPtr bitmap, out IntPtr hbmReturn);
  441. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  442. [DllImport("gdiplus.dll")]
  443. public static extern Status GdipDisposeImage(IntPtr image);
  444. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  445. [DllImport("gdiplus.dll")]
  446. public static extern Status GdipImageForceValidation(IntPtr image);
  447. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  448. [DllImport("gdiplus.dll")]
  449. public static extern Status GdiplusStartup(out IntPtr token, StartupInput input, out StartupOutput output);
  450. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  451. [DllImport("gdiplus.dll")]
  452. public static extern Status GdiplusShutdown(IntPtr token);
  453. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  454. [DllImport("user32.dll")]
  455. [return: MarshalAs(UnmanagedType.Bool)]
  456. public static extern bool IsWindowVisible(IntPtr hwnd);
  457. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  458. [DllImport("kernel32.dll", EntryPoint = "LocalFree", SetLastError = true)]
  459. private static extern IntPtr _LocalFree(IntPtr hMem);
  460. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  461. [DllImport("user32.dll")]
  462. public static extern IntPtr MonitorFromWindow(IntPtr hwnd, uint dwFlags);
  463. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  464. [DllImport("user32.dll", EntryPoint = "PostMessage", SetLastError = true)]
  465. [return: MarshalAs(UnmanagedType.Bool)]
  466. private static extern bool _PostMessage(IntPtr hWnd, WM Msg, IntPtr wParam, IntPtr lParam);
  467. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  468. public static void PostMessage(IntPtr hWnd, WM Msg, IntPtr wParam, IntPtr lParam)
  469. {
  470. if (!NativeMethods._PostMessage(hWnd, Msg, wParam, lParam))
  471. {
  472. throw new Win32Exception();
  473. }
  474. }
  475. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  476. [DllImport("user32.dll", EntryPoint = "RegisterClassExW", SetLastError = true)]
  477. private static extern short _RegisterClassEx([In] ref WNDCLASSEX lpwcx);
  478. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  479. public static short RegisterClassEx(ref WNDCLASSEX lpwcx)
  480. {
  481. short num = NativeMethods._RegisterClassEx(ref lpwcx);
  482. if (num == 0)
  483. {
  484. HRESULT.ThrowLastError();
  485. }
  486. return num;
  487. }
  488. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  489. [DllImport("user32.dll", CharSet = CharSet.Unicode, EntryPoint = "RegisterWindowMessage", SetLastError = true)]
  490. private static extern uint _RegisterWindowMessage([MarshalAs(UnmanagedType.LPWStr)] string lpString);
  491. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  492. public static WM RegisterWindowMessage(string lpString)
  493. {
  494. uint num = NativeMethods._RegisterWindowMessage(lpString);
  495. if (num == 0u)
  496. {
  497. HRESULT.ThrowLastError();
  498. }
  499. return (WM) num;
  500. }
  501. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  502. [DllImport("user32.dll", EntryPoint = "SetActiveWindow", SetLastError = true)]
  503. private static extern IntPtr _SetActiveWindow(IntPtr hWnd);
  504. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  505. public static IntPtr SetActiveWindow(IntPtr hwnd)
  506. {
  507. Verify.IsNotDefault<IntPtr>(hwnd, "hwnd");
  508. IntPtr intPtr = NativeMethods._SetActiveWindow(hwnd);
  509. if (intPtr == IntPtr.Zero)
  510. {
  511. HRESULT.ThrowLastError();
  512. }
  513. return intPtr;
  514. }
  515. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  516. public static IntPtr SetClassLongPtr(IntPtr hwnd, GCLP nIndex, IntPtr dwNewLong)
  517. {
  518. if (8 == IntPtr.Size)
  519. {
  520. return NativeMethods.SetClassLongPtr64(hwnd, nIndex, dwNewLong);
  521. }
  522. return new IntPtr(NativeMethods.SetClassLongPtr32(hwnd, nIndex, dwNewLong.ToInt32()));
  523. }
  524. [SuppressMessage("Microsoft.Interoperability", "CA1400:PInvokeEntryPointsShouldExist")]
  525. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  526. [DllImport("user32.dll", EntryPoint = "SetClassLong", SetLastError = true)]
  527. private static extern int SetClassLongPtr32(IntPtr hWnd, GCLP nIndex, int dwNewLong);
  528. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  529. [SuppressMessage("Microsoft.Interoperability", "CA1400:PInvokeEntryPointsShouldExist")]
  530. [DllImport("user32.dll", EntryPoint = "SetClassLongPtr", SetLastError = true)]
  531. private static extern IntPtr SetClassLongPtr64(IntPtr hWnd, GCLP nIndex, IntPtr dwNewLong);
  532. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  533. [DllImport("kernel32.dll", SetLastError = true)]
  534. public static extern ErrorModes SetErrorMode(ErrorModes newMode);
  535. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  536. [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize", SetLastError = true)]
  537. [return: MarshalAs(UnmanagedType.Bool)]
  538. private static extern bool _SetProcessWorkingSetSize(IntPtr hProcess, IntPtr dwMinimiumWorkingSetSize, IntPtr dwMaximumWorkingSetSize);
  539. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  540. public static void SetProcessWorkingSetSize(IntPtr hProcess, int dwMinimumWorkingSetSize, int dwMaximumWorkingSetSize)
  541. {
  542. if (!NativeMethods._SetProcessWorkingSetSize(hProcess, new IntPtr(dwMinimumWorkingSetSize), new IntPtr(dwMaximumWorkingSetSize)))
  543. {
  544. throw new Win32Exception();
  545. }
  546. }
  547. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  548. public static IntPtr SetWindowLongPtr(IntPtr hwnd, GWL nIndex, IntPtr dwNewLong)
  549. {
  550. if (8 == IntPtr.Size)
  551. {
  552. return NativeMethods.SetWindowLongPtr64(hwnd, nIndex, dwNewLong);
  553. }
  554. return new IntPtr(NativeMethods.SetWindowLongPtr32(hwnd, nIndex, dwNewLong.ToInt32()));
  555. }
  556. [SuppressMessage("Microsoft.Interoperability", "CA1400:PInvokeEntryPointsShouldExist")]
  557. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  558. [DllImport("user32.dll", EntryPoint = "SetWindowLong", SetLastError = true)]
  559. private static extern int SetWindowLongPtr32(IntPtr hWnd, GWL nIndex, int dwNewLong);
  560. [SuppressMessage("Microsoft.Interoperability", "CA1400:PInvokeEntryPointsShouldExist")]
  561. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  562. [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", SetLastError = true)]
  563. private static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, GWL nIndex, IntPtr dwNewLong);
  564. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  565. [DllImport("user32.dll", EntryPoint = "SetWindowRgn", SetLastError = true)]
  566. private static extern int _SetWindowRgn(IntPtr hWnd, IntPtr hRgn, [MarshalAs(UnmanagedType.Bool)] bool bRedraw);
  567. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  568. public static void SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool bRedraw)
  569. {
  570. if (NativeMethods._SetWindowRgn(hWnd, hRgn, bRedraw) == 0)
  571. {
  572. throw new Win32Exception();
  573. }
  574. }
  575. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  576. [DllImport("user32.dll", EntryPoint = "SetWindowPos", SetLastError = true)]
  577. [return: MarshalAs(UnmanagedType.Bool)]
  578. private static extern bool _SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, SWP uFlags);
  579. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  580. public static bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, SWP uFlags)
  581. {
  582. return NativeMethods._SetWindowPos(hWnd, hWndInsertAfter, x, y, cx, cy, uFlags);
  583. }
  584. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  585. [DllImport("shell32.dll")]
  586. public static extern Win32Error SHFileOperation(ref SHFILEOPSTRUCT lpFileOp);
  587. [DllImport("user32.dll")]
  588. [return: MarshalAs(UnmanagedType.Bool)]
  589. public static extern bool ShowWindow(IntPtr hwnd, SW nCmdShow);
  590. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  591. [DllImport("user32.dll", EntryPoint = "SystemParametersInfoW", SetLastError = true)]
  592. [return: MarshalAs(UnmanagedType.Bool)]
  593. private static extern bool _SystemParametersInfo_String(SPI uiAction, int uiParam, [MarshalAs(UnmanagedType.LPWStr)] string pvParam, SPIF fWinIni);
  594. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  595. [DllImport("user32.dll", CharSet = CharSet.Unicode, EntryPoint = "SystemParametersInfoW", SetLastError = true)]
  596. [return: MarshalAs(UnmanagedType.Bool)]
  597. private static extern bool _SystemParametersInfo_NONCLIENTMETRICS(SPI uiAction, int uiParam, [In][Out] ref NONCLIENTMETRICS pvParam, SPIF fWinIni);
  598. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  599. [DllImport("user32.dll", CharSet = CharSet.Unicode, EntryPoint = "SystemParametersInfoW", SetLastError = true)]
  600. [return: MarshalAs(UnmanagedType.Bool)]
  601. private static extern bool _SystemParametersInfo_HIGHCONTRAST(SPI uiAction, int uiParam, [In][Out] ref HIGHCONTRAST pvParam, SPIF fWinIni);
  602. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  603. public static void SystemParametersInfo(SPI uiAction, int uiParam, string pvParam, SPIF fWinIni)
  604. {
  605. if (!NativeMethods._SystemParametersInfo_String(uiAction, uiParam, pvParam, fWinIni))
  606. {
  607. HRESULT.ThrowLastError();
  608. }
  609. }
  610. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  611. public static NONCLIENTMETRICS SystemParameterInfo_GetNONCLIENTMETRICS()
  612. {
  613. NONCLIENTMETRICS result = Utility.IsOSVistaOrNewer ? NONCLIENTMETRICS.VistaMetricsStruct : NONCLIENTMETRICS.XPMetricsStruct;
  614. if (!NativeMethods._SystemParametersInfo_NONCLIENTMETRICS(SPI.GETNONCLIENTMETRICS, result.cbSize, ref result, SPIF.None))
  615. {
  616. HRESULT.ThrowLastError();
  617. }
  618. return result;
  619. }
  620. [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
  621. public static HIGHCONTRAST SystemParameterInfo_GetHIGHCONTRAST()
  622. {
  623. HIGHCONTRAST result = new HIGHCONTRAST
  624. {
  625. cbSize = Marshal.SizeOf(typeof(HIGHCONTRAST))
  626. };
  627. if (!NativeMethods._SystemParametersInfo_HIGHCONTRAST(SPI.GETHIGHCONTRAST, result.cbSize, ref result, SPIF.None))
  628. {
  629. HRESULT.ThrowLastError();
  630. }
  631. return result;
  632. }
  633. [DllImport("user32.dll")]
  634. public static extern uint TrackPopupMenuEx(IntPtr hmenu, uint fuFlags, int x, int y, IntPtr hwnd, IntPtr lptpm);
  635. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  636. [DllImport("gdi32.dll", EntryPoint = "SelectObject", SetLastError = true)]
  637. private static extern IntPtr _SelectObject(SafeDC hdc, IntPtr hgdiobj);
  638. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  639. public static IntPtr SelectObject(SafeDC hdc, IntPtr hgdiobj)
  640. {
  641. IntPtr intPtr = NativeMethods._SelectObject(hdc, hgdiobj);
  642. if (intPtr == IntPtr.Zero)
  643. {
  644. HRESULT.ThrowLastError();
  645. }
  646. return intPtr;
  647. }
  648. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  649. [DllImport("gdi32.dll", EntryPoint = "SelectObject", SetLastError = true)]
  650. private static extern IntPtr _SelectObjectSafeHBITMAP(SafeDC hdc, SafeHBITMAP hgdiobj);
  651. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  652. public static IntPtr SelectObject(SafeDC hdc, SafeHBITMAP hgdiobj)
  653. {
  654. IntPtr intPtr = NativeMethods._SelectObjectSafeHBITMAP(hdc, hgdiobj);
  655. if (intPtr == IntPtr.Zero)
  656. {
  657. HRESULT.ThrowLastError();
  658. }
  659. return intPtr;
  660. }
  661. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  662. [DllImport("user32.dll", SetLastError = true)]
  663. public static extern int SendInput(int nInputs, ref INPUT pInputs, int cbSize);
  664. [DllImport("user32.dll", SetLastError = true)]
  665. public static extern IntPtr SendMessage(IntPtr hWnd, WM Msg, IntPtr wParam, IntPtr lParam);
  666. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  667. [DllImport("user32.dll", EntryPoint = "UnregisterClass", SetLastError = true)]
  668. [return: MarshalAs(UnmanagedType.Bool)]
  669. private static extern bool _UnregisterClassAtom(IntPtr lpClassName, IntPtr hInstance);
  670. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  671. [DllImport("user32.dll", CharSet = CharSet.Unicode, EntryPoint = "UnregisterClass", SetLastError = true)]
  672. [return: MarshalAs(UnmanagedType.Bool)]
  673. private static extern bool _UnregisterClassName(string lpClassName, IntPtr hInstance);
  674. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  675. public static void UnregisterClass(short atom, IntPtr hinstance)
  676. {
  677. if (!NativeMethods._UnregisterClassAtom(new IntPtr((int) atom), hinstance))
  678. {
  679. HRESULT.ThrowLastError();
  680. }
  681. }
  682. public static void UnregisterClass(string lpClassName, IntPtr hInstance)
  683. {
  684. if (!NativeMethods._UnregisterClassName(lpClassName, hInstance))
  685. {
  686. HRESULT.ThrowLastError();
  687. }
  688. }
  689. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  690. [DllImport("user32.dll", EntryPoint = "UpdateLayeredWindow", SetLastError = true)]
  691. [return: MarshalAs(UnmanagedType.Bool)]
  692. private static extern bool _UpdateLayeredWindow(IntPtr hwnd, SafeDC hdcDst, [In] ref POINT pptDst, [In] ref SIZE psize, SafeDC hdcSrc, [In] ref POINT pptSrc, int crKey, ref BLENDFUNCTION pblend, ULW dwFlags);
  693. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  694. [DllImport("user32.dll", EntryPoint = "UpdateLayeredWindow", SetLastError = true)]
  695. [return: MarshalAs(UnmanagedType.Bool)]
  696. private static extern bool _UpdateLayeredWindowIntPtr(IntPtr hwnd, IntPtr hdcDst, IntPtr pptDst, IntPtr psize, IntPtr hdcSrc, IntPtr pptSrc, int crKey, ref BLENDFUNCTION pblend, ULW dwFlags);
  697. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  698. public static void UpdateLayeredWindow(IntPtr hwnd, SafeDC hdcDst, ref POINT pptDst, ref SIZE psize, SafeDC hdcSrc, ref POINT pptSrc, int crKey, ref BLENDFUNCTION pblend, ULW dwFlags)
  699. {
  700. if (!NativeMethods._UpdateLayeredWindow(hwnd, hdcDst, ref pptDst, ref psize, hdcSrc, ref pptSrc, crKey, ref pblend, dwFlags))
  701. {
  702. HRESULT.ThrowLastError();
  703. }
  704. }
  705. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  706. public static void UpdateLayeredWindow(IntPtr hwnd, int crKey, ref BLENDFUNCTION pblend, ULW dwFlags)
  707. {
  708. if (!NativeMethods._UpdateLayeredWindowIntPtr(hwnd, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, crKey, ref pblend, dwFlags))
  709. {
  710. HRESULT.ThrowLastError();
  711. }
  712. }
  713. [DllImport("shell32.dll", EntryPoint = "SHAddToRecentDocs")]
  714. private static extern void _SHAddToRecentDocs_String(SHARD uFlags, [MarshalAs(UnmanagedType.LPWStr)] string pv);
  715. [DllImport("shell32.dll", EntryPoint = "SHAddToRecentDocs")]
  716. private static extern void _SHAddToRecentDocs_ShellLink(SHARD uFlags, IShellLinkW pv);
  717. public static void SHAddToRecentDocs(string path)
  718. {
  719. NativeMethods._SHAddToRecentDocs_String(SHARD.PATHW, path);
  720. }
  721. public static void SHAddToRecentDocs(IShellLinkW shellLink)
  722. {
  723. NativeMethods._SHAddToRecentDocs_ShellLink(SHARD.LINK, shellLink);
  724. }
  725. [DllImport("dwmapi.dll", EntryPoint = "DwmGetCompositionTimingInfo")]
  726. private static extern HRESULT _DwmGetCompositionTimingInfo(IntPtr hwnd, ref DWM_TIMING_INFO pTimingInfo);
  727. public static DWM_TIMING_INFO? DwmGetCompositionTimingInfo(IntPtr hwnd)
  728. {
  729. if (!Utility.IsOSVistaOrNewer)
  730. {
  731. return null;
  732. }
  733. DWM_TIMING_INFO value = new DWM_TIMING_INFO
  734. {
  735. cbSize = Marshal.SizeOf(typeof(DWM_TIMING_INFO))
  736. };
  737. HRESULT hrLeft = NativeMethods._DwmGetCompositionTimingInfo(hwnd, ref value);
  738. if (hrLeft == HRESULT.E_PENDING)
  739. {
  740. return null;
  741. }
  742. hrLeft.ThrowIfFailed();
  743. return new DWM_TIMING_INFO?(value);
  744. }
  745. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  746. [DllImport("dwmapi.dll", PreserveSig = false)]
  747. public static extern void DwmInvalidateIconicBitmaps(IntPtr hwnd);
  748. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  749. [DllImport("dwmapi.dll", PreserveSig = false)]
  750. public static extern void DwmSetIconicThumbnail(IntPtr hwnd, IntPtr hbmp, DWM_SIT dwSITFlags);
  751. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  752. [DllImport("dwmapi.dll", PreserveSig = false)]
  753. public static extern void DwmSetIconicLivePreviewBitmap(IntPtr hwnd, IntPtr hbmp, RefPOINT pptClient, DWM_SIT dwSITFlags);
  754. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  755. [DllImport("shell32.dll", PreserveSig = false)]
  756. public static extern void SHGetItemFromDataObject(IDataObject pdtobj, DOGIF dwFlags, [In] ref Guid riid, [MarshalAs(UnmanagedType.Interface)] out object ppv);
  757. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  758. [DllImport("shell32.dll", PreserveSig = false)]
  759. public static extern HRESULT SHCreateItemFromParsingName([MarshalAs(UnmanagedType.LPWStr)] string pszPath, IBindCtx pbc, [In] ref Guid riid, [MarshalAs(UnmanagedType.Interface)] out object ppv);
  760. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  761. [DllImport("shell32.dll")]
  762. [return: MarshalAs(UnmanagedType.Bool)]
  763. public static extern bool Shell_NotifyIcon(NIM dwMessage, [In] NOTIFYICONDATA lpdata);
  764. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  765. [DllImport("shell32.dll", PreserveSig = false)]
  766. public static extern void SetCurrentProcessExplicitAppUserModelID([MarshalAs(UnmanagedType.LPWStr)] string AppID);
  767. [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
  768. [DllImport("shell32.dll")]
  769. public static extern HRESULT GetCurrentProcessExplicitAppUserModelID([MarshalAs(UnmanagedType.LPWStr)] out string AppID);
  770. }