Release 1.6-rc2.
[wine/testsucceed.git] / dlls / kernel32 / tests / format_msg.c
blob005e6c4a66cf26fe8b5dbbdfd50c8123141f894d
1 /* Unit test suite for FormatMessageA/W
3 * Copyright 2002 Mike McCormack for CodeWeavers
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.
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.
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
20 #include <stdarg.h>
22 #include "wine/test.h"
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winnls.h"
27 static DWORD __cdecl doit(DWORD flags, LPCVOID src, DWORD msg_id, DWORD lang_id,
28 LPSTR out, DWORD outsize, ... )
30 __ms_va_list list;
31 DWORD r;
33 __ms_va_start(list, outsize);
34 r = FormatMessageA(flags, src, msg_id,
35 lang_id, out, outsize, &list);
36 __ms_va_end(list);
37 return r;
40 static DWORD __cdecl doitW(DWORD flags, LPCVOID src, DWORD msg_id, DWORD lang_id,
41 LPWSTR out, DWORD outsize, ... )
43 __ms_va_list list;
44 DWORD r;
46 __ms_va_start(list, outsize);
47 r = FormatMessageW(flags, src, msg_id,
48 lang_id, out, outsize, &list);
49 __ms_va_end(list);
50 return r;
53 static void test_message_from_string_wide(void)
55 static const WCHAR test[] = {'t','e','s','t',0};
56 static const WCHAR empty[] = {0};
57 static const WCHAR te[] = {'t','e',0};
58 static const WCHAR st[] = {'s','t',0};
59 static const WCHAR t[] = {'t',0};
60 static const WCHAR e[] = {'e',0};
61 static const WCHAR s[] = {'s',0};
62 static const WCHAR fmt_null[] = {'%',0};
63 static const WCHAR fmt_tnull[] = {'t','e','s','t','%',0};
64 static const WCHAR fmt_1[] = {'%','1',0};
65 static const WCHAR fmt_12[] = {'%','1','%','2',0};
66 static const WCHAR fmt_123[] = {'%','1','%','3','%','2','%','1',0};
67 static const WCHAR fmt_123c[] = {'%','1','!','c','!','%','2','!','c','!','%','3','!','c','!','%','1','!','c','!',0};
68 static const WCHAR fmt_123lc[] = {'%','1','!','l','c','!','%','2','!','l','c','!','%','3','!','l','c','!','%','1','!','l','c','!',0};
69 static const WCHAR fmt_123wc[] = {'%','1','!','w','c','!','%','2','!','w','c','!','%','3','!','w','c','!','%','1','!','w','c','!',0};
70 static const WCHAR fmt_123C[] = {'%','1','!','C','!','%','2','!','C','!','%','3','!','C','!','%','1','!','C','!',0};
71 static const WCHAR fmt_123d[] = {'%','1','!','d','!','%','2','!','d','!','%','3','!','d','!',0};
72 static const WCHAR fmt_1s[] = {'%','1','!','s','!',0};
73 static const WCHAR fmt_s[] = {'%','!','s','!',0};
74 static const WCHAR fmt_ls[] = {'%','!','l','s','!',0};
75 static const WCHAR fmt_ws[] = {'%','!','w','s','!',0};
76 static const WCHAR fmt_S[] = {'%','!','S','!',0};
77 static const WCHAR fmt_14d[] = {'%','1','!','4','d','!',0};
78 static const WCHAR fmt_14x[] = {'%','1','!','4','x','!',0};
79 static const WCHAR fmt_14X[] = {'%','1','!','4','X','!',0};
80 static const WCHAR fmt_1_4X[] = {'%','1','!','-','4','X','!',0};
81 static const WCHAR fmt_1_4d[] = {'%','1','!','-','4','d','!',0};
82 static const WCHAR fmt_2pct[] = {' ','%','%','%','%',' ',0};
83 static const WCHAR fmt_2dot1d[] = {' ', '%','.','%','.',' ',' ','%','1','!','d','!',0};
84 static const WCHAR fmt_t0t[] = {'t','e','s','t','%','0','t','e','s','t',0};
85 static const WCHAR fmt_yah[] = {'y','a','h','%','!','%','0',' ',' ',' ',0};
86 static const WCHAR fmt_space[] = {'%',' ','%',' ',' ',' ',0};
87 static const WCHAR fmt_nrt[] = {'%','n','%','r','%','t',0};
88 static const WCHAR fmt_hi_lf[] = {'h','i','\n',0};
89 static const WCHAR fmt_hi_crlf[] = {'h','i','\r','\n',0};
90 static const WCHAR fmt_cr[] = {'\r',0};
91 static const WCHAR fmt_crcrlf[] = {'\r','\r','\n',0};
92 static const WCHAR fmt_13s[] = {'%','1','!','3','s','!',0};
93 static const WCHAR fmt_1os[] = {'%','1','!','*','s','!',0};
94 static const WCHAR fmt_142u[] = {'%','1','!','4','.','2','u','!',0};
95 static const WCHAR fmt_1oou[] = {'%','1','!','*','.','*','u','!',0};
96 static const WCHAR fmt_1oou1oou[] = {'%','1','!','*','.','*','u','!',',','%','1','!','*','.','*','u','!',0};
97 static const WCHAR fmt_1oou3oou[] = {'%','1','!','*','.','*','u','!',',','%','3','!','*','.','*','u','!',0};
98 static const WCHAR fmt_1oou4oou[] = {'%','1','!','*','.','*','u','!',',','%','4','!','*','.','*','u','!',0};
100 static const WCHAR s_123d[] = {'1','2','3',0};
101 static const WCHAR s_14d[] = {' ',' ',' ','1',0};
102 static const WCHAR s_14x[] = {' ',' ',' ','b',0};
103 static const WCHAR s_14X[] = {' ',' ',' ','B',0};
104 static const WCHAR s_1_4X[] = {'B',' ',' ',' ',0};
105 static const WCHAR s_14d2[] = {' ',' ','1','1',0};
106 static const WCHAR s_1_4d[] = {'1',' ',' ',' ',0};
107 static const WCHAR s_1AB[] = {' ','1','A','B',0};
108 static const WCHAR s_2pct[] = {' ','%','%',' ',0};
109 static const WCHAR s_2dot147[] = {' ','.','.',' ',' ','4','2','7',0};
110 static const WCHAR s_yah[] = {'y','a','h','!',0};
111 static const WCHAR s_space[] = {' ',' ',' ',' ',0};
112 static const WCHAR s_nrt[] = {'\r','\n','\r','\t',0};
113 static const WCHAR s_hi_crlf[] = {'h','i','\r','\n',0};
114 static const WCHAR s_crlf[] = {'\r','\n',0};
115 static const WCHAR s_crlfcrlf[] = {'\r','\n','\r','\n',0};
116 static const WCHAR s_hi_sp[] = {'h','i',' ',0};
117 static const WCHAR s_sp[] = {' ',0};
118 static const WCHAR s_2sp[] = {' ',' ',0};
119 static const WCHAR s_spt[] = {' ',' ','t',0};
120 static const WCHAR s_sp3t[] = {' ',' ',' ','t',0};
121 static const WCHAR s_sp03[] = {' ',' ','0','3',0};
122 static const WCHAR s_sp001[] = {' ',' ','0','0','1',0};
123 static const WCHAR s_sp001002[] = {' ',' ','0','0','1',',',' ','0','0','0','2',0};
124 static const WCHAR s_sp001sp002[] = {' ',' ','0','0','1',',',' ',' ','0','0','0','2',0};
125 static const WCHAR s_sp002sp001[] = {' ',' ','0','0','0','2',',',' ',' ','0','0','1',0};
126 static const WCHAR s_sp002sp003[] = {' ',' ','0','0','0','2',',',' ','0','0','0','0','3',0};
127 static const WCHAR s_sp001004[] = {' ',' ','0','0','1',',','0','0','0','0','0','4',0};
128 static const WCHAR s_null[] = {'(','n','u','l','l',')',0};
130 static const WCHAR init_buf[] = {'x', 'x', 'x', 'x', 'x', 'x'};
131 static const WCHAR broken_buf[] = {'t','e','s','t','x','x'};
133 WCHAR out[0x100] = {0};
134 DWORD r, error;
136 /* the basics */
137 r = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, test, 0,
138 0, out, sizeof(out)/sizeof(WCHAR), NULL);
139 ok(!lstrcmpW(test, out), "failed out=%s\n", wine_dbgstr_w(out));
140 ok(r==4, "failed: r=%d\n", r);
142 /* null string, crashes on Windows */
143 if (0)
145 SetLastError(0xdeadbeef);
146 memcpy(out, init_buf, sizeof(init_buf));
147 FormatMessageW(FORMAT_MESSAGE_FROM_STRING, NULL, 0,
148 0, out, sizeof(out)/sizeof(WCHAR), NULL);
151 /* empty string */
152 SetLastError(0xdeadbeef);
153 memcpy(out, init_buf, sizeof(init_buf));
154 r = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, empty, 0,
155 0, out, sizeof(out)/sizeof(WCHAR), NULL);
156 error = GetLastError();
157 ok(!lstrcmpW(empty, out), "failed out=%s\n", wine_dbgstr_w(out));
158 ok(r==0, "succeeded: r=%d\n", r);
159 ok(error==0xdeadbeef, "last error %u\n", error);
161 /* format placeholder with no specifier */
162 SetLastError(0xdeadbeef);
163 memcpy(out, init_buf, sizeof(init_buf));
164 r = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, fmt_null, 0,
165 0, out, sizeof(out)/sizeof(WCHAR), NULL);
166 error = GetLastError();
167 ok(!memcmp(out, init_buf, sizeof(init_buf)),
168 "Expected the buffer to be unchanged\n");
169 ok(r==0, "succeeded: r=%d\n", r);
170 ok(error==ERROR_INVALID_PARAMETER, "last error %u\n", error);
172 /* test string with format placeholder with no specifier */
173 SetLastError(0xdeadbeef);
174 memcpy(out, init_buf, sizeof(init_buf));
175 r = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, fmt_tnull, 0,
176 0, out, sizeof(out)/sizeof(WCHAR), NULL);
177 error = GetLastError();
178 ok(!memcmp(out, init_buf, sizeof(init_buf)) ||
179 broken(!memcmp(out, broken_buf, sizeof(broken_buf))), /* W2K3+ */
180 "Expected the buffer to be unchanged\n");
181 ok(r==0, "succeeded: r=%d\n", r);
182 ok(error==ERROR_INVALID_PARAMETER, "last error %u\n", error);
184 /* insertion with no variadic arguments */
185 SetLastError(0xdeadbeef);
186 memcpy(out, init_buf, sizeof(init_buf));
187 r = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, fmt_1, 0,
188 0, out, sizeof(out)/sizeof(WCHAR), NULL);
189 error = GetLastError();
190 ok(!memcmp(out, init_buf, sizeof(init_buf)),
191 "Expected the buffer to be unchanged\n");
192 ok(r==0, "succeeded: r=%d\n", r);
193 ok(error==ERROR_INVALID_PARAMETER, "last error %u\n", error);
195 SetLastError(0xdeadbeef);
196 memcpy(out, init_buf, sizeof(init_buf));
197 r = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY, fmt_1, 0,
198 0, out, sizeof(out)/sizeof(WCHAR), NULL);
199 error = GetLastError();
200 ok(!memcmp(out, init_buf, sizeof(init_buf)),
201 "Expected the buffer to be unchanged\n");
202 ok(r==0, "succeeded: r=%d\n", r);
203 ok(error==ERROR_INVALID_PARAMETER, "last error %u\n", error);
205 /* using the format feature */
206 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_1s, 0,
207 0, out, sizeof(out)/sizeof(WCHAR), test);
208 ok(!lstrcmpW(test, out), "failed out=%s\n", wine_dbgstr_w(out));
209 ok(r==4,"failed: r=%d\n", r);
211 /* no format */
212 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_1, 0,
213 0, out, sizeof(out)/sizeof(WCHAR), test);
214 ok(!lstrcmpW(test, out), "failed out=%s\n", wine_dbgstr_w(out));
215 ok(r==4,"failed: r=%d\n", r);
217 /* two pieces */
218 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_12, 0,
219 0, out, sizeof(out)/sizeof(WCHAR), te, st);
220 ok(!lstrcmpW(test, out), "failed out=%s\n", wine_dbgstr_w(out));
221 ok(r==4,"failed: r=%d\n", r);
223 /* three pieces */
224 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_123, 0,
225 0, out, sizeof(out)/sizeof(WCHAR), t, s, e);
226 ok(!lstrcmpW(test, out), "failed out=%s\n", wine_dbgstr_w(out));
227 ok(r==4,"failed: r=%d\n", r);
229 /* s doesn't seem to work in format strings */
230 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_s, 0,
231 0, out, sizeof(out)/sizeof(WCHAR), test);
232 ok(!lstrcmpW(&fmt_s[1], out), "failed out=%s\n", wine_dbgstr_w(out));
233 ok(r==3, "failed: r=%d\n", r);
235 /* nor ls */
236 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_ls, 0,
237 0, out, sizeof(out)/sizeof(WCHAR), test);
238 ok(!lstrcmpW(&fmt_ls[1], out), "failed out=%s\n", wine_dbgstr_w(out));
239 ok(r==4, "failed: r=%d\n", r);
241 /* nor S */
242 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_S, 0,
243 0, out, sizeof(out)/sizeof(WCHAR), test);
244 ok(!lstrcmpW(&fmt_S[1], out), "failed out=%s\n", wine_dbgstr_w(out));
245 ok(r==3, "failed: r=%d\n", r);
247 /* nor ws */
248 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_ws, 0,
249 0, out, sizeof(out)/sizeof(WCHAR), test);
250 ok(!lstrcmpW(&fmt_ws[1], out), "failed out=%s\n", wine_dbgstr_w(out));
251 ok(r==4, "failed: r=%d\n", r);
253 /* as characters */
254 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_123c, 0,
255 0, out, sizeof(out)/sizeof(WCHAR), 't', 'e', 's');
256 ok(!lstrcmpW(test, out), "failed out=%s\n", wine_dbgstr_w(out));
257 ok(r==4,"failed: r=%d\n", r);
259 /* lc is unicode */
260 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_123lc, 0,
261 0, out, sizeof(out)/sizeof(WCHAR), 't', 'e', 's');
262 ok(!lstrcmpW(test, out), "failed out=%s\n", wine_dbgstr_w(out));
263 ok(r==4,"failed: r=%d\n", r);
265 /* wc is unicode */
266 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_123wc, 0,
267 0, out, sizeof(out)/sizeof(WCHAR), 't', 'e', 's');
268 ok(!lstrcmpW(test, out), "failed out=%s\n", wine_dbgstr_w(out));
269 ok(r==4,"failed: r=%d\n", r);
271 /* C is unicode */
272 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_123C, 0,
273 0, out, sizeof(out)/sizeof(WCHAR), 't', 'e', 's');
274 ok(!lstrcmpW(test, out), "failed out=%s\n", wine_dbgstr_w(out));
275 ok(r==4,"failed: r=%d\n", r);
277 /* some numbers */
278 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_123d, 0,
279 0, out, sizeof(out)/sizeof(WCHAR), 1, 2, 3);
280 ok(!lstrcmpW(s_123d, out), "failed out=%s\n", wine_dbgstr_w(out));
281 ok(r==3,"failed: r=%d\n", r);
283 /* a single digit with some spacing */
284 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_14d, 0,
285 0, out, sizeof(out)/sizeof(WCHAR), 1);
286 ok(!lstrcmpW(s_14d, out), "failed out=%s\n", wine_dbgstr_w(out));
287 ok(r==4,"failed: r=%d\n", r);
289 /* a single digit, left justified */
290 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_1_4d, 0,
291 0, out, sizeof(out)/sizeof(CHAR), 1);
292 ok(!lstrcmpW(s_1_4d, out), "failed out=%s\n", wine_dbgstr_w(out));
293 ok(r==4,"failed: r=%d\n", r);
295 /* two digit decimal number */
296 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_14d, 0,
297 0, out, sizeof(out)/sizeof(WCHAR), 11);
298 ok(!lstrcmpW(s_14d2, out), "failed out=%s\n", wine_dbgstr_w(out));
299 ok(r==4,"failed: r=%d\n", r);
301 /* a hex number */
302 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_14x, 0,
303 0, out, sizeof(out)/sizeof(WCHAR), 11);
304 ok(!lstrcmpW(s_14x, out), "failed out=%s\n", wine_dbgstr_w(out));
305 ok(r==4,"failed: r=%d\n", r);
307 /* a hex number, upper case */
308 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_14X, 0,
309 0, out, sizeof(out)/sizeof(WCHAR), 11);
310 ok(!lstrcmpW(s_14X, out), "failed out=%s\n", wine_dbgstr_w(out));
311 ok(r==4,"failed: r=%d\n", r);
313 /* a hex number, upper case, left justified */
314 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_1_4X, 0,
315 0, out, sizeof(out)/sizeof(WCHAR), 11);
316 ok(!lstrcmpW(s_1_4X, out), "failed out=%s\n", wine_dbgstr_w(out));
317 ok(r==4,"failed: r=%d\n", r);
319 /* a long hex number, upper case */
320 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_14X, 0,
321 0, out, sizeof(out)/sizeof(WCHAR), 0x1ab);
322 ok(!lstrcmpW(s_1AB, out), "failed out=%s\n", wine_dbgstr_w(out));
323 ok(r==4,"failed: r=%d\n", r);
325 /* two percent... */
326 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_2pct, 0,
327 0, out, sizeof(out)/sizeof(WCHAR));
328 ok(!lstrcmpW(s_2pct, out), "failed out=%s\n", wine_dbgstr_w(out));
329 ok(r==4,"failed: r=%d\n", r);
331 /* periods are special cases */
332 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_2dot1d, 0,
333 0, out, sizeof(out)/sizeof(WCHAR), 0x1ab);
334 ok(!lstrcmpW(s_2dot147, out), "failed out=%s\n", wine_dbgstr_w(out));
335 ok(r==8,"failed: r=%d\n", r);
337 /* %0 ends the line */
338 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_t0t, 0,
339 0, out, sizeof(out)/sizeof(WCHAR));
340 ok(!lstrcmpW(test, out), "failed out=%s\n", wine_dbgstr_w(out));
341 ok(r==4,"failed: r=%d\n", r);
343 /* %! prints an exclamation */
344 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_yah, 0,
345 0, out, sizeof(out)/sizeof(WCHAR));
346 ok(!lstrcmpW(s_yah, out), "failed out=%s\n", wine_dbgstr_w(out));
347 ok(r==4,"failed: r=%d\n", r);
349 /* %space */
350 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_space, 0,
351 0, out, sizeof(out)/sizeof(WCHAR));
352 ok(!lstrcmpW(s_space, out), "failed out=%s\n", wine_dbgstr_w(out));
353 ok(r==4,"failed: r=%d\n", r);
355 /* %n yields \r\n, %r yields \r, %t yields \t */
356 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_nrt, 0,
357 0, out, sizeof(out)/sizeof(WCHAR));
358 ok(!lstrcmpW(s_nrt, out), "failed out=%s\n", wine_dbgstr_w(out));
359 ok(r==4,"failed: r=%d\n", r);
361 /* line feed */
362 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_hi_lf, 0,
363 0, out, sizeof(out)/sizeof(WCHAR));
364 ok(!lstrcmpW(s_hi_crlf, out), "failed out=%s\n", wine_dbgstr_w(out));
365 ok(r==4,"failed: r=%d\n", r);
367 /* carriage return line feed */
368 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_hi_crlf, 0,
369 0, out, sizeof(out)/sizeof(WCHAR));
370 ok(!lstrcmpW(s_hi_crlf, out), "failed out=%s\n", wine_dbgstr_w(out));
371 ok(r==4,"failed: r=%d\n", r);
373 /* carriage return */
374 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_cr, 0,
375 0, out, sizeof(out)/sizeof(WCHAR));
376 ok(!lstrcmpW(s_crlf, out), "failed out=%s\n", wine_dbgstr_w(out));
377 ok(r==2,"failed: r=%d\n", r);
379 /* double carriage return line feed */
380 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_crcrlf, 0,
381 0, out, sizeof(out)/sizeof(WCHAR));
382 ok(!lstrcmpW(s_crlfcrlf, out), "failed out=%s\n", wine_dbgstr_w(out));
383 ok(r==4,"failed: r=%d\n", r);
385 /* null string as argument */
386 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_1, 0,
387 0, out, sizeof(out)/sizeof(WCHAR), NULL);
388 ok(!lstrcmpW(s_null, out),"failed out=[%s]\n", wine_dbgstr_w(out));
389 ok(r==6,"failed: r=%d\n",r);
391 /* precision and width */
393 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_13s,
394 0, 0, out, sizeof(out)/sizeof(WCHAR), t );
395 ok(!lstrcmpW(s_spt, out),"failed out=[%s]\n", wine_dbgstr_w(out));
396 ok(r==3, "failed: r=%d\n",r);
397 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_1os,
398 0, 0, out, sizeof(out)/sizeof(WCHAR), 4, t );
399 ok(!lstrcmpW( s_sp3t, out),"failed out=[%s]\n", wine_dbgstr_w(out));
400 ok(r==4,"failed: r=%d\n",r);
401 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_142u,
402 0, 0, out, sizeof(out)/sizeof(WCHAR), 3 );
403 ok(!lstrcmpW( s_sp03, out),"failed out=[%s]\n", wine_dbgstr_w(out));
404 ok(r==4,"failed: r=%d\n",r);
405 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_1oou,
406 0, 0, out, sizeof(out)/sizeof(WCHAR), 5, 3, 1 );
407 ok(!lstrcmpW( s_sp001, out),"failed out=[%s]\n", wine_dbgstr_w(out));
408 ok(r==5,"failed: r=%d\n",r);
409 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_1oou1oou,
410 0, 0, out, sizeof(out)/sizeof(WCHAR), 5, 3, 1, 4, 2 );
411 ok(!lstrcmpW( s_sp001002, out),"failed out=[%s]\n", wine_dbgstr_w(out));
412 ok(r==11,"failed: r=%d\n",r);
413 r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_1oou3oou,
414 0, 0, out, sizeof(out)/sizeof(WCHAR), 5, 3, 1, 6, 4, 2 );
415 ok(!lstrcmpW( s_sp001sp002, out) ||
416 broken(!lstrcmpW(s_sp001004, out)), /* NT4/Win2k */
417 "failed out=[%s]\n", wine_dbgstr_w(out));
418 ok(r==12,"failed: r=%d\n",r);
419 /* args are not counted the same way with an argument array */
421 ULONG_PTR args[] = { 6, 4, 2, 5, 3, 1 };
422 r = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY, fmt_1oou1oou,
423 0, 0, out, sizeof(out)/sizeof(WCHAR), (__ms_va_list *)args );
424 ok(!lstrcmpW(s_sp002sp003, out),"failed out=[%s]\n", wine_dbgstr_w(out));
425 ok(r==13,"failed: r=%d\n",r);
426 r = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY, fmt_1oou4oou,
427 0, 0, out, sizeof(out)/sizeof(WCHAR), (__ms_va_list *)args );
428 ok(!lstrcmpW(s_sp002sp001, out),"failed out=[%s]\n", wine_dbgstr_w(out));
429 ok(r==12,"failed: r=%d\n",r);
432 /* change of pace... test the low byte of dwflags */
434 /* line feed */
435 r = doitW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, fmt_hi_lf, 0,
436 0, out, sizeof(out)/sizeof(WCHAR));
437 ok(!lstrcmpW(s_hi_sp, out), "failed out=%s\n", wine_dbgstr_w(out));
438 ok(r==3,"failed: r=%d\n", r);
440 /* carriage return line feed */
441 r = doitW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, fmt_hi_crlf, 0,
442 0, out, sizeof(out)/sizeof(WCHAR));
443 ok(!lstrcmpW(s_hi_sp, out), "failed out=%s\n", wine_dbgstr_w(out));
444 ok(r==3,"failed: r=%d\n", r);
446 /* carriage return */
447 r = doitW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, fmt_cr, 0,
448 0, out, sizeof(out)/sizeof(WCHAR));
449 ok(!lstrcmpW(s_sp, out), "failed out=%s\n", wine_dbgstr_w(out));
450 ok(r==1,"failed: r=%d\n", r);
452 /* double carriage return line feed */
453 r = doitW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, fmt_crcrlf, 0,
454 0, out, sizeof(out)/sizeof(WCHAR));
455 ok(!lstrcmpW(s_2sp, out), "failed out=%s\n", wine_dbgstr_w(out));
456 ok(r==2,"failed: r=%d\n", r);
459 static void test_message_from_string(void)
461 CHAR out[0x100] = {0};
462 DWORD r;
463 static const char init_buf[] = {'x', 'x', 'x', 'x', 'x', 'x'};
464 static const WCHAR szwTest[] = { 't','e','s','t',0};
466 /* the basics */
467 r = FormatMessageA(FORMAT_MESSAGE_FROM_STRING, "test", 0,
468 0, out, sizeof(out)/sizeof(CHAR),NULL);
469 ok(!strcmp("test", out),"failed out=[%s]\n",out);
470 ok(r==4,"failed: r=%d\n",r);
472 /* null string, crashes on Windows */
473 if (0)
475 SetLastError(0xdeadbeef);
476 memcpy(out, init_buf, sizeof(init_buf));
477 FormatMessageA(FORMAT_MESSAGE_FROM_STRING, NULL, 0,
478 0, out, sizeof(out)/sizeof(CHAR), NULL);
481 /* empty string */
482 SetLastError(0xdeadbeef);
483 memcpy(out, init_buf, sizeof(init_buf));
484 r = FormatMessageA(FORMAT_MESSAGE_FROM_STRING, "", 0,
485 0, out, sizeof(out)/sizeof(CHAR), NULL);
486 ok(!memcmp(out, init_buf, sizeof(init_buf)), "Expected the buffer to be untouched\n");
487 ok(r==0, "succeeded: r=%d\n", r);
488 ok(GetLastError()==0xdeadbeef,
489 "last error %u\n", GetLastError());
491 /* format placeholder with no specifier */
492 SetLastError(0xdeadbeef);
493 memcpy(out, init_buf, sizeof(init_buf));
494 r = FormatMessageA(FORMAT_MESSAGE_FROM_STRING, "%", 0,
495 0, out, sizeof(out)/sizeof(CHAR), NULL);
496 ok(!memcmp(out, init_buf, sizeof(init_buf)),
497 "Expected the buffer to be untouched\n");
498 ok(r==0, "succeeded: r=%d\n", r);
499 ok(GetLastError()==ERROR_INVALID_PARAMETER,
500 "last error %u\n", GetLastError());
502 /* test string with format placeholder with no specifier */
503 SetLastError(0xdeadbeef);
504 memcpy(out, init_buf, sizeof(init_buf));
505 r = FormatMessageA(FORMAT_MESSAGE_FROM_STRING, "test%", 0,
506 0, out, sizeof(out)/sizeof(CHAR), NULL);
507 ok(!memcmp(out, init_buf, sizeof(init_buf)),
508 "Expected the buffer to be untouched\n");
509 ok(r==0, "succeeded: r=%d\n", r);
510 ok(GetLastError()==ERROR_INVALID_PARAMETER,
511 "last error %u\n", GetLastError());
513 /* insertion with no variadic arguments */
514 SetLastError(0xdeadbeef);
515 memcpy(out, init_buf, sizeof(init_buf));
516 r = FormatMessageA(FORMAT_MESSAGE_FROM_STRING, "%1", 0,
517 0, out, sizeof(out)/sizeof(CHAR), NULL);
518 ok(!memcmp(out, init_buf, sizeof(init_buf)), "Expected the buffer to be untouched\n");
519 ok(r==0, "succeeded: r=%d\n", r);
520 ok(GetLastError()==ERROR_INVALID_PARAMETER, "last error %u\n", GetLastError());
522 SetLastError(0xdeadbeef);
523 memcpy(out, init_buf, sizeof(init_buf));
524 r = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY, "%1", 0,
525 0, out, sizeof(out)/sizeof(CHAR), NULL);
526 ok(!memcmp(out, init_buf, sizeof(init_buf)), "Expected the buffer to be untouched\n");
527 ok(r==0, "succeeded: r=%d\n", r);
528 ok(GetLastError()==ERROR_INVALID_PARAMETER, "last error %u\n", GetLastError());
530 /* using the format feature */
531 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!s!", 0,
532 0, out, sizeof(out)/sizeof(CHAR), "test");
533 ok(!strcmp("test", out),"failed out=[%s]\n",out);
534 ok(r==4,"failed: r=%d\n",r);
536 /* no format */
537 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1", 0,
538 0, out, sizeof(out)/sizeof(CHAR), "test");
539 ok(!strcmp("test", out),"failed out=[%s]\n",out);
540 ok(r==4,"failed: r=%d\n",r);
542 /* two pieces */
543 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1%2", 0,
544 0, out, sizeof(out)/sizeof(CHAR), "te","st");
545 ok(!strcmp("test", out),"failed out=[%s]\n",out);
546 ok(r==4,"failed: r=%d\n",r);
548 /* three pieces */
549 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1%3%2%1", 0,
550 0, out, sizeof(out)/sizeof(CHAR), "t","s","e");
551 ok(!strcmp("test", out),"failed out=[%s]\n",out);
552 ok(r==4,"failed: r=%d\n",r);
554 /* s doesn't seem to work in format strings */
555 r = doit(FORMAT_MESSAGE_FROM_STRING, "%!s!", 0,
556 0, out, sizeof(out)/sizeof(CHAR), "test");
557 ok(!strcmp("!s!", out),"failed out=[%s]\n",out);
558 ok(r==3,"failed: r=%d\n",r);
560 /* ls is unicode */
561 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!ls!", 0,
562 0, out, sizeof(out)/sizeof(CHAR), szwTest);
563 ok(!strcmp("test", out),"failed out=[%s]\n",out);
564 ok(r==4,"failed: r=%d\n",r);
566 /* S is unicode */
567 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!S!", 0,
568 0, out, sizeof(out)/sizeof(CHAR), szwTest);
569 ok(!strcmp("test", out),"failed out=[%s]\n",out);
570 ok(r==4,"failed: r=%d\n",r);
572 /* ws is unicode */
573 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!ws!", 0,
574 0, out, sizeof(out)/sizeof(CHAR), szwTest);
575 ok(!strcmp("test", out),"failed out=[%s]\n",out);
576 ok(r==4,"failed: r=%d\n",r);
578 /* as characters */
579 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!c!%2!c!%3!c!%1!c!", 0,
580 0, out, sizeof(out)/sizeof(CHAR), 't','e','s');
581 ok(!strcmp("test", out),"failed out=[%s]\n",out);
582 ok(r==4,"failed: r=%d\n",r);
584 /* lc is unicode */
585 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!lc!%2!lc!%3!lc!%1!lc!", 0,
586 0, out, sizeof(out)/sizeof(CHAR), 't','e','s');
587 ok(!strcmp("test", out),"failed out=[%s]\n",out);
588 ok(r==4,"failed: r=%d\n",r);
590 /* wc is unicode */
591 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!wc!%2!wc!%3!wc!%1!wc!", 0,
592 0, out, sizeof(out)/sizeof(CHAR), 't','e','s');
593 ok(!strcmp("test", out),"failed out=[%s]\n",out);
594 ok(r==4,"failed: r=%d\n",r);
596 /* C is unicode */
597 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!C!%2!C!%3!C!%1!C!", 0,
598 0, out, sizeof(out)/sizeof(CHAR), 't','e','s');
599 ok(!strcmp("test", out),"failed out=[%s]\n",out);
600 ok(r==4,"failed: r=%d\n",r);
602 /* some numbers */
603 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!d!%2!d!%3!d!", 0,
604 0, out, sizeof(out)/sizeof(CHAR), 1,2,3);
605 ok(!strcmp("123", out),"failed out=[%s]\n",out);
606 ok(r==3,"failed: r=%d\n",r);
608 /* a single digit with some spacing */
609 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4d!", 0,
610 0, out, sizeof(out)/sizeof(CHAR), 1);
611 ok(!strcmp(" 1", out),"failed out=[%s]\n",out);
612 ok(r==4,"failed: r=%d\n",r);
614 /* a single digit, left justified */
615 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!-4d!", 0,
616 0, out, sizeof(out)/sizeof(CHAR), 1);
617 ok(!strcmp("1 ", out),"failed out=[%s]\n",out);
618 ok(r==4,"failed: r=%d\n",r);
620 /* two digit decimal number */
621 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4d!", 0,
622 0, out, sizeof(out)/sizeof(CHAR), 11);
623 ok(!strcmp(" 11", out),"failed out=[%s]\n",out);
624 ok(r==4,"failed: r=%d\n",r);
626 /* a hex number */
627 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4x!", 0,
628 0, out, sizeof(out)/sizeof(CHAR), 11);
629 ok(!strcmp(" b", out),"failed out=[%s]\n",out);
630 ok(r==4,"failed: r=%d\n",r);
632 /* a hex number, upper case */
633 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4X!", 0,
634 0, out, sizeof(out)/sizeof(CHAR), 11);
635 ok(!strcmp(" B", out),"failed out=[%s]\n",out);
636 ok(r==4,"failed: r=%d\n",r);
638 /* a hex number, upper case, left justified */
639 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!-4X!", 0,
640 0, out, sizeof(out)/sizeof(CHAR), 11);
641 ok(!strcmp("B ", out),"failed out=[%s]\n",out);
642 ok(r==4,"failed: r=%d\n",r);
644 /* a long hex number, upper case */
645 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4X!", 0,
646 0, out, sizeof(out)/sizeof(CHAR), 0x1ab);
647 ok(!strcmp(" 1AB", out),"failed out=[%s]\n",out);
648 ok(r==4,"failed: r=%d\n",r);
650 /* two percent... */
651 r = doit(FORMAT_MESSAGE_FROM_STRING, " %%%% ", 0,
652 0, out, sizeof(out)/sizeof(CHAR));
653 ok(!strcmp(" %% ", out),"failed out=[%s]\n",out);
654 ok(r==4,"failed: r=%d\n",r);
656 /* periods are special cases */
657 r = doit(FORMAT_MESSAGE_FROM_STRING, " %.%. %1!d!", 0,
658 0, out, sizeof(out)/sizeof(CHAR), 0x1ab);
659 ok(!strcmp(" .. 427", out),"failed out=[%s]\n",out);
660 ok(r==7,"failed: r=%d\n",r);
662 /* %0 ends the line */
663 r = doit(FORMAT_MESSAGE_FROM_STRING, "test%0test", 0,
664 0, out, sizeof(out)/sizeof(CHAR));
665 ok(!strcmp("test", out),"failed out=[%s]\n",out);
666 ok(r==4,"failed: r=%d\n",r);
668 /* %! prints an exclamation */
669 r = doit(FORMAT_MESSAGE_FROM_STRING, "yah%!%0 ", 0,
670 0, out, sizeof(out)/sizeof(CHAR));
671 ok(!strcmp("yah!", out),"failed out=[%s]\n",out);
672 ok(r==4,"failed: r=%d\n",r);
674 /* %space */
675 r = doit(FORMAT_MESSAGE_FROM_STRING, "% % ", 0,
676 0, out, sizeof(out)/sizeof(CHAR));
677 ok(!strcmp(" ", out),"failed out=[%s]\n",out);
678 ok(r==4,"failed: r=%d\n",r);
680 /* %n yields \r\n, %r yields \r, %t yields \t */
681 r = doit(FORMAT_MESSAGE_FROM_STRING, "%n%r%t", 0,
682 0, out, sizeof(out)/sizeof(CHAR));
683 ok(!strcmp("\r\n\r\t", out),"failed out=[%s]\n",out);
684 ok(r==4,"failed: r=%d\n",r);
686 /* line feed */
687 r = doit(FORMAT_MESSAGE_FROM_STRING, "hi\n", 0,
688 0, out, sizeof(out)/sizeof(CHAR));
689 ok(!strcmp("hi\r\n", out),"failed out=[%s]\n",out);
690 ok(r==4,"failed: r=%d\n",r);
692 /* carriage return line feed */
693 r = doit(FORMAT_MESSAGE_FROM_STRING, "hi\r\n", 0,
694 0, out, sizeof(out)/sizeof(CHAR));
695 ok(!strcmp("hi\r\n", out),"failed out=[%s]\n",out);
696 ok(r==4,"failed: r=%d\n",r);
698 /* carriage return */
699 r = doit(FORMAT_MESSAGE_FROM_STRING, "\r", 0,
700 0, out, sizeof(out)/sizeof(CHAR));
701 ok(!strcmp("\r\n", out),"failed out=[%s]\n",out);
702 ok(r==2,"failed: r=%d\n",r);
704 /* double carriage return line feed */
705 r = doit(FORMAT_MESSAGE_FROM_STRING, "\r\r\n", 0,
706 0, out, sizeof(out)/sizeof(CHAR));
707 ok(!strcmp("\r\n\r\n", out),"failed out=[%s]\n",out);
708 ok(r==4,"failed: r=%d\n",r);
710 /* null string as argument */
711 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1", 0,
712 0, out, sizeof(out)/sizeof(CHAR), NULL);
713 ok(!strcmp("(null)", out),"failed out=[%s]\n",out);
714 ok(r==6,"failed: r=%d\n",r);
716 /* precision and width */
718 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!3s!",
719 0, 0, out, sizeof(out), "t" );
720 ok(!strcmp(" t", out),"failed out=[%s]\n",out);
721 ok(r==3, "failed: r=%d\n",r);
722 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!*s!",
723 0, 0, out, sizeof(out), 4, "t");
724 if (!strcmp("*s",out)) win_skip( "width/precision not supported\n" );
725 else
727 ok(!strcmp( " t", out),"failed out=[%s]\n",out);
728 ok(r==4,"failed: r=%d\n",r);
729 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4.2u!",
730 0, 0, out, sizeof(out), 3 );
731 ok(!strcmp( " 03", out),"failed out=[%s]\n",out);
732 ok(r==4,"failed: r=%d\n",r);
733 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!*.*u!",
734 0, 0, out, sizeof(out), 5, 3, 1 );
735 ok(!strcmp( " 001", out),"failed out=[%s]\n",out);
736 ok(r==5,"failed: r=%d\n",r);
737 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!*.*u!,%1!*.*u!",
738 0, 0, out, sizeof(out), 5, 3, 1, 4, 2 );
739 ok(!strcmp( " 001, 0002", out),"failed out=[%s]\n",out);
740 ok(r==11,"failed: r=%d\n",r);
741 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!*.*u!,%3!*.*u!",
742 0, 0, out, sizeof(out), 5, 3, 1, 6, 4, 2 );
743 /* older Win versions marked as broken even though this is arguably the correct behavior */
744 /* but the new (brain-damaged) behavior is specified on MSDN */
745 ok(!strcmp( " 001, 0002", out) ||
746 broken(!strcmp(" 001,000004", out)), /* NT4/Win2k */
747 "failed out=[%s]\n",out);
748 ok(r==12,"failed: r=%d\n",r);
749 /* args are not counted the same way with an argument array */
751 ULONG_PTR args[] = { 6, 4, 2, 5, 3, 1 };
752 r = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY,
753 "%1!*.*u!,%1!*.*u!", 0, 0, out, sizeof(out), (__ms_va_list *)args );
754 ok(!strcmp(" 0002, 00003", out),"failed out=[%s]\n",out);
755 ok(r==13,"failed: r=%d\n",r);
756 r = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY,
757 "%1!*.*u!,%4!*.*u!", 0, 0, out, sizeof(out), (__ms_va_list *)args );
758 ok(!strcmp(" 0002, 001", out),"failed out=[%s]\n",out);
759 ok(r==12,"failed: r=%d\n",r);
763 /* change of pace... test the low byte of dwflags */
765 /* line feed */
766 r = doit(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, "hi\n", 0,
767 0, out, sizeof(out)/sizeof(CHAR));
768 ok(!strcmp("hi ", out), "failed out=[%s]\n",out);
769 ok(r==3, "failed: r=%d\n",r);
771 /* carriage return line feed */
772 r = doit(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, "hi\r\n", 0,
773 0, out, sizeof(out)/sizeof(CHAR));
774 ok(!strcmp("hi ", out),"failed out=[%s]\n",out);
775 ok(r==3,"failed: r=%d\n",r);
777 /* carriage return */
778 r = doit(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, "\r", 0,
779 0, out, sizeof(out)/sizeof(CHAR));
780 ok(!strcmp(" ", out),"failed out=[%s]\n",out);
781 ok(r==1,"failed: r=%d\n",r);
783 /* double carriage return line feed */
784 r = doit(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, "\r\r\n", 0,
785 0, out, sizeof(out)/sizeof(CHAR));
786 ok(!strcmp(" ", out),"failed out=[%s]\n",out);
787 ok(r==2,"failed: r=%d\n",r);
790 static void test_message_ignore_inserts(void)
792 static const char init_buf[] = {'x', 'x', 'x', 'x', 'x'};
794 DWORD ret;
795 CHAR out[256];
797 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, "test", 0, 0, out,
798 sizeof(out)/sizeof(CHAR), NULL);
799 ok(ret == 4, "Expected FormatMessageA to return 4, got %d\n", ret);
800 ok(!strcmp("test", out), "Expected output string \"test\", got %s\n", out);
802 /* The %0 escape sequence is handled. */
803 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, "test%0", 0, 0, out,
804 sizeof(out)/sizeof(CHAR), NULL);
805 ok(ret == 4, "Expected FormatMessageA to return 4, got %d\n", ret);
806 ok(!strcmp("test", out), "Expected output string \"test\", got %s\n", out);
808 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, "test%0test", 0, 0, out,
809 sizeof(out)/sizeof(CHAR), NULL);
810 ok(ret == 4, "Expected FormatMessageA to return 4, got %d\n", ret);
811 ok(!strcmp("test", out), "Expected output string \"test\", got %s\n", out);
813 /* While FormatMessageA returns 0 in this case, no last error code is set. */
814 SetLastError(0xdeadbeef);
815 memcpy(out, init_buf, sizeof(init_buf));
816 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, "%0test", 0, 0, out,
817 sizeof(out)/sizeof(CHAR), NULL);
818 ok(ret == 0, "Expected FormatMessageA to return 0, got %d\n", ret);
819 ok(!memcmp(out, init_buf, sizeof(init_buf)), "Expected the output buffer to be untouched\n");
820 ok(GetLastError() == 0xdeadbeef, "Expected GetLastError() to return 0xdeadbeef, got %u\n", GetLastError());
822 /* Insert sequences are ignored. */
823 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, "test%1%2!*.*s!%99", 0, 0, out,
824 sizeof(out)/sizeof(CHAR), NULL);
825 ok(ret == 17, "Expected FormatMessageA to return 17, got %d\n", ret);
826 ok(!strcmp("test%1%2!*.*s!%99", out), "Expected output string \"test%%1%%2!*.*s!%%99\", got %s\n", out);
828 /* Only the "%n", "%r", and "%t" escape sequences are processed. */
829 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, "%%% %.%!", 0, 0, out,
830 sizeof(out)/sizeof(CHAR), NULL);
831 ok(ret == 8, "Expected FormatMessageA to return 8, got %d\n", ret);
832 ok(!strcmp("%%% %.%!", out), "Expected output string \"%%%%%% %%.%%!\", got %s\n", out);
834 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, "%n%r%t", 0, 0, out,
835 sizeof(out)/sizeof(CHAR), NULL);
836 ok(ret == 4, "Expected FormatMessageA to return 4, got %d\n", ret);
837 ok(!strcmp("\r\n\r\t", out), "Expected output string \"\\r\\n\\r\\t\", got %s\n", out);
839 /* CRLF characters are processed normally. */
840 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, "hi\n", 0, 0, out,
841 sizeof(out)/sizeof(CHAR), NULL);
842 ok(ret == 4, "Expected FormatMessageA to return 4, got %d\n", ret);
843 ok(!strcmp("hi\r\n", out), "Expected output string \"hi\\r\\n\", got %s\n", out);
845 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, "hi\r\n", 0, 0, out,
846 sizeof(out)/sizeof(CHAR), NULL);
847 ok(ret == 4, "Expected FormatMessageA to return 4, got %d\n", ret);
848 ok(!strcmp("hi\r\n", out), "Expected output string \"hi\\r\\n\", got %s\n", out);
850 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, "\r", 0, 0, out,
851 sizeof(out)/sizeof(CHAR), NULL);
852 ok(ret == 2, "Expected FormatMessageA to return 2, got %d\n", ret);
853 ok(!strcmp("\r\n", out), "Expected output string \"\\r\\n\", got %s\n", out);
855 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, "\r\r\n", 0, 0, out,
856 sizeof(out)/sizeof(CHAR), NULL);
857 ok(ret == 4, "Expected FormatMessageA to return 4, got %d\n", ret);
858 ok(!strcmp("\r\n\r\n", out), "Expected output string \"\\r\\n\\r\\n\", got %s\n", out);
860 /* The width parameter is handled the same also. */
861 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS |
862 FORMAT_MESSAGE_MAX_WIDTH_MASK, "hi\n", 0, 0, out,
863 sizeof(out)/sizeof(CHAR), NULL);
864 ok(!strcmp("hi ", out), "Expected output string \"hi \", got %s\n", out);
865 ok(ret == 3, "Expected FormatMessageA to return 3, got %d\n", ret);
867 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS |
868 FORMAT_MESSAGE_MAX_WIDTH_MASK, "hi\r\n", 0, 0, out,
869 sizeof(out)/sizeof(CHAR), NULL);
870 ok(ret == 3, "Expected FormatMessageA to return 3, got %d\n", ret);
871 ok(!strcmp("hi ", out), "Expected output string \"hi \", got %s\n", out);
873 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS |
874 FORMAT_MESSAGE_MAX_WIDTH_MASK, "\r", 0, 0, out,
875 sizeof(out)/sizeof(CHAR), NULL);
876 ok(ret == 1, "Expected FormatMessageA to return 1, got %d\n", ret);
877 ok(!strcmp(" ", out), "Expected output string \" \", got %s\n", out);
879 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS |
880 FORMAT_MESSAGE_MAX_WIDTH_MASK, "\r\r\n", 0, 0, out,
881 sizeof(out)/sizeof(CHAR), NULL);
882 ok(ret == 2, "Expected FormatMessageA to return 2, got %d\n", ret);
883 ok(!strcmp(" ", out), "Expected output string \" \", got %s\n", out);
886 static void test_message_ignore_inserts_wide(void)
888 static const WCHAR test[] = {'t','e','s','t',0};
889 static const WCHAR empty[] = {0};
890 static const WCHAR fmt_t0[] = {'t','e','s','t','%','0',0};
891 static const WCHAR fmt_t0t[] = {'t','e','s','t','%','0','t','e','s','t',0};
892 static const WCHAR fmt_0t[] = {'%','0','t','e','s','t',0};
893 static const WCHAR fmt_t12oos99[] = {'t','e','s','t','%','1','%','2','!','*','.','*','s','!','%','9','9',0};
894 static const WCHAR fmt_pctspacedot[] = {'%','%','%',' ','%','.','%','!',0};
895 static const WCHAR fmt_nrt[] = {'%','n','%','r','%','t',0};
896 static const WCHAR fmt_hi_lf[] = {'h','i','\n',0};
897 static const WCHAR fmt_hi_crlf[] = {'h','i','\r','\n',0};
898 static const WCHAR fmt_cr[] = {'\r',0};
899 static const WCHAR fmt_crcrlf[] = {'\r','\r','\n',0};
901 static const WCHAR s_nrt[] = {'\r','\n','\r','\t',0};
902 static const WCHAR s_hi_crlf[] = {'h','i','\r','\n',0};
903 static const WCHAR s_crlf[] = {'\r','\n',0};
904 static const WCHAR s_crlfcrlf[] = {'\r','\n','\r','\n',0};
905 static const WCHAR s_hi_sp[] = {'h','i',' ',0};
906 static const WCHAR s_sp[] = {' ',0};
907 static const WCHAR s_2sp[] = {' ',' ',0};
909 DWORD ret;
910 WCHAR out[256];
912 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, test, 0, 0, out,
913 sizeof(out)/sizeof(WCHAR), NULL);
914 ok(ret == 4, "Expected FormatMessageW to return 4, got %d\n", ret);
915 ok(!lstrcmpW(test, out), "Expected output string \"test\", got %s\n", wine_dbgstr_w(out));
917 /* The %0 escape sequence is handled. */
918 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, fmt_t0, 0, 0, out,
919 sizeof(out)/sizeof(WCHAR), NULL);
920 ok(ret == 4, "Expected FormatMessageW to return 4, got %d\n", ret);
921 ok(!lstrcmpW(test, out), "Expected output string \"test\", got %s\n", wine_dbgstr_w(out));
923 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, fmt_t0t, 0, 0, out,
924 sizeof(out)/sizeof(WCHAR), NULL);
925 ok(ret == 4, "Expected FormatMessageW to return 4, got %d\n", ret);
926 ok(!lstrcmpW(test, out), "Expected output string \"test\", got %s\n", wine_dbgstr_w(out));
928 /* While FormatMessageA returns 0 in this case, no last error code is set. */
929 SetLastError(0xdeadbeef);
930 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, fmt_0t, 0, 0, out,
931 sizeof(out)/sizeof(WCHAR), NULL);
932 ok(ret == 0, "Expected FormatMessageW to return 0, got %d\n", ret);
933 ok(!lstrcmpW(empty, out), "Expected the output buffer to be the empty string, got %s\n", wine_dbgstr_w(out));
934 ok(GetLastError() == 0xdeadbeef, "Expected GetLastError() to return 0xdeadbeef, got %u\n", GetLastError());
936 /* Insert sequences are ignored. */
937 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, fmt_t12oos99, 0, 0, out,
938 sizeof(out)/sizeof(WCHAR), NULL);
939 ok(ret == 17, "Expected FormatMessageW to return 17, got %d\n", ret);
940 ok(!lstrcmpW(fmt_t12oos99, out), "Expected output string \"test%%1%%2!*.*s!%%99\", got %s\n", wine_dbgstr_w(out));
942 /* Only the "%n", "%r", and "%t" escape sequences are processed. */
943 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, fmt_pctspacedot, 0, 0, out,
944 sizeof(out)/sizeof(WCHAR), NULL);
945 ok(ret == 8, "Expected FormatMessageW to return 8, got %d\n", ret);
946 ok(!lstrcmpW(fmt_pctspacedot, out), "Expected output string \"%%%%%% %%.%%!\", got %s\n", wine_dbgstr_w(out));
948 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, fmt_nrt, 0, 0, out,
949 sizeof(out)/sizeof(WCHAR), NULL);
950 ok(ret == 4, "Expected FormatMessageW to return 4, got %d\n", ret);
951 ok(!lstrcmpW(s_nrt, out), "Expected output string \"\\r\\n\\r\\t\", got %s\n", wine_dbgstr_w(out));
953 /* CRLF characters are processed normally. */
954 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, fmt_hi_lf, 0, 0, out,
955 sizeof(out)/sizeof(WCHAR), NULL);
956 ok(ret == 4, "Expected FormatMessageW to return 4, got %d\n", ret);
957 ok(!lstrcmpW(s_hi_crlf, out), "Expected output string \"hi\\r\\n\", got %s\n", wine_dbgstr_w(out));
959 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, fmt_hi_crlf, 0, 0, out,
960 sizeof(out)/sizeof(WCHAR), NULL);
961 ok(ret == 4, "Expected FormatMessageW to return 4, got %d\n", ret);
962 ok(!lstrcmpW(s_hi_crlf, out), "Expected output string \"hi\\r\\n\", got %s\n", wine_dbgstr_w(out));
964 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, fmt_cr, 0, 0, out,
965 sizeof(out)/sizeof(WCHAR), NULL);
966 ok(ret == 2, "Expected FormatMessageW to return 2, got %d\n", ret);
967 ok(!lstrcmpW(s_crlf, out), "Expected output string \"\\r\\n\", got %s\n", wine_dbgstr_w(out));
969 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, fmt_crcrlf, 0, 0, out,
970 sizeof(out)/sizeof(WCHAR), NULL);
971 ok(ret == 4, "Expected FormatMessageW to return 4, got %d\n", ret);
972 ok(!lstrcmpW(s_crlfcrlf, out), "Expected output string \"\\r\\n\\r\\n\", got %s\n", wine_dbgstr_w(out));
974 /* The width parameter is handled the same also. */
975 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS |
976 FORMAT_MESSAGE_MAX_WIDTH_MASK, fmt_hi_lf, 0, 0, out,
977 sizeof(out)/sizeof(WCHAR), NULL);
978 ok(ret == 3, "Expected FormatMessageW to return 3, got %d\n", ret);
979 ok(!lstrcmpW(s_hi_sp, out), "Expected output string \"hi \", got %s\n", wine_dbgstr_w(out));
981 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS |
982 FORMAT_MESSAGE_MAX_WIDTH_MASK, fmt_hi_crlf, 0, 0, out,
983 sizeof(out)/sizeof(WCHAR), NULL);
984 ok(ret == 3, "Expected FormatMessageW to return 3, got %d\n", ret);
985 ok(!lstrcmpW(s_hi_sp, out), "Expected output string \"hi \", got %s\n", wine_dbgstr_w(out));
987 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS |
988 FORMAT_MESSAGE_MAX_WIDTH_MASK, fmt_cr, 0, 0, out,
989 sizeof(out)/sizeof(WCHAR), NULL);
990 ok(ret == 1, "Expected FormatMessageW to return 1, got %d\n", ret);
991 ok(!lstrcmpW(s_sp, out), "Expected output string \" \", got %s\n", wine_dbgstr_w(out));
993 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS |
994 FORMAT_MESSAGE_MAX_WIDTH_MASK, fmt_crcrlf, 0, 0, out,
995 sizeof(out)/sizeof(WCHAR), NULL);
996 ok(ret == 2, "Expected FormatMessageW to return 2, got %d\n", ret);
997 ok(!lstrcmpW(s_2sp, out), "Expected output string \" \", got %s\n", wine_dbgstr_w(out));
1000 static void test_message_wrap(void)
1002 DWORD ret;
1003 int i;
1004 CHAR in[300], out[300], ref[300];
1006 /* No need for wrapping */
1007 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 20,
1008 "short long line", 0, 0, out, sizeof(out), NULL);
1009 ok(ret == 15, "Expected FormatMessageW to return 15, got %d\n", ret);
1010 ok(!strcmp("short long line", out),"failed out=[%s]\n",out);
1012 /* Wrap the last word */
1013 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 11,
1014 "short long line", 0, 0, out, sizeof(out), NULL);
1015 ok(ret == 16, "Expected FormatMessageW to return 16, got %d\n", ret);
1016 ok(!strcmp("short long\r\nline", out),"failed out=[%s]\n",out);
1018 /* Wrap the very last word */
1019 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 20,
1020 "short long long line", 0, 0, out, sizeof(out), NULL);
1021 ok(ret == 21, "Expected FormatMessageW to return 21, got %d\n", ret);
1022 ok(!strcmp("short long long\r\nline", out),"failed out=[%s]\n",out);
1024 /* Strictly less than 10 characters per line! */
1025 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 10,
1026 "short long line", 0, 0, out, sizeof(out), NULL);
1027 ok(ret == 16, "Expected FormatMessageW to return 16, got %d\n", ret);
1028 ok(!strcmp("short\r\nlong line", out),"failed out=[%s]\n",out);
1030 /* Handling of duplicate spaces */
1031 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 16,
1032 "short long line", 0, 0, out, sizeof(out), NULL);
1033 ok(ret == 16, "Expected FormatMessageW to return 16, got %d\n", ret);
1034 ok(!strcmp("short long\r\nline", out),"failed out=[%s]\n",out);
1036 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 16,
1037 "short long wordlongerthanaline", 0, 0, out, sizeof(out), NULL);
1038 ok(ret == 33, "Expected FormatMessageW to return 33, got %d\n", ret);
1039 ok(!strcmp("short long\r\nwordlongerthanal\r\nine", out),"failed out=[%s]\n",out);
1041 /* Breaking in the middle of spaces */
1042 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 12,
1043 "short long line", 0, 0, out, sizeof(out), NULL);
1044 ok(ret == 18, "Expected FormatMessageW to return 18, got %d\n", ret);
1045 ok(!strcmp("short long\r\n line", out),"failed out=[%s]\n",out);
1047 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 12,
1048 "short long wordlongerthanaline", 0, 0, out, sizeof(out), NULL);
1049 ok(ret == 35, "Expected FormatMessageW to return 35, got %d\n", ret);
1050 ok(!strcmp("short long\r\n\r\nwordlongerth\r\nanaline", out),"failed out=[%s]\n",out);
1052 /* Handling of start-of-string spaces */
1053 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 15,
1054 " short line", 0, 0, out, sizeof(out), NULL);
1055 ok(ret == 13, "Expected FormatMessageW to return 13, got %d\n", ret);
1056 ok(!strcmp(" short line", out),"failed out=[%s]\n",out);
1058 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 11,
1059 " shortlong line", 0, 0, out, sizeof(out), NULL);
1060 ok(ret == 17, "Expected FormatMessageW to return 17, got %d\n", ret);
1061 ok(!strcmp("\r\nshortlong\r\nline", out),"failed out=[%s]\n",out);
1063 /* Handling of start-of-line spaces */
1064 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 11,
1065 "l1%n shortlong line", 0, 0, out, sizeof(out), NULL);
1066 ok(ret == 21, "Expected FormatMessageW to return 21, got %d\n", ret);
1067 ok(!strcmp("l1\r\n\r\nshortlong\r\nline", out),"failed out=[%s]\n",out);
1069 /* Pure space wrapping */
1070 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 5,
1071 " ", 0, 0, out, sizeof(out), NULL);
1072 ok(ret == 7, "Expected FormatMessageW to return 7, got %d\n", ret);
1073 ok(!strcmp("\r\n\r\n\r\n ", out),"failed out=[%s]\n",out);
1075 /* Handling of trailing spaces */
1076 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 5,
1077 "l1 ", 0, 0, out, sizeof(out), NULL);
1078 ok(ret == 10, "Expected FormatMessageW to return 10, got %d\n", ret);
1079 ok(!strcmp("l1\r\n\r\n\r\n ", out),"failed out=[%s]\n",out);
1081 /* Word that just fills the line */
1082 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 8,
1083 "shortlon", 0, 0, out, sizeof(out), NULL);
1084 ok(ret == 10, "Expected FormatMessageW to return 10, got %d\n", ret);
1085 ok(!strcmp("shortlon\r\n", out),"failed out=[%s]\n",out);
1087 /* Word longer than the line */
1088 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 8,
1089 "shortlongline", 0, 0, out, sizeof(out), NULL);
1090 ok(ret == 15, "Expected FormatMessageW to return 15, got %d\n", ret);
1091 ok(!strcmp("shortlon\r\ngline", out),"failed out=[%s]\n",out);
1093 /* Wrap the line multiple times */
1094 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 7,
1095 "short long line", 0, 0, out, sizeof(out), NULL);
1096 ok(ret == 17, "Expected FormatMessageW to return 17, got %d\n", ret);
1097 ok(!strcmp("short\r\nlong\r\nline", out),"failed out=[%s]\n",out);
1099 /* '\n's in the source are ignored */
1100 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 11,
1101 "short\nlong line", 0, 0, out, sizeof(out), NULL);
1102 ok(ret == 16, "Expected FormatMessageW to return 16, got %d\n", ret);
1103 ok(!strcmp("short long\r\nline", out),"failed out=[%s]\n",out);
1105 /* Wrap even before a '%n' */
1106 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 8,
1107 "shortlon%n", 0, 0, out, sizeof(out), NULL);
1108 ok(ret == 12, "Expected FormatMessageW to return 12, got %d\n", ret);
1109 ok(!strcmp("shortlon\r\n\r\n", out),"failed out=[%s]\n",out);
1111 /* '%n's count as starting a new line and combine with line wrapping */
1112 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 10,
1113 "short%nlong line", 0, 0, out, sizeof(out), NULL);
1114 ok(ret == 16, "Expected FormatMessageW to return 16, got %d\n", ret);
1115 ok(!strcmp("short\r\nlong line", out),"failed out=[%s]\n",out);
1117 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 8,
1118 "short%nlong line", 0, 0, out, sizeof(out), NULL);
1119 ok(ret == 17, "Expected FormatMessageW to return 17, got %d\n", ret);
1120 ok(!strcmp("short\r\nlong\r\nline", out),"failed out=[%s]\n",out);
1122 /* '%r's also count as starting a new line and all */
1123 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 10,
1124 "short%rlong line", 0, 0, out, sizeof(out), NULL);
1125 ok(ret == 15, "Expected FormatMessageW to return 15, got %d\n", ret);
1126 ok(!strcmp("short\rlong line", out),"failed out=[%s]\n",out);
1128 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 8,
1129 "short%rlong line", 0, 0, out, sizeof(out), NULL);
1130 ok(ret == 16, "Expected FormatMessageW to return 16, got %d\n", ret);
1131 ok(!strcmp("short\rlong\r\nline", out),"failed out=[%s]\n",out);
1133 /* IGNORE_INSERTS does not prevent line wrapping or disable '%n' */
1134 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS | 8,
1135 "short%nlong line%1", 0, 0, out, sizeof(out), NULL);
1136 ok(ret == 19, "Expected FormatMessageW to return 19, got %d\n", ret);
1137 ok(!strcmp("short\r\nlong\r\nline%1", out),"failed out=[%s]\n",out);
1139 /* MAX_WIDTH_MASK is the same as specifying an infinite line width */
1140 strcpy(in, "first line%n");
1141 strcpy(ref, "first line\r\n");
1142 for (i=0; i < 26; i++)
1144 strcat(in, "123456789 ");
1145 strcat(ref, "123456789 ");
1147 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK,
1148 in, 0, 0, out, sizeof(out), NULL);
1149 ok(ret == 272, "Expected FormatMessageW to return 272, got %d\n", ret);
1150 ok(!strcmp(ref, out),"failed out=[%s]\n",out);
1152 /* Wrapping and non-space characters */
1153 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 11,
1154 "short long\tline", 0, 0, out, sizeof(out), NULL);
1155 ok(ret == 16, "Expected FormatMessageW to return 16, got %d\n", ret);
1156 ok(!strcmp("short\r\nlong\tline", out),"failed out=[%s]\n",out);
1158 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 11,
1159 "short long-line", 0, 0, out, sizeof(out), NULL);
1160 ok(ret == 16, "Expected FormatMessageW to return 16, got %d\n", ret);
1161 ok(!strcmp("short\r\nlong-line", out),"failed out=[%s]\n",out);
1163 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 11,
1164 "short long_line", 0, 0, out, sizeof(out), NULL);
1165 ok(ret == 16, "Expected FormatMessageW to return 16, got %d\n", ret);
1166 ok(!strcmp("short\r\nlong_line", out),"failed out=[%s]\n",out);
1168 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 11,
1169 "short long.line", 0, 0, out, sizeof(out), NULL);
1170 ok(ret == 16, "Expected FormatMessageW to return 16, got %d\n", ret);
1171 ok(!strcmp("short\r\nlong.line", out),"failed out=[%s]\n",out);
1173 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 11,
1174 "short long,line", 0, 0, out, sizeof(out), NULL);
1175 ok(ret == 16, "Expected FormatMessageW to return 16, got %d\n", ret);
1176 ok(!strcmp("short\r\nlong,line", out),"failed out=[%s]\n",out);
1178 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 11,
1179 "short long!line", 0, 0, out, sizeof(out), NULL);
1180 ok(ret == 16, "Expected FormatMessageW to return 16, got %d\n", ret);
1181 ok(!strcmp("short\r\nlong!line", out),"failed out=[%s]\n",out);
1183 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 11,
1184 "short long?line", 0, 0, out, sizeof(out), NULL);
1185 ok(ret == 16, "Expected FormatMessageW to return 16, got %d\n", ret);
1186 ok(!strcmp("short\r\nlong?line", out),"failed out=[%s]\n",out);
1189 static void test_message_insufficient_buffer(void)
1191 static const char init_buf[] = {'x', 'x', 'x', 'x', 'x'};
1192 static const char expected_buf[] = {'x', 'x', 'x', 'x', 'x'};
1193 DWORD ret;
1194 CHAR out[5];
1196 SetLastError(0xdeadbeef);
1197 memcpy(out, init_buf, sizeof(init_buf));
1198 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING, "test", 0, 0, out, 0, NULL);
1199 ok(ret == 0, "Expected FormatMessageA to return 0, got %u\n", ret);
1200 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1201 "Expected GetLastError() to return ERROR_INSUFFICIENT_BUFFER, got %u\n",
1202 GetLastError());
1203 ok(!memcmp(expected_buf, out, sizeof(expected_buf)),
1204 "Expected the buffer to be untouched\n");
1206 SetLastError(0xdeadbeef);
1207 memcpy(out, init_buf, sizeof(init_buf));
1208 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING, "test", 0, 0, out, 1, NULL);
1209 ok(ret == 0, "Expected FormatMessageA to return 0, got %u\n", ret);
1210 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1211 "Expected GetLastError() to return ERROR_INSUFFICIENT_BUFFER, got %u\n",
1212 GetLastError());
1213 ok(!memcmp(expected_buf, out, sizeof(expected_buf)),
1214 "Expected the buffer to be untouched\n");
1216 SetLastError(0xdeadbeef);
1217 memcpy(out, init_buf, sizeof(init_buf));
1218 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING, "test", 0, 0, out, sizeof(out)/sizeof(out[0]) - 1, NULL);
1219 ok(ret == 0, "Expected FormatMessageA to return 0, got %u\n", ret);
1220 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1221 "Expected GetLastError() to return ERROR_INSUFFICIENT_BUFFER, got %u\n",
1222 GetLastError());
1223 ok(!memcmp(expected_buf, out, sizeof(expected_buf)),
1224 "Expected the buffer to be untouched\n");
1227 static void test_message_insufficient_buffer_wide(void)
1229 static const WCHAR test[] = {'t','e','s','t',0};
1230 static const WCHAR init_buf[] = {'x', 'x', 'x', 'x', 'x'};
1231 static const WCHAR expected_buf[] = {'x', 'x', 'x', 'x', 'x'};
1232 static const WCHAR broken_buf[] = {0, 'x', 'x', 'x', 'x'};
1233 static const WCHAR broken2_buf[] = {'t','e','s',0,'x'};
1235 DWORD ret;
1236 WCHAR out[5];
1238 SetLastError(0xdeadbeef);
1239 memcpy(out, init_buf, sizeof(init_buf));
1240 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, test, 0, 0, out, 0, NULL);
1241 ok(ret == 0, "Expected FormatMessageA to return 0, got %u\n", ret);
1242 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1243 "Expected GetLastError() to return ERROR_INSUFFICIENT_BUFFER, got %u\n",
1244 GetLastError());
1245 ok(!memcmp(expected_buf, out, sizeof(expected_buf)),
1246 "Expected the buffer to be untouched\n");
1248 /* Windows Server 2003 and newer report failure but copy a
1249 * truncated string to the buffer for non-zero buffer sizes. */
1250 SetLastError(0xdeadbeef);
1251 memcpy(out, init_buf, sizeof(init_buf));
1252 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, test, 0, 0, out, 1, NULL);
1253 ok(ret == 0, "Expected FormatMessageA to return 0, got %u\n", ret);
1254 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1255 "Expected GetLastError() to return ERROR_INSUFFICIENT_BUFFER, got %u\n",
1256 GetLastError());
1257 ok(!memcmp(expected_buf, out, sizeof(expected_buf)) ||
1258 broken(!memcmp(broken_buf, out, sizeof(broken_buf))), /* W2K3+ */
1259 "Expected the buffer to be untouched\n");
1261 SetLastError(0xdeadbeef);
1262 memcpy(out, init_buf, sizeof(init_buf));
1263 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, test, 0, 0, out, sizeof(out)/sizeof(out[0]) - 1, NULL);
1264 ok(ret == 0, "Expected FormatMessageA to return 0, got %u\n", ret);
1265 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1266 "Expected GetLastError() to return ERROR_INSUFFICIENT_BUFFER, got %u\n",
1267 GetLastError());
1268 ok(!memcmp(expected_buf, out, sizeof(expected_buf)) ||
1269 broken(!memcmp(broken2_buf, out, sizeof(broken2_buf))), /* W2K3+ */
1270 "Expected the buffer to be untouched\n");
1273 static void test_message_null_buffer(void)
1275 DWORD ret, error;
1277 /* Without FORMAT_MESSAGE_ALLOCATE_BUFFER, only the specified buffer size is checked. */
1278 SetLastError(0xdeadbeef);
1279 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, 0, 0, NULL, 0, NULL);
1280 error = GetLastError();
1281 ok(!ret, "FormatMessageA returned %u\n", ret);
1282 ok(error == ERROR_INSUFFICIENT_BUFFER, "last error %u\n", error);
1284 SetLastError(0xdeadbeef);
1285 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, 0, 0, NULL, 1, NULL);
1286 error = GetLastError();
1287 ok(!ret, "FormatMessageA returned %u\n", ret);
1288 ok(error == ERROR_INSUFFICIENT_BUFFER, "last error %u\n", error);
1290 if (0) /* crashes on Windows */
1292 SetLastError(0xdeadbeef);
1293 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, 0, 0, NULL, 256, NULL);
1296 SetLastError(0xdeadbeef);
1297 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, 0, 0, NULL, 0, NULL);
1298 error = GetLastError();
1299 ok(!ret, "FormatMessageA returned %u\n", ret);
1300 ok(error == ERROR_NOT_ENOUGH_MEMORY, "last error %u\n", error);
1302 SetLastError(0xdeadbeef);
1303 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, 0, 0, NULL, 1, NULL);
1304 error = GetLastError();
1305 ok(!ret, "FormatMessageA returned %u\n", ret);
1306 ok(error == ERROR_NOT_ENOUGH_MEMORY, "last error %u\n", error);
1308 SetLastError(0xdeadbeef);
1309 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, 0, 0, NULL, 256, NULL);
1310 error = GetLastError();
1311 ok(!ret, "FormatMessageA returned %u\n", ret);
1312 ok(error == ERROR_NOT_ENOUGH_MEMORY, "last error %u\n", error);
1315 static void test_message_null_buffer_wide(void)
1317 DWORD ret, error;
1319 SetLastError(0xdeadbeef);
1320 ret = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, NULL, 0, 0, NULL, 0, NULL);
1321 error = GetLastError();
1322 ok(!ret, "FormatMessageW returned %u\n", ret);
1323 ok(error == ERROR_INVALID_PARAMETER, "last error %u\n", error);
1325 SetLastError(0xdeadbeef);
1326 ret = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, NULL, 0, 0, NULL, 1, NULL);
1327 error = GetLastError();
1328 ok(!ret, "FormatMessageW returned %u\n", ret);
1329 ok(error == ERROR_INVALID_PARAMETER, "last error %u\n", error);
1331 SetLastError(0xdeadbeef);
1332 ret = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, NULL, 0, 0, NULL, 256, NULL);
1333 error = GetLastError();
1334 ok(!ret, "FormatMessageW returned %u\n", ret);
1335 ok(error == ERROR_INVALID_PARAMETER, "last error %u\n", error);
1337 SetLastError(0xdeadbeef);
1338 ret = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, 0, 0, NULL, 0, NULL);
1339 error = GetLastError();
1340 ok(!ret, "FormatMessageW returned %u\n", ret);
1341 ok(error == ERROR_INVALID_PARAMETER, "last error %u\n", error);
1343 SetLastError(0xdeadbeef);
1344 ret = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, 0, 0, NULL, 1, NULL);
1345 error = GetLastError();
1346 ok(!ret, "FormatMessageW returned %u\n", ret);
1347 ok(error == ERROR_INVALID_PARAMETER, "last error %u\n", error);
1349 SetLastError(0xdeadbeef);
1350 ret = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, 0, 0, NULL, 256, NULL);
1351 error = GetLastError();
1352 ok(!ret, "FormatMessageW returned %u\n", ret);
1353 ok(error == ERROR_INVALID_PARAMETER, "last error %u\n", error);
1356 static void test_message_allocate_buffer(void)
1358 DWORD ret;
1359 char *buf;
1361 /* While MSDN suggests that FormatMessageA allocates a buffer whose size is
1362 * the larger of the output string and the requested buffer size, the tests
1363 * will not try to determine the actual size of the buffer allocated, as
1364 * the return value of LocalSize cannot be trusted for the purpose, and it should
1365 * in any case be safe for FormatMessageA to allocate in the manner that
1366 * MSDN suggests. */
1368 SetLastError(0xdeadbeef);
1369 buf = (char *)0xdeadbeef;
1370 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1371 "", 0, 0, (char *)&buf, 0, NULL);
1372 ok(ret == 0, "Expected FormatMessageA to return 0, got %u\n", ret);
1373 ok(buf == NULL, "Expected output buffer pointer to be NULL\n");
1374 ok(GetLastError() == 0xdeadbeef,
1375 "Expected last error to be untouched, got %u\n", GetLastError());
1377 buf = (char *)0xdeadbeef;
1378 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1379 "test", 0, 0, (char *)&buf, 0, NULL);
1380 ok(ret == 4, "Expected FormatMessageA to return 4, got %u\n", ret);
1381 ok(buf != NULL && buf != (char *)0xdeadbeef,
1382 "Expected output buffer pointer to be valid\n");
1383 if (buf != NULL && buf != (char *)0xdeadbeef)
1385 ok(!strcmp("test", buf),
1386 "Expected buffer to contain \"test\", got %s\n", buf);
1387 LocalFree(buf);
1390 buf = (char *)0xdeadbeef;
1391 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1392 "test", 0, 0, (char *)&buf, strlen("test"), NULL);
1393 ok(ret == 4, "Expected FormatMessageA to return 4, got %u\n", ret);
1394 ok(buf != NULL && buf != (char *)0xdeadbeef,
1395 "Expected output buffer pointer to be valid\n");
1396 if (buf != NULL && buf != (char *)0xdeadbeef)
1398 ok(!strcmp("test", buf),
1399 "Expected buffer to contain \"test\", got %s\n", buf);
1400 LocalFree(buf);
1403 buf = (char *)0xdeadbeef;
1404 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1405 "test", 0, 0, (char *)&buf, strlen("test") + 1, NULL);
1406 ok(ret == 4, "Expected FormatMessageA to return 4, got %u\n", ret);
1407 ok(buf != NULL && buf != (char *)0xdeadbeef,
1408 "Expected output buffer pointer to be valid\n");
1409 if (buf != NULL && buf != (char *)0xdeadbeef)
1411 ok(!strcmp("test", buf),
1412 "Expected buffer to contain \"test\", got %s\n", buf);
1413 LocalFree(buf);
1416 buf = (char *)0xdeadbeef;
1417 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1418 "test", 0, 0, (char *)&buf, strlen("test") + 2, NULL);
1419 ok(ret == 4, "Expected FormatMessageA to return 4, got %u\n", ret);
1420 ok(buf != NULL && buf != (char *)0xdeadbeef,
1421 "Expected output buffer pointer to be valid\n");
1422 if (buf != NULL && buf != (char *)0xdeadbeef)
1424 ok(!strcmp("test", buf),
1425 "Expected buffer to contain \"test\", got %s\n", buf);
1426 LocalFree(buf);
1429 buf = (char *)0xdeadbeef;
1430 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1431 "test", 0, 0, (char *)&buf, 1024, NULL);
1432 ok(ret == 4, "Expected FormatMessageA to return 4, got %u\n", ret);
1433 ok(buf != NULL && buf != (char *)0xdeadbeef,
1434 "Expected output buffer pointer to be valid\n");
1435 if (buf != NULL && buf != (char *)0xdeadbeef)
1437 ok(!strcmp("test", buf),
1438 "Expected buffer to contain \"test\", got %s\n", buf);
1439 LocalFree(buf);
1443 static void test_message_allocate_buffer_wide(void)
1445 static const WCHAR empty[] = {0};
1446 static const WCHAR test[] = {'t','e','s','t',0};
1448 DWORD ret;
1449 WCHAR *buf;
1451 /* While MSDN suggests that FormatMessageW allocates a buffer whose size is
1452 * the larger of the output string and the requested buffer size, the tests
1453 * will not try to determine the actual size of the buffer allocated, as
1454 * the return value of LocalSize cannot be trusted for the purpose, and it should
1455 * in any case be safe for FormatMessageW to allocate in the manner that
1456 * MSDN suggests. */
1458 if (0) /* crashes on Windows */
1460 buf = (WCHAR *)0xdeadbeef;
1461 FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1462 NULL, 0, 0, (WCHAR *)&buf, 0, NULL);
1465 SetLastError(0xdeadbeef);
1466 buf = (WCHAR *)0xdeadbeef;
1467 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1468 empty, 0, 0, (WCHAR *)&buf, 0, NULL);
1469 ok(ret == 0, "Expected FormatMessageW to return 0, got %u\n", ret);
1470 ok(buf == NULL, "Expected output buffer pointer to be NULL\n");
1471 ok(GetLastError() == 0xdeadbeef,
1472 "Expected last error to be untouched, got %u\n", GetLastError());
1474 buf = (WCHAR *)0xdeadbeef;
1475 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1476 test, 0, 0, (WCHAR *)&buf, 0, NULL);
1477 ok(ret == 4, "Expected FormatMessageA to return 4, got %u\n", ret);
1478 ok(buf != NULL && buf != (WCHAR *)0xdeadbeef,
1479 "Expected output buffer pointer to be valid\n");
1480 if (buf != NULL && buf != (WCHAR *)0xdeadbeef)
1482 ok(!lstrcmpW(test, buf),
1483 "Expected buffer to contain \"test\", got %s\n", wine_dbgstr_w(buf));
1484 LocalFree(buf);
1487 buf = (WCHAR *)0xdeadbeef;
1488 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1489 test, 0, 0, (WCHAR *)&buf, sizeof(test)/sizeof(WCHAR) - 1, NULL);
1490 ok(ret == 4, "Expected FormatMessageA to return 4, got %u\n", ret);
1491 ok(buf != NULL && buf != (WCHAR *)0xdeadbeef,
1492 "Expected output buffer pointer to be valid\n");
1493 if (buf != NULL && buf != (WCHAR *)0xdeadbeef)
1495 ok(!lstrcmpW(test, buf),
1496 "Expected buffer to contain \"test\", got %s\n", wine_dbgstr_w(buf));
1497 LocalFree(buf);
1500 buf = (WCHAR *)0xdeadbeef;
1501 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1502 test, 0, 0, (WCHAR *)&buf, sizeof(test)/sizeof(WCHAR), NULL);
1503 ok(ret == 4, "Expected FormatMessageA to return 4, got %u\n", ret);
1504 ok(buf != NULL && buf != (WCHAR *)0xdeadbeef,
1505 "Expected output buffer pointer to be valid\n");
1506 if (buf != NULL && buf != (WCHAR *)0xdeadbeef)
1508 ok(!lstrcmpW(test, buf),
1509 "Expected buffer to contain \"test\", got %s\n", wine_dbgstr_w(buf));
1510 LocalFree(buf);
1513 buf = (WCHAR *)0xdeadbeef;
1514 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1515 test, 0, 0, (WCHAR *)&buf, sizeof(test)/sizeof(WCHAR) + 1, NULL);
1516 ok(ret == 4, "Expected FormatMessageA to return 4, got %u\n", ret);
1517 ok(buf != NULL && buf != (WCHAR *)0xdeadbeef,
1518 "Expected output buffer pointer to be valid\n");
1519 if (buf != NULL && buf != (WCHAR *)0xdeadbeef)
1521 ok(!lstrcmpW(test, buf),
1522 "Expected buffer to contain \"test\", got %s\n", wine_dbgstr_w(buf));
1523 LocalFree(buf);
1526 buf = (WCHAR *)0xdeadbeef;
1527 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1528 test, 0, 0, (WCHAR *)&buf, 1024, NULL);
1529 ok(ret == 4, "Expected FormatMessageA to return 4, got %u\n", ret);
1530 ok(buf != NULL && buf != (WCHAR *)0xdeadbeef,
1531 "Expected output buffer pointer to be valid\n");
1532 if (buf != NULL && buf != (WCHAR *)0xdeadbeef)
1534 ok(!lstrcmpW(test, buf),
1535 "Expected buffer to contain \"test\", got %s\n", wine_dbgstr_w(buf));
1536 LocalFree(buf);
1540 static void test_message_from_hmodule(void)
1542 DWORD ret, error;
1543 HMODULE h;
1544 CHAR out[0x100] = {0};
1546 h = GetModuleHandle("kernel32.dll");
1547 ok(h != 0, "GetModuleHandle failed\n");
1549 /*Test existing messageID; as the message strings from wine's kernel32 differ from windows' kernel32 we don't compare
1550 the strings but only test that FormatMessage doesn't return 0*/
1551 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, h, 7/*=ERROR_ARENA_TRASHED*/,
1552 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), out, sizeof(out)/sizeof(CHAR), NULL);
1553 ok(ret != 0, "FormatMessageA returned 0\n");
1555 /* Test a message string with an insertion without passing any variadic arguments. */
1556 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, h, 193 /* ERROR_BAD_EXE_FORMAT */,
1557 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), out, sizeof(out)/sizeof(CHAR), NULL);
1558 ok(ret == 0, "FormatMessageA returned non-zero\n");
1560 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE |
1561 FORMAT_MESSAGE_ARGUMENT_ARRAY, h, 193 /* ERROR_BAD_EXE_FORMAT */,
1562 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), out, sizeof(out)/sizeof(CHAR), NULL);
1563 ok(ret == 0, "FormatMessageA returned non-zero\n");
1565 /*Test nonexistent messageID with varying language IDs Note: FormatMessageW behaves the same*/
1566 SetLastError(0xdeadbeef);
1567 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, h, 3044,
1568 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), out, sizeof(out)/sizeof(CHAR), NULL);
1569 error = GetLastError();
1570 ok(ret == 0, "FormatMessageA returned %u instead of 0\n", ret);
1571 ok(error == ERROR_MR_MID_NOT_FOUND || error == ERROR_MUI_FILE_NOT_FOUND, "last error %u\n", error);
1573 SetLastError(0xdeadbeef);
1574 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, h, 3044,
1575 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), out, sizeof(out)/sizeof(CHAR), NULL);
1576 error = GetLastError();
1577 ok(ret == 0, "FormatMessageA returned %u instead of 0\n", ret);
1578 ok(error == ERROR_MR_MID_NOT_FOUND || error == ERROR_MUI_FILE_NOT_LOADED, "last error %u\n", error);
1580 SetLastError(0xdeadbeef);
1581 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, h, 3044,
1582 MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT), out, sizeof(out)/sizeof(CHAR), NULL);
1583 error = GetLastError();
1584 ok(ret == 0, "FormatMessageA returned %u instead of 0\n", ret);
1585 ok(error == ERROR_MR_MID_NOT_FOUND || error == ERROR_MUI_FILE_NOT_LOADED, "last error %u\n", error);
1587 SetLastError(0xdeadbeef);
1588 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, h, 3044,
1589 MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), out, sizeof(out)/sizeof(CHAR), NULL);
1590 error = GetLastError();
1591 ok(ret == 0, "FormatMessageA returned %u instead of 0\n", ret);
1592 ok(error == ERROR_RESOURCE_LANG_NOT_FOUND ||
1593 error == ERROR_MR_MID_NOT_FOUND ||
1594 error == ERROR_MUI_FILE_NOT_FOUND ||
1595 error == ERROR_MUI_FILE_NOT_LOADED,
1596 "last error %u\n", error);
1598 SetLastError(0xdeadbeef);
1599 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, h, 3044,
1600 MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_UK), out, sizeof(out)/sizeof(CHAR), NULL);
1601 error = GetLastError();
1602 ok(ret == 0, "FormatMessageA returned %u instead of 0\n", ret);
1603 ok(error == ERROR_RESOURCE_LANG_NOT_FOUND ||
1604 error == ERROR_MR_MID_NOT_FOUND ||
1605 error == ERROR_MUI_FILE_NOT_FOUND ||
1606 error == ERROR_MUI_FILE_NOT_LOADED,
1607 "last error %u\n", error);
1610 static void test_message_invalid_flags(void)
1612 static const char init_buf[] = {'x', 'x', 'x', 'x', 'x'};
1614 DWORD ret;
1615 CHAR out[5];
1616 char *ptr;
1618 SetLastError(0xdeadbeef);
1619 memcpy(out, init_buf, sizeof(init_buf));
1620 ret = FormatMessageA(0, "test", 0, 0, out, sizeof(out)/sizeof(CHAR), NULL);
1621 ok(ret == 0, "Expected FormatMessageA to return 0, got %u\n", ret);
1622 ok(!memcmp(out, init_buf, sizeof(init_buf)),
1623 "Expected the output buffer to be untouched\n");
1624 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1625 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %u\n",
1626 GetLastError());
1628 SetLastError(0xdeadbeef);
1629 ptr = (char *)0xdeadbeef;
1630 ret = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER, "test", 0, 0, (char *)&ptr, 0, NULL);
1631 ok(ret == 0, "Expected FormatMessageA to return 0, got %u\n", ret);
1632 ok(ptr == NULL, "Expected output pointer to be initialized to NULL, got %p\n", ptr);
1633 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1634 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %u\n",
1635 GetLastError());
1637 SetLastError(0xdeadbeef);
1638 memcpy(out, init_buf, sizeof(init_buf));
1639 ret = FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS, "test", 0, 0, out, sizeof(out)/sizeof(CHAR), NULL);
1640 ok(ret == 0, "Expected FormatMessageA to return 0, got %u\n", ret);
1641 ok(!memcmp(out, init_buf, sizeof(init_buf)),
1642 "Expected the output buffer to be untouched\n");
1643 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1644 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %u\n",
1645 GetLastError());
1647 SetLastError(0xdeadbeef);
1648 memcpy(out, init_buf, sizeof(init_buf));
1649 ret = FormatMessageA(FORMAT_MESSAGE_ARGUMENT_ARRAY, "test", 0, 0, out, sizeof(out)/sizeof(CHAR), NULL);
1650 ok(ret == 0, "Expected FormatMessageA to return 0, got %u\n", ret);
1651 ok(!memcmp(out, init_buf, sizeof(init_buf)),
1652 "Expected the output buffer to be untouched\n");
1653 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1654 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %u\n",
1655 GetLastError());
1657 SetLastError(0xdeadbeef);
1658 memcpy(out, init_buf, sizeof(init_buf));
1659 ret = FormatMessageA(FORMAT_MESSAGE_MAX_WIDTH_MASK, "test", 0, 0, out, sizeof(out)/sizeof(CHAR), NULL);
1660 ok(ret == 0, "Expected FormatMessageA to return 0, got %u\n", ret);
1661 ok(!memcmp(out, init_buf, sizeof(init_buf)),
1662 "Expected the output buffer to be untouched\n");
1663 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1664 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %u\n",
1665 GetLastError());
1667 /* Simultaneously setting FORMAT_MESSAGE_FROM_STRING with other source
1668 * flags is apparently permissible, and FORMAT_MESSAGE_FROM_STRING takes
1669 * precedence in this case. */
1671 memcpy(out, init_buf, sizeof(init_buf));
1672 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_FROM_SYSTEM,
1673 "test", 0, 0, out, sizeof(out)/sizeof(CHAR), NULL);
1674 ok(ret == 4, "Expected FormatMessageA to return 4, got %u\n", ret);
1675 ok(!strcmp("test", out),
1676 "Expected the output buffer to be untouched\n");
1678 memcpy(out, init_buf, sizeof(init_buf));
1679 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_FROM_HMODULE,
1680 "test", 0, 0, out, sizeof(out)/sizeof(CHAR), NULL);
1681 ok(ret == 4, "Expected FormatMessageA to return 4, got %u\n", ret);
1682 ok(!strcmp("test", out),
1683 "Expected the output buffer to be untouched\n");
1685 memcpy(out, init_buf, sizeof(init_buf));
1686 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_FROM_HMODULE |
1687 FORMAT_MESSAGE_FROM_SYSTEM, "test", 0, 0, out,
1688 sizeof(out)/sizeof(CHAR), NULL);
1689 ok(ret == 4, "Expected FormatMessageA to return 4, got %u\n", ret);
1690 ok(!strcmp("test", out),
1691 "Expected the output buffer to be untouched\n");
1694 static void test_message_invalid_flags_wide(void)
1696 static const WCHAR init_buf[] = {'x', 'x', 'x', 'x', 'x'};
1697 static const WCHAR test[] = {'t','e','s','t',0};
1699 DWORD ret;
1700 WCHAR out[5];
1701 WCHAR *ptr;
1703 SetLastError(0xdeadbeef);
1704 memcpy(out, init_buf, sizeof(init_buf));
1705 ret = FormatMessageW(0, test, 0, 0, out, sizeof(out)/sizeof(WCHAR), NULL);
1706 ok(ret == 0, "Expected FormatMessageW to return 0, got %u\n", ret);
1707 ok(!memcmp(out, init_buf, sizeof(init_buf)),
1708 "Expected the output buffer to be untouched\n");
1709 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1710 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %u\n",
1711 GetLastError());
1713 SetLastError(0xdeadbeef);
1714 ptr = (WCHAR *)0xdeadbeef;
1715 ret = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER, test, 0, 0, (WCHAR *)&ptr, 0, NULL);
1716 ok(ret == 0, "Expected FormatMessageW to return 0, got %u\n", ret);
1717 ok(ptr == NULL, "Expected output pointer to be initialized to NULL, got %p\n", ptr);
1718 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1719 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %u\n",
1720 GetLastError());
1722 SetLastError(0xdeadbeef);
1723 memcpy(out, init_buf, sizeof(init_buf));
1724 ret = FormatMessageW(FORMAT_MESSAGE_IGNORE_INSERTS, test, 0, 0, out, sizeof(out)/sizeof(WCHAR), NULL);
1725 ok(ret == 0, "Expected FormatMessageW to return 0, got %u\n", ret);
1726 ok(!memcmp(out, init_buf, sizeof(init_buf)),
1727 "Expected the output buffer to be untouched\n");
1728 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1729 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %u\n",
1730 GetLastError());
1732 SetLastError(0xdeadbeef);
1733 memcpy(out, init_buf, sizeof(init_buf));
1734 ret = FormatMessageW(FORMAT_MESSAGE_ARGUMENT_ARRAY, test, 0, 0, out, sizeof(out)/sizeof(WCHAR), NULL);
1735 ok(ret == 0, "Expected FormatMessageW to return 0, got %u\n", ret);
1736 ok(!memcmp(out, init_buf, sizeof(init_buf)),
1737 "Expected the output buffer to be untouched\n");
1738 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1739 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %u\n",
1740 GetLastError());
1742 SetLastError(0xdeadbeef);
1743 memcpy(out, init_buf, sizeof(init_buf));
1744 ret = FormatMessageW(FORMAT_MESSAGE_MAX_WIDTH_MASK, test, 0, 0, out, sizeof(out)/sizeof(WCHAR), NULL);
1745 ok(ret == 0, "Expected FormatMessageW to return 0, got %u\n", ret);
1746 ok(!memcmp(out, init_buf, sizeof(init_buf)),
1747 "Expected the output buffer to be untouched\n");
1748 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1749 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %u\n",
1750 GetLastError());
1752 /* Simultaneously setting FORMAT_MESSAGE_FROM_STRING with other source
1753 * flags is apparently permissible, and FORMAT_MESSAGE_FROM_STRING takes
1754 * precedence in this case. */
1756 memcpy(out, init_buf, sizeof(init_buf));
1757 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_FROM_SYSTEM,
1758 test, 0, 0, out, sizeof(out)/sizeof(WCHAR), NULL);
1759 ok(ret == 4, "Expected FormatMessageW to return 4, got %u\n", ret);
1760 ok(!lstrcmpW(test, out),
1761 "Expected the output buffer to be untouched\n");
1763 memcpy(out, init_buf, sizeof(init_buf));
1764 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_FROM_HMODULE,
1765 test, 0, 0, out, sizeof(out)/sizeof(WCHAR), NULL);
1766 ok(ret == 4, "Expected FormatMessageW to return 4, got %u\n", ret);
1767 ok(!lstrcmpW(test, out),
1768 "Expected the output buffer to be untouched\n");
1770 memcpy(out, init_buf, sizeof(init_buf));
1771 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_FROM_HMODULE |
1772 FORMAT_MESSAGE_FROM_SYSTEM, test, 0, 0, out,
1773 sizeof(out)/sizeof(WCHAR), NULL);
1774 ok(ret == 4, "Expected FormatMessageW to return 4, got %u\n", ret);
1775 ok(!lstrcmpW(test, out),
1776 "Expected the output buffer to be untouched\n");
1779 START_TEST(format_msg)
1781 DWORD ret;
1783 test_message_from_string();
1784 test_message_ignore_inserts();
1785 test_message_wrap();
1786 test_message_insufficient_buffer();
1787 test_message_null_buffer();
1788 test_message_allocate_buffer();
1789 test_message_from_hmodule();
1790 test_message_invalid_flags();
1792 SetLastError(0xdeadbeef);
1793 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, NULL, 0, 0, NULL, 0, NULL);
1794 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1796 win_skip("FormatMessageW is not implemented\n");
1797 return;
1800 test_message_from_string_wide();
1801 test_message_ignore_inserts_wide();
1802 test_message_insufficient_buffer_wide();
1803 test_message_null_buffer_wide();
1804 test_message_allocate_buffer_wide();
1805 test_message_invalid_flags_wide();