This repository has been archived by the owner on Aug 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Cmr.Win32.cs
2372 lines (2248 loc) · 84.3 KB
/
Cmr.Win32.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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace Chrones
{
public static partial class Cmr
{
public static partial class Win32
{
#region ConsoleFunctions
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
// http://pinvoke.net/default.aspx/kernel32/AddConsoleAlias.html
[DllImport("kernel32", SetLastError = true)]
public static extern bool AddConsoleAlias(
string Source,
string Target,
string ExeName
);
// http://pinvoke.net/default.aspx/kernel32/AllocConsole.html
[DllImport("kernel32", SetLastError = true)]
public static extern bool AllocConsole();
// http://pinvoke.net/default.aspx/kernel32/AttachConsole.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool AttachConsole(
uint dwProcessId
);
// http://pinvoke.net/default.aspx/kernel32/CreateConsoleScreenBuffer.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr CreateConsoleScreenBuffer(
uint dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
uint dwFlags,
IntPtr lpScreenBufferData
);
// http://pinvoke.net/default.aspx/kernel32/FillConsoleOutputAttribute.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool FillConsoleOutputAttribute(
IntPtr hConsoleOutput,
ushort wAttribute,
uint nLength,
COORD dwWriteCoord,
out uint lpNumberOfAttrsWritten
);
// http://pinvoke.net/default.aspx/kernel32/FillConsoleOutputCharacter.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool FillConsoleOutputCharacter(
IntPtr hConsoleOutput,
char cCharacter,
uint nLength,
COORD dwWriteCoord,
out uint lpNumberOfCharsWritten
);
// http://pinvoke.net/default.aspx/kernel32/FlushConsoleInputBuffer.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool FlushConsoleInputBuffer(
IntPtr hConsoleInput
);
// http://pinvoke.net/default.aspx/kernel32/FreeConsole.html
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
public static extern bool FreeConsole();
// http://pinvoke.net/default.aspx/kernel32/GenerateConsoleCtrlEvent.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GenerateConsoleCtrlEvent(
uint dwCtrlEvent,
uint dwProcessGroupId
);
// http://pinvoke.net/default.aspx/kernel32/GetConsoleAlias.html
[DllImport("kernel32", SetLastError = true)]
public static extern bool GetConsoleAlias(
string Source,
out StringBuilder TargetBuffer,
uint TargetBufferLength,
string ExeName
);
// http://pinvoke.net/default.aspx/kernel32/GetConsoleAliases.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern uint GetConsoleAliases(
StringBuilder[] lpTargetBuffer,
uint targetBufferLength,
string lpExeName
);
// http://pinvoke.net/default.aspx/kernel32/GetConsoleAliasesLength.html
[DllImport("kernel32", SetLastError = true)]
public static extern uint GetConsoleAliasesLength(
string ExeName
);
// http://pinvoke.net/default.aspx/kernel32/GetConsoleAliasExes.html
[DllImport("kernel32", SetLastError = true)]
public static extern uint GetConsoleAliasExes(
out StringBuilder ExeNameBuffer,
uint ExeNameBufferLength
);
// http://pinvoke.net/default.aspx/kernel32/GetConsoleAliasExesLength.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern uint GetConsoleAliasExesLength();
// http://pinvoke.net/default.aspx/kernel32/GetConsoleCP.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern uint GetConsoleCP();
// http://pinvoke.net/default.aspx/kernel32/GetConsoleCursorInfo.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetConsoleCursorInfo(
IntPtr hConsoleOutput,
out CONSOLE_CURSOR_INFO lpConsoleCursorInfo
);
// http://pinvoke.net/default.aspx/kernel32/GetConsoleDisplayMode.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetConsoleDisplayMode(
out uint ModeFlags
);
// http://pinvoke.net/default.aspx/kernel32/GetConsoleFontSize.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern COORD GetConsoleFontSize(
IntPtr hConsoleOutput,
Int32 nFont
);
// http://pinvoke.net/default.aspx/kernel32/GetConsoleHistoryInfo.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetConsoleHistoryInfo(
out CONSOLE_HISTORY_INFO ConsoleHistoryInfo
);
// http://pinvoke.net/default.aspx/kernel32/GetConsoleMode.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetConsoleMode(
IntPtr hConsoleHandle,
out uint lpMode
);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetConsoleMode(
IntPtr hConsoleHandle,
out int lpMode
);
// http://pinvoke.net/default.aspx/kernel32/GetConsoleOriginalTitle.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern uint GetConsoleOriginalTitle(
out StringBuilder ConsoleTitle,
uint Size
);
// http://pinvoke.net/default.aspx/kernel32/GetConsoleOutputCP.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern uint GetConsoleOutputCP();
// http://pinvoke.net/default.aspx/kernel32/GetConsoleProcessList.html
// TODO: Test - what's an out uint[] during interop? This probably isn't quite right, but provides a starting point:
[DllImport("kernel32.dll", SetLastError = true)]
public static extern uint GetConsoleProcessList(
out uint[] ProcessList,
uint ProcessCount
);
// http://pinvoke.net/default.aspx/kernel32/GetConsoleScreenBufferInfo.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetConsoleScreenBufferInfo(
IntPtr hConsoleOutput,
out CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo
);
// http://pinvoke.net/default.aspx/kernel32/GetConsoleScreenBufferInfoEx.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetConsoleScreenBufferInfoEx(
IntPtr hConsoleOutput,
ref CONSOLE_SCREEN_BUFFER_INFO_EX ConsoleScreenBufferInfo
);
// http://pinvoke.net/default.aspx/kernel32/GetConsoleSelectionInfo.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetConsoleSelectionInfo(
CONSOLE_SELECTION_INFO ConsoleSelectionInfo
);
// http://pinvoke.net/default.aspx/kernel32/GetConsoleTitle.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern uint GetConsoleTitle(
[Out] StringBuilder lpConsoleTitle,
uint nSize
);
// http://pinvoke.net/default.aspx/kernel32/GetConsoleWindow.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GetConsoleWindow();
// http://pinvoke.net/default.aspx/kernel32/GetCurrentConsoleFont.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetCurrentConsoleFont(
IntPtr hConsoleOutput,
bool bMaximumWindow,
out CONSOLE_FONT_INFO lpConsoleCurrentFont
);
// http://pinvoke.net/default.aspx/kernel32/GetCurrentConsoleFontEx.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetCurrentConsoleFontEx(
IntPtr ConsoleOutput,
bool MaximumWindow,
out CONSOLE_FONT_INFO_EX ConsoleCurrentFont
);
// http://pinvoke.net/default.aspx/kernel32/GetLargestConsoleWindowSize.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern COORD GetLargestConsoleWindowSize(
IntPtr hConsoleOutput
);
// http://pinvoke.net/default.aspx/kernel32/GetNumberOfConsoleInputEvents.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetNumberOfConsoleInputEvents(
IntPtr hConsoleInput,
out uint lpcNumberOfEvents
);
// http://pinvoke.net/default.aspx/kernel32/GetNumberOfConsoleMouseButtons.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetNumberOfConsoleMouseButtons(
ref uint lpNumberOfMouseButtons
);
// http://pinvoke.net/default.aspx/kernel32/GetStdHandle.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GetStdHandle(
int nStdHandle
);
// http://pinvoke.net/default.aspx/kernel32/HandlerRoutine.html
// Delegate type to be used as the Handler Routine for SCCH
public delegate bool ConsoleCtrlDelegate(CtrlTypes CtrlType);
// http://pinvoke.net/default.aspx/kernel32/PeekConsoleInput.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool PeekConsoleInput(
IntPtr hConsoleInput,
[Out] INPUT_RECORD[] lpBuffer,
uint nLength,
out uint lpNumberOfEventsRead
);
// http://pinvoke.net/default.aspx/kernel32/ReadConsole.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool ReadConsole(
IntPtr hConsoleInput,
[Out] StringBuilder lpBuffer,
uint nNumberOfCharsToRead,
out uint lpNumberOfCharsRead,
IntPtr lpReserved
);
// http://pinvoke.net/default.aspx/kernel32/ReadConsoleInput.html
[DllImport("kernel32.dll", EntryPoint = "ReadConsoleInputW", CharSet = CharSet.Unicode)]
public static extern bool ReadConsoleInput(
IntPtr hConsoleInput,
[Out] INPUT_RECORD[] lpBuffer,
uint nLength,
out uint lpNumberOfEventsRead
);
// http://pinvoke.net/default.aspx/kernel32/ReadConsoleOutput.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool ReadConsoleOutput(
IntPtr hConsoleOutput,
[Out] CHAR_INFO[] lpBuffer,
COORD dwBufferSize,
COORD dwBufferCoord,
ref SMALL_RECT lpReadRegion
);
// http://pinvoke.net/default.aspx/kernel32/ReadConsoleOutputAttribute.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool ReadConsoleOutputAttribute(
IntPtr hConsoleOutput,
[Out] ushort[] lpAttribute,
uint nLength,
COORD dwReadCoord,
out uint lpNumberOfAttrsRead
);
// http://pinvoke.net/default.aspx/kernel32/ReadConsoleOutputCharacter.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool ReadConsoleOutputCharacter(
IntPtr hConsoleOutput,
[Out] StringBuilder lpCharacter,
uint nLength,
COORD dwReadCoord,
out uint lpNumberOfCharsRead
);
// http://pinvoke.net/default.aspx/kernel32/ScrollConsoleScreenBuffer.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool ScrollConsoleScreenBuffer(
IntPtr hConsoleOutput,
[In] ref SMALL_RECT lpScrollRectangle,
IntPtr lpClipRectangle,
COORD dwDestinationOrigin,
[In] ref CHAR_INFO lpFill
);
// http://pinvoke.net/default.aspx/kernel32/SetConsoleActiveScreenBuffer.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetConsoleActiveScreenBuffer(
IntPtr hConsoleOutput
);
// http://pinvoke.net/default.aspx/kernel32/SetConsoleCP.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetConsoleCP(
uint wCodePageID
);
// http://pinvoke.net/default.aspx/kernel32/SetConsoleCtrlHandler.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetConsoleCtrlHandler(
ConsoleCtrlDelegate HandlerRoutine,
bool Add
);
// http://pinvoke.net/default.aspx/kernel32/SetConsoleCursorInfo.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetConsoleCursorInfo(
IntPtr hConsoleOutput,
[In] ref CONSOLE_CURSOR_INFO lpConsoleCursorInfo
);
// http://pinvoke.net/default.aspx/kernel32/SetConsoleCursorPosition.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetConsoleCursorPosition(
IntPtr hConsoleOutput,
COORD dwCursorPosition
);
// http://pinvoke.net/default.aspx/kernel32/SetConsoleDisplayMode.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetConsoleDisplayMode(
IntPtr ConsoleOutput,
uint Flags,
out COORD NewScreenBufferDimensions
);
// http://pinvoke.net/default.aspx/kernel32/SetConsoleHistoryInfo.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetConsoleHistoryInfo(
CONSOLE_HISTORY_INFO ConsoleHistoryInfo
);
// http://pinvoke.net/default.aspx/kernel32/SetConsoleMode.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetConsoleMode(
IntPtr hConsoleHandle,
uint dwMode
);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetConsoleMode(
IntPtr hConsoleHandle,
int dwMode
);
// http://pinvoke.net/default.aspx/kernel32/SetConsoleOutputCP.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetConsoleOutputCP(
uint wCodePageID
);
// http://pinvoke.net/default.aspx/kernel32/SetConsoleScreenBufferInfoEx.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetConsoleScreenBufferInfoEx(
IntPtr ConsoleOutput,
CONSOLE_SCREEN_BUFFER_INFO_EX ConsoleScreenBufferInfoEx
);
// http://pinvoke.net/default.aspx/kernel32/SetConsoleScreenBufferSize.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetConsoleScreenBufferSize(
IntPtr hConsoleOutput,
COORD dwSize
);
// http://pinvoke.net/default.aspx/kernel32/SetConsoleTextAttribute.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetConsoleTextAttribute(
IntPtr hConsoleOutput,
ushort wAttributes
);
// http://pinvoke.net/default.aspx/kernel32/SetConsoleTitle.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetConsoleTitle(
string lpConsoleTitle
);
// http://pinvoke.net/default.aspx/kernel32/SetConsoleWindowInfo.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetConsoleWindowInfo(
IntPtr hConsoleOutput,
bool bAbsolute,
[In] ref SMALL_RECT lpConsoleWindow
);
// http://pinvoke.net/default.aspx/kernel32/SetCurrentConsoleFontEx.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetCurrentConsoleFontEx(
IntPtr ConsoleOutput,
bool MaximumWindow,
ref CONSOLE_FONT_INFO_EX ConsoleCurrentFontEx
);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetCurrentConsoleFontEx(
IntPtr ConsoleOutput,
bool MaximumWindow,
CONSOLE_FONT_INFO_EX ConsoleCurrentFontEx
);
// http://pinvoke.net/default.aspx/kernel32/SetStdHandle.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetStdHandle(
uint nStdHandle,
IntPtr hHandle
);
// http://pinvoke.net/default.aspx/kernel32/WriteConsole.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool WriteConsole(
IntPtr hConsoleOutput,
string lpBuffer,
uint nNumberOfCharsToWrite,
out uint lpNumberOfCharsWritten,
IntPtr lpReserved
);
// http://pinvoke.net/default.aspx/kernel32/WriteConsoleInput.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool WriteConsoleInput(
IntPtr hConsoleInput,
INPUT_RECORD[] lpBuffer,
uint nLength,
out uint lpNumberOfEventsWritten
);
// http://pinvoke.net/default.aspx/kernel32/WriteConsoleOutput.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool WriteConsoleOutput(
IntPtr hConsoleOutput,
CHAR_INFO[] lpBuffer,
COORD dwBufferSize,
COORD dwBufferCoord,
ref SMALL_RECT lpWriteRegion
);
// http://pinvoke.net/default.aspx/kernel32/WriteConsoleOutputAttribute.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool WriteConsoleOutputAttribute(
IntPtr hConsoleOutput,
ushort[] lpAttribute,
uint nLength,
COORD dwWriteCoord,
out uint lpNumberOfAttrsWritten
);
// http://pinvoke.net/default.aspx/kernel32/WriteConsoleOutputCharacter.html
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool WriteConsoleOutputCharacter(
IntPtr hConsoleOutput,
string lpCharacter,
uint nLength,
COORD dwWriteCoord,
out uint lpNumberOfCharsWritten
);
[StructLayout(LayoutKind.Sequential)]
public struct COORD
{
internal short X;
internal short Y;
internal COORD(short x, short y)
{
X = x;
Y = y;
}
}
public struct SMALL_RECT
{
public short Left;
public short Top;
public short Right;
public short Bottom;
}
public struct CONSOLE_SCREEN_BUFFER_INFO
{
public COORD dwSize;
public COORD dwCursorPosition;
public short wAttributes;
public SMALL_RECT srWindow;
public COORD dwMaximumWindowSize;
}
[StructLayout(LayoutKind.Sequential)]
public struct CONSOLE_SCREEN_BUFFER_INFO_EX
{
public uint cbSize;
public COORD dwSize;
public COORD dwCursorPosition;
public short wAttributes;
public SMALL_RECT srWindow;
public COORD dwMaximumWindowSize;
public ushort wPopupAttributes;
public bool bFullscreenSupported;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public COLORREF[] ColorTable;
public static CONSOLE_SCREEN_BUFFER_INFO_EX Create()
{
return new CONSOLE_SCREEN_BUFFER_INFO_EX { cbSize = 96 };
}
}
//[StructLayout(LayoutKind.Sequential)]
//struct COLORREF
//{
// public byte R;
// public byte G;
// public byte B;
//}
[StructLayout(LayoutKind.Sequential)]
public struct COLORREF
{
public uint ColorDWORD;
public COLORREF(System.Drawing.Color color)
{
ColorDWORD = (uint)color.R + (((uint)color.G) << 8) + (((uint)color.B) << 16);
}
public System.Drawing.Color GetColor()
{
return System.Drawing.Color.FromArgb((int)(0x000000FFU & ColorDWORD),
(int)(0x0000FF00U & ColorDWORD) >> 8, (int)(0x00FF0000U & ColorDWORD) >> 16);
}
public void SetColor(System.Drawing.Color color)
{
ColorDWORD = (uint)color.R + (((uint)color.G) << 8) + (((uint)color.B) << 16);
}
}
[StructLayout(LayoutKind.Sequential)]
public struct CONSOLE_FONT_INFO
{
public int nFont;
public COORD dwFontSize;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public unsafe struct CONSOLE_FONT_INFO_EX
{
internal uint cbSize;
internal uint nFont;
internal COORD dwFontSize;
internal int FontFamily;
internal int FontWeight;
internal fixed char FaceName[32];
}
[StructLayout(LayoutKind.Explicit)]
public struct INPUT_RECORD
{
[FieldOffset(0)]
public ushort EventType;
[FieldOffset(4)]
public KEY_EVENT_RECORD KeyEvent;
[FieldOffset(4)]
public MOUSE_EVENT_RECORD MouseEvent;
[FieldOffset(4)]
public WINDOW_BUFFER_SIZE_RECORD WindowBufferSizeEvent;
[FieldOffset(4)]
public MENU_EVENT_RECORD MenuEvent;
[FieldOffset(4)]
public FOCUS_EVENT_RECORD FocusEvent;
};
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode)]
public struct KEY_EVENT_RECORD
{
[FieldOffset(0), MarshalAs(UnmanagedType.Bool)]
public bool bKeyDown;
[FieldOffset(4), MarshalAs(UnmanagedType.U2)]
public ushort wRepeatCount;
[FieldOffset(6), MarshalAs(UnmanagedType.U2)]
//public VirtualKeys wVirtualKeyCode;
public ushort wVirtualKeyCode;
[FieldOffset(8), MarshalAs(UnmanagedType.U2)]
public ushort wVirtualScanCode;
[FieldOffset(10)]
public char UnicodeChar;
[FieldOffset(12), MarshalAs(UnmanagedType.U4)]
//public ControlKeyState dwControlKeyState;
public uint dwControlKeyState;
}
[StructLayout(LayoutKind.Sequential)]
public struct MOUSE_EVENT_RECORD
{
public COORD dwMousePosition;
public uint dwButtonState;
public uint dwControlKeyState;
public uint dwEventFlags;
}
public struct WINDOW_BUFFER_SIZE_RECORD
{
public COORD dwSize;
public WINDOW_BUFFER_SIZE_RECORD(short x, short y)
{
dwSize = new COORD();
dwSize.X = x;
dwSize.Y = y;
}
}
[StructLayout(LayoutKind.Sequential)]
public struct MENU_EVENT_RECORD
{
public uint dwCommandId;
}
[StructLayout(LayoutKind.Sequential)]
public struct FOCUS_EVENT_RECORD
{
public uint bSetFocus;
}
//CHAR_INFO struct, which was a union in the old days
// so we want to use LayoutKind.Explicit to mimic it as closely
// as we can
[StructLayout(LayoutKind.Explicit)]
public struct CHAR_INFO
{
[FieldOffset(0)]
char UnicodeChar;
[FieldOffset(0)]
char AsciiChar;
[FieldOffset(2)] //2 bytes seems to work properly
UInt16 Attributes;
}
[StructLayout(LayoutKind.Sequential)]
public struct CONSOLE_CURSOR_INFO
{
uint Size;
bool Visible;
}
[StructLayout(LayoutKind.Sequential)]
public struct CONSOLE_HISTORY_INFO
{
ushort cbSize;
ushort HistoryBufferSize;
ushort NumberOfHistoryBuffers;
uint dwFlags;
}
[StructLayout(LayoutKind.Sequential)]
public struct CONSOLE_SELECTION_INFO
{
uint Flags;
COORD SelectionAnchor;
SMALL_RECT Selection;
// Flags values:
const uint CONSOLE_MOUSE_DOWN = 0x0008; // Mouse is down
const uint CONSOLE_MOUSE_SELECTION = 0x0004; //Selecting with the mouse
const uint CONSOLE_NO_SELECTION = 0x0000; //No selection
const uint CONSOLE_SELECTION_IN_PROGRESS = 0x0001; //Selection has begun
const uint CONSOLE_SELECTION_NOT_EMPTY = 0x0002; //Selection rectangle is not empty
}
// Enumerated type for the control messages sent to the handler routine
public enum CtrlTypes : uint
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT,
CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT
}
#endregion
#region User32.dll
[DllImport("user32.dll", SetLastError = true)]
public static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool GetWindowRect(IntPtr hwnd, out SimpleRECT lpRect);
[DllImport("user32.dll")]
public static extern int GetSystemMetrics(SystemMetric smIndex);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
[DllImport("user32")]
public static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi);
[DllImport("User32")]
public static extern IntPtr MonitorFromWindow(IntPtr handle, int flags);
[DllImport("User32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int Width, int Height, bool Repaint);
[DllImport("user32.dll", SetLastError = true)]
public static extern short GetAsyncKeyState(int key);
[DllImport("user32.dll", SetLastError = true)]
public static extern short GetAsyncKeyState(short key);
[DllImport("user32.dll")]
public static extern short GetAsyncKeyState(VirtualKeys vKey);
[DllImport("user32.dll")]
public static extern short GetAsyncKeyState(VK vKey);
[DllImport("user32.dll", SetLastError = true)]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
public static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowThreadProcessId(IntPtr handle, out int processId);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs, int cbSize);
[DllImport("User32.dll")]
public static extern Int32 SendMessage(int hWnd, int Msg, int wParam, IntPtr lParam);
[DllImport("user32.dll")]
public static extern void mouse_event(uint dwFlags, int dx, int dy, uint dwData, int dwExtraInfo);
[DllImport("user32.dll")]
public static extern void mouse_event(MouseEventFlags dwFlags, int dx, int dy, uint dwData, int dwExtraInfo);
[DllImport("user32")]
public static extern int SetCursorPos(int x, int y);
#region Structs and Enums
[Flags]
public enum MouseEventFlags : uint
{
LEFTDOWN = 0x00000002,
LEFTUP = 0x00000004,
MIDDLEDOWN = 0x00000020,
MIDDLEUP = 0x00000040,
MOVE = 0x00000001,
ABSOLUTE = 0x00008000,
RIGHTDOWN = 0x00000008,
RIGHTUP = 0x00000010,
WHEEL = 0x00000800,
XDOWN = 0x00000080,
XUP = 0x00000100
}
public enum MouseEventDataXButtons : uint
{
XBUTTON1 = 0x00000001,
XBUTTON2 = 0x00000002
}
[StructLayout(LayoutKind.Sequential)]
public struct INPUT
{
internal uint Type;
internal InputUnion U;
internal static int Size
{
get { return Marshal.SizeOf(typeof(INPUT)); }
}
}
[StructLayout(LayoutKind.Explicit)]
public struct InputUnion
{
[FieldOffset(0)]
internal MOUSEINPUT MouseInput;
[FieldOffset(0)]
internal KEYBDINPUT KeyboardInput;
[FieldOffset(0)]
internal HARDWAREINPUT HardwareInput;
}
[StructLayout(LayoutKind.Sequential)]
internal struct HARDWAREINPUT
{
internal int uMsg;
internal short wParamL;
internal short wParamH;
}
[StructLayout(LayoutKind.Sequential)]
public struct MOUSEINPUT
{
internal int dx;
internal int dy;
internal int mouseData;
internal MOUSEEVENTF dwFlags;
internal uint time;
internal UIntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
internal struct KEYBDINPUT
{
internal VirtualKeys wVk;
internal ScanCodeShort wScan;
internal KEYEVENTF dwFlags;
internal int time;
internal UIntPtr dwExtraInfo;
}
public enum ScanCodeShort : short
{
LBUTTON = 0,
RBUTTON = 0,
CANCEL = 70,
MBUTTON = 0,
XBUTTON1 = 0,
XBUTTON2 = 0,
BACK = 14,
TAB = 15,
CLEAR = 76,
RETURN = 28,
SHIFT = 42,
CONTROL = 29,
MENU = 56,
PAUSE = 0,
CAPITAL = 58,
KANA = 0,
HANGUL = 0,
JUNJA = 0,
FINAL = 0,
HANJA = 0,
KANJI = 0,
ESCAPE = 1,
CONVERT = 0,
NONCONVERT = 0,
ACCEPT = 0,
MODECHANGE = 0,
SPACE = 57,
PRIOR = 73,
NEXT = 81,
END = 79,
HOME = 71,
LEFT = 75,
UP = 72,
RIGHT = 77,
DOWN = 80,
SELECT = 0,
PRINT = 0,
EXECUTE = 0,
SNAPSHOT = 84,
INSERT = 82,
DELETE = 83,
HELP = 99,
KEY_0 = 11,
KEY_1 = 2,
KEY_2 = 3,
KEY_3 = 4,
KEY_4 = 5,
KEY_5 = 6,
KEY_6 = 7,
KEY_7 = 8,
KEY_8 = 9,
KEY_9 = 10,
KEY_A = 30,
KEY_B = 48,
KEY_C = 46,
KEY_D = 32,
KEY_E = 18,
KEY_F = 33,
KEY_G = 34,
KEY_H = 35,
KEY_I = 23,
KEY_J = 36,
KEY_K = 37,
KEY_L = 38,
KEY_M = 50,
KEY_N = 49,
KEY_O = 24,
KEY_P = 25,
KEY_Q = 16,
KEY_R = 19,
KEY_S = 31,
KEY_T = 20,
KEY_U = 22,
KEY_V = 47,
KEY_W = 17,
KEY_X = 45,
KEY_Y = 21,
KEY_Z = 44,
LWIN = 91,
RWIN = 92,
APPS = 93,
SLEEP = 95,
NUMPAD0 = 82,
NUMPAD1 = 79,
NUMPAD2 = 80,
NUMPAD3 = 81,
NUMPAD4 = 75,
NUMPAD5 = 76,
NUMPAD6 = 77,
NUMPAD7 = 71,
NUMPAD8 = 72,
NUMPAD9 = 73,
MULTIPLY = 55,
ADD = 78,
SEPARATOR = 0,
SUBTRACT = 74,
DECIMAL = 83,
DIVIDE = 53,
F1 = 59,
F2 = 60,
F3 = 61,
F4 = 62,
F5 = 63,
F6 = 64,
F7 = 65,
F8 = 66,
F9 = 67,
F10 = 68,
F11 = 87,
F12 = 88,
F13 = 100,
F14 = 101,
F15 = 102,
F16 = 103,
F17 = 104,
F18 = 105,
F19 = 106,
F20 = 107,
F21 = 108,
F22 = 109,
F23 = 110,
F24 = 118,
NUMLOCK = 69,
SCROLL = 70,
LSHIFT = 42,
RSHIFT = 54,
LCONTROL = 29,
RCONTROL = 29,
LMENU = 56,
RMENU = 56,
BROWSER_BACK = 106,
BROWSER_FORWARD = 105,
BROWSER_REFRESH = 103,
BROWSER_STOP = 104,
BROWSER_SEARCH = 101,
BROWSER_FAVORITES = 102,
BROWSER_HOME = 50,
VOLUME_MUTE = 32,
VOLUME_DOWN = 46,
VOLUME_UP = 48,
MEDIA_NEXT_TRACK = 25,
MEDIA_PREV_TRACK = 16,
MEDIA_STOP = 36,
MEDIA_PLAY_PAUSE = 34,
LAUNCH_MAIL = 108,
LAUNCH_MEDIA_SELECT = 109,
LAUNCH_APP1 = 107,
LAUNCH_APP2 = 33,
OEM_1 = 39,
OEM_PLUS = 13,
OEM_COMMA = 51,
OEM_MINUS = 12,
OEM_PERIOD = 52,
OEM_2 = 53,
OEM_3 = 41,
OEM_4 = 26,
OEM_5 = 43,
OEM_6 = 27,