-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMConsole.bas
1801 lines (1581 loc) · 68 KB
/
MConsole.bas
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
Attribute VB_Name = "Con"
' *************************************************************************
' Copyright ©1996-2010 Karl E. Peterson
' All Rights Reserved, http://vb.mvps.org/
' *************************************************************************
' You are free to use this code within your own applications, but you
' are expressly forbidden from selling or otherwise distributing this
' source code, non-compiled, without prior written consent.
' *************************************************************************
' Redistributed - with full permission - on http://www.vbadvance.com
' *************************************************************************
' Portions blatently "stolen" from Peter Young, author of vbAdvance (the
' tool I use to compile VB5/6 console applications), who contributed the
' notion of creating a lightweight COM object rather than use a full-blown
' class in order to allow for the callback handling within a single module.
' For a very cool tool, see: http://www.vbadvance.com
' *************************************************************************
' Release History.
' Version 1.00 - February 2004
' * Initial release with vbAdvance (v3.00).
' Version 1.01 - March 16, 2004
' * Added assignment to m_hWnd in Initialize, which allows all usage of
' that variable to actually work prior to explicit call to hWnd prop.
' * Changed Initialize to return ConsoleLaunchModes enum.
' * Added ParentProcessID r/o property.
' * Added ParentFileName r/o property.
' * Added LaunchType r/o property.
' * Added GetProcessParent private method.
' * Added GetProcessFileName private method.
' * Added FindConsole private method.
' * Added numerous declares to support new properties and methods!
' Version 1.02 - March 18, 2004
' * Added TaskVisible public property.
' Version 1.03 - June 8, 2006
' * Added FlashWindow public method.
' * Added ReadChar public method.
' * Added ReadPassword public method.
' Version 1.04 - February 17, 2010
' * Fixed GetProcessFileName to work with 64-bit images.
' * Added MapDeviceName private method.
' *************************************************************************
Option Explicit
' Console related Win32 API declarations
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Long) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Any) As Long
Private Declare Function ReadFileEx Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpOverlapped As Any, ByVal lpCompletionRoutine As Long) As Long
Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As Any) As Long
Private Declare Function WriteFileEx Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpOverlapped As Any, ByVal lpCompletionRoutine As Long) As Long
Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
Private Declare Function SetStdHandle Lib "kernel32" (ByVal nStdHandle As Long, ByVal nHandle As Long) As Long
Private Declare Function AllocConsole Lib "kernel32" () As Long
Private Declare Function FreeConsole Lib "kernel32" () As Long
Private Declare Function PeekConsoleInput Lib "kernel32" Alias "PeekConsoleInputA" (ByVal hConsoleInput As Long, ByRef lpBuffer As Any, ByVal nRecords As Long, ByRef lpNumberOfEventsRead As Long) As Long
Private Declare Function GetNumberOfConsoleInputEvents Lib "kernel32" (ByVal hConsoleInput As Long, ByRef lpNumberOfEvents As Long) As Long
Private Declare Function ReadConsole Lib "kernel32" Alias "ReadConsoleA" (ByVal hConsoleInput As Long, lpBuffer As Any, ByVal nNumberOfCharsToRead As Long, lpNumberOfCharsRead As Long, lpReserved As Any) As Long
Private Declare Function ReadConsoleOutput Lib "kernel32" Alias "ReadConsoleOutputA" (ByVal hConsoleOutput As Long, lpBuffer As CHAR_INFO, dwBufferSize As COORD, dwBufferCoord As COORD, lpReadRegion As SMALL_RECT) As Long
Private Declare Function ReadConsoleOutputAttribute Lib "kernel32" (ByVal hConsoleOutput As Long, lpAttribute As Long, ByVal nLength As Long, dwReadCoord As COORD, lpNumberOfAttrsRead As Long) As Long
Private Declare Function ReadConsoleOutputCharacter Lib "kernel32" Alias "ReadConsoleOutputCharacterA" (ByVal hConsoleOutput As Long, ByVal lpCharacter As String, ByVal nLength As Long, dwReadCoord As COORD, lpNumberOfCharsRead As Long) As Long
Private Declare Function WriteConsole Lib "kernel32" Alias "WriteConsoleA" (ByVal hConsoleOutput As Long, lpBuffer As Any, ByVal nNumberOfCharsToWrite As Long, lpNumberOfCharsWritten As Long, lpReserved As Any) As Long
Private Declare Function WriteConsoleOutput Lib "kernel32" Alias "WriteConsoleOutputA" (ByVal hConsoleOutput As Long, lpBuffer As CHAR_INFO, dwBufferSize As COORD, dwBufferCoord As COORD, lpWriteRegion As SMALL_RECT) As Long
Private Declare Function WriteConsoleOutputAttribute Lib "kernel32" (ByVal hConsoleOutput As Long, lpAttribute As Integer, ByVal nLength As Long, dwWriteCoord As COORD, lpNumberOfAttrsWritten As Long) As Long
Private Declare Function WriteConsoleOutputCharacter Lib "kernel32" Alias "WriteConsoleOutputCharacterA" (ByVal hConsoleOutput As Long, ByVal lpCharacter As String, ByVal nLength As Long, dwWriteCoord As COORD, lpNumberOfCharsWritten As Long) As Long
Private Declare Function FlushConsoleInputBuffer Lib "kernel32" (ByVal hConsoleInput As Long) As Long
Private Declare Function ReadConsoleInput Lib "kernel32" Alias "ReadConsoleInputA" (ByVal hConsoleInput As Long, lpBuffer As Any, ByVal nLength As Long, lpNumberOfEventsRead As Long) As Long
Private Declare Function WriteConsoleInput Lib "kernel32" Alias "WriteConsoleOutputA" (ByVal hConsoleInput As Long, ByVal lpBuffer As Any, ByVal nLength As Long, lpNumberOfEventsWritten As Long) As Long
Private Declare Function GetConsoleCursorInfo Lib "kernel32" (ByVal hConsoleOutput As Long, lpConsoleCursorInfo As CONSOLE_CURSOR_INFO) As Long
Private Declare Function GetConsoleMode Lib "kernel32" (ByVal hConsoleHandle As Long, lpMode As Long) As Long
Private Declare Function GetConsoleScreenBufferInfo Lib "kernel32" (ByVal hConsoleOutput As Long, lpConsoleScreenBufferInfo As CONSOLE_SCREEN_BUFFER_INFO) As Long
Private Declare Function GetConsoleTitle Lib "kernel32" Alias "GetConsoleTitleA" (ByVal lpConsoleTitle As String, ByVal nSize As Long) As Long
Private Declare Function SetConsoleActiveScreenBuffer Lib "kernel32" (ByVal hConsoleOutput As Long) As Long
Private Declare Function SetConsoleCtrlHandler Lib "kernel32" (ByVal HandlerRoutine As Long, ByVal Add As Long) As Long
Private Declare Function SetConsoleCursorInfo Lib "kernel32" (ByVal hConsoleOutput As Long, lpConsoleCursorInfo As CONSOLE_CURSOR_INFO) As Long
Private Declare Function SetConsoleCursorPosition Lib "kernel32" (ByVal hConsoleOutput As Long, dwCursorPosition As Any) As Long
Private Declare Function SetConsoleMode Lib "kernel32" (ByVal hConsoleHandle As Long, ByVal dwMode As Long) As Long
Private Declare Function SetConsoleScreenBufferSize Lib "kernel32" (ByVal hConsoleOutput As Long, dwSize As Any) As Long
Private Declare Function SetConsoleTextAttribute Lib "kernel32" (ByVal hConsoleOutput As Long, ByVal wAttributes As Long) As Long
Private Declare Function SetConsoleTitle Lib "kernel32" Alias "SetConsoleTitleA" (ByVal lpConsoleTitle As String) As Long
Private Declare Function SetConsoleWindowInfo Lib "kernel32" (ByVal hConsoleOutput As Long, ByVal bAbsolute As Long, lpConsoleWindow As SMALL_RECT) As Long
Private Declare Function GetConsoleCP Lib "kernel32" () As Long
Private Declare Function SetConsoleCP Lib "kernel32" (ByVal wCodePageID As Integer) As Long
Private Declare Function GetConsoleOutputCP Lib "kernel32" () As Long
Private Declare Function SetConsoleOutputCP Lib "kernel32" (ByVal wCodePageID As Integer) As Long
Private Declare Function GetConsoleDisplayMode Lib "kernel32" (lpModeFlags As Long) As Long
Private Declare Function SetConsoleDisplayMode Lib "kernel32" (ByVal hConsoleHandle As Long, ByVal dwConsoleDisplayMode As Long, dwPreviousDisplayMode As Long) As Long
Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Declare Function QueryDosDevice Lib "kernel32" Alias "QueryDosDeviceA" (ByVal lpDeviceName As String, ByVal lpTargetPath As String, ByVal ucchMax As Long) As Long
' PSAPI Declares (available only in NT4 and later!)...
' Although, this download *used* to be offered for NT 3.51 as well:
' http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=3D1FBAED-D122-45CF-9D46-1CAE384097AC
Private Declare Function GetModuleFileNameEx Lib "PSAPI" Alias "GetModuleFileNameExA" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFileName As String, nSize As Long) As Long
Private Declare Function GetProcessImageFileName Lib "PSAPI" Alias "GetProcessImageFileNameA" (ByVal hProcess As Long, ByVal lpImageFileName As String, ByVal nSize As Long) As Long
' Process info API declarations - NT4 and earlier - undocumented.
Private Declare Function NtQueryInformationProcess Lib "ntdll" (ByVal ProcessHandle As Long, ByVal ProcessInformationClass As SYSTEM_INFORMATION_CLASS, ByRef ProcessInformation As Any, ByVal lProcessInformationLength As Long, ByRef lReturnLength As Long) As Long
' Process info API declarations - 9x/NT5+
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, ByRef lppe As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, ByRef lppe As PROCESSENTRY32) As Long
' NT5 (Windows 2000) and later, only!
Private Declare Function GetConsoleWindow Lib "kernel32" () As Long
' Other Win32 API declarations.
Private Declare Function EnumThreadWindows Lib "user32" (ByVal dwThreadId As Long, ByVal lpfn As Long, ByVal lParam As Long) As Long
Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, ByVal revert As Long) As Long
Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Private Declare Function IsIconic Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function IsZoomed Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function IsWindowVisible Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function FlashWindowEx Lib "user32" (pfi As FLASHWINFO) As Long
Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As Any) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessID As Long) As Long
Private Declare Sub OutputDebugString Lib "kernel32" Alias "OutputDebugStringA" (ByVal lpOutputString As String)
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function SleepEx Lib "kernel32" (ByVal dwMilliseconds As Long, ByVal bAlertable As Long) As Long
Private Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)
' Used to determine if an API function is exported.
Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
' Maximum path length (without special handling) in NT
Private Const MAX_PATH As Long = 260&
' Uncover process information on 9x/NT5+.
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type
' Process information under NT4- (undoc'd)
Public Type PROCESS_BASIC_INFORMATION
ExitStatus As Long
PebBaseAddress As Long
AffinityMask As Long
BasePriority As Long
UniqueProcessId As Long
InheritedFromUniqueProcessId As Long ' ParentProcessID
End Type
' Process information types
Private Enum SYSTEM_INFORMATION_CLASS
SystemBasicInformation = 0
SystemPerformanceInformation = 2
SystemTimeOfDayInformation = 3
SystemProcessInformation = 5
SystemProcessorPerformanceInformation = 8
SystemInterruptInformation = 23
SystemExceptionInformation = 33
SystemRegistryQuotaInformation = 37
SystemLookasideInformation = 45
End Enum
' Used to find hidden controller window.
Private Const GWL_HWNDPARENT As Long = (-8)
Private Const GWL_STYLE As Long = (-16)
Private Const WS_SYSMENU As Long = &H80000
' Toolhelp constants.
Private Const TH32CS_SNAPPROCESS As Long = &H2&
' Used by the OpenProcess API call
Private Const PROCESS_ALL_ACCESS As Long = &H1F0FFF
Private Const PROCESS_QUERY_INFORMATION As Long = &H400
Private Const PROCESS_VM_READ As Long = &H10
' Some calls need to know OS
Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type
' Used with FlashWindowEx - Note Win98/2000+ only!
Private Type FLASHWINFO
cbSize As Long
hWnd As Long
dwFlags As Long
uCount As Long
dwTimeout As Long
End Type
' FlashWindow flag constants
Private Const FLASHW_STOP As Long = 0&
Private Const FLASHW_CAPTION As Long = 1&
Private Const FLASHW_TRAY As Long = 2&
Private Const FLASHW_ALL As Long = FLASHW_CAPTION Or FLASHW_TRAY
Private Const FLASHW_TIMER As Long = 4&
Private Const FLASHW_TIMERNOFG As Long = &HC&
' Platform ID constants
Private Const VER_PLATFORM_WIN32s As Long = &H0
Private Const VER_PLATFORM_WIN32_WINDOWS As Long = &H1
Private Const VER_PLATFORM_WIN32_NT As Long = &H2
' Standard I/O handle constants.
Private Const STD_ERROR_HANDLE As Long = -12&
Private Const STD_INPUT_HANDLE As Long = -10&
Private Const STD_OUTPUT_HANDLE As Long = -11&
' Used to understand console display mode.
Private Const CONSOLE_WINDOWED As Long = 0
Private Const CONSOLE_FULLSCREEN As Long = 1 ' fullscreen console
Private Const CONSOLE_FULLSCREEN_HARDWARE As Long = 2 ' console owns the hardware
' Input Mode flags:
Private Const ENABLE_PROCESSED_INPUT As Long = &H1&
Private Const ENABLE_LINE_INPUT As Long = &H2&
Private Const ENABLE_ECHO_INPUT As Long = &H4&
Private Const ENABLE_WINDOW_INPUT As Long = &H8&
Private Const ENABLE_MOUSE_INPUT As Long = &H10&
' Output Mode flags:
Private Const ENABLE_PROCESSED_OUTPUT As Long = &H1&
Private Const ENABLE_WRAP_AT_EOL_OUTPUT As Long = &H2&
' Attributes flags.
Private Const FOREGROUND_BLUE As Long = &H1& ' text color contains blue.
Private Const FOREGROUND_GREEN As Long = &H2& ' text color contains green.
Private Const FOREGROUND_RED As Long = &H4& ' text color contains red.
Private Const FOREGROUND_INTENSITY As Long = &H8& ' text color is intensified.
Private Const BACKGROUND_BLUE As Long = &H10& ' background color contains blue.
Private Const BACKGROUND_GREEN As Long = &H20& ' background color contains green.
Private Const BACKGROUND_RED As Long = &H40& ' background color contains red.
Private Const BACKGROUND_INTENSITY As Long = &H80& ' background color is intensified.
' Type of control signal received by the handler
Private Const CTRL_C_EVENT = 0
Private Const CTRL_BREAK_EVENT = 1
Private Const CTRL_CLOSE_EVENT = 2
' 3 is reserved!
' 4 is reserved!
Private Const CTRL_LOGOFF_EVENT = 5
Private Const CTRL_SHUTDOWN_EVENT = 6
' ShowWindow() Commands
Private Const SW_HIDE = 0
Private Const SW_SHOWNORMAL = 1
Private Const SW_NORMAL = 1
Private Const SW_SHOWMINIMIZED = 2
Private Const SW_SHOWMAXIMIZED = 3
Private Const SW_MAXIMIZE = 3
Private Const SW_SHOWNOACTIVATE = 4
Private Const SW_SHOW = 5
Private Const SW_MINIMIZE = 6
Private Const SW_SHOWMINNOACTIVE = 7
Private Const SW_SHOWNA = 8
Private Const SW_RESTORE = 9
Private Const SW_SHOWDEFAULT = 10
Private Const SW_FORCEMINIMIZE = 11
Private Const SW_MAX = 11
' Structures used with API.
Private Type OVERLAPPED
Internal As Long
InternalHigh As Long
offset As Long
OffsetHigh As Long
hEvent As Long
End Type
Private Type CHAR_INFO
Char As Integer
Attributes As Integer
End Type
Private Type CONSOLE_CURSOR_INFO
dwSize As Long
bVisible As Long
End Type
Private Type COORD
x As Integer
y As Integer
End Type
Private Type SMALL_RECT
Left As Integer
Top As Integer
Right As Integer
Bottom As Integer
End Type
Private Type CONSOLE_SCREEN_BUFFER_INFO
dwSize As COORD
dwCursorPosition As COORD
wAttributes As Integer
srWindow As SMALL_RECT
dwMaximumWindowSize As COORD
End Type
' Combination of INPUT_RECORD and KEY_EVENT_RECORD structures.
Private Type INPUT_KEY_EVENT_RECORD
EventType As Integer ' WORD EventType;
bKeyDown As Long ' BOOL bKeyDown;
wRepeatCount As Integer ' WORD wRepeatCount;
wVirtualKeyCode As Integer ' WORD wVirtualKeyCode;
wVirtualScanCode As Integer ' WORD wVirtualScanCode;
AsciiChar As Integer ' CHAR AsciiChar;
dwControlKeyState As Long ' DWORD dwControlKeyState;
End Type
' Possible types of console events.
Private Const KEY_EVENT As Integer = &H1 ' Event contains key event record
Private Const MOUSE_EVENT As Integer = &H2 ' Event contains mouse event record
Private Const WINDOW_BUFFER_SIZE_EVENT As Integer = &H4 ' Event contains window change event record
Private Const MENU_EVENT As Integer = &H8 ' Event contains menu event record
Private Const FOCUS_EVENT As Integer = &H10 ' Event contains focus change
' Structures used in creation of lightweight object.
Private Type ConsoleType
pVTable As Long
pThisObject As IUnknown
End Type
Private Type VTable
VTable(0 To 2) As Long
End Type
' Window class constant(s)
Const ConsoleClassName As String = "ConsoleWindowClass"
Const ConsoleClassName95 As String = "tty"
' Member variables used to manage lightweight object.
Private m_CT As ConsoleType
Private m_VTable As VTable
Private m_pVTable As Long
' Task related member variables.
Private m_StdError As Long
Private m_StdInput As Long
Private m_StdOutput As Long
Private m_OriginalInputMode As Long
Private m_OriginalOutputMode As Long
Private m_OriginalColors As Long
Private m_CloseProgram As Boolean
Private m_ControlEvent As Long
Private m_BackColor As Long
Private m_ForeColor As Long
Private m_Compiled As Boolean
Private m_Redirected As Boolean
Private m_ExitCode As Long
Private m_hWnd As Long
' *** Added at v1.01 ***
Private m_ParentProcessID As Long
Private m_ParentFilename As String
' Consumable enumerations
Public Enum ConsoleControlSignals
conEventNone = -1
conEventControlC = CTRL_C_EVENT
conEventControlBreak = CTRL_BREAK_EVENT
conEventClose = CTRL_CLOSE_EVENT
conEventLogoff = CTRL_LOGOFF_EVENT
conEventShutdown = CTRL_SHUTDOWN_EVENT
End Enum
Public Enum ConsoleWriteAlignments
conAlignNone
conAlignLeft
conAlignCentered
conAlignRight
End Enum
Public Enum ConsoleOutputDestinations
conStandardOutput
conStandardError
End Enum
' *** Added v1.01 ***
Public Enum ConsoleLaunchModes
conLaunchUnknown = 0 'indeterminate - NT versions prior to 4.0
conLaunchConsole = 1 'launched at command line.
conLaunchExplorer = 2 'double-clicked, from shortcut, etc.
conLaunchVBIDE = 4 'running within the IDE
End Enum
' Enumeration of character attributes.
Public Enum ConsoleColors
[_ColorMin] = 0&
conBlack = 0&
conBlue = FOREGROUND_BLUE
conGreen = FOREGROUND_GREEN
conCyan = FOREGROUND_BLUE Or FOREGROUND_GREEN
conRed = FOREGROUND_RED
conMagenta = FOREGROUND_RED Or FOREGROUND_BLUE
conYellow = FOREGROUND_RED Or FOREGROUND_GREEN
conWhite = FOREGROUND_BLUE Or FOREGROUND_GREEN Or FOREGROUND_RED
conBlackHi = FOREGROUND_INTENSITY
conBlueHi = FOREGROUND_BLUE Or FOREGROUND_INTENSITY
conCyanHi = FOREGROUND_BLUE Or FOREGROUND_GREEN Or FOREGROUND_INTENSITY
conGreenHi = FOREGROUND_GREEN Or FOREGROUND_INTENSITY
conRedHi = FOREGROUND_RED Or FOREGROUND_INTENSITY
conMagentaHi = FOREGROUND_RED Or FOREGROUND_BLUE Or FOREGROUND_INTENSITY
conYellowHi = FOREGROUND_RED Or FOREGROUND_GREEN Or FOREGROUND_INTENSITY
conWhiteHi = FOREGROUND_BLUE Or FOREGROUND_GREEN Or FOREGROUND_RED Or FOREGROUND_INTENSITY
[_ColorMax] = FOREGROUND_BLUE Or FOREGROUND_GREEN Or FOREGROUND_RED Or FOREGROUND_INTENSITY
End Enum
' ******************************************
' Initialize / Terminate
' Release is called automatically when
' application is terminating.
' ******************************************
Public Function Initialize() As ConsoleLaunchModes
Dim csbi As CONSOLE_SCREEN_BUFFER_INFO
Dim lpBuffer As Long, CharsRead As Long
Dim nRet As Long, nErr As Long
Const ERROR_INVALID_HANDLE As Long = 6
' ****************************************************
' If the VTable pointer is uninitialized, we haven't
' been through this routine yet, so only do the init
' stuff when that's the case...
' ****************************************************
' Create the lightweight COM object that provides
' us with both automatic teardown notification,
' and allows us to sink notifications directly.
If m_pVTable = 0 Then
With m_CT
If .pVTable = 0 Then
' Create the lightweight's VTable:
With m_VTable
.VTable(0) = FuncPtr(AddressOf QueryInterface)
.VTable(1) = FuncPtr(AddressOf AddRef)
.VTable(2) = FuncPtr(AddressOf Release)
m_pVTable = VarPtr(.VTable(0))
End With
' Finish setting up the lightweight.
.pVTable = m_pVTable
CopyMemory .pThisObject, VarPtr(.pVTable), 4
End If
End With
' Create a console to play in, if running in the IDE.
' Cache handle to console window, either way.
m_Compiled = IsCompiled()
If m_Compiled Then
' *** Added in v1.01 ***
m_hWnd = Con.hWnd
Else
' *** Changed in v1.01 ***
m_hWnd = LaunchConsole()
End If
'Set up the handler callback.
Call SetConsoleCtrlHandler(AddressOf HandlerRoutine, True)
' Get the standard handles.
m_StdError = GetStdHandle(STD_ERROR_HANDLE)
m_StdInput = GetStdHandle(STD_INPUT_HANDLE)
m_StdOutput = GetStdHandle(STD_OUTPUT_HANDLE)
' Save the current INPUT and OUTPUT modes.
Call GetConsoleMode(m_StdInput, m_OriginalInputMode)
Call GetConsoleMode(m_StdOutput, m_OriginalOutputMode)
' Set a default INPUT mode.
Const defInputMode = ENABLE_LINE_INPUT Or _
ENABLE_PROCESSED_INPUT Or _
ENABLE_ECHO_INPUT
Call SetConsoleMode(m_StdInput, defInputMode)
' Set a default OUTPUT mode.
Const defOutputMode = ENABLE_PROCESSED_OUTPUT Or _
ENABLE_WRAP_AT_EOL_OUTPUT
Call SetConsoleMode(m_StdOutput, defOutputMode)
' Get the current colors.
Call GetConsoleScreenBufferInfo(m_StdOutput, csbi)
m_OriginalColors = csbi.wAttributes
m_BackColor = csbi.wAttributes \ &H10
m_ForeColor = csbi.wAttributes Mod &H10
' Test to see whether standard input has been redirected.
' In this case, Err.LastDllError returns 0, oddly enough.
' Source: Dr. GUI, July 2003, "Do You Know Where That Stream's Been?"
' http://msdn.microsoft.com/library/en-us/dnaskdr/html/askgui07152003.asp
nRet = PeekConsoleInput(m_StdInput, lpBuffer, 0, CharsRead)
nErr = Err.LastDllError
m_Redirected = (nRet = 0) And (nErr = ERROR_INVALID_HANDLE)
' Initial control signal status.
m_ControlEvent = conEventNone
' *** Added at v1.01 ***
' Determine parent process name, cache.
m_ParentFilename = GetProcessParent()
' *** Added at v1.02 ***
' Hide hidden controller window so we disappear from
' Applications tab in Task Manager.
Const defTaskVisible As Boolean = False
Con.TaskVisible = defTaskVisible
' *** Added at v1.01 ***
' Assign LaunchMode to retval.
Initialize = LaunchMode()
End If
End Function
' *****************************************************
' Lightweight object's Release method will be called
' automatically when application terminates.
' Release, in turn, calls Terminate to perform all
' task-related clean-up activities.
' *****************************************************
Private Sub Terminate()
' Restore original colors
Call SetConsoleTextAttribute(m_StdOutput, m_OriginalColors)
' Restore original INPUT and OUTPUT modes
Call SetConsoleMode(m_StdInput, m_OriginalInputMode)
Call SetConsoleMode(m_StdOutput, m_OriginalOutputMode)
' Kill off IDE-hosted console, after pausing
' to allow results to be viewed.
If Not m_Compiled Then
Call Con.SetFocus(True)
Con.PressAnyKey vbCrLf & vbCrLf & _
" --- Execution Complete: Press any key to return to the IDE --- "
Call FreeConsole
End If
' Close all the standard handles
Call CloseHandle(m_StdError)
Call CloseHandle(m_StdInput)
Call CloseHandle(m_StdOutput)
' Return appropriate exit code, but *only*
' if running from EXE, else IDE exits too.
' App *must* be compiled to native code to
' avoid a nasty shutdown GPF in runtime!
If m_Compiled Then
Call ExitProcess(m_ExitCode)
End If
End Sub
' ******************************************
' Public Properties: Read/Write
' ******************************************
Public Property Let BackColor(ByVal NewBackColor As ConsoleColors)
Dim csbi As CONSOLE_SCREEN_BUFFER_INFO
' Attempt to set a new backcolor.
If NewBackColor >= [_ColorMin] And NewBackColor <= [_ColorMax] Then
Call GetConsoleScreenBufferInfo(m_StdOutput, csbi)
m_ForeColor = csbi.wAttributes Mod &H10
m_BackColor = NewBackColor * &H10
Call SetConsoleTextAttribute(m_StdOutput, m_ForeColor Or m_BackColor)
End If
End Property
Public Property Get BackColor() As ConsoleColors
Dim csbi As CONSOLE_SCREEN_BUFFER_INFO
' Get the current colors, return backcolor.
Call GetConsoleScreenBufferInfo(m_StdOutput, csbi)
m_BackColor = csbi.wAttributes \ &H10
BackColor = m_BackColor
End Property
Public Property Let Break(ByVal NewVal As Boolean)
' Give client a chance to reset this flag so it
' can proceed to clean up after itself.
m_CloseProgram = NewVal
End Property
Public Property Get Break() As Boolean
' When the user attempts to manually shutdown
' the console app, our Handler will be tickled
' and set a flag that the process can check.
' If the process ignores this flag, the system
' tends to call ExitProcess in response.
Break = m_CloseProgram
End Property
Public Property Let BufferHeight(ByVal NewHeight As Integer)
Dim sz As COORD
' Attempt setting a new height for console buffer.
If NewHeight > 0 Then
sz.x = Con.BufferWidth
sz.y = NewHeight
Call SetConsoleScreenBufferSize(m_StdOutput, ByVal CoordToLong(sz))
Debug.Print "BufferHeight: "; Err.LastDllError
End If
End Property
Public Property Get BufferHeight() As Integer
Dim csbi As CONSOLE_SCREEN_BUFFER_INFO
' Return height of console buffer.
Call GetConsoleScreenBufferInfo(m_StdOutput, csbi)
BufferHeight = csbi.dwSize.y
End Property
Public Property Let BufferWidth(ByVal NewWidth As Integer)
Dim sz As COORD
' Attempt setting a new width for console buffer.
If NewWidth > 0 Then
sz.x = NewWidth
sz.y = Con.BufferHeight
Call SetConsoleScreenBufferSize(m_StdOutput, ByVal CoordToLong(sz))
End If
End Property
Public Property Get BufferWidth() As Integer
Dim csbi As CONSOLE_SCREEN_BUFFER_INFO
' Return width of console buffer.
Call GetConsoleScreenBufferInfo(m_StdOutput, csbi)
BufferWidth = csbi.dwSize.x
End Property
Public Property Let CodePageI(ByVal NewCP As Integer)
' Attempt to set current input codepage ID.
Call SetConsoleCP(NewCP)
End Property
Public Property Get CodePageI() As Integer
' Retrieve current input codepage ID.
CodePageI = GetConsoleCP()
End Property
Public Property Let CodePageO(ByVal NewCP As Integer)
' Attempt to set current output codepage ID.
Call SetConsoleOutputCP(NewCP)
End Property
Public Property Get CodePageO() As Integer
' Retrieve current input codepage ID.
CodePageO = GetConsoleOutputCP()
End Property
Public Property Let CurrentX(ByVal NewPosition As Integer)
Dim csbi As CONSOLE_SCREEN_BUFFER_INFO
' Determine current cursor position.
Call GetConsoleScreenBufferInfo(m_StdOutput, csbi)
' Clamping request at buffer extents in extreme cases.
If NewPosition > csbi.dwSize.x Then
csbi.dwCursorPosition.x = csbi.dwSize.x
ElseIf NewPosition < 0 Then
csbi.dwCursorPosition.x = 0
Else
csbi.dwCursorPosition.x = NewPosition
End If
' Attempt to set new cursor position.
Call SetConsoleCursorPosition(m_StdOutput, ByVal CoordToLong(csbi.dwCursorPosition))
End Property
Public Property Get CurrentX() As Integer
Dim csbi As CONSOLE_SCREEN_BUFFER_INFO
' Return X-position of cursor; 0-based.
Call GetConsoleScreenBufferInfo(m_StdOutput, csbi)
CurrentX = csbi.dwCursorPosition.x
End Property
Public Property Let CurrentY(ByVal NewPosition As Integer)
Dim csbi As CONSOLE_SCREEN_BUFFER_INFO
' Determine current cursor position.
Call GetConsoleScreenBufferInfo(m_StdOutput, csbi)
' Clamping request at buffer extents in extreme cases.
If NewPosition > csbi.dwSize.y Then
csbi.dwCursorPosition.y = csbi.dwSize.y
ElseIf NewPosition < 0 Then
csbi.dwCursorPosition.y = 0
Else
csbi.dwCursorPosition.y = NewPosition
End If
' Attempt to set new cursor position.
Call SetConsoleCursorPosition(m_StdOutput, ByVal CoordToLong(csbi.dwCursorPosition))
End Property
Public Property Get CurrentY() As Integer
Dim csbi As CONSOLE_SCREEN_BUFFER_INFO
' Return Y-position of cursor; 0-based.
Call GetConsoleScreenBufferInfo(m_StdOutput, csbi)
CurrentY = csbi.dwCursorPosition.y
End Property
Public Property Let CursorHeight(ByVal NewPercentage As Integer)
Dim cci As CONSOLE_CURSOR_INFO
' Cursor height is restricted to 1-100% of cell size.
If NewPercentage >= 1 And NewPercentage <= 100 Then
' Get current values.
Call GetConsoleCursorInfo(m_StdOutput, cci)
cci.dwSize = NewPercentage
Call SetConsoleCursorInfo(m_StdOutput, cci)
End If
End Property
Public Property Get CursorHeight() As Integer
Dim cci As CONSOLE_CURSOR_INFO
' Return cursor height as a percentage of character cell size.
Call GetConsoleCursorInfo(m_StdOutput, cci)
CursorHeight = cci.dwSize
End Property
Public Property Let CursorVisible(ByVal NewVisible As Boolean)
Dim cci As CONSOLE_CURSOR_INFO
' Get current values, and set as requested.
Call GetConsoleCursorInfo(m_StdOutput, cci)
cci.bVisible = NewVisible
Call SetConsoleCursorInfo(m_StdOutput, cci)
End Property
Public Property Get CursorVisible() As Boolean
Dim cci As CONSOLE_CURSOR_INFO
' Return cursor visibility.
Call GetConsoleCursorInfo(m_StdOutput, cci)
CursorVisible = cci.bVisible
End Property
Public Property Let ExitCode(ByVal NewExitCode As Long)
' Simply stash exitcode to use as app is terminating.
m_ExitCode = NewExitCode
End Property
Public Property Get ExitCode() As Long
' Return cached value.
ExitCode = m_ExitCode
End Property
Public Property Let ForeColor(ByVal NewForeColor As ConsoleColors)
Dim csbi As CONSOLE_SCREEN_BUFFER_INFO
' Attempt to set a new forecolor.
If NewForeColor >= [_ColorMin] And NewForeColor <= [_ColorMax] Then
Call GetConsoleScreenBufferInfo(m_StdOutput, csbi)
m_BackColor = (csbi.wAttributes \ &H10) * &H10
m_ForeColor = NewForeColor
Call SetConsoleTextAttribute(m_StdOutput, m_ForeColor Or m_BackColor)
End If
End Property
Public Property Get ForeColor() As ConsoleColors
Dim csbi As CONSOLE_SCREEN_BUFFER_INFO
' Get the current colors, return forecolor.
Call GetConsoleScreenBufferInfo(m_StdOutput, csbi)
m_ForeColor = csbi.wAttributes Mod &H10
ForeColor = m_ForeColor
End Property
Public Property Let FullScreen(ByVal NewVal As Boolean)
Dim lpModeFlags As Long
Dim dwPrevMode As Long
' Attempt to set full-screen status. Not supported on Win9x!
If Exported("kernel32", "SetConsoleDisplayMode") = False Or _
Exported("kernel32", "GetConsoleDisplayMode") = False Then
' No need to continue!
Exit Property
End If
' Make sure there is a need to change.
If GetConsoleDisplayMode(lpModeFlags) Then
If CBool(lpModeFlags And CONSOLE_FULLSCREEN_HARDWARE) Then
If NewVal = False Then
' We are currently running full-screen,
' and we need to switch to windowed.
Call SetConsoleDisplayMode(m_StdOutput, 0&, dwPrevMode)
End If
Else
If NewVal = True Then
' We are currently running windowed, and
' we need to switch to full-screen.
Call SetConsoleDisplayMode(m_StdOutput, 1&, dwPrevMode)
End If
End If
End If
End Property
Public Property Get FullScreen() As Boolean
Dim lpModeFlags As Long
' Attempt to set full-screen status. Not supported on Win9x!
If Exported("kernel32", "GetConsoleDisplayMode") Then
If GetConsoleDisplayMode(lpModeFlags) Then
FullScreen = CBool(lpModeFlags And CONSOLE_FULLSCREEN_HARDWARE)
End If
End If
End Property
Public Property Let Height(ByVal NewHeight As Integer)
Dim csbi As CONSOLE_SCREEN_BUFFER_INFO
' Determine maximum height (chars) of console window.
Call GetConsoleScreenBufferInfo(m_StdOutput, csbi)
' Adjust structure elements to be sure they're 0-based.
csbi.srWindow.Top = 0
csbi.srWindow.Right = csbi.srWindow.Right - csbi.srWindow.Left
csbi.srWindow.Left = 0
' Make sure requested height is valid (0-based).
If NewHeight > csbi.dwMaximumWindowSize.y Then
csbi.srWindow.Bottom = csbi.dwMaximumWindowSize.y - 1
Else
csbi.srWindow.Bottom = NewHeight - 1
End If
' Attempt setting new console window height.
Call SetConsoleWindowInfo(m_StdOutput, True, csbi.srWindow)
End Property
Public Property Get Height() As Integer
Dim csbi As CONSOLE_SCREEN_BUFFER_INFO
' Return height (chars) of console window.
Call GetConsoleScreenBufferInfo(m_StdOutput, csbi)
Height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1
End Property
Public Property Let TaskVisible(ByVal NewVal As Boolean)
' Attempt to set task's current visibility state.
' This affects the Applications tab in Task Manager.
' If True, there are two icons - one for the console
' task itself, and one for this task running within
' the console. If false, only the console icon shows.
If NewVal Then
Call ShowWindow(FindHiddenTopWindow(), SW_SHOW)
Else
Call ShowWindow(FindHiddenTopWindow(), SW_HIDE)
End If
End Property
Public Property Get TaskVisible() As Boolean
' Return current state of task visibility.
TaskVisible = IsWindowVisible(FindHiddenTopWindow())
End Property
Public Property Let Title(ByVal NewTitle As String)
' Update the console title text
Call SetConsoleTitle(NewTitle)
End Property
Public Property Get Title() As String
Dim Buffer As String
Dim nRet As Long
' Read title text of console
Buffer = Space$(1024)
nRet = GetConsoleTitle(Buffer, Len(Buffer))
If nRet Then
Title = Left$(Buffer, nRet)
End If
End Property
Public Property Let Visible(ByVal NewVal As Boolean)
' Attempt to set current visibility state.
If NewVal Then
Call ShowWindow(m_hWnd, SW_SHOW)
Else
Call ShowWindow(m_hWnd, SW_HIDE)
End If
End Property
Public Property Get Visible() As Boolean
' Return current state of visibility.
Visible = IsWindowVisible(m_hWnd)
End Property
Public Property Let Width(ByVal NewWidth As Integer)
Dim csbi As CONSOLE_SCREEN_BUFFER_INFO
' Determine maximum height (chars) of console window.
Call GetConsoleScreenBufferInfo(m_StdOutput, csbi)
' Adjust structure elements to be sure they're 0-based.
csbi.srWindow.Left = 0
csbi.srWindow.Bottom = csbi.srWindow.Bottom - csbi.srWindow.Top
csbi.srWindow.Top = 0
' Make sure requested height is valid (0-based).
If NewWidth > csbi.dwMaximumWindowSize.x Then
csbi.srWindow.Right = csbi.dwMaximumWindowSize.x - 1
Else
csbi.srWindow.Right = NewWidth - 1
End If
' Attempt setting new console window height.
Call SetConsoleWindowInfo(m_StdOutput, True, csbi.srWindow)
End Property
Public Property Get Width() As Integer
Dim csbi As CONSOLE_SCREEN_BUFFER_INFO
' Return width (chars) of console window.
Call GetConsoleScreenBufferInfo(m_StdOutput, csbi)
Width = csbi.srWindow.Right - csbi.srWindow.Left + 1
End Property
Public Property Let WindowState(ByVal NewState As FormWindowStateConstants)
' Set new state as requested.
Select Case NewState
Case vbNormal
Call ShowWindow(m_hWnd, SW_RESTORE)
Case vbMinimized
Call ShowWindow(m_hWnd, SW_MINIMIZE)
Case vbMaximized
Call ShowWindow(m_hWnd, SW_MAXIMIZE)
End Select
End Property
Public Property Get WindowState() As FormWindowStateConstants
' Return current state.
If IsIconic(m_hWnd) Then
WindowState = vbMinimized
ElseIf IsZoomed(m_hWnd) Then
WindowState = vbMaximized
Else
WindowState = vbNormal
End If
End Property
' ******************************************
' Public Properties: Read-Only
' ******************************************
Public Property Get ControlEvent() As ConsoleControlSignals
' This property may be queried if the Break property is found to
' be True. It indicates what sort of event occurred that
' requires the application to shutdown.
' ================================================================
' Note: A Win95 bug prevents some events from signaling!
' http://support.microsoft.com/default.aspx?scid=kb;en-us;130717
' ================================================================
ControlEvent = m_ControlEvent
End Property
Public Property Get Compiled() As Boolean
' Return cached value.
Compiled = m_Compiled
End Property
Public Property Get hStdErr() As Long
' Return handle to standard error.
hStdErr = m_StdError
End Property
Public Property Get hStdIn() As Long
' Return handle to standard input.
hStdIn = m_StdInput
End Property
Public Property Get hStdOut() As Long
' Return handle to standard output.
hStdOut = m_StdOutput
End Property
Public Property Get hWnd() As Long
' 124103 - HOWTO: Obtain a Console Window Handle (HWND)
' http://support.microsoft.com/default.aspx?scid=KB;en-us;q124103
Dim os As OSVERSIONINFO
Dim Title As String
Dim Unique As String
Dim nRet As Long
' Returned cached value, if possible.
If m_hWnd = 0 Then
' Determine what operating system this is.
os.dwOSVersionInfoSize = Len(os)
Call GetVersionEx(os)
If os.dwPlatformId = VER_PLATFORM_WIN32_NT _
And os.dwMajorVersion >= 5 Then
' This is Windows2000 or later!
m_hWnd = GetConsoleWindow()
Else ' Take the tortuous path...
' Cache the associated title.
Title = Space$(1024)
nRet = GetConsoleTitle(Title, Len(Title))
If nRet Then
Title = Left$(Title, nRet)
End If
' Construct unique string to use as new title.
Unique = Format$(Now, "yyyymmddhhnnss") & Hex$(GetCurrentProcessId())
' Set new title to use for search.
If SetConsoleTitle(Unique) Then
' Find window most likely to be our console.
m_hWnd = FindConsole(Unique)
' Restore original title.
Call SetConsoleTitle(Title)
End If