-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWinAPI.cs
899 lines (807 loc) · 40.5 KB
/
WinAPI.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace Dargon {
//Pretty much everything copied from http://www.pinvoke.net
public static class WinAPI {
public delegate bool CallBackPtr(int hwnd, int lParam);
/// <summary>
/// Callback should return true to have enumwindows return the hwnd to the given window.
/// </summary>
/// <param name="callPtr"></param>
/// <param name="lPar"></param>
/// <returns></returns>
[DllImport("user32.dll")]
public static extern int EnumWindows(CallBackPtr callPtr, int lPar);
public delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
public static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool SetWindowText(IntPtr hwnd, String lpString);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsWindow(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
public static Rectangle GetWindowRect(IntPtr hWnd) {
RECT rect = new RECT();
GetWindowRect(hWnd, ref rect);
return new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
}
[StructLayout(LayoutKind.Sequential)]
private struct RECT {
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}
[DllImport("user32.dll")]
public static extern short GetAsyncKeyState(Keys vKey);
[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern IntPtr SetFocus(IntPtr hwnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool FlashWindowEx(ref FLASHWINFO pwfi);
[StructLayout(LayoutKind.Sequential)]
public struct FLASHWINFO {
public UInt32 cbSize;
public IntPtr hwnd;
public UInt32 dwFlags;
public UInt32 uCount;
public UInt32 dwTimeout;
}
//Stop flashing. The system restores the window to its original state.
public const UInt32 FLASHW_STOP = 0;
//Flash the window caption.
public const UInt32 FLASHW_CAPTION = 1;
//Flash the taskbar button.
public const UInt32 FLASHW_TRAY = 2;
//Flash both the window caption and taskbar button.
//This is equivalent to setting the FLASHW_CAPTION | FLASHW_TRAY flags.
public const UInt32 FLASHW_ALL = 3;
//Flash continuously, until the FLASHW_STOP flag is set.
public const UInt32 FLASHW_TIMER = 4;
//Flash continuously until the window comes to the foreground.
public const UInt32 FLASHW_TIMERNOFG = 12;
/// <summary>
/// The MoveWindow function changes the position and dimensions of the specified window. For a top-level window, the position and dimensions are relative to the upper-left corner of the screen. For a child window, they are relative to the upper-left corner of the m_parent window's client area.
/// </summary>
/// <param name="hWnd">Handle to the window.</param>
/// <param name="X">Specifies the new position of the left side of the window.</param>
/// <param name="Y">Specifies the new position of the top of the window.</param>
/// <param name="nWidth">Specifies the new width of the window.</param>
/// <param name="nHeight">Specifies the new height of the window.</param>
/// <param name="bRepaint">Specifies whether the window is to be repainted. If this parameter is TRUE, the window receives a message. If the parameter is FALSE, no repainting of any kind occurs. This applies to the client area, the nonclient area (including the title bar and scroll bars), and any part of the m_parent window uncovered as a result of moving a child window.</param>
/// <returns>If the function succeeds, the return value is nonzero.
/// <para>If the function fails, the return value is zero. To get extended error information, call GetLastError.</para></returns>
[DllImport("user32.dll", SetLastError = true)]
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
//Extensions by me
public static string GetWindowTextAsString(IntPtr hWnd) {
StringBuilder sb = new StringBuilder(1024);
GetWindowText(hWnd, sb, 1024);
return sb.ToString();
}
public static bool KeyPressed(Keys key) { return GetAsyncKeyState(key) > 0; }
[DllImport("user32.dll", SetLastError = true)]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
public const int GWL_ID = (-12);
public const int GWL_STYLE = (-16);
public const int GWL_EXSTYLE = (-20);
// Window Styles
public const int WS_OVERLAPPED = 0;
//public const int WS_POPUP = 0x80000000;
public const int WS_CHILD = 0x40000000;
public const int WS_MINIMIZE = 0x20000000;
public const int WS_VISIBLE = 0x10000000;
public const int WS_DISABLED = 0x8000000;
public const int WS_CLIPSIBLINGS = 0x4000000;
public const int WS_CLIPCHILDREN = 0x2000000;
public const int WS_MAXIMIZE = 0x1000000;
public const int WS_CAPTION = 0xC00000; // WS_BORDER or WS_DLGFRAME
public const int WS_BORDER = 0x800000;
public const int WS_DLGFRAME = 0x400000;
public const int WS_VSCROLL = 0x200000;
public const int WS_HSCROLL = 0x100000;
public const int WS_SYSMENU = 0x80000;
public const int WS_THICKFRAME = 0x40000;
public const int WS_GROUP = 0x20000;
public const int WS_TABSTOP = 0x10000;
public const int WS_MINIMIZEBOX = 0x20000;
public const int WS_MAXIMIZEBOX = 0x10000;
public const int WS_TILED = WS_OVERLAPPED;
public const int WS_ICONIC = WS_MINIMIZE;
public const int WS_SIZEBOX = WS_THICKFRAME;
// Extended Window Styles
public const int WS_EX_DLGMODALFRAME = 0x0001;
public const int WS_EX_NOPARENTNOTIFY = 0x0004;
public const int WS_EX_TOPMOST = 0x0008;
public const int WS_EX_ACCEPTFILES = 0x0010;
public const int WS_EX_TRANSPARENT = 0x0020;
public const int WS_EX_MDICHILD = 0x0040;
public const int WS_EX_TOOLWINDOW = 0x0080;
public const int WS_EX_WINDOWEDGE = 0x0100;
public const int WS_EX_CLIENTEDGE = 0x0200;
public const int WS_EX_CONTEXTHELP = 0x0400;
public const int WS_EX_RIGHT = 0x1000;
public const int WS_EX_LEFT = 0x0000;
public const int WS_EX_RTLREADING = 0x2000;
public const int WS_EX_LTRREADING = 0x0000;
public const int WS_EX_LEFTSCROLLBAR = 0x4000;
public const int WS_EX_RIGHTSCROLLBAR = 0x0000;
public const int WS_EX_CONTROLPARENT = 0x10000;
public const int WS_EX_STATICEDGE = 0x20000;
public const int WS_EX_APPWINDOW = 0x40000;
public const int WS_EX_OVERLAPPEDWINDOW = (WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
public const int WS_EX_PALETTEWINDOW = (WS_EX_WINDOWEDGE | WS_EX_TOOLWINDOW | WS_EX_TOPMOST);
public const int WS_EX_LAYERED = 0x00080000;
public const int WS_EX_NOINHERITLAYOUT = 0x00100000; // Disable inheritence of mirroring by m_children
public const int WS_EX_LAYOUTRTL = 0x00400000; // Right to left mirroring
public const int WS_EX_COMPOSITED = 0x02000000;
public const int WS_EX_NOACTIVATE = 0x08000000;
[DllImport("user32.dll", SetLastError = false)]
public static extern IntPtr GetDesktopWindow();
[DllImport("kernel32.dll", ExactSpelling = true)]
public static extern IntPtr GetConsoleWindow();
private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
private static readonly IntPtr HWND_TOP = new IntPtr(0);
private static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
/// <summary>
/// Window handles (HWND) used for hWndInsertAfter
/// </summary>
public static class HWND {
public static IntPtr
NoTopMost = new IntPtr(-2),
TopMost = new IntPtr(-1),
Top = new IntPtr(0),
Bottom = new IntPtr(1);
}
/// <summary>
/// SetWindowPos Flags
/// </summary>
public static class SWP {
public static readonly int
NOSIZE = 0x0001,
NOMOVE = 0x0002,
NOZORDER = 0x0004,
NOREDRAW = 0x0008,
NOACTIVATE = 0x0010,
DRAWFRAME = 0x0020,
FRAMECHANGED = 0x0020,
SHOWWINDOW = 0x0040,
HIDEWINDOW = 0x0080,
NOCOPYBITS = 0x0100,
NOOWNERZORDER = 0x0200,
NOREPOSITION = 0x0200,
NOSENDCHANGING = 0x0400,
DEFERERASE = 0x2000,
ASYNCWINDOWPOS = 0x4000;
}
/// <summary>
/// Special window handles
/// </summary>
public enum SpecialWindowHandles {
// ReSharper disable InconsistentNaming
/// <summary>
/// Places the window at the bottom of the Z order. If the hWnd parameter identifies a topmost window, the window loses its topmost status and is placed at the bottom of all other windows.
/// </summary>
HWND_TOP = 0,
/// <summary>
/// Places the window above all non-topmost windows (that is, behind all topmost windows). This flag has no effect if the window is already a non-topmost window.
/// </summary>
HWND_BOTTOM = 1,
/// <summary>
/// Places the window at the top of the Z order.
/// </summary>
HWND_TOPMOST = -1,
/// <summary>
/// Places the window above all non-topmost windows. The window maintains its topmost position even when it is deactivated.
/// </summary>
HWND_NOTOPMOST = -2
// ReSharper restore InconsistentNaming
}
[Flags]
public enum SetWindowPosFlags : uint {
// ReSharper disable InconsistentNaming
/// <summary>
/// If the calling thread and the thread that owns the window are attached to different input queues, the system posts the request to the thread that owns the window. This prevents the calling thread from blocking its execution while other threads process the request.
/// </summary>
SWP_ASYNCWINDOWPOS = 0x4000,
/// <summary>
/// Prevents generation of the WM_SYNCPAINT message.
/// </summary>
SWP_DEFERERASE = 0x2000,
/// <summary>
/// Draws a frame (defined in the window's class description) around the window.
/// </summary>
SWP_DRAWFRAME = 0x0020,
/// <summary>
/// Applies new frame styles set using the SetWindowLong function. Sends a WM_NCCALCSIZE message to the window, even if the window's size is not being changed. If this flag is not specified, WM_NCCALCSIZE is sent only when the window's size is being changed.
/// </summary>
SWP_FRAMECHANGED = 0x0020,
/// <summary>
/// Hides the window.
/// </summary>
SWP_HIDEWINDOW = 0x0080,
/// <summary>
/// Does not activate the window. If this flag is not set, the window is activated and moved to the top of either the topmost or non-topmost group (depending on the setting of the hWndInsertAfter parameter).
/// </summary>
SWP_NOACTIVATE = 0x0010,
/// <summary>
/// Discards the entire contents of the client area. If this flag is not specified, the valid contents of the client area are saved and copied back into the client area after the window is sized or repositioned.
/// </summary>
SWP_NOCOPYBITS = 0x0100,
/// <summary>
/// Retains the current position (ignores X and Y parameters).
/// </summary>
SWP_NOMOVE = 0x0002,
/// <summary>
/// Does not change the owner window's position in the Z order.
/// </summary>
SWP_NOOWNERZORDER = 0x0200,
/// <summary>
/// Does not redraw changes. If this flag is set, no repainting of any kind occurs. This applies to the client area, the nonclient area (including the title bar and scroll bars), and any part of the m_parent window uncovered as a result of the window being moved. When this flag is set, the application must explicitly invalidate or redraw any parts of the window and m_parent window that need redrawing.
/// </summary>
SWP_NOREDRAW = 0x0008,
/// <summary>
/// Same as the SWP_NOOWNERZORDER flag.
/// </summary>
SWP_NOREPOSITION = 0x0200,
/// <summary>
/// Prevents the window from receiving the WM_WINDOWPOSCHANGING message.
/// </summary>
SWP_NOSENDCHANGING = 0x0400,
/// <summary>
/// Retains the current size (ignores the cx and cy parameters).
/// </summary>
SWP_NOSIZE = 0x0001,
/// <summary>
/// Retains the current Z order (ignores the hWndInsertAfter parameter).
/// </summary>
SWP_NOZORDER = 0x0004,
/// <summary>
/// Displays the window.
/// </summary>
SWP_SHOWWINDOW = 0x0040,
// ReSharper restore InconsistentNaming
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags);
[DllImport("kernel32.dll", SetLastError = true)]
[PreserveSig]
public static extern uint GetModuleFileName
(
[In] IntPtr hModule,
[Out] StringBuilder lpFilename,
[In] [MarshalAs(UnmanagedType.U4)] int nSize
);
public static string GetModuleFileName(IntPtr hModule) {
StringBuilder fileName = new StringBuilder(255);
GetModuleFileName(IntPtr.Zero, fileName, fileName.Capacity);
return fileName.ToString();
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
public const UInt32 WM_ACTIVATE = 0x0006;
public const UInt32 WM_ACTIVATEAPP = 0x001C;
public const UInt32 WM_AFXFIRST = 0x0360;
public const UInt32 WM_AFXLAST = 0x037F;
public const UInt32 WM_APP = 0x8000;
public const UInt32 WM_ASKCBFORMATNAME = 0x030C;
public const UInt32 WM_CANCELJOURNAL = 0x004B;
public const UInt32 WM_CANCELMODE = 0x001F;
public const UInt32 WM_CAPTURECHANGED = 0x0215;
public const UInt32 WM_CHANGECBCHAIN = 0x030D;
public const UInt32 WM_CHANGEUISTATE = 0x0127;
public const UInt32 WM_CHAR = 0x0102;
public const UInt32 WM_CHARTOITEM = 0x002F;
public const UInt32 WM_CHILDACTIVATE = 0x0022;
public const UInt32 WM_CLEAR = 0x0303;
public const UInt32 WM_CLOSE = 0x0010;
public const UInt32 WM_COMMAND = 0x0111;
public const UInt32 WM_COMPACTING = 0x0041;
public const UInt32 WM_COMPAREITEM = 0x0039;
public const UInt32 WM_CONTEXTMENU = 0x007B;
public const UInt32 WM_COPY = 0x0301;
public const UInt32 WM_COPYDATA = 0x004A;
public const UInt32 WM_CREATE = 0x0001;
public const UInt32 WM_CTLCOLORBTN = 0x0135;
public const UInt32 WM_CTLCOLORDLG = 0x0136;
public const UInt32 WM_CTLCOLOREDIT = 0x0133;
public const UInt32 WM_CTLCOLORLISTBOX = 0x0134;
public const UInt32 WM_CTLCOLORMSGBOX = 0x0132;
public const UInt32 WM_CTLCOLORSCROLLBAR = 0x0137;
public const UInt32 WM_CTLCOLORSTATIC = 0x0138;
public const UInt32 WM_CUT = 0x0300;
public const UInt32 WM_DEADCHAR = 0x0103;
public const UInt32 WM_DELETEITEM = 0x002D;
public const UInt32 WM_DESTROY = 0x0002;
public const UInt32 WM_DESTROYCLIPBOARD = 0x0307;
public const UInt32 WM_DEVICECHANGE = 0x0219;
public const UInt32 WM_DEVMODECHANGE = 0x001B;
public const UInt32 WM_DISPLAYCHANGE = 0x007E;
public const UInt32 WM_DRAWCLIPBOARD = 0x0308;
public const UInt32 WM_DRAWITEM = 0x002B;
public const UInt32 WM_DROPFILES = 0x0233;
public const UInt32 WM_ENABLE = 0x000A;
public const UInt32 WM_ENDSESSION = 0x0016;
public const UInt32 WM_ENTERIDLE = 0x0121;
public const UInt32 WM_ENTERMENULOOP = 0x0211;
public const UInt32 WM_ENTERSIZEMOVE = 0x0231;
public const UInt32 WM_ERASEBKGND = 0x0014;
public const UInt32 WM_EXITMENULOOP = 0x0212;
public const UInt32 WM_EXITSIZEMOVE = 0x0232;
public const UInt32 WM_FONTCHANGE = 0x001D;
public const UInt32 WM_GETDLGCODE = 0x0087;
public const UInt32 WM_GETFONT = 0x0031;
public const UInt32 WM_GETHOTKEY = 0x0033;
public const UInt32 WM_GETICON = 0x007F;
public const UInt32 WM_GETMINMAXINFO = 0x0024;
public const UInt32 WM_GETOBJECT = 0x003D;
public const UInt32 WM_GETTEXT = 0x000D;
public const UInt32 WM_GETTEXTLENGTH = 0x000E;
public const UInt32 WM_HANDHELDFIRST = 0x0358;
public const UInt32 WM_HANDHELDLAST = 0x035F;
public const UInt32 WM_HELP = 0x0053;
public const UInt32 WM_HOTKEY = 0x0312;
public const UInt32 WM_HSCROLL = 0x0114;
public const UInt32 WM_HSCROLLCLIPBOARD = 0x030E;
public const UInt32 WM_ICONERASEBKGND = 0x0027;
public const UInt32 WM_IME_CHAR = 0x0286;
public const UInt32 WM_IME_COMPOSITION = 0x010F;
public const UInt32 WM_IME_COMPOSITIONFULL = 0x0284;
public const UInt32 WM_IME_CONTROL = 0x0283;
public const UInt32 WM_IME_ENDCOMPOSITION = 0x010E;
public const UInt32 WM_IME_KEYDOWN = 0x0290;
public const UInt32 WM_IME_KEYLAST = 0x010F;
public const UInt32 WM_IME_KEYUP = 0x0291;
public const UInt32 WM_IME_NOTIFY = 0x0282;
public const UInt32 WM_IME_REQUEST = 0x0288;
public const UInt32 WM_IME_SELECT = 0x0285;
public const UInt32 WM_IME_SETCONTEXT = 0x0281;
public const UInt32 WM_IME_STARTCOMPOSITION = 0x010D;
public const UInt32 WM_INITDIALOG = 0x0110;
public const UInt32 WM_INITMENU = 0x0116;
public const UInt32 WM_INITMENUPOPUP = 0x0117;
public const UInt32 WM_INPUTLANGCHANGE = 0x0051;
public const UInt32 WM_INPUTLANGCHANGEREQUEST = 0x0050;
public const UInt32 WM_KEYDOWN = 0x0100;
public const UInt32 WM_KEYFIRST = 0x0100;
public const UInt32 WM_KEYLAST = 0x0108;
public const UInt32 WM_KEYUP = 0x0101;
public const UInt32 WM_KILLFOCUS = 0x0008;
public const UInt32 WM_LBUTTONDBLCLK = 0x0203;
public const UInt32 WM_LBUTTONDOWN = 0x0201;
public const UInt32 WM_LBUTTONUP = 0x0202;
public const UInt32 WM_MBUTTONDBLCLK = 0x0209;
public const UInt32 WM_MBUTTONDOWN = 0x0207;
public const UInt32 WM_MBUTTONUP = 0x0208;
public const UInt32 WM_MDIACTIVATE = 0x0222;
public const UInt32 WM_MDICASCADE = 0x0227;
public const UInt32 WM_MDICREATE = 0x0220;
public const UInt32 WM_MDIDESTROY = 0x0221;
public const UInt32 WM_MDIGETACTIVE = 0x0229;
public const UInt32 WM_MDIICONARRANGE = 0x0228;
public const UInt32 WM_MDIMAXIMIZE = 0x0225;
public const UInt32 WM_MDINEXT = 0x0224;
public const UInt32 WM_MDIREFRESHMENU = 0x0234;
public const UInt32 WM_MDIRESTORE = 0x0223;
public const UInt32 WM_MDISETMENU = 0x0230;
public const UInt32 WM_MDITILE = 0x0226;
public const UInt32 WM_MEASUREITEM = 0x002C;
public const UInt32 WM_MENUCHAR = 0x0120;
public const UInt32 WM_MENUCOMMAND = 0x0126;
public const UInt32 WM_MENUDRAG = 0x0123;
public const UInt32 WM_MENUGETOBJECT = 0x0124;
public const UInt32 WM_MENURBUTTONUP = 0x0122;
public const UInt32 WM_MENUSELECT = 0x011F;
public const UInt32 WM_MOUSEACTIVATE = 0x0021;
public const UInt32 WM_MOUSEFIRST = 0x0200;
public const UInt32 WM_MOUSEHOVER = 0x02A1;
public const UInt32 WM_MOUSELAST = 0x020D;
public const UInt32 WM_MOUSELEAVE = 0x02A3;
public const UInt32 WM_MOUSEMOVE = 0x0200;
public const UInt32 WM_MOUSEWHEEL = 0x020A;
public const UInt32 WM_MOUSEHWHEEL = 0x020E;
public const UInt32 WM_MOVE = 0x0003;
public const UInt32 WM_MOVING = 0x0216;
public const UInt32 WM_NCACTIVATE = 0x0086;
public const UInt32 WM_NCCALCSIZE = 0x0083;
public const UInt32 WM_NCCREATE = 0x0081;
public const UInt32 WM_NCDESTROY = 0x0082;
public const UInt32 WM_NCHITTEST = 0x0084;
public const UInt32 WM_NCLBUTTONDBLCLK = 0x00A3;
public const UInt32 WM_NCLBUTTONDOWN = 0x00A1;
public const UInt32 WM_NCLBUTTONUP = 0x00A2;
public const UInt32 WM_NCMBUTTONDBLCLK = 0x00A9;
public const UInt32 WM_NCMBUTTONDOWN = 0x00A7;
public const UInt32 WM_NCMBUTTONUP = 0x00A8;
public const UInt32 WM_NCMOUSEMOVE = 0x00A0;
public const UInt32 WM_NCPAINT = 0x0085;
public const UInt32 WM_NCRBUTTONDBLCLK = 0x00A6;
public const UInt32 WM_NCRBUTTONDOWN = 0x00A4;
public const UInt32 WM_NCRBUTTONUP = 0x00A5;
public const UInt32 WM_NEXTDLGCTL = 0x0028;
public const UInt32 WM_NEXTMENU = 0x0213;
public const UInt32 WM_NOTIFY = 0x004E;
public const UInt32 WM_NOTIFYFORMAT = 0x0055;
public const UInt32 WM_NULL = 0x0000;
public const UInt32 WM_PAINT = 0x000F;
public const UInt32 WM_PAINTCLIPBOARD = 0x0309;
public const UInt32 WM_PAINTICON = 0x0026;
public const UInt32 WM_PALETTECHANGED = 0x0311;
public const UInt32 WM_PALETTEISCHANGING = 0x0310;
public const UInt32 WM_PARENTNOTIFY = 0x0210;
public const UInt32 WM_PASTE = 0x0302;
public const UInt32 WM_PENWINFIRST = 0x0380;
public const UInt32 WM_PENWINLAST = 0x038F;
public const UInt32 WM_POWER = 0x0048;
public const UInt32 WM_POWERBROADCAST = 0x0218;
public const UInt32 WM_PRINT = 0x0317;
public const UInt32 WM_PRINTCLIENT = 0x0318;
public const UInt32 WM_QUERYDRAGICON = 0x0037;
public const UInt32 WM_QUERYENDSESSION = 0x0011;
public const UInt32 WM_QUERYNEWPALETTE = 0x030F;
public const UInt32 WM_QUERYOPEN = 0x0013;
public const UInt32 WM_QUEUESYNC = 0x0023;
public const UInt32 WM_QUIT = 0x0012;
public const UInt32 WM_RBUTTONDBLCLK = 0x0206;
public const UInt32 WM_RBUTTONDOWN = 0x0204;
public const UInt32 WM_RBUTTONUP = 0x0205;
public const UInt32 WM_RENDERALLFORMATS = 0x0306;
public const UInt32 WM_RENDERFORMAT = 0x0305;
public const UInt32 WM_SETCURSOR = 0x0020;
public const UInt32 WM_SETFOCUS = 0x0007;
public const UInt32 WM_SETFONT = 0x0030;
public const UInt32 WM_SETHOTKEY = 0x0032;
public const UInt32 WM_SETICON = 0x0080;
public const UInt32 WM_SETREDRAW = 0x000B;
public const UInt32 WM_SETTEXT = 0x000C;
public const UInt32 WM_SETTINGCHANGE = 0x001A;
public const UInt32 WM_SHOWWINDOW = 0x0018;
public const UInt32 WM_SIZE = 0x0005;
public const UInt32 WM_SIZECLIPBOARD = 0x030B;
public const UInt32 WM_SIZING = 0x0214;
public const UInt32 WM_SPOOLERSTATUS = 0x002A;
public const UInt32 WM_STYLECHANGED = 0x007D;
public const UInt32 WM_STYLECHANGING = 0x007C;
public const UInt32 WM_SYNCPAINT = 0x0088;
public const UInt32 WM_SYSCHAR = 0x0106;
public const UInt32 WM_SYSCOLORCHANGE = 0x0015;
public const UInt32 WM_SYSCOMMAND = 0x0112;
public const UInt32 WM_SYSDEADCHAR = 0x0107;
public const UInt32 WM_SYSKEYDOWN = 0x0104;
public const UInt32 WM_SYSKEYUP = 0x0105;
public const UInt32 WM_TCARD = 0x0052;
public const UInt32 WM_TIMECHANGE = 0x001E;
public const UInt32 WM_TIMER = 0x0113;
public const UInt32 WM_UNDO = 0x0304;
public const UInt32 WM_UNINITMENUPOPUP = 0x0125;
public const UInt32 WM_USER = 0x0400;
public const UInt32 WM_USERCHANGED = 0x0054;
public const UInt32 WM_VKEYTOITEM = 0x002E;
public const UInt32 WM_VSCROLL = 0x0115;
public const UInt32 WM_VSCROLLCLIPBOARD = 0x030A;
public const UInt32 WM_WINDOWPOSCHANGED = 0x0047;
public const UInt32 WM_WINDOWPOSCHANGING = 0x0046;
public const UInt32 WM_WININICHANGE = 0x001A;
public const UInt32 WM_XBUTTONDBLCLK = 0x020D;
public const UInt32 WM_XBUTTONDOWN = 0x020B;
public const UInt32 WM_XBUTTONUP = 0x020C;
/// <summary>The GetForegroundWindow function returns a handle to the foreground window.</summary>
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetMenu(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool SetMenu(IntPtr hWnd, IntPtr hMenu);
[DllImport("user32.dll")]
public static extern bool InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
[DllImport("user32.dll")]
public static extern bool UpdateWindow(IntPtr hWnd);
/// <summary>Shows a Window</summary>
/// <remarks>
/// <para>To perform certain special effects when showing or hiding a
/// window, use AnimateWindow.</para>
///<para>The first time an application calls ShowWindow, it should use
///the WinMain function's nCmdShow parameter as its nCmdShow parameter.
///Subsequent calls to ShowWindow must use one of the values in the
///given list, instead of the one specified by the WinMain function's
///nCmdShow parameter.</para>
///<para>As noted in the discussion of the nCmdShow parameter, the
///nCmdShow value is ignored in the first call to ShowWindow if the
///program that launched the application specifies startup information
///in the structure. In this case, ShowWindow uses the information
///specified in the STARTUPINFO structure to show the window. On
///subsequent calls, the application must call ShowWindow with nCmdShow
///set to SW_SHOWDEFAULT to use the startup information provided by the
///program that launched the application. This behavior is designed for
///the following situations: </para>
///<list type="">
/// <item>Applications create their main window by calling CreateWindow
/// with the WS_VISIBLE flag set. </item>
/// <item>Applications create their main window by calling CreateWindow
/// with the WS_VISIBLE flag cleared, and later call ShowWindow with the
/// SW_SHOW flag set to make it visible.</item>
///</list></remarks>
/// <param name="hWnd">Handle to the window.</param>
/// <param name="nCmdShow">Specifies how the window is to be shown.
/// This parameter is ignored the first time an application calls
/// ShowWindow, if the program that launched the application provides a
/// STARTUPINFO structure. Otherwise, the first time ShowWindow is called,
/// the value should be the value obtained by the WinMain function in its
/// nCmdShow parameter. In subsequent calls, this parameter can be one of
/// the WindowShowStyle members.</param>
/// <returns>
/// If the window was previously visible, the return value is nonzero.
/// If the window was previously hidden, the return value is zero.
/// </returns>
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, WindowShowStyle nCmdShow);
/// <summary>Enumeration of the different ways of showing a window using
/// ShowWindow</summary>
public enum WindowShowStyle : uint {
/// <summary>Hides the window and activates another window.</summary>
/// <remarks>See SW_HIDE</remarks>
Hide = 0,
/// <summary>Activates and displays a window. If the window is minimized
/// or maximized, the system restores it to its original size and
/// position. An application should specify this flag when displaying
/// the window for the first time.</summary>
/// <remarks>See SW_SHOWNORMAL</remarks>
ShowNormal = 1,
/// <summary>Activates the window and displays it as a minimized window.</summary>
/// <remarks>See SW_SHOWMINIMIZED</remarks>
ShowMinimized = 2,
/// <summary>Activates the window and displays it as a maximized window.</summary>
/// <remarks>See SW_SHOWMAXIMIZED</remarks>
ShowMaximized = 3,
/// <summary>Maximizes the specified window.</summary>
/// <remarks>See SW_MAXIMIZE</remarks>
Maximize = 3,
/// <summary>Displays a window in its most recent size and position.
/// This value is similar to "ShowNormal", except the window is not
/// actived.</summary>
/// <remarks>See SW_SHOWNOACTIVATE</remarks>
ShowNormalNoActivate = 4,
/// <summary>Activates the window and displays it in its current size
/// and position.</summary>
/// <remarks>See SW_SHOW</remarks>
Show = 5,
/// <summary>Minimizes the specified window and activates the next
/// top-level window in the Z order.</summary>
/// <remarks>See SW_MINIMIZE</remarks>
Minimize = 6,
/// <summary>Displays the window as a minimized window. This value is
/// similar to "ShowMinimized", except the window is not activated.</summary>
/// <remarks>See SW_SHOWMINNOACTIVE</remarks>
ShowMinNoActivate = 7,
/// <summary>Displays the window in its current size and position. This
/// value is similar to "Show", except the window is not activated.</summary>
/// <remarks>See SW_SHOWNA</remarks>
ShowNoActivate = 8,
/// <summary>Activates and displays the window. If the window is
/// minimized or maximized, the system restores it to its original size
/// and position. An application should specify this flag when restoring
/// a minimized window.</summary>
/// <remarks>See SW_RESTORE</remarks>
Restore = 9,
/// <summary>Sets the show state based on the SW_ value specified in the
/// STARTUPINFO structure passed to the CreateProcess function by the
/// program that started the application.</summary>
/// <remarks>See SW_SHOWDEFAULT</remarks>
ShowDefault = 10,
/// <summary>Windows 2000/XP: Minimizes a window, even if the thread
/// that owns the window is hung. This flag should only be used when
/// minimizing windows from a different thread.</summary>
/// <remarks>See SW_FORCEMINIMIZE</remarks>
ForceMinimized = 11
}
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetFileInformationByHandle(IntPtr hFile, out BY_HANDLE_FILE_INFORMATION lpFileInformation);
[StructLayout(LayoutKind.Sequential)]
public struct BY_HANDLE_FILE_INFORMATION {
public uint FileAttributes;
public FILETIME CreationTime;
public FILETIME LastAccessTime;
public FILETIME LastWriteTime;
public uint VolumeSerialNumber;
public uint FileSizeHigh;
public uint FileSizeLow;
public uint NumberOfLinks;
public uint FileIndexHigh;
public uint FileIndexLow;
}
/// <summary>
/// Reserves or commits a region of memory within the virtual address space of a specified
/// process. The function initializes the memory it allocates to zero, unless MEM_RESET is
/// used.
/// </summary>
/// <param name="hProcess">Handle to the process</param>
/// <param name="lpAddress">
/// The pointer that specifies a desired starting address for the region of pages that you
/// want to allocate.
/// If you are reserving memory, the function rounds this address down to the nearest
/// multiple of the allocation granularity.
/// If you are committing memory that is already reserved, the function rounds this address
/// down to the nearest page boundary. To determine the size of a page and the allocation
/// granularity on the host computer, use the GetSystemInfo function.
/// If lpAddress is NULL, the function determines where to allocate the region.
/// </param>
/// <param name="dwSize">
/// The size of the region of memory to allocate, in bytes.
/// If lpAddress is NULL, the function rounds dwSize up to the next page boundary.
/// If lpAddress is not NULL, the function allocates all pages that contain one or more
/// bytes in the range from lpAddress to lpAddress+dwSize. This means, for example,
/// that a 2-byte range that straddles a page boundary causes the function to allocate both
/// pages
/// </param>
/// <param name="flAllocationType">The type of memory allocation. </param>
/// <param name="flProtect">
/// The memory protection for the region of pages to be allocated.
/// If the pages are being committed, you can specify any one of the
/// </param>
/// <returns></returns>
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
public static extern IntPtr VirtualAllocEx(
IntPtr hProcess,
IntPtr lpAddress,
uint dwSize,
AllocationType flAllocationType,
MemoryProtection flProtect
);
/// <summary>
/// Releases, decommits, or releases and decommits a region of memory within the
/// virtual address space of a specified process.
/// </summary>
/// <param name="hProcess">Handle to the process whose VM we are freeing</param>
/// <param name="lpAddress">Pointer to starting address of the target memory region</param>
/// <param name="dwSize">Size of the memory region to free, in bytes</param>
/// <param name="dwFreeType">The type of free operation.</param>
/// <returns></returns>
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
public static extern bool VirtualFreeEx(
IntPtr hProcess,
IntPtr lpAddress,
int dwSize,
FreeType dwFreeType
);
/// <summary>
/// HANDLE WINAPI CreateRemoteThread(
/// __in HANDLE hProcess,
/// __in LPSECURITY_ATTRIBUTES lpThreadAttributes,
/// __in SIZE_T dwStackSize,
/// __in LPTHREAD_START_ROUTINE lpStartAddress,
/// __in LPVOID lpParameter,
/// __in DWORD dwCreationFlags,
/// __out LPDWORD lpThreadId
/// );
/// </summary>
/// <param name="hProcess">Handle to the given process</param>
/// <param name="lpThreadAttributes">null</param>
/// <param name="dwStackSize">0 for default</param>
/// <param name="lpStartAddress">Pointer on other process for ThreadStart</param>
/// <param name="lpParameter">
/// Parameter passed to the invoked method.
/// Since we're calling LoadLibraryA, we pass the pointer to the library name.
/// </param>
/// <param name="dwCreationFlags">Zero so the thread starts immediately</param>
/// <param name="lpThreadId">Out variable for the created thread's ID</param>
[DllImport("kernel32")]
public static extern IntPtr CreateRemoteThread(
IntPtr hProcess,
IntPtr lpThreadAttributes,
uint dwStackSize,
UIntPtr lpStartAddress,
IntPtr lpParameter,
uint dwCreationFlags,
out uint lpThreadId
);
/// <summary>
/// Gets a handle to the process of the given process id.
/// This is necessary because you cannot use Process.Handle:
/// "Only processes started through a call to BeginAggregate set the
/// Handle property of the corresponding Process instances."
/// </summary>
/// <param name="dwDesiredProcessAccess"></param>
/// <param name="bInheritHandle">A BOOL - True if this caller process should inherit the handle, false otherwise</param>
/// <param name="dwProcessId">PID of the process which we are opening a handle to</param>
/// <returns></returns>
[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(
ProcessAccessRights dwDesiredProcessAccess,
Int32 bInheritHandle,
Int32 dwProcessId
);
/// <summary>
/// Closes a handle
/// </summary>
/// <param name="hObject">The handle to close</param>
/// <returns>successful</returns>
[DllImport("kernel32.dll", SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseHandle(
IntPtr hObject
);
/// <summary>
/// Gets the address of an exported procedure in the given module.
/// </summary>
/// <param name="hModule">Handle to a loaded module</param>
/// <param name="procName">Name of exported procedure whose address we are finding</param>
/// <returns>Pointer to the procedure or NULL</returns>
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
public static extern UIntPtr GetProcAddress(
IntPtr hModule,
string procName
);
/// <summary>
/// Writes data to an area of memory in a specified process.
/// The entire area to be written to must be accessible or the operation fails.
/// </summary>
/// <param name="hProcess">
/// A handle to the process memory to be modified.
/// The handle must have PROCESS_VM_WRITE and PROCESS_VM_OPERATION access to the process.
/// </param>
/// <param name="lpBaseAddress">
/// A pointer to the base address in the specified process to which data is written.
/// Before data transfer occurs, the system verifies that all data in the base address
/// and memory of the specified size is accessible for write access, and if it is not
/// accessible, the function fails.
/// </param>
/// <param name="lpBuffer">
/// A pointer to the buffer that contains data to be written in the address space
/// of the specified process.
/// </param>
/// <param name="nSize">The number of bytes to be written to the specified process.</param>
/// <param name="lpNumberOfBytesWritten">
/// A pointer to a variable that receives the number of bytes transferred into the
/// specified process. This parameter is optional. If lpNumberOfBytesWritten is NULL, the
/// parameter is ignored.
/// </param>
/// <returns></returns>
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool WriteProcessMemory(
IntPtr hProcess,
IntPtr lpBaseAddress,
byte[] lpBuffer,
uint nSize,
out int lpNumberOfBytesWritten //FIXME: not sure if this is right
);
/// <summary>
/// Gets the handle of a loaded module
/// </summary>
/// <param name="lpModuleName">The name of the module (ex: Kernel32.dll)</param>
/// <returns>Handle to the module or NULL</returns>
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetModuleHandle(
string lpModuleName
);
/// <summary>
/// In the case of a process or thread, this waits for the object to complete (ex: reach end of execution)
/// </summary>
/// <param name="handle"></param>
/// <param name="milliseconds"></param>
/// <returns></returns>
[DllImport("kernel32", SetLastError = true, ExactSpelling = true)]
public static extern WaitForSingleObjectResult WaitForSingleObject(
IntPtr handle,
Int32 milliseconds
);
}
}