1 /*
2   KeePass Password Safe - The Open-Source Password Manager
3   Copyright (C) 2003-2021 Dominik Reichl <dominik.reichl@t-online.de>
4 
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 2 of the License, or
8   (at your option) any later version.
9 
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19 
20 using System;
21 using System.Diagnostics;
22 using System.Runtime.InteropServices;
23 using System.Security;
24 using System.Text;
25 using System.Windows.Forms;
26 
27 namespace KeePass.Native
28 {
29 	internal static partial class NativeMethods
30 	{
31 		internal const int WM_SETFOCUS = 0x0007;
32 		internal const int WM_KILLFOCUS = 0x0008;
33 		internal const int WM_SETTINGCHANGE = 0x001A;
34 		internal const int WM_CONTEXTMENU = 0x007B;
35 		internal const int WM_KEYDOWN = 0x0100;
36 		internal const int WM_KEYUP = 0x0101;
37 		internal const int WM_SYSKEYDOWN = 0x0104;
38 		internal const int WM_SYSKEYUP = 0x0105;
39 		internal const int WM_DRAWCLIPBOARD = 0x0308;
40 		internal const int WM_CHANGECBCHAIN = 0x030D;
41 		internal const int WM_HOTKEY = 0x0312;
42 		internal const int WM_USER = 0x0400;
43 		internal const int WM_SYSCOMMAND = 0x0112;
44 		internal const int WM_POWERBROADCAST = 0x0218;
45 		internal const int WM_COPYDATA = 0x004A;
46 		// internal const int WM_MOUSEMOVE = 0x0200;
47 		internal const int WM_LBUTTONDOWN = 0x0201;
48 		internal const int WM_RBUTTONDOWN = 0x0204;
49 		internal const int WM_MBUTTONDOWN = 0x0207;
50 		// internal const int WM_MOUSEWHEEL = 0x020A;
51 		// internal const int WM_ERASEBKGND = 0x0014;
52 		internal const int WM_INPUTLANGCHANGEREQUEST = 0x0050;
53 
54 		internal const int WM_NOTIFY = 0x004E;
55 
56 		// See Control.ReflectMessageInternal;
57 		// https://msdn.microsoft.com/en-us/library/eeah46xd.aspx
58 		internal const int WM_REFLECT = 0x2000;
59 
60 		internal const int WM_NOTIFY_REFLECT = (WM_NOTIFY + WM_REFLECT);
61 
62 		internal const int WM_GETTEXTLENGTH = 0x000E;
63 		internal const int WM_GETICON = 0x007F;
64 
65 		internal const int HWND_BROADCAST = 0xFFFF;
66 
67 		internal const uint SMTO_NORMAL = 0x0000;
68 		internal const uint SMTO_BLOCK = 0x0001;
69 		internal const uint SMTO_ABORTIFHUNG = 0x0002;
70 		internal const uint SMTO_NOTIMEOUTIFNOTHUNG = 0x0008;
71 
72 		internal const uint INPUT_MOUSE = 0;
73 		internal const uint INPUT_KEYBOARD = 1;
74 		internal const uint INPUT_HARDWARE = 2;
75 
76 		// internal const int VK_RETURN = 0x0D;
77 		internal const int VK_SHIFT = 0x10;
78 		internal const int VK_CONTROL = 0x11;
79 		internal const int VK_MENU = 0x12;
80 		internal const int VK_CAPITAL = 0x14; // Caps Lock
81 		// internal const int VK_ESCAPE = 0x1B;
82 		// internal const int VK_SPACE = 0x20;
83 		internal const int VK_NUMLOCK = 0x90;
84 		internal const int VK_LSHIFT = 0xA0;
85 		internal const int VK_RSHIFT = 0xA1;
86 		internal const int VK_LCONTROL = 0xA2;
87 		internal const int VK_RCONTROL = 0xA3;
88 		internal const int VK_LMENU = 0xA4;
89 		internal const int VK_RMENU = 0xA5;
90 		internal const int VK_LWIN = 0x5B;
91 		internal const int VK_RWIN = 0x5C;
92 		internal const int VK_SNAPSHOT = 0x2C;
93 
94 		// internal const int VK_F5 = 0x74;
95 		// internal const int VK_F6 = 0x75;
96 		// internal const int VK_F7 = 0x76;
97 		// internal const int VK_F8 = 0x77;
98 
99 		internal const uint MAPVK_VK_TO_VSC = 0;
100 
101 		internal const uint KEYEVENTF_EXTENDEDKEY = 1;
102 		internal const uint KEYEVENTF_KEYUP = 2;
103 		internal const uint KEYEVENTF_UNICODE = 4;
104 
105 		// private const int KL_NAMELENGTH = 9;
106 
107 		internal const ushort LANG_CZECH = 0x05;
108 		internal const ushort LANG_POLISH = 0x15;
109 
110 		// internal const uint GW_CHILD = 5;
111 		internal const uint GW_HWNDNEXT = 2;
112 
113 		internal const int GWL_STYLE = -16;
114 		internal const int GWL_EXSTYLE = -20;
115 
116 		internal const int WS_VISIBLE = 0x10000000;
117 
118 		// internal const int WS_EX_COMPOSITED = 0x02000000;
119 		internal const int WS_EX_TOPMOST = 0x00000008;
120 
121 		internal const int SW_SHOW = 5;
122 
123 		internal const int GCLP_HICON = -14;
124 		internal const int GCLP_HICONSM = -34;
125 
126 		internal const int ICON_SMALL = 0;
127 		internal const int ICON_BIG = 1;
128 		internal const int ICON_SMALL2 = 2;
129 
130 		internal const int EM_GETCHARFORMAT = WM_USER + 58;
131 		internal const int EM_SETCHARFORMAT = WM_USER + 68;
132 
133 		internal const int ES_WANTRETURN = 0x1000;
134 
135 		internal const int SCF_SELECTION = 0x0001;
136 
137 		internal const uint CFM_BOLD = 0x00000001;
138 		internal const uint CFM_ITALIC = 0x00000002;
139 		internal const uint CFM_UNDERLINE = 0x00000004;
140 		internal const uint CFM_STRIKEOUT = 0x00000008;
141 		internal const uint CFM_LINK = 0x00000020;
142 		internal const uint CFM_SIZE = 0x80000000;
143 
144 		internal const uint CFE_BOLD = 0x0001;
145 		internal const uint CFE_ITALIC = 0x0002;
146 		internal const uint CFE_UNDERLINE = 0x0004;
147 		internal const uint CFE_STRIKEOUT = 0x0008;
148 		internal const uint CFE_LINK = 0x0020;
149 
150 		internal const int SC_MINIMIZE = 0xF020;
151 		internal const int SC_MAXIMIZE = 0xF030;
152 
153 		internal const int IDANI_CAPTION = 3;
154 
155 		internal const int PBT_APMQUERYSUSPEND = 0x0000;
156 		internal const int PBT_APMSUSPEND = 0x0004;
157 
158 		internal const int ECM_FIRST = 0x1500;
159 		internal const int EM_SETCUEBANNER = ECM_FIRST + 1;
160 
161 		internal const uint INVALID_FILE_ATTRIBUTES = 0xFFFFFFFFU;
162 
163 		internal const uint FSCTL_LOCK_VOLUME = 589848;
164 		internal const uint FSCTL_UNLOCK_VOLUME = 589852;
165 
166 		// private const uint NM_FIRST = 0;
167 		// internal const uint NM_RCLICK = unchecked(NM_FIRST - 5);
168 
169 		internal const int LVM_FIRST = 0x1000;
170 		// internal const int LVM_ENSUREVISIBLE = LVM_FIRST + 19;
171 		internal const int LVM_SCROLL = LVM_FIRST + 20;
172 		internal const int LVM_GETTOOLTIPS = LVM_FIRST + 78;
173 		// internal const int LVM_SETGROUPINFO = LVM_FIRST + 147; // >= Vista
174 		// internal const int LVM_GETGROUPINFOBYINDEX = LVM_FIRST + 153; // >= Vista
175 
176 		internal const int TV_FIRST = 0x1100;
177 		internal const int TVM_GETTOOLTIPS = TV_FIRST + 25;
178 		internal const int TVM_SETEXTENDEDSTYLE = TV_FIRST + 44;
179 
180 		internal const int TTM_SETDELAYTIME = WM_USER + 3;
181 		internal const int TTM_GETDELAYTIME = WM_USER + 21;
182 		internal const int TTDT_RESHOW = 1;
183 		internal const int TTDT_AUTOPOP = 2;
184 		internal const int TTDT_INITIAL = 3;
185 
186 		internal const int WM_MOUSEACTIVATE = 0x21;
187 		internal const int MA_ACTIVATE = 1;
188 		internal const int MA_ACTIVATEANDEAT = 2;
189 		internal const int MA_NOACTIVATE = 3;
190 		internal const int MA_NOACTIVATEANDEAT = 4;
191 
192 		internal const int BCM_SETSHIELD = 0x160C;
193 
194 		internal const int SHCNE_ASSOCCHANGED = 0x08000000;
195 		internal const uint SHCNF_IDLIST = 0x0000;
196 
197 		// internal const int SHCNRF_SHELLLEVEL = 0x0002;
198 
199 		// internal const uint SW_INVALIDATE = 0x0002;
200 
201 		internal const uint TVS_FULLROWSELECT = 0x1000;
202 		internal const uint TVS_NONEVENHEIGHT = 0x4000;
203 		internal const uint TVS_EX_DOUBLEBUFFER = 0x0004;
204 		internal const uint TVS_EX_FADEINOUTEXPANDOS = 0x0040;
205 
206 		internal const int HDI_FORMAT = 0x0004;
207 		internal const int HDF_SORTUP = 0x0400;
208 		internal const int HDF_SORTDOWN = 0x0200;
209 		internal const int LVM_GETHEADER = LVM_FIRST + 31;
210 		internal const int HDM_FIRST = 0x1200;
211 		internal const int HDM_GETITEMA = HDM_FIRST + 3;
212 		internal const int HDM_GETITEMW = HDM_FIRST + 11;
213 		internal const int HDM_SETITEMA = HDM_FIRST + 4;
214 		internal const int HDM_SETITEMW = HDM_FIRST + 12;
215 
216 		// internal const int OFN_DONTADDTORECENT = 0x02000000;
217 
218 		internal const uint SHGFI_DISPLAYNAME = 0x000000200;
219 		internal const uint SHGFI_ICON = 0x000000100;
220 		internal const uint SHGFI_SMALLICON = 0x000000001;
221 		// internal const uint SHGFI_TYPENAME = 0x000000400;
222 
223 		internal const uint MOD_ALT = 1;
224 		internal const uint MOD_CONTROL = 2;
225 		internal const uint MOD_SHIFT = 4;
226 		internal const uint MOD_WIN = 8;
227 
228 		internal const int IDHOT_SNAPDESKTOP = -2;
229 		internal const int IDHOT_SNAPWINDOW = -1;
230 
231 		internal const uint GHND = 0x0042;
232 		internal const uint GMEM_MOVEABLE = 0x0002;
233 		internal const uint GMEM_ZEROINIT = 0x0040;
234 
235 		internal const uint CF_TEXT = 1;
236 		internal const uint CF_UNICODETEXT = 13;
237 
238 		internal const uint SND_ASYNC = 0x0001;
239 		internal const uint SND_FILENAME = 0x00020000;
240 		internal const uint SND_NODEFAULT = 0x0002;
241 
242 		internal const int LOGPIXELSX = 88;
243 		internal const int LOGPIXELSY = 90;
244 
245 		// internal const int SM_CXSMICON = 49;
246 		// internal const int SM_CYSMICON = 50;
247 
248 		// internal const uint PROCESS_QUERY_INFORMATION = 0x0400;
249 
250 		internal const uint ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID = 0x04;
251 
252 		internal const int INFOTIPSIZE = 1024;
253 
254 		// Value directly in NOTIFYICONDATA definition, no name;
255 		// the tip size is 128 when NTDDI_VERSION >= NTDDI_WIN2K,
256 		// but in the NotifyIcon.cs file of the .NET Framework,
257 		// 63 is defined as inclusive upper bound
258 		internal const int NOTIFYICONDATA_TIP_SIZE = 64;
259 
260 		// Value directly in NOTIFYICONDATA definition, no name
261 		internal const int NOTIFYICONDATA_INFO_SIZE = 256;
262 
263 		// internal const uint DI_NORMAL = 0x0003;
264 
265 		// internal const int LVN_FIRST = -100;
266 		// internal const int LVN_LINKCLICK = LVN_FIRST - 84;
267 
268 		// internal const uint LVGF_NONE = 0x00000000;
269 		// internal const uint LVGF_HEADER = 0x00000001;
270 		// internal const uint LVGF_FOOTER = 0x00000002;
271 		// internal const uint LVGF_STATE = 0x00000004;
272 		// internal const uint LVGF_ALIGN = 0x00000008;
273 		// internal const uint LVGF_GROUPID = 0x00000010;
274 		// internal const uint LVGF_SUBTITLE = 0x00000100;
275 		// internal const uint LVGF_TASK = 0x00000200;
276 		// internal const uint LVGF_DESCRIPTIONTOP = 0x00000400;
277 		// internal const uint LVGF_DESCRIPTIONBOTTOM = 0x00000800;
278 		// internal const uint LVGF_TITLEIMAGE = 0x00001000;
279 		// internal const uint LVGF_EXTENDEDIMAGE = 0x00002000;
280 		// internal const uint LVGF_ITEMS = 0x00004000;
281 		// internal const uint LVGF_SUBSET = 0x00008000;
282 		// internal const uint LVGF_SUBSETITEMS = 0x00010000;
283 
284 		// internal const uint LVGS_NORMAL = 0x00000000;
285 		// internal const uint LVGS_COLLAPSED = 0x00000001;
286 		// internal const uint LVGS_HIDDEN = 0x00000002;
287 		// internal const uint LVGS_NOHEADER = 0x00000004;
288 		// internal const uint LVGS_COLLAPSIBLE = 0x00000008;
289 		// internal const uint LVGS_FOCUSED = 0x00000010;
290 		// internal const uint LVGS_SELECTED = 0x00000020;
291 		// internal const uint LVGS_SUBSETED = 0x00000040;
292 		// internal const uint LVGS_SUBSETLINKFOCUSED = 0x00000080;
293 
294 		// private const int TTN_FIRST = -520;
295 		// internal const int TTN_NEEDTEXTA = TTN_FIRST;
296 		// internal const int TTN_NEEDTEXTW = TTN_FIRST - 10;
297 
298 		internal const uint WDA_NONE = 0x00000000;
299 		internal const uint WDA_MONITOR = 0x00000001;
300 
301 		internal const uint SPI_GETSCREENREADER = 0x0046;
302 
303 		[return: MarshalAs(UnmanagedType.Bool)]
EnumWindowsProc(IntPtr hWnd, IntPtr lParam)304 		internal delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
305 
306 		internal enum ComboBoxButtonState : uint
307 		{
308 			STATE_SYSTEM_NONE = 0,
309 			STATE_SYSTEM_INVISIBLE = 0x00008000,
310 			STATE_SYSTEM_PRESSED = 0x00000008
311 		}
312 
313 		[Flags]
314 		internal enum DesktopFlags : uint
315 		{
316 			ReadObjects = 0x0001,
317 			CreateWindow = 0x0002,
318 			CreateMenu = 0x0004,
319 			HookControl = 0x0008,
320 			JournalRecord = 0x0010,
321 			JournalPlayback = 0x0020,
322 			Enumerate = 0x0040,
323 			WriteObjects = 0x0080,
324 			SwitchDesktop = 0x0100,
325 		}
326 
327 		[Flags]
328 		internal enum EFileAccess : uint
329 		{
330 			GenericRead = 0x80000000,
331 			GenericWrite = 0x40000000,
332 			GenericExecute = 0x20000000,
333 			GenericAll = 0x10000000
334 		}
335 
336 		[Flags]
337 		internal enum EFileShare : uint
338 		{
339 			None = 0x00000000,
340 			Read = 0x00000001,
341 			Write = 0x00000002,
342 			Delete = 0x00000004
343 		}
344 
345 		internal enum ECreationDisposition : uint
346 		{
347 			CreateNew = 1,
348 			CreateAlways = 2,
349 			OpenExisting = 3,
350 			OpenAlways = 4,
351 			TruncateExisting = 5
352 		}
353 
354 		private enum ScrollBarDirection : int
355 		{
356 			SB_HORZ = 0,
357 			SB_VERT = 1,
358 			SB_CTL = 2,
359 			SB_BOTH = 3
360 		}
361 
362 		private enum ScrollInfoMask : uint
363 		{
364 			SIF_RANGE = 0x1,
365 			SIF_PAGE = 0x2,
366 			SIF_POS = 0x4,
367 			SIF_DISABLENOSCROLL = 0x8,
368 			SIF_TRACKPOS = 0x10,
369 			SIF_ALL = (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS)
370 		}
371 
372 		[Flags]
373 		internal enum MessageBoxFlags : uint
374 		{
375 			// Buttons
376 			MB_ABORTRETRYIGNORE = 0x00000002,
377 			MB_CANCELTRYCONTINUE = 0x00000006,
378 			MB_HELP = 0x00004000,
379 			MB_OK = 0,
380 			MB_OKCANCEL = 0x00000001,
381 			MB_RETRYCANCEL = 0x00000005,
382 			MB_YESNO = 0x00000004,
383 			MB_YESNOCANCEL = 0x00000003,
384 
385 			// Icons
386 			MB_ICONEXCLAMATION = 0x00000030,
387 			MB_ICONWARNING = 0x00000030,
388 			MB_ICONINFORMATION = 0x00000040,
389 			MB_ICONASTERISK = 0x00000040,
390 			MB_ICONQUESTION = 0x00000020,
391 			MB_ICONSTOP = 0x00000010,
392 			MB_ICONERROR = 0x00000010,
393 			MB_ICONHAND = 0x00000010,
394 
395 			// Default buttons
396 			MB_DEFBUTTON1 = 0,
397 			MB_DEFBUTTON2 = 0x00000100,
398 			MB_DEFBUTTON3 = 0x00000200,
399 			MB_DEFBUTTON4 = 0x00000300,
400 
401 			// Modality
402 			MB_APPLMODAL = 0,
403 			MB_SYSTEMMODAL = 0x00001000,
404 			MB_TASKMODAL = 0x00002000,
405 
406 			// Other options
407 			MB_DEFAULT_DESKTOP_ONLY = 0x00020000,
408 			MB_RIGHT = 0x00080000,
409 			MB_RTLREADING = 0x00100000,
410 			MB_SETFOREGROUND = 0x00010000,
411 			MB_TOPMOST = 0x00040000,
412 			MB_SERVICE_NOTIFICATION = 0x00200000
413 		}
414 
415 		// See DialogResult
416 		/* internal enum CommandID
417 		{
418 			None = 0,
419 			OK = 1, // IDOK
420 			Cancel = 2, // IDCANCEL
421 			Abort = 3, // IDABORT
422 			Retry = 4, // IDRETRY
423 			Ignore = 5, // IDIGNORE
424 			Yes = 6, // IDYES
425 			No = 7, // IDNO
426 			Close = 8, // IDCLOSE
427 			Help = 9, // IDHELP
428 			TryAgain = 10, // IDTRYAGAIN
429 			Continue = 11, // IDCONTINUE
430 			TimeOut = 32000 // IDTIMEOUT
431 		} */
432 
433 		[Flags]
434 		internal enum ToolHelpFlags : uint
435 		{
436 			SnapHeapList = 0x00000001,
437 			SnapProcess = 0x00000002,
438 			SnapThread = 0x00000004,
439 			SnapModule = 0x00000008,
440 			SnapModule32 = 0x00000010,
441 
442 			SnapAll = (SnapHeapList | SnapProcess | SnapThread | SnapModule),
443 
444 			Inherit = 0x80000000U
445 		}
446 
447 		// https://msdn.microsoft.com/en-us/library/windows/desktop/aa380337.aspx
448 		[Flags]
449 		internal enum STGM : uint
450 		{
451 			Read = 0x00000000,
452 			Write = 0x00000001,
453 			ReadWrite = 0x00000002,
454 
455 			ShareDenyNone = 0x00000040,
456 			ShareDenyRead = 0x00000030,
457 			ShareDenyWrite = 0x00000020,
458 			ShareExclusive = 0x00000010
459 		}
460 
461 		internal enum ProcessDpiAwareness : uint
462 		{
463 			Unaware = 0,
464 			SystemAware = 1,
465 			PerMonitorAware = 2
466 		}
467 	}
468 }
469