SafeNativeMethods.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. using Microsoft.Win32.SafeHandles;
  2. using System;
  3. using System.Runtime.InteropServices;
  4. using System.Security;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. namespace PaintDotNet.SystemLayer
  8. {
  9. [SuppressUnmanagedCodeSecurity]
  10. public static class SafeNativeMethods
  11. {
  12. [DllImport("user32.dll")]
  13. public static extern IntPtr GetDesktopWindow();
  14. [DllImport("user32.dll")]
  15. public static extern IntPtr GetWindowDC(IntPtr hWnd);
  16. [DllImport("user32.dll")]
  17. public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
  18. [DllImport("user32.dll")]
  19. public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
  20. public const int SRCCOPY = 0x00CC0020;
  21. [DllImport("gdi32.dll")]
  22. public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest, int nWidth,
  23. int nHeight, IntPtr hObjectSource, int nXSrc, int nYSrc, int dwRop);
  24. [DllImport("gdi32.dll")]
  25. public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth, int nHeight);
  26. [DllImport("kernel32.dll", SetLastError = false)]
  27. [return: MarshalAs(UnmanagedType.Bool)]
  28. internal static extern bool IsProcessorFeaturePresent(uint ProcessorFeature);
  29. [DllImport("user32.dll", SetLastError = true)]
  30. [return: MarshalAs(UnmanagedType.Bool)]
  31. internal static extern bool DrawMenuBar(IntPtr hWnd);
  32. [DllImport("user32.dll", SetLastError = false)]
  33. internal static extern IntPtr GetSystemMenu(
  34. IntPtr hWnd,
  35. [MarshalAs(UnmanagedType.Bool)] bool bRevert);
  36. [DllImport("user32.dll", SetLastError = false)]
  37. internal static extern int EnableMenuItem(
  38. IntPtr hMenu,
  39. uint uIDEnableItem,
  40. uint uEnable);
  41. [DllImport("user32.dll", SetLastError = false)]
  42. [return: MarshalAs(UnmanagedType.Bool)]
  43. internal static extern bool FlashWindow(
  44. IntPtr hWnd,
  45. [MarshalAs(UnmanagedType.Bool)] bool bInvert);
  46. [DllImport("dwmapi.dll")]
  47. internal unsafe static extern int DwmGetWindowAttribute(
  48. IntPtr hwnd,
  49. uint dwAttribute,
  50. void* pvAttribute,
  51. uint cbAttribute);
  52. [DllImport("kernel32.dll", SetLastError = false)]
  53. internal static extern IntPtr GetCurrentThread();
  54. [DllImport("kernel32.dll", SetLastError = true)]
  55. [return: MarshalAs(UnmanagedType.Bool)]
  56. internal static extern bool SetThreadPriority(
  57. IntPtr hThread,
  58. int nPriority);
  59. [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  60. internal static extern IntPtr CreateFileMappingW(
  61. IntPtr hFile,
  62. IntPtr lpFileMappingAttributes,
  63. uint flProtect,
  64. uint dwMaximumSizeHigh,
  65. uint dwMaximumSizeLow,
  66. [MarshalAs(UnmanagedType.LPTStr)] string lpName);
  67. [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  68. internal static extern IntPtr MapViewOfFile(
  69. IntPtr hFileMappingObject,
  70. uint dwDesiredAccess,
  71. uint dwFileOffsetHigh,
  72. uint dwFileOffsetLow,
  73. UIntPtr dwNumberOfBytesToMap);
  74. [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  75. [return: MarshalAs(UnmanagedType.Bool)]
  76. internal static extern bool UnmapViewOfFile(IntPtr lpBaseAddress);
  77. [DllImport("user32.dll", SetLastError = true)]
  78. [return: MarshalAs(UnmanagedType.Bool)]
  79. internal static extern bool ShowScrollBar(
  80. IntPtr hWnd,
  81. int wBar,
  82. [MarshalAs(UnmanagedType.Bool)] bool bShow);
  83. [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  84. [return: MarshalAs(UnmanagedType.Bool)]
  85. internal static extern bool GetVersionEx(ref NativeStructs.OSVERSIONINFOEX lpVersionInfo);
  86. [DllImport("user32.dll", SetLastError = true)]
  87. [return: MarshalAs(UnmanagedType.Bool)]
  88. internal static extern bool GetLayeredWindowAttributes(
  89. IntPtr hwnd,
  90. out uint pcrKey,
  91. out byte pbAlpha,
  92. out uint pdwFlags);
  93. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Portability", "CA1901:PInvokeDeclarationsShouldBePortable", MessageId = "2")]
  94. [DllImport("user32.dll", SetLastError = true)]
  95. [return: MarshalAs(UnmanagedType.Bool)]
  96. internal static extern bool SetLayeredWindowAttributes(
  97. IntPtr hwnd,
  98. uint crKey,
  99. byte bAlpha,
  100. uint dwFlags);
  101. [DllImport("gdi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  102. internal static extern IntPtr CreateFontW(
  103. int nHeight,
  104. int nWidth,
  105. int nEscapement,
  106. int nOrientation,
  107. int fnWeight,
  108. uint fdwItalic,
  109. uint fdwUnderline,
  110. uint fdwStrikeOut,
  111. uint fdwCharSet,
  112. uint fdwOutputPrecision,
  113. uint fdwClipPrecision,
  114. uint fdwQuality,
  115. uint fdwPitchAndFamily,
  116. string lpszFace);
  117. [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  118. internal static extern int DrawTextW(
  119. IntPtr hdc,
  120. string lpString,
  121. int nCount,
  122. ref NativeStructs.RECT lpRect,
  123. uint uFormat);
  124. [DllImport("gdi32.dll", SetLastError = true)]
  125. internal static extern IntPtr CreateDIBSection(
  126. IntPtr hdc,
  127. ref NativeStructs.BITMAPINFO pbmi,
  128. uint iUsage,
  129. out IntPtr ppvBits,
  130. IntPtr hSection,
  131. uint dwOffset);
  132. [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  133. internal static extern IntPtr CreateFileW(
  134. string lpFileName,
  135. uint dwDesiredAccess,
  136. uint dwShareMode,
  137. IntPtr lpSecurityAttributes,
  138. uint dwCreationDisposition,
  139. uint dwFlagsAndAttributes,
  140. IntPtr hTemplateFile);
  141. [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  142. [return: MarshalAs(UnmanagedType.Bool)]
  143. internal unsafe static extern bool WriteFile(
  144. IntPtr hFile,
  145. void* lpBuffer,
  146. uint nNumberOfBytesToWrite,
  147. out uint lpNumberOfBytesWritten,
  148. IntPtr lpOverlapped);
  149. [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  150. [return: MarshalAs(UnmanagedType.Bool)]
  151. internal unsafe static extern bool ReadFile(
  152. SafeFileHandle sfhFile,
  153. void* lpBuffer,
  154. uint nNumberOfBytesToRead,
  155. out uint lpNumberOfBytesRead,
  156. IntPtr lpOverlapped);
  157. [DllImport("kernel32.dll", SetLastError = true)]
  158. [return: MarshalAs(UnmanagedType.Bool)]
  159. internal static extern bool CloseHandle(IntPtr hObject);
  160. [DllImport("kernel32.dll", SetLastError = true)]
  161. [return: MarshalAs(UnmanagedType.Bool)]
  162. internal static extern bool SetHandleInformation(
  163. IntPtr hObject,
  164. uint dwMask,
  165. uint dwFlags);
  166. [DllImport("user32.dll", SetLastError = false)]
  167. internal static extern int GetUpdateRgn(
  168. IntPtr hWnd,
  169. IntPtr hRgn,
  170. [MarshalAs(UnmanagedType.Bool)] bool bErase);
  171. [DllImport("user32.dll", SetLastError = false)]
  172. internal static extern uint GetWindowThreadProcessId(
  173. IntPtr hWnd,
  174. out uint lpdwProcessId);
  175. [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  176. internal static extern IntPtr FindWindowW(
  177. [MarshalAs(UnmanagedType.LPWStr)] string lpClassName,
  178. [MarshalAs(UnmanagedType.LPWStr)] string lpWindowName);
  179. [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  180. internal static extern IntPtr FindWindowExW(
  181. IntPtr hwndParent,
  182. IntPtr hwndChildAfter,
  183. [MarshalAs(UnmanagedType.LPWStr)] string lpszClass,
  184. [MarshalAs(UnmanagedType.LPWStr)] string lpszWindow);
  185. [DllImport("user32.dll", SetLastError = false)]
  186. internal static extern IntPtr SendMessageW(
  187. IntPtr hWnd,
  188. uint msg,
  189. IntPtr wParam,
  190. IntPtr lParam);
  191. [DllImport("user32.dll", SetLastError = true)]
  192. [return: MarshalAs(UnmanagedType.Bool)]
  193. internal extern static bool PostMessageW(
  194. IntPtr handle,
  195. uint msg,
  196. IntPtr wParam,
  197. IntPtr lParam);
  198. [DllImport("user32.dll", SetLastError = true)]
  199. internal static extern uint GetWindowLongW(
  200. IntPtr hWnd,
  201. int nIndex);
  202. [DllImport("user32.dll", SetLastError = true)]
  203. internal static extern uint SetWindowLongW(
  204. IntPtr hWnd,
  205. int nIndex,
  206. uint dwNewLong);
  207. [DllImport("kernel32.dll", SetLastError = true)]
  208. [return: MarshalAs(UnmanagedType.Bool)]
  209. internal static extern bool QueryPerformanceCounter(out ulong lpPerformanceCount);
  210. [DllImport("kernel32.dll", SetLastError = true)]
  211. [return: MarshalAs(UnmanagedType.Bool)]
  212. internal static extern bool QueryPerformanceFrequency(out ulong lpFrequency);
  213. [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
  214. internal static extern unsafe void memcpy(void* dst, void* src, UIntPtr length);
  215. [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
  216. internal static extern unsafe void memset(void* dst, int c, UIntPtr length);
  217. [DllImport("User32.dll", SetLastError = false)]
  218. internal static extern int GetSystemMetrics(int nIndex);
  219. [DllImport("kernel32.dll", SetLastError = true)]
  220. internal static extern uint WaitForSingleObject(
  221. IntPtr hHandle,
  222. uint dwMilliseconds);
  223. [DllImport("kernel32.dll", SetLastError = true)]
  224. internal static extern uint WaitForMultipleObjects(
  225. uint nCount,
  226. IntPtr[] lpHandles,
  227. [MarshalAs(UnmanagedType.Bool)] bool bWaitAll,
  228. uint dwMilliseconds);
  229. internal static uint WaitForMultipleObjects(IntPtr[] lpHandles, bool bWaitAll, uint dwMilliseconds)
  230. {
  231. return WaitForMultipleObjects((uint)lpHandles.Length, lpHandles, bWaitAll, dwMilliseconds);
  232. }
  233. [DllImport("wtsapi32.dll", SetLastError = true)]
  234. internal static extern uint WTSRegisterSessionNotification(IntPtr hWnd, uint dwFlags);
  235. [DllImport("wtsapi32.dll", SetLastError = true)]
  236. internal static extern uint WTSUnRegisterSessionNotification(IntPtr hWnd);
  237. [DllImport("Gdi32.dll", SetLastError = true)]
  238. internal unsafe static extern uint GetRegionData(
  239. IntPtr hRgn,
  240. uint dwCount,
  241. NativeStructs.RGNDATA* lpRgnData);
  242. [DllImport("Gdi32.dll", SetLastError = true)]
  243. internal unsafe static extern IntPtr CreateRectRgn(
  244. int nLeftRect,
  245. int nTopRect,
  246. int nRightRect,
  247. int nBottomRect);
  248. [DllImport("Gdi32.dll", SetLastError = true)]
  249. [return: MarshalAs(UnmanagedType.Bool)]
  250. internal extern static bool MoveToEx(
  251. IntPtr hdc,
  252. int X,
  253. int Y,
  254. out NativeStructs.POINT lpPoint);
  255. [DllImport("Gdi32.dll", SetLastError = true)]
  256. [return: MarshalAs(UnmanagedType.Bool)]
  257. internal extern static bool LineTo(
  258. IntPtr hdc,
  259. int nXEnd,
  260. int nYEnd);
  261. [DllImport("User32.dll", SetLastError = true)]
  262. internal extern static int FillRect(
  263. IntPtr hDC,
  264. ref NativeStructs.RECT lprc,
  265. IntPtr hbr);
  266. [DllImport("Gdi32.dll", SetLastError = true)]
  267. internal extern static IntPtr CreatePen(
  268. int fnPenStyle,
  269. int nWidth,
  270. uint crColor);
  271. [DllImport("Gdi32.dll", SetLastError = true)]
  272. internal extern static IntPtr CreateSolidBrush(uint crColor);
  273. [DllImport("Gdi32.dll", SetLastError = false)]
  274. public extern static IntPtr SelectObject(
  275. IntPtr hdc,
  276. IntPtr hgdiobj);
  277. [DllImport("Gdi32.dll", SetLastError = true)]
  278. [return: MarshalAs(UnmanagedType.Bool)]
  279. public extern static bool DeleteObject(IntPtr hObject);
  280. [DllImport("Gdi32.dll", SetLastError = true)]
  281. public extern static uint DeleteDC(IntPtr hdc);
  282. [DllImport("Gdi32.Dll", SetLastError = true)]
  283. public extern static IntPtr CreateCompatibleDC(IntPtr hdc);
  284. [DllImport("Gdi32.Dll", SetLastError = true)]
  285. internal extern static uint BitBlt(
  286. IntPtr hdcDest,
  287. int nXDest,
  288. int nYDest,
  289. int nWidth,
  290. int nHeight,
  291. IntPtr hdcSrc,
  292. int nXSrc,
  293. int nYSrc,
  294. uint dwRop);
  295. [DllImport("Gdi32.Dll", SetLastError = true)]
  296. internal extern static uint StretchBlt(
  297. IntPtr hdcDest,
  298. int nXOriginDest,
  299. int nYOriginDest,
  300. int nWidthDest,
  301. int nHeightDest,
  302. IntPtr hdcSrc,
  303. int nXOriginSrc,
  304. int nYOriginSrc,
  305. int nWidthSrc,
  306. int nHeightSrc,
  307. uint dwRop);
  308. [DllImport("kernel32.dll", SetLastError = true)]
  309. internal static extern IntPtr VirtualAlloc(
  310. IntPtr lpAddress,
  311. UIntPtr dwSize,
  312. uint flAllocationType,
  313. uint flProtect);
  314. [DllImport("kernel32.dll", SetLastError = true)]
  315. [return: MarshalAs(UnmanagedType.Bool)]
  316. internal static extern bool VirtualFree(
  317. IntPtr lpAddress,
  318. UIntPtr dwSize,
  319. uint dwFreeType);
  320. [DllImport("kernel32.dll", SetLastError = true)]
  321. [return: MarshalAs(UnmanagedType.Bool)]
  322. internal static extern bool VirtualProtect(
  323. IntPtr lpAddress,
  324. UIntPtr dwSize,
  325. uint flNewProtect,
  326. out uint lpflOldProtect);
  327. [DllImport("Kernel32.dll", SetLastError = false)]
  328. internal static extern IntPtr HeapAlloc(IntPtr hHeap, uint dwFlags, UIntPtr dwBytes);
  329. [DllImport("Kernel32.dll", SetLastError = true)]
  330. [return: MarshalAs(UnmanagedType.Bool)]
  331. internal static extern bool HeapFree(IntPtr hHeap, uint dwFlags, IntPtr lpMem);
  332. [DllImport("Kernel32.dll", SetLastError = false)]
  333. internal static extern UIntPtr HeapSize(IntPtr hHeap, uint dwFlags, IntPtr lpMem);
  334. [DllImport("Kernel32.dll", SetLastError = true)]
  335. internal static extern IntPtr HeapCreate(
  336. uint flOptions,
  337. [MarshalAs(UnmanagedType.SysUInt)] IntPtr dwInitialSize,
  338. [MarshalAs(UnmanagedType.SysUInt)] IntPtr dwMaximumSize
  339. );
  340. [DllImport("Kernel32.dll", SetLastError = true)]
  341. internal static extern uint HeapDestroy(IntPtr hHeap);
  342. [DllImport("Kernel32.Dll", SetLastError = true)]
  343. internal unsafe static extern uint HeapSetInformation(
  344. IntPtr HeapHandle,
  345. int HeapInformationClass,
  346. void* HeapInformation,
  347. uint HeapInformationLength
  348. );
  349. [DllImport("winhttp.dll", CharSet = CharSet.Unicode)]
  350. [return: MarshalAs(UnmanagedType.Bool)]
  351. internal static extern bool WinHttpGetIEProxyConfigForCurrentUser(ref NativeStructs.WINHTTP_CURRENT_USER_IE_PROXY_CONFIG pProxyConfig);
  352. [DllImport("kernel32.dll", SetLastError = true)]
  353. internal static extern IntPtr GlobalFree(IntPtr hMem);
  354. [DllImport("user32.dll", SetLastError = false)]
  355. [return: MarshalAs(UnmanagedType.Bool)]
  356. internal static extern bool SetForegroundWindow(IntPtr hWnd);
  357. [DllImport("user32.dll", SetLastError = false)]
  358. [return: MarshalAs(UnmanagedType.Bool)]
  359. internal static extern bool IsIconic(IntPtr hWnd);
  360. [DllImport("user32.dll", SetLastError = false)]
  361. [return: MarshalAs(UnmanagedType.Bool)]
  362. internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  363. //
  364. // ÒÔÏÂÊDzο¼https://www.fangsi.net/30.html£¬ÎªÁËʵÏÖhint
  365. //
  366. /// <summary>
  367. ///
  368. /// </summary>
  369. /// <param name="hWnd"></param>
  370. /// <param name="msg"></param>
  371. /// <param name="wParam"></param>
  372. /// <param name="lParam"></param>
  373. /// <returns></returns>
  374. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  375. private static extern Int32 SendMessage(IntPtr hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);
  376. [DllImport("user32.dll")]
  377. private static extern bool SendMessage(IntPtr hwnd, int msg, int wParam, StringBuilder lParam);
  378. [DllImport("user32.dll")]
  379. private static extern bool GetComboBoxInfo(IntPtr hwnd, ref COMBOBOXINFO pcbi);
  380. [StructLayout(LayoutKind.Sequential)]
  381. private struct COMBOBOXINFO
  382. {
  383. public int cbSize;
  384. public RECT rcItem;
  385. public RECT rcButton;
  386. public IntPtr stateButton;
  387. public IntPtr hwndCombo;
  388. public IntPtr hwndItem;
  389. public IntPtr hwndList;
  390. }
  391. [StructLayout(LayoutKind.Sequential)]
  392. public struct RECT
  393. {
  394. public int left;
  395. public int top;
  396. public int right;
  397. public int bottom;
  398. }
  399. private const int EM_SETCUEBANNER = 0x1501;
  400. private const int EM_GETCUEBANNER = 0x1502;
  401. public static void SetCueText(Control control, string text)
  402. {
  403. if (control is ComboBox)
  404. {
  405. COMBOBOXINFO info = GetComboBoxInfo(control);
  406. SendMessage(info.hwndItem, EM_SETCUEBANNER, 0, text);
  407. }
  408. else
  409. {
  410. SendMessage(control.Handle, EM_SETCUEBANNER, 0, text);
  411. }
  412. }
  413. private static COMBOBOXINFO GetComboBoxInfo(Control control)
  414. {
  415. COMBOBOXINFO info = new COMBOBOXINFO();
  416. //a combobox is made up of three controls, a button, a list and textbox;
  417. //we want the textbox
  418. info.cbSize = Marshal.SizeOf(info);
  419. GetComboBoxInfo(control.Handle, ref info);
  420. return info;
  421. }
  422. public static string GetCueText(Control control)
  423. {
  424. StringBuilder builder = new StringBuilder();
  425. if (control is ComboBox)
  426. {
  427. COMBOBOXINFO info = new COMBOBOXINFO();
  428. //a combobox is made up of two controls, a list and textbox;
  429. //we want the textbox
  430. info.cbSize = Marshal.SizeOf(info);
  431. GetComboBoxInfo(control.Handle, ref info);
  432. SendMessage(info.hwndItem, EM_GETCUEBANNER, 0, builder);
  433. }
  434. else
  435. {
  436. SendMessage(control.Handle, EM_GETCUEBANNER, 0, builder);
  437. }
  438. return builder.ToString();
  439. }
  440. }
  441. }