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
use std::ptr;
use std::mem;
use winapi::DWORD;
use defs::{MessageParams, MessageButtons, MessageIcons, MessageChoice};
pub fn to_utf16<'a>(s: &'a str) -> Vec<u16> {
use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt;
OsStr::new(s)
.encode_wide()
.chain(Some(0u16).into_iter())
.collect()
}
pub fn from_utf16(s: &[u16]) -> String {
use std::ffi::OsString;
use std::os::windows::ffi::OsStringExt;
let null_index = s.iter().position(|&i| i==0).unwrap_or(s.len());
let os_string = OsString::from_wide(&s[0..null_index]);
os_string.into_string().unwrap_or("Decoding error".to_string())
}
pub unsafe fn from_wide_ptr(ptr: *mut u16) -> String {
use std::slice::from_raw_parts;
let mut length: isize = 0;
while *&*ptr.offset(length) != 0 {
length += 1;
}
let array: &[u16] = from_raw_parts(ptr, length as usize);
from_utf16(array)
}
pub unsafe fn get_system_error() -> (DWORD, String) {
use kernel32::{GetLastError, FormatMessageW};
use winapi::{FORMAT_MESSAGE_FROM_SYSTEM, MAKELANGID, LANG_NEUTRAL, SUBLANG_DEFAULT};
use std::ffi::OsString;
use std::os::windows::ffi::OsStringExt;
let code = GetLastError();
let lang = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT) as DWORD;
let mut buf: Vec<u16> = Vec::with_capacity(1024);
buf.set_len(1024);
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, ptr::null(), code, lang, buf.as_mut_ptr(), 1024, ptr::null_mut());
let end = buf.iter().position(|&i| i==0).unwrap_or(1024);
let error_message = OsString::from_wide(&buf[..end])
.into_string()
.unwrap_or("Error while decoding system error message".to_string());
(code, error_message)
}
pub unsafe fn enable_visual_styles() {
use kernel32::{ActivateActCtx, CreateActCtxW, GetSystemDirectoryW};
use winapi::{MAX_PATH, ULONG, ACTCTXW, ULONG_PTR, ICC_STANDARD_CLASSES, ICC_DATE_CLASSES, ICC_PROGRESS_CLASS, ICC_WIN95_CLASSES, INITCOMMONCONTROLSEX};
use comctl32::InitCommonControlsEx;
use low::defs::{ACTCTX_FLAG_RESOURCE_NAME_VALID, ACTCTX_FLAG_SET_PROCESS_DEFAULT, ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID};
let mut sys_dir: Vec<u16> = Vec::with_capacity(MAX_PATH);
sys_dir.set_len(MAX_PATH);
GetSystemDirectoryW(sys_dir.as_mut_ptr(), MAX_PATH as u32);
let mut source = to_utf16("shell32.dll");
let mut activation_cookie: ULONG_PTR = 0;
let mut act_ctx = ACTCTXW {
cbSize: mem::size_of::<ACTCTXW> as ULONG,
dwFlags: ACTCTX_FLAG_RESOURCE_NAME_VALID | ACTCTX_FLAG_SET_PROCESS_DEFAULT | ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID,
lpSource: source.as_mut_ptr(),
wProcessorArchitecture: 0,
wLangId: 0,
lpAssemblyDirectory: sys_dir.as_mut_ptr(),
lpResourceName: mem::transmute(124usize),
lpApplicationName: ptr::null_mut(),
hModule: ptr::null_mut()
};
let handle = CreateActCtxW(&mut act_ctx);
ActivateActCtx(handle, &mut activation_cookie);
let controls_classes = INITCOMMONCONTROLSEX {
dwSize: mem::size_of::<INITCOMMONCONTROLSEX> as DWORD,
dwICC: ICC_DATE_CLASSES|ICC_STANDARD_CLASSES|ICC_PROGRESS_CLASS|ICC_WIN95_CLASSES
};
InitCommonControlsEx(&controls_classes);
}
pub unsafe fn enable_com() {
use ole32::CoInitializeEx;
use winapi::{COINIT_APARTMENTTHREADED, COINIT_DISABLE_OLE1DDE};
CoInitializeEx(ptr::null_mut(), COINIT_APARTMENTTHREADED|COINIT_DISABLE_OLE1DDE);
}
pub fn message<'a>(params: &MessageParams) -> MessageChoice {
use winapi::{MB_ABORTRETRYIGNORE, MB_CANCELTRYCONTINUE, MB_OK, MB_OKCANCEL, MB_RETRYCANCEL, MB_YESNO,
MB_YESNOCANCEL, MB_ICONSTOP, MB_ICONINFORMATION, MB_ICONQUESTION, MB_ICONEXCLAMATION};
use low::defs::{IDABORT, IDCANCEL, IDCONTINUE, IDIGNORE, IDNO, IDOK, IDRETRY, IDTRYAGAIN, IDYES};
use user32::MessageBoxW;
let text = to_utf16(params.content);
let title = to_utf16(params.title);
let buttons = match params.buttons {
MessageButtons::AbortTryIgnore => MB_ABORTRETRYIGNORE,
MessageButtons::CancelTryContinue => MB_CANCELTRYCONTINUE,
MessageButtons::Ok => MB_OK,
MessageButtons::OkCancel => MB_OKCANCEL,
MessageButtons::RetryCancel => MB_RETRYCANCEL,
MessageButtons::YesNo => MB_YESNO,
MessageButtons::YesNoCancel => MB_YESNOCANCEL
};
let icons = match params.icons {
MessageIcons::Error => MB_ICONSTOP,
MessageIcons::Info => MB_ICONINFORMATION,
MessageIcons::None => 0,
MessageIcons::Question => MB_ICONQUESTION,
MessageIcons::Warning => MB_ICONEXCLAMATION
};
let answer = unsafe{ MessageBoxW(ptr::null_mut(), text.as_ptr(), title.as_ptr(), buttons | icons) };
match answer {
IDABORT => MessageChoice::Abort,
IDCANCEL => MessageChoice::Cancel,
IDCONTINUE => MessageChoice::Continue,
IDIGNORE => MessageChoice::Ignore,
IDNO => MessageChoice::No,
IDOK => MessageChoice::Ok,
IDRETRY => MessageChoice::Retry,
IDTRYAGAIN => MessageChoice::TryAgain,
IDYES => MessageChoice::Yes,
_ => MessageChoice::Cancel
}
}
pub fn fatal_message<'a>(title: &'a str, content: &'a str) -> ! {
error_message(title, content);
panic!("{} - {}", title, content);
}
pub fn error_message<'a>(title: &'a str, content: &'a str) -> MessageChoice {
let params = MessageParams {
title: title,
content: content,
buttons: MessageButtons::Ok,
icons: MessageIcons::Error
};
message(¶ms)
}
pub fn simple_message<'a>(title: &'a str, content: &'a str) -> MessageChoice {
let params = MessageParams {
title: title,
content: content,
buttons: MessageButtons::Ok,
icons: MessageIcons::Info
};
message(¶ms)
}