-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessagebox.c
More file actions
154 lines (126 loc) · 5.61 KB
/
Copy pathmessagebox.c
File metadata and controls
154 lines (126 loc) · 5.61 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
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
/*
* MessageBox Utility v1.2
* A simple command-line tool for displaying Windows message boxes
* Copyright (c) Dark Net Studio 2019 - 2025. All rights reserved.
*/
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Function pointer type for MessageBoxTimeoutA
typedef int (WINAPI *MessageBoxTimeoutA_t)(HWND, LPCSTR, LPCSTR, UINT, WORD, DWORD);
// Case-insensitive string comparison
int equals(const char* a, const char* b) {
return _stricmp(a, b) == 0;
}
// Display help information with colored output
void showHelp() {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
printf("\nMessageBox Utility - Help Version 1.2\n\n");
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
printf("Command-Line Arguments:\n");
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
printf(" message : The text to display in the message box.\n");
printf(" title : The title of the message box window.\n");
printf(" icon : Icon style (ERROR, WARNING, INFORMATION, QUESTION).\n");
printf(" buttons : Button options (OK, OKCANCEL, RETRYCANCEL, YESNO, YESNOCANCEL).\n");
printf(" timeout : Timeout in seconds (optional, default: 15).\n\n");
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
printf("Usage Examples:\n");
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
printf(" messagebox.exe \"Hello!\" \"Greetings\" INFORMATION OK\n");
printf(" messagebox.exe \"Delete file?\" \"Confirm\" QUESTION YESNOCANCEL 10\n");
printf(" messagebox.exe /?\n\n");
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
printf("Output:\n");
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
printf(" Shows button pressed: OK, Cancel, Yes, No, Retry, or Timeout.\n\n");
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
printf("This utility is fully compatible with Windows operating systems, including Windows 7, 8, 8.1, 10, and 11, ensuring reliable performance across a wide range of environments.\n\n");
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
// Convert message box result code to string
const char* resultToString(int res) {
switch (res) {
case IDOK: return "OK";
case IDCANCEL: return "Cancel";
case IDYES: return "Yes";
case IDNO: return "No";
case IDRETRY: return "Retry";
case IDTIMEOUT: return "Timeout";
default: return "Unknown";
}
}
// Get icon flag based on icon name
UINT getIconFlag(const char* icon) {
if (equals(icon, "ERROR")) return MB_ICONERROR;
if (equals(icon, "WARNING")) return MB_ICONWARNING;
if (equals(icon, "INFORMATION")) return MB_ICONINFORMATION;
if (equals(icon, "QUESTION")) return MB_ICONQUESTION;
return 0;
}
// Get button flag based on button name
UINT getButtonFlag(const char* btn) {
if (equals(btn, "OK")) return MB_OK;
if (equals(btn, "OKCANCEL")) return MB_OKCANCEL;
if (equals(btn, "RETRYCANCEL")) return MB_RETRYCANCEL;
if (equals(btn, "YESNO")) return MB_YESNO;
if (equals(btn, "YESNOCANCEL")) return MB_YESNOCANCEL;
return 0;
}
// Main function
int main(int argc, char* argv[]) {
// Check if we're in a console subsystem build
// For output redirection to work properly, we should avoid AttachConsole/AllocConsole
// when the executable is already compiled as a console application
// Show help if requested
if (argc == 2 && equals(argv[1], "/?")) {
showHelp();
return 0;
}
// Validate argument count
if (argc < 5 || argc > 6) {
fprintf(stderr, "Invalid arguments. Use /? for help.\n");
return 1;
}
// Parse command line arguments
const char* message = argv[1];
const char* title = argv[2];
UINT icon = getIconFlag(argv[3]);
UINT button = getButtonFlag(argv[4]);
DWORD timeout = (argc == 6) ? atoi(argv[5]) * 1000 : 15000;
// Validate icon type
if (!icon) {
fprintf(stderr, "Invalid icon type. Use ERROR, WARNING, INFORMATION, QUESTION.\n");
return 1;
}
// Validate button type
if (getButtonFlag(argv[4]) == 0 && !equals(argv[4], "OK")) {
fprintf(stderr, "Invalid button type. Use OK, OKCANCEL, RETRYCANCEL, YESNO, YESNOCANCEL.\n");
return 1;
}
// Load user32.dll and get MessageBoxTimeoutA function
UINT type = icon | button;
HMODULE hUser32 = LoadLibraryA("user32.dll");
if (!hUser32) {
fprintf(stderr, "Could not load user32.dll\n");
return 1;
}
MessageBoxTimeoutA_t pMessageBoxTimeoutA =
(MessageBoxTimeoutA_t)GetProcAddress(hUser32, "MessageBoxTimeoutA");
// Show message box with timeout if available, otherwise fall back to regular MessageBox
int result = 0;
if (pMessageBoxTimeoutA) {
result = pMessageBoxTimeoutA(NULL, message, title, type, 0, timeout);
} else {
fprintf(stderr, "MessageBoxTimeoutA not available. Falling back to MessageBoxA (no timeout).\n");
result = MessageBoxA(NULL, message, title, type);
}
// Cleanup and return result
FreeLibrary(hUser32);
// Make sure output is flushed before exiting
printf("%s\n", resultToString(result));
fflush(stdout);
return 0;
}