-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole_test.c
More file actions
71 lines (58 loc) · 1.99 KB
/
console_test.c
File metadata and controls
71 lines (58 loc) · 1.99 KB
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
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
#include <windows.h>
#include <io.h>
#include <fcntl.h>
#include <debugapi.h>
#include <wincon.h>
#endif
int main(int argc, char *argv[])
{
printf("=== CONSOLE OUTPUT TEST ===\n");
#ifdef _WIN32
printf("Running on Windows platform\n");
BOOL console_allocated = FALSE;
// Try to allocate or attach to console
if (AllocConsole()) {
console_allocated = TRUE;
printf("New console allocated\n");
} else if (AttachConsole(ATTACH_PARENT_PROCESS)) {
console_allocated = TRUE;
printf("Attached to parent console\n");
} else {
HWND console_window = GetConsoleWindow();
if (console_window != NULL) {
console_allocated = TRUE;
printf("Using existing console\n");
}
}
if (console_allocated) {
// Redirect streams
FILE *fp_out, *fp_err, *fp_in;
freopen_s(&fp_out, "CONOUT$", "w", stdout);
freopen_s(&fp_err, "CONOUT$", "w", stderr);
freopen_s(&fp_in, "CONIN$", "r", stdin);
SetConsoleTitle(TEXT("Console Output Test"));
printf("Console redirection successful!\n");
fprintf(stderr, "Error output test: stderr working\n");
// Test various output methods
printf("Testing printf: %s\n", "SUCCESS");
fprintf(stdout, "Testing fprintf to stdout: %s\n", "SUCCESS");
fprintf(stderr, "Testing fprintf to stderr: %s\n", "SUCCESS");
fflush(stdout);
fflush(stderr);
printf("\nConsole test completed successfully!\n");
printf("Press Enter to exit...\n");
getchar();
} else {
OutputDebugStringA("Console test: Failed to allocate/attach console\n");
printf("Failed to setup console output\n");
}
#else
printf("Running on non-Windows platform\n");
printf("Console output should work normally\n");
#endif
printf("=== TEST COMPLETED ===\n");
return 0;
}