sync kernel32 winetest with wine 1.1.33
[reactos.git] / rostests / winetests / kernel32 / format_msg.c
1 /* Unit test suite for FormatMessageA/W
2 *
3 * Copyright 2002 Mike McCormack for CodeWeavers
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 */
19
20 #include <stdarg.h>
21
22 #include "wine/test.h"
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winnls.h"
26
27 /* #define ok(cond,failstr) if(!(cond)) {printf("line %d : %s\n",__LINE__,failstr);exit(1);} */
28
29 static DWORD __cdecl doit(DWORD flags, LPCVOID src, DWORD msg_id, DWORD lang_id,
30 LPSTR out, DWORD outsize, ... )
31 {
32 __ms_va_list list;
33 DWORD r;
34
35 __ms_va_start(list, outsize);
36 r = FormatMessageA(flags, src, msg_id,
37 lang_id, out, outsize, &list);
38 __ms_va_end(list);
39 return r;
40 }
41
42 static DWORD __cdecl doitW(DWORD flags, LPCVOID src, DWORD msg_id, DWORD lang_id,
43 LPWSTR out, DWORD outsize, ... )
44 {
45 __ms_va_list list;
46 DWORD r;
47
48 __ms_va_start(list, outsize);
49 r = FormatMessageW(flags, src, msg_id,
50 lang_id, out, outsize, &list);
51 __ms_va_end(list);
52 return r;
53 }
54
55 static void test_message_from_string_wide(void)
56 {
57 static const WCHAR test[] = {'t','e','s','t',0};
58 static const WCHAR te[] = {'t','e',0};
59 static const WCHAR st[] = {'s','t',0};
60 static const WCHAR t[] = {'t',0};
61 static const WCHAR e[] = {'e',0};
62 static const WCHAR s[] = {'s',0};
63 static const WCHAR fmt_1[] = {'%','1',0};
64 static const WCHAR fmt_12[] = {'%','1','%','2',0};
65 static const WCHAR fmt_123[] = {'%','1','%','3','%','2','%','1',0};
66 static const WCHAR fmt_123c[] = {'%','1','!','c','!','%','2','!','c','!','%','3','!','c','!','%','1','!','c','!',0};
67 static const WCHAR fmt_123lc[] = {'%','1','!','l','c','!','%','2','!','l','c','!','%','3','!','l','c','!','%','1','!','l','c','!',0};
68 static const WCHAR fmt_123wc[] = {'%','1','!','w','c','!','%','2','!','w','c','!','%','3','!','w','c','!','%','1','!','w','c','!',0};
69 static const WCHAR fmt_123C[] = {'%','1','!','C','!','%','2','!','C','!','%','3','!','C','!','%','1','!','C','!',0};
70 static const WCHAR fmt_123d[] = {'%','1','!','d','!','%','2','!','d','!','%','3','!','d','!',0};
71 static const WCHAR fmt_1s[] = {'%','1','!','s','!',0};
72 static const WCHAR fmt_s[] = {'%','!','s','!',0};
73 static const WCHAR fmt_ls[] = {'%','!','l','s','!',0};
74 static const WCHAR fmt_ws[] = {'%','!','w','s','!',0};
75 static const WCHAR fmt_S[] = {'%','!','S','!',0};
76 static const WCHAR fmt_14d[] = {'%','1','!','4','d','!',0};
77 static const WCHAR fmt_14x[] = {'%','1','!','4','x','!',0};
78 static const WCHAR fmt_14X[] = {'%','1','!','4','X','!',0};
79 static const WCHAR fmt_1_4X[] = {'%','1','!','-','4','X','!',0};
80 static const WCHAR fmt_1_4d[] = {'%','1','!','-','4','d','!',0};
81 static const WCHAR fmt_2pct[] = {' ','%','%','%','%',' ',0};
82 static const WCHAR fmt_2dot1d[] = {' ', '%','.','%','.',' ',' ','%','1','!','d','!',0};
83 static const WCHAR fmt_t0t[] = {'t','e','s','t','%','0','t','e','s','t',0};
84 static const WCHAR fmt_yah[] = {'y','a','h','%','!','%','0',' ',' ',' ',0};
85 static const WCHAR fmt_space[] = {'%',' ','%',' ',' ',' ',0};
86 static const WCHAR fmt_hi_lf[] = {'h','i','\n',0};
87 static const WCHAR fmt_hi_crlf[] = {'h','i','\r','\n',0};
88 static const WCHAR fmt_cr[] = {'\r',0};
89 static const WCHAR fmt_crcrlf[] = {'\r','\r','\n',0};
90 static const WCHAR fmt_13s[] = {'%','1','!','3','s','!',0};
91 static const WCHAR fmt_1os[] = {'%','1','!','*','s','!',0};
92 static const WCHAR fmt_142u[] = {'%','1','!','4','.','2','u','!',0};
93 static const WCHAR fmt_1oou[] = {'%','1','!','*','.','*','u','!',0};
94 static const WCHAR fmt_1oou1oou[] = {'%','1','!','*','.','*','u','!',',','%','1','!','*','.','*','u','!',0};
95 static const WCHAR fmt_1oou3oou[] = {'%','1','!','*','.','*','u','!',',','%','3','!','*','.','*','u','!',0};
96 static const WCHAR fmt_1oou4oou[] = {'%','1','!','*','.','*','u','!',',','%','4','!','*','.','*','u','!',0};
97
98 static const WCHAR s_123d[] = {'1','2','3',0};
99 static const WCHAR s_14d[] = {' ',' ',' ','1',0};
100 static const WCHAR s_14x[] = {' ',' ',' ','b',0};
101 static const WCHAR s_14X[] = {' ',' ',' ','B',0};
102 static const WCHAR s_1_4X[] = {'B',' ',' ',' ',0};
103 static const WCHAR s_14d2[] = {' ',' ','1','1',0};
104 static const WCHAR s_1_4d[] = {'1',' ',' ',' ',0};
105 static const WCHAR s_1AB[] = {' ','1','A','B',0};
106 static const WCHAR s_2pct[] = {' ','%','%',' ',0};
107 static const WCHAR s_2dot147[] = {' ','.','.',' ',' ','4','2','7',0};
108 static const WCHAR s_yah[] = {'y','a','h','!',0};
109 static const WCHAR s_space[] = {' ',' ',' ',' ',0};
110 static const WCHAR s_hi_crlf[] = {'h','i','\r','\n',0};
111 static const WCHAR s_crlf[] = {'\r','\n',0};
112 static const WCHAR s_crlfcrlf[] = {'\r','\n','\r','\n',0};
113 static const WCHAR s_hi_sp[] = {'h','i',' ',0};
114 static const WCHAR s_sp[] = {' ',0};
115 static const WCHAR s_2sp[] = {' ',' ',0};
116 static const WCHAR s_spt[] = {' ',' ','t',0};
117 static const WCHAR s_sp3t[] = {' ',' ',' ','t',0};
118 static const WCHAR s_sp03[] = {' ',' ','0','3',0};
119 static const WCHAR s_sp001[] = {' ',' ','0','0','1',0};
120 static const WCHAR s_sp001002[] = {' ',' ','0','0','1',',',' ','0','0','0','2',0};
121 static const WCHAR s_sp001sp002[] = {' ',' ','0','0','1',',',' ',' ','0','0','0','2',0};
122 static const WCHAR s_sp002sp001[] = {' ',' ','0','0','0','2',',',' ',' ','0','0','1',0};
123 static const WCHAR s_sp002sp003[] = {' ',' ','0','0','0','2',',',' ','0','0','0','0','3',0};
124 static const WCHAR s_sp001004[] = {' ',' ','0','0','1',',','0','0','0','0','0','4',0};
125
126 WCHAR out[0x100] = {0};
127 DWORD r, error;
128
129 SetLastError(0xdeadbeef);
130 r = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, NULL, 0, 0, NULL, 0, NULL);
131 error = GetLastError();
132 if (!r && error == ERROR_CALL_NOT_IMPLEMENTED)
133 {
134 win_skip("FormatMessageW is not implemented\n");
135 return;
136 }
137
138 /* the basics */
139 r = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, test, 0,
140 0, out, sizeof(out)/sizeof(WCHAR), NULL);
141 ok(!lstrcmpW(test, out), "failed out=%s\n", wine_dbgstr_w(out));
142 ok(r==4, "failed: r=%d\n", r);
143
144 /* using the format feature */
145 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_1s, 0,
146 0, out, sizeof(out)/sizeof(WCHAR), test);
147 ok(!lstrcmpW(test, out), "failed out=%s\n", wine_dbgstr_w(out));
148 ok(r==4,"failed: r=%d\n", r);
149
150 /* no format */
151 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_1, 0,
152 0, out, sizeof(out)/sizeof(WCHAR), test);
153 ok(!lstrcmpW(test, out), "failed out=%s\n", wine_dbgstr_w(out));
154 ok(r==4,"failed: r=%d\n", r);
155
156 /* two pieces */
157 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_12, 0,
158 0, out, sizeof(out)/sizeof(WCHAR), te, st);
159 ok(!lstrcmpW(test, out), "failed out=%s\n", wine_dbgstr_w(out));
160 ok(r==4,"failed: r=%d\n", r);
161
162 /* three pieces */
163 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_123, 0,
164 0, out, sizeof(out)/sizeof(WCHAR), t, s, e);
165 ok(!lstrcmpW(test, out), "failed out=%s\n", wine_dbgstr_w(out));
166 ok(r==4,"failed: r=%d\n", r);
167
168 /* s doesn't seem to work in format strings */
169 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_s, 0,
170 0, out, sizeof(out)/sizeof(WCHAR), test);
171 ok(!lstrcmpW(&fmt_s[1], out), "failed out=%s\n", wine_dbgstr_w(out));
172 ok(r==3, "failed: r=%d\n", r);
173
174 /* nor ls */
175 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_ls, 0,
176 0, out, sizeof(out)/sizeof(WCHAR), test);
177 ok(!lstrcmpW(&fmt_ls[1], out), "failed out=%s\n", wine_dbgstr_w(out));
178 ok(r==4, "failed: r=%d\n", r);
179
180 /* nor S */
181 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_S, 0,
182 0, out, sizeof(out)/sizeof(WCHAR), test);
183 ok(!lstrcmpW(&fmt_S[1], out), "failed out=%s\n", wine_dbgstr_w(out));
184 ok(r==3, "failed: r=%d\n", r);
185
186 /* nor ws */
187 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_ws, 0,
188 0, out, sizeof(out)/sizeof(WCHAR), test);
189 ok(!lstrcmpW(&fmt_ws[1], out), "failed out=%s\n", wine_dbgstr_w(out));
190 ok(r==4, "failed: r=%d\n", r);
191
192 /* as characters */
193 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_123c, 0,
194 0, out, sizeof(out)/sizeof(WCHAR), 't', 'e', 's');
195 ok(!lstrcmpW(test, out), "failed out=%s\n", wine_dbgstr_w(out));
196 ok(r==4,"failed: r=%d\n", r);
197
198 /* lc is unicode */
199 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_123lc, 0,
200 0, out, sizeof(out)/sizeof(WCHAR), 't', 'e', 's');
201 ok(!lstrcmpW(test, out), "failed out=%s\n", wine_dbgstr_w(out));
202 ok(r==4,"failed: r=%d\n", r);
203
204 /* wc is unicode */
205 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_123wc, 0,
206 0, out, sizeof(out)/sizeof(WCHAR), 't', 'e', 's');
207 ok(!lstrcmpW(test, out), "failed out=%s\n", wine_dbgstr_w(out));
208 ok(r==4,"failed: r=%d\n", r);
209
210 /* C is unicode */
211 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_123C, 0,
212 0, out, sizeof(out)/sizeof(WCHAR), 't', 'e', 's');
213 ok(!lstrcmpW(test, out), "failed out=%s\n", wine_dbgstr_w(out));
214 ok(r==4,"failed: r=%d\n", r);
215
216 /* some numbers */
217 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_123d, 0,
218 0, out, sizeof(out)/sizeof(WCHAR), 1, 2, 3);
219 ok(!lstrcmpW(s_123d, out), "failed out=%s\n", wine_dbgstr_w(out));
220 ok(r==3,"failed: r=%d\n", r);
221
222 /* a single digit with some spacing */
223 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_14d, 0,
224 0, out, sizeof(out)/sizeof(WCHAR), 1);
225 ok(!lstrcmpW(s_14d, out), "failed out=%s\n", wine_dbgstr_w(out));
226
227 /* a single digit, left justified */
228 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_1_4d, 0,
229 0, out, sizeof(out)/sizeof(CHAR), 1);
230 ok(!lstrcmpW(s_1_4d, out), "failed out=%s\n", wine_dbgstr_w(out));
231 ok(r==4,"failed: r=%d\n", r);
232
233 /* two digit decimal number */
234 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_14d, 0,
235 0, out, sizeof(out)/sizeof(WCHAR), 11);
236 ok(!lstrcmpW(s_14d2, out), "failed out=%s\n", wine_dbgstr_w(out));
237 ok(r==4,"failed: r=%d\n", r);
238
239 /* a hex number */
240 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_14x, 0,
241 0, out, sizeof(out)/sizeof(WCHAR), 11);
242 ok(!lstrcmpW(s_14x, out), "failed out=%s\n", wine_dbgstr_w(out));
243 ok(r==4,"failed: r=%d\n", r);
244
245 /* a hex number, upper case */
246 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_14X, 0,
247 0, out, sizeof(out)/sizeof(WCHAR), 11);
248 ok(!lstrcmpW(s_14X, out), "failed out=%s\n", wine_dbgstr_w(out));
249 ok(r==4,"failed: r=%d\n", r);
250
251 /* a hex number, upper case, left justified */
252 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_1_4X, 0,
253 0, out, sizeof(out)/sizeof(WCHAR), 11);
254 ok(!lstrcmpW(s_1_4X, out), "failed out=%s\n", wine_dbgstr_w(out));
255 ok(r==4,"failed: r=%d\n", r);
256
257 /* a long hex number, upper case */
258 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_14X, 0,
259 0, out, sizeof(out)/sizeof(WCHAR), 0x1ab);
260 ok(!lstrcmpW(s_1AB, out), "failed out=%s\n", wine_dbgstr_w(out));
261 ok(r==4,"failed: r=%d\n", r);
262
263 /* two percent... */
264 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_2pct, 0,
265 0, out, sizeof(out)/sizeof(WCHAR));
266 ok(!lstrcmpW(s_2pct, out), "failed out=%s\n", wine_dbgstr_w(out));
267 ok(r==4,"failed: r=%d\n", r);
268
269 /* periods are special cases */
270 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_2dot1d, 0,
271 0, out, sizeof(out)/sizeof(WCHAR), 0x1ab);
272 ok(!lstrcmpW(s_2dot147, out), "failed out=%s\n", wine_dbgstr_w(out));
273 ok(r==8,"failed: r=%d\n", r);
274
275 /* %0 ends the line */
276 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_t0t, 0,
277 0, out, sizeof(out)/sizeof(WCHAR));
278 ok(!lstrcmpW(test, out), "failed out=%s\n", wine_dbgstr_w(out));
279 ok(r==4,"failed: r=%d\n", r);
280
281 /* %! prints an exclamation */
282 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_yah, 0,
283 0, out, sizeof(out)/sizeof(WCHAR));
284 ok(!lstrcmpW(s_yah, out), "failed out=%s\n", wine_dbgstr_w(out));
285 ok(r==4,"failed: r=%d\n", r);
286
287 /* %space */
288 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_space, 0,
289 0, out, sizeof(out)/sizeof(WCHAR));
290 ok(!lstrcmpW(s_space, out), "failed out=%s\n", wine_dbgstr_w(out));
291 ok(r==4,"failed: r=%d\n", r);
292
293 /* line feed */
294 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_hi_lf, 0,
295 0, out, sizeof(out)/sizeof(WCHAR));
296 ok(!lstrcmpW(s_hi_crlf, out), "failed out=%s\n", wine_dbgstr_w(out));
297 ok(r==4,"failed: r=%d\n", r);
298
299 /* carriage return line feed */
300 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_hi_crlf, 0,
301 0, out, sizeof(out)/sizeof(WCHAR));
302 ok(!lstrcmpW(s_hi_crlf, out), "failed out=%s\n", wine_dbgstr_w(out));
303 ok(r==4,"failed: r=%d\n", r);
304
305 /* carriage return */
306 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_cr, 0,
307 0, out, sizeof(out)/sizeof(WCHAR));
308 ok(!lstrcmpW(s_crlf, out), "failed out=%s\n", wine_dbgstr_w(out));
309 ok(r==2,"failed: r=%d\n", r);
310
311 /* double carriage return line feed */
312 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_crcrlf, 0,
313 0, out, sizeof(out)/sizeof(WCHAR));
314 ok(!lstrcmpW(s_crlfcrlf, out), "failed out=%s\n", wine_dbgstr_w(out));
315 ok(r==4,"failed: r=%d\n", r);
316
317 /* precision and width */
318
319 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_13s,
320 0, 0, out, sizeof(out)/sizeof(WCHAR), t );
321 ok(!lstrcmpW(s_spt, out),"failed out=[%s]\n", wine_dbgstr_w(out));
322 ok(r==3, "failed: r=%d\n",r);
323 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_1os,
324 0, 0, out, sizeof(out)/sizeof(WCHAR), 4, t );
325 ok(!lstrcmpW( s_sp3t, out),"failed out=[%s]\n", wine_dbgstr_w(out));
326 ok(r==4,"failed: r=%d\n",r);
327 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_142u,
328 0, 0, out, sizeof(out)/sizeof(WCHAR), 3 );
329 ok(!lstrcmpW( s_sp03, out),"failed out=[%s]\n", wine_dbgstr_w(out));
330 ok(r==4,"failed: r=%d\n",r);
331 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_1oou,
332 0, 0, out, sizeof(out)/sizeof(WCHAR), 5, 3, 1 );
333 ok(!lstrcmpW( s_sp001, out),"failed out=[%s]\n", wine_dbgstr_w(out));
334 ok(r==5,"failed: r=%d\n",r);
335 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_1oou1oou,
336 0, 0, out, sizeof(out)/sizeof(WCHAR), 5, 3, 1, 4, 2 );
337 ok(!lstrcmpW( s_sp001002, out),"failed out=[%s]\n", wine_dbgstr_w(out));
338 ok(r==11,"failed: r=%d\n",r);
339 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_1oou3oou,
340 0, 0, out, sizeof(out)/sizeof(WCHAR), 5, 3, 1, 6, 4, 2 );
341 ok(!lstrcmpW( s_sp001sp002, out) || broken(!lstrcmpW(s_sp001004, out)),
342 "failed out=[%s]\n", wine_dbgstr_w(out));
343 ok(r==12,"failed: r=%d\n",r);
344 /* args are not counted the same way with an argument array */
345 {
346 ULONG_PTR args[] = { 6, 4, 2, 5, 3, 1 };
347 r = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY, fmt_1oou1oou,
348 0, 0, out, sizeof(out)/sizeof(WCHAR), (__ms_va_list *)args );
349 ok(!lstrcmpW(s_sp002sp003, out),"failed out=[%s]\n", wine_dbgstr_w(out));
350 ok(r==13,"failed: r=%d\n",r);
351 r = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY, fmt_1oou4oou,
352 0, 0, out, sizeof(out)/sizeof(WCHAR), (__ms_va_list *)args );
353 ok(!lstrcmpW(s_sp002sp001, out),"failed out=[%s]\n", wine_dbgstr_w(out));
354 ok(r==12,"failed: r=%d\n",r);
355 }
356
357 /* change of pace... test the low byte of dwflags */
358
359 /* line feed */
360 r = doitW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, fmt_hi_lf, 0,
361 0, out, sizeof(out)/sizeof(WCHAR));
362 ok(!lstrcmpW(s_hi_sp, out) || !lstrcmpW(s_hi_crlf, out), "failed out=%s\n", wine_dbgstr_w(out));
363 ok(r==3 || r==4,"failed: r=%d\n", r);
364
365 /* carriage return line feed */
366 r = doitW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, fmt_hi_crlf, 0,
367 0, out, sizeof(out)/sizeof(WCHAR));
368 ok(!lstrcmpW(s_hi_sp, out), "failed out=%s\n", wine_dbgstr_w(out));
369 ok(r==3,"failed: r=%d\n", r);
370
371 /* carriage return */
372 r = doitW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, fmt_cr, 0,
373 0, out, sizeof(out)/sizeof(WCHAR));
374 ok(!lstrcmpW(s_sp, out), "failed out=%s\n", wine_dbgstr_w(out));
375 ok(r==1,"failed: r=%d\n", r);
376
377 /* double carriage return line feed */
378 r = doitW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, fmt_crcrlf, 0,
379 0, out, sizeof(out)/sizeof(WCHAR));
380 ok(!lstrcmpW(s_2sp, out), "failed out=%s\n", wine_dbgstr_w(out));
381 ok(r==2,"failed: r=%d\n", r);
382 }
383
384 static void test_message_from_string(void)
385 {
386 CHAR out[0x100] = {0};
387 DWORD r;
388 static const WCHAR szwTest[] = { 't','e','s','t',0};
389
390 /* the basics */
391 r = FormatMessageA(FORMAT_MESSAGE_FROM_STRING, "test", 0,
392 0, out, sizeof(out)/sizeof(CHAR),NULL);
393 ok(!strcmp("test", out),"failed out=[%s]\n",out);
394 ok(r==4,"failed: r=%d\n",r);
395
396 /* using the format feature */
397 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!s!", 0,
398 0, out, sizeof(out)/sizeof(CHAR), "test");
399 ok(!strcmp("test", out),"failed out=[%s]\n",out);
400 ok(r==4,"failed: r=%d\n",r);
401
402 /* no format */
403 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1", 0,
404 0, out, sizeof(out)/sizeof(CHAR), "test");
405 ok(!strcmp("test", out),"failed out=[%s]\n",out);
406 ok(r==4,"failed: r=%d\n",r);
407
408 /* two pieces */
409 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1%2", 0,
410 0, out, sizeof(out)/sizeof(CHAR), "te","st");
411 ok(!strcmp("test", out),"failed out=[%s]\n",out);
412 ok(r==4,"failed: r=%d\n",r);
413
414 /* three pieces */
415 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1%3%2%1", 0,
416 0, out, sizeof(out)/sizeof(CHAR), "t","s","e");
417 ok(!strcmp("test", out),"failed out=[%s]\n",out);
418 ok(r==4,"failed: r=%d\n",r);
419
420 /* s doesn't seem to work in format strings */
421 r = doit(FORMAT_MESSAGE_FROM_STRING, "%!s!", 0,
422 0, out, sizeof(out)/sizeof(CHAR), "test");
423 ok(!strcmp("!s!", out),"failed out=[%s]\n",out);
424 ok(r==3,"failed: r=%d\n",r);
425
426 /* ls is unicode */
427 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!ls!", 0,
428 0, out, sizeof(out)/sizeof(CHAR), szwTest);
429 ok(!strcmp("test", out),"failed out=[%s]\n",out);
430 ok(r==4,"failed: r=%d\n",r);
431
432 /* S is unicode */
433 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!S!", 0,
434 0, out, sizeof(out)/sizeof(CHAR), szwTest);
435 ok(!strcmp("test", out),"failed out=[%s]\n",out);
436 ok(r==4,"failed: r=%d\n",r);
437
438 /* ws is unicode */
439 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!ws!", 0,
440 0, out, sizeof(out)/sizeof(CHAR), szwTest);
441 ok(!strcmp("test", out),"failed out=[%s]\n",out);
442 ok(r==4,"failed: r=%d\n",r);
443
444 /* as characters */
445 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!c!%2!c!%3!c!%1!c!", 0,
446 0, out, sizeof(out)/sizeof(CHAR), 't','e','s');
447 ok(!strcmp("test", out),"failed out=[%s]\n",out);
448 ok(r==4,"failed: r=%d\n",r);
449
450 /* lc is unicode */
451 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!lc!%2!lc!%3!lc!%1!lc!", 0,
452 0, out, sizeof(out)/sizeof(CHAR), 't','e','s');
453 ok(!strcmp("test", out),"failed out=[%s]\n",out);
454 ok(r==4,"failed: r=%d\n",r);
455
456 /* wc is unicode */
457 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!wc!%2!wc!%3!wc!%1!wc!", 0,
458 0, out, sizeof(out)/sizeof(CHAR), 't','e','s');
459 ok(!strcmp("test", out),"failed out=[%s]\n",out);
460 ok(r==4,"failed: r=%d\n",r);
461
462 /* C is unicode */
463 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!C!%2!C!%3!C!%1!C!", 0,
464 0, out, sizeof(out)/sizeof(CHAR), 't','e','s');
465 ok(!strcmp("test", out),"failed out=[%s]\n",out);
466 ok(r==4,"failed: r=%d\n",r);
467
468 /* some numbers */
469 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!d!%2!d!%3!d!", 0,
470 0, out, sizeof(out)/sizeof(CHAR), 1,2,3);
471 ok(!strcmp("123", out),"failed out=[%s]\n",out);
472 ok(r==3,"failed: r=%d\n",r);
473
474 /* a single digit with some spacing */
475 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4d!", 0,
476 0, out, sizeof(out)/sizeof(CHAR), 1);
477 ok(!strcmp(" 1", out),"failed out=[%s]\n",out);
478 ok(r==4,"failed: r=%d\n",r);
479
480 /* a single digit, left justified */
481 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!-4d!", 0,
482 0, out, sizeof(out)/sizeof(CHAR), 1);
483 ok(!strcmp("1 ", out),"failed out=[%s]\n",out);
484 ok(r==4,"failed: r=%d\n",r);
485
486 /* two digit decimal number */
487 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4d!", 0,
488 0, out, sizeof(out)/sizeof(CHAR), 11);
489 ok(!strcmp(" 11", out),"failed out=[%s]\n",out);
490 ok(r==4,"failed: r=%d\n",r);
491
492 /* a hex number */
493 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4x!", 0,
494 0, out, sizeof(out)/sizeof(CHAR), 11);
495 ok(!strcmp(" b", out),"failed out=[%s]\n",out);
496 ok(r==4,"failed: r=%d\n",r);
497
498 /* a hex number, upper case */
499 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4X!", 0,
500 0, out, sizeof(out)/sizeof(CHAR), 11);
501 ok(!strcmp(" B", out),"failed out=[%s]\n",out);
502 ok(r==4,"failed: r=%d\n",r);
503
504 /* a hex number, upper case, left justified */
505 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!-4X!", 0,
506 0, out, sizeof(out)/sizeof(CHAR), 11);
507 ok(!strcmp("B ", out),"failed out=[%s]\n",out);
508 ok(r==4,"failed: r=%d\n",r);
509
510 /* a long hex number, upper case */
511 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4X!", 0,
512 0, out, sizeof(out)/sizeof(CHAR), 0x1ab);
513 ok(!strcmp(" 1AB", out),"failed out=[%s]\n",out);
514 ok(r==4,"failed: r=%d\n",r);
515
516 /* two percent... */
517 r = doit(FORMAT_MESSAGE_FROM_STRING, " %%%% ", 0,
518 0, out, sizeof(out)/sizeof(CHAR));
519 ok(!strcmp(" %% ", out),"failed out=[%s]\n",out);
520 ok(r==4,"failed: r=%d\n",r);
521
522 /* periods are special cases */
523 r = doit(FORMAT_MESSAGE_FROM_STRING, " %.%. %1!d!", 0,
524 0, out, sizeof(out)/sizeof(CHAR), 0x1ab);
525 ok(!strcmp(" .. 427", out),"failed out=[%s]\n",out);
526 ok(r==7,"failed: r=%d\n",r);
527
528 /* %0 ends the line */
529 r = doit(FORMAT_MESSAGE_FROM_STRING, "test%0test", 0,
530 0, out, sizeof(out)/sizeof(CHAR));
531 ok(!strcmp("test", out),"failed out=[%s]\n",out);
532 ok(r==4,"failed: r=%d\n",r);
533
534 /* %! prints an exclamation */
535 r = doit(FORMAT_MESSAGE_FROM_STRING, "yah%!%0 ", 0,
536 0, out, sizeof(out)/sizeof(CHAR));
537 ok(!strcmp("yah!", out),"failed out=[%s]\n",out);
538 ok(r==4,"failed: r=%d\n",r);
539
540 /* %space */
541 r = doit(FORMAT_MESSAGE_FROM_STRING, "% % ", 0,
542 0, out, sizeof(out)/sizeof(CHAR));
543 ok(!strcmp(" ", out),"failed out=[%s]\n",out);
544 ok(r==4,"failed: r=%d\n",r);
545
546 /* line feed */
547 r = doit(FORMAT_MESSAGE_FROM_STRING, "hi\n", 0,
548 0, out, sizeof(out)/sizeof(CHAR));
549 ok(!strcmp("hi\r\n", out),"failed out=[%s]\n",out);
550 ok(r==4,"failed: r=%d\n",r);
551
552 /* carriage return line feed */
553 r = doit(FORMAT_MESSAGE_FROM_STRING, "hi\r\n", 0,
554 0, out, sizeof(out)/sizeof(CHAR));
555 ok(!strcmp("hi\r\n", out),"failed out=[%s]\n",out);
556 ok(r==4,"failed: r=%d\n",r);
557
558 /* carriage return */
559 r = doit(FORMAT_MESSAGE_FROM_STRING, "\r", 0,
560 0, out, sizeof(out)/sizeof(CHAR));
561 ok(!strcmp("\r\n", out),"failed out=[%s]\n",out);
562 ok(r==2,"failed: r=%d\n",r);
563
564 /* double carriage return line feed */
565 r = doit(FORMAT_MESSAGE_FROM_STRING, "\r\r\n", 0,
566 0, out, sizeof(out)/sizeof(CHAR));
567 ok(!strcmp("\r\n\r\n", out),"failed out=[%s]\n",out);
568 ok(r==4,"failed: r=%d\n",r);
569
570 /* precision and width */
571
572 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!3s!",
573 0, 0, out, sizeof(out), "t" );
574 ok(!strcmp(" t", out),"failed out=[%s]\n",out);
575 ok(r==3, "failed: r=%d\n",r);
576 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!*s!",
577 0, 0, out, sizeof(out), 4, "t");
578 if (!strcmp("*s",out)) win_skip( "width/precision not supported\n" );
579 else
580 {
581 ok(!strcmp( " t", out),"failed out=[%s]\n",out);
582 ok(r==4,"failed: r=%d\n",r);
583 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4.2u!",
584 0, 0, out, sizeof(out), 3 );
585 ok(!strcmp( " 03", out),"failed out=[%s]\n",out);
586 ok(r==4,"failed: r=%d\n",r);
587 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!*.*u!",
588 0, 0, out, sizeof(out), 5, 3, 1 );
589 ok(!strcmp( " 001", out),"failed out=[%s]\n",out);
590 ok(r==5,"failed: r=%d\n",r);
591 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!*.*u!,%1!*.*u!",
592 0, 0, out, sizeof(out), 5, 3, 1, 4, 2 );
593 ok(!strcmp( " 001, 0002", out),"failed out=[%s]\n",out);
594 ok(r==11,"failed: r=%d\n",r);
595 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!*.*u!,%3!*.*u!",
596 0, 0, out, sizeof(out), 5, 3, 1, 6, 4, 2 );
597 /* older Win versions marked as broken even though this is arguably the correct behavior */
598 /* but the new (brain-damaged) behavior is specified on MSDN */
599 ok(!strcmp( " 001, 0002", out) || broken(!strcmp(" 001,000004", out)),
600 "failed out=[%s]\n",out);
601 ok(r==12,"failed: r=%d\n",r);
602 /* args are not counted the same way with an argument array */
603 {
604 ULONG_PTR args[] = { 6, 4, 2, 5, 3, 1 };
605 r = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY,
606 "%1!*.*u!,%1!*.*u!", 0, 0, out, sizeof(out), (__ms_va_list *)args );
607 ok(!strcmp(" 0002, 00003", out),"failed out=[%s]\n",out);
608 ok(r==13,"failed: r=%d\n",r);
609 r = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY,
610 "%1!*.*u!,%4!*.*u!", 0, 0, out, sizeof(out), (__ms_va_list *)args );
611 ok(!strcmp(" 0002, 001", out),"failed out=[%s]\n",out);
612 ok(r==12,"failed: r=%d\n",r);
613 }
614 }
615
616 /* change of pace... test the low byte of dwflags */
617
618 /* line feed */
619 r = doit(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, "hi\n", 0,
620 0, out, sizeof(out)/sizeof(CHAR));
621 ok(!strcmp("hi ", out) || !strcmp("hi\r\n", out),"failed out=[%s]\n",out);
622 ok(r==3 || r==4,"failed: r=%d\n",r);
623
624 /* carriage return line feed */
625 r = doit(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, "hi\r\n", 0,
626 0, out, sizeof(out)/sizeof(CHAR));
627 ok(!strcmp("hi ", out),"failed out=[%s]\n",out);
628 ok(r==3,"failed: r=%d\n",r);
629
630 /* carriage return */
631 r = doit(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, "\r", 0,
632 0, out, sizeof(out)/sizeof(CHAR));
633 ok(!strcmp(" ", out),"failed out=[%s]\n",out);
634 ok(r==1,"failed: r=%d\n",r);
635
636 /* double carriage return line feed */
637 r = doit(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, "\r\r\n", 0,
638 0, out, sizeof(out)/sizeof(CHAR));
639 ok(!strcmp(" ", out),"failed out=[%s]\n",out);
640 ok(r==2,"failed: r=%d\n",r);
641 }
642
643 static void test_message_null_buffer(void)
644 {
645 DWORD ret, error;
646
647 SetLastError(0xdeadbeef);
648 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, 0, 0, NULL, 0, NULL);
649 error = GetLastError();
650 ok(!ret, "FormatMessageA returned %u\n", ret);
651 ok(error == ERROR_NOT_ENOUGH_MEMORY ||
652 error == ERROR_INVALID_PARAMETER, /* win9x */
653 "last error %u\n", error);
654
655 SetLastError(0xdeadbeef);
656 ret = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, 0, 0, NULL, 0, NULL);
657 error = GetLastError();
658 if (!ret && error == ERROR_CALL_NOT_IMPLEMENTED)
659 {
660 win_skip("FormatMessageW is not implemented\n");
661 return;
662 }
663
664 ok(!ret, "FormatMessageW returned %u\n", ret);
665 ok(error == ERROR_INVALID_PARAMETER, "last error %u\n", error);
666 }
667
668 static void test_message_from_hmodule(void)
669 {
670 DWORD ret, error;
671 HMODULE h;
672 CHAR out[0x100] = {0};
673
674 h = GetModuleHandle("kernel32.dll");
675 ok(h != 0, "GetModuleHandle failed\n");
676
677 /*Test existing messageID; as the message strings from wine's kernel32 differ from windows' kernel32 we don't compare
678 the strings but only test that FormatMessage doesn't return 0*/
679 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, h, 7/*=ERROR_ARENA_TRASHED*/,
680 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), out, sizeof(out)/sizeof(CHAR), NULL);
681 ok(ret != 0, "FormatMessageA returned 0\n");
682
683 /*Test nonexistent messageID with varying language ID's Note: FormatMessageW behaves the same*/
684 SetLastError(0xdeadbeef);
685 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, h, 3044,
686 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), out, sizeof(out)/sizeof(CHAR), NULL);
687 error = GetLastError();
688 ok(ret == 0, "FormatMessageA returned %u instead of 0\n", ret);
689 ok(error == ERROR_MR_MID_NOT_FOUND || error == ERROR_MUI_FILE_NOT_FOUND, "last error %u\n", error);
690
691 SetLastError(0xdeadbeef);
692 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, h, 3044,
693 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), out, sizeof(out)/sizeof(CHAR), NULL);
694 error = GetLastError();
695 ok(ret == 0, "FormatMessageA returned %u instead of 0\n", ret);
696 ok(error == ERROR_MR_MID_NOT_FOUND || error == ERROR_MUI_FILE_NOT_LOADED, "last error %u\n", error);
697
698 SetLastError(0xdeadbeef);
699 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, h, 3044,
700 MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT), out, sizeof(out)/sizeof(CHAR), NULL);
701 error = GetLastError();
702 ok(ret == 0, "FormatMessageA returned %u instead of 0\n", ret);
703 ok(error == ERROR_MR_MID_NOT_FOUND || error == ERROR_MUI_FILE_NOT_LOADED, "last error %u\n", error);
704
705 SetLastError(0xdeadbeef);
706 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, h, 3044,
707 MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), out, sizeof(out)/sizeof(CHAR), NULL);
708 error = GetLastError();
709 ok(ret == 0, "FormatMessageA returned %u instead of 0\n", ret);
710 ok(error == ERROR_RESOURCE_LANG_NOT_FOUND ||
711 error == ERROR_MR_MID_NOT_FOUND ||
712 error == ERROR_MUI_FILE_NOT_LOADED,
713 "last error %u\n", error);
714
715 SetLastError(0xdeadbeef);
716 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, h, 3044,
717 MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_UK), out, sizeof(out)/sizeof(CHAR), NULL);
718 error = GetLastError();
719 ok(ret == 0, "FormatMessageA returned %u instead of 0\n", ret);
720 ok(error == ERROR_RESOURCE_LANG_NOT_FOUND ||
721 error == ERROR_MR_MID_NOT_FOUND ||
722 error == ERROR_MUI_FILE_NOT_FOUND,
723 "last error %u\n", error);
724 }
725
726 START_TEST(format_msg)
727 {
728 test_message_from_string();
729 test_message_from_string_wide();
730 test_message_null_buffer();
731 test_message_from_hmodule();
732 }