ReactOS 0.4.16-dev-2207-geb15453
format_msg.c
Go to the documentation of this file.
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 ULL(a,b) (((ULONG64)(a) << 32) | (b))
28
30 LPSTR out, DWORD outsize, ... )
31{
33 DWORD r;
34
37 lang_id, out, outsize, &list);
38 va_end(list);
39 return r;
40}
41
43 LPWSTR out, DWORD outsize, ... )
44{
46 DWORD r;
47
50 lang_id, out, outsize, &list);
51 va_end(list);
52 return r;
53}
54
56{
57 WCHAR out[0x100] = {0};
58 DWORD r, error;
59
60 /* the basics */
62 ok(!lstrcmpW(L"test", out), "failed out=%s\n", wine_dbgstr_w(out));
63 ok(r==4, "failed: r=%ld\n", r);
64
65 /* null string, crashes on Windows */
66 if (0)
67 {
68 SetLastError(0xdeadbeef);
69 lstrcpyW( out, L"xxxxxx" );
71 }
72
73 /* empty string */
74 SetLastError(0xdeadbeef);
75 lstrcpyW( out, L"xxxxxx" );
78 ok(!lstrcmpW(L"", out), "failed out=%s\n", wine_dbgstr_w(out));
79 ok(r==0, "succeeded: r=%ld\n", r);
80 ok(error == ERROR_NO_WORK_DONE || broken(error == 0xdeadbeef), "last error %lu\n", error);
81
82 /* format placeholder with no specifier */
83 SetLastError(0xdeadbeef);
84 lstrcpyW( out, L"xxxxxx" );
87 ok(!lstrcmpW( out, L"xxxxxx" ),
88 "Expected the buffer to be unchanged\n");
89 ok(r==0, "succeeded: r=%ld\n", r);
90 ok(error==ERROR_INVALID_PARAMETER, "last error %lu\n", error);
91
92 /* test string with format placeholder with no specifier */
93 SetLastError(0xdeadbeef);
94 lstrcpyW( out, L"xxxxxx" );
97 ok(!lstrcmpW(out, L"testxx") || broken(!lstrcmpW( out, L"xxxxxx" )), /* winxp */
98 "Expected the buffer to be unchanged\n");
99 ok(r==0, "succeeded: r=%ld\n", r);
100 ok(error==ERROR_INVALID_PARAMETER, "last error %lu\n", error);
101
102 /* insertion with no variadic arguments */
103 SetLastError(0xdeadbeef);
104 lstrcpyW( out, L"xxxxxx" );
107 ok(!lstrcmpW( out, L"xxxxxx" ),
108 "Expected the buffer to be unchanged\n");
109 ok(r==0, "succeeded: r=%ld\n", r);
110 ok(error==ERROR_INVALID_PARAMETER, "last error %lu\n", error);
111
112 SetLastError(0xdeadbeef);
113 lstrcpyW( out, L"xxxxxx" );
115 0, out, ARRAY_SIZE(out), NULL);
117 ok(!lstrcmpW( out, L"xxxxxx" ),
118 "Expected the buffer to be unchanged\n");
119 ok(r==0, "succeeded: r=%ld\n", r);
120 ok(error==ERROR_INVALID_PARAMETER, "last error %lu\n", error);
121
122 /* using the format feature */
123 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1!s!", 0, 0, out, ARRAY_SIZE(out), L"test");
124 ok(!lstrcmpW(L"test", out), "failed out=%s\n", wine_dbgstr_w(out));
125 ok(r==4,"failed: r=%ld\n", r);
126
127 /* no format */
128 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1", 0, 0, out, ARRAY_SIZE(out), L"test");
129 ok(!lstrcmpW(L"test", out), "failed out=%s\n", wine_dbgstr_w(out));
130 ok(r==4,"failed: r=%ld\n", r);
131
132 /* two pieces */
133 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1%2", 0, 0, out, ARRAY_SIZE(out), L"te", L"st");
134 ok(!lstrcmpW(L"test", out), "failed out=%s\n", wine_dbgstr_w(out));
135 ok(r==4,"failed: r=%ld\n", r);
136
137 /* three pieces */
138 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1%3%2%1", 0, 0, out, ARRAY_SIZE(out), L"t", L"s", L"e");
139 ok(!lstrcmpW(L"test", out), "failed out=%s\n", wine_dbgstr_w(out));
140 ok(r==4,"failed: r=%ld\n", r);
141
142 /* ls is unicode */
143 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1!ls!", 0, 0, out, ARRAY_SIZE(out), L"test");
144 ok(!lstrcmpW(L"test", out), "failed out=%s\n", wine_dbgstr_w(out));
145 ok(r==4, "failed: r=%ld\n", r);
146
147 /* S is ansi */
148 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1!S!", 0, 0, out, ARRAY_SIZE(out), "test");
149 ok(!lstrcmpW(L"test", out), "failed out=%s\n", wine_dbgstr_w(out));
150 ok(r==4, "failed: r=%ld\n", r);
151
152 /* ws is unicode */
153 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1!ws!", 0, 0, out, ARRAY_SIZE(out), L"test");
154 ok(!lstrcmpW(L"test", out), "failed out=%s\n", wine_dbgstr_w(out));
155 ok(r==4, "failed: r=%ld\n", r);
156
157 /* as characters */
158 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1!c!%2!c!%3!c!%1!c!", 0, 0, out, ARRAY_SIZE(out), 't', 'e', 's');
159 ok(!lstrcmpW(L"test", out), "failed out=%s\n", wine_dbgstr_w(out));
160 ok(r==4,"failed: r=%ld\n", r);
161
162 /* lc is unicode */
163 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1!lc!%2!lc!%3!lc!%1!lc!", 0, 0, out, ARRAY_SIZE(out), 't', 'e', 's');
164 ok(!lstrcmpW(L"test", out), "failed out=%s\n", wine_dbgstr_w(out));
165 ok(r==4,"failed: r=%ld\n", r);
166
167 /* wc is unicode */
168 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1!wc!%2!wc!%3!wc!%1!wc!", 0, 0, out, ARRAY_SIZE(out), 't', 'e', 's');
169 ok(!lstrcmpW(L"test", out), "failed out=%s\n", wine_dbgstr_w(out));
170 ok(r==4,"failed: r=%ld\n", r);
171
172 /* C is unicode */
173 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1!C!%2!C!%3!C!%1!C!", 0, 0, out, ARRAY_SIZE(out), 't', 'e', 's');
174 ok(!lstrcmpW(L"test", out), "failed out=%s\n", wine_dbgstr_w(out));
175 ok(r==4,"failed: r=%ld\n", r);
176
177 /* some numbers */
178 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1!d!%2!d!%3!d!", 0, 0, out, ARRAY_SIZE(out), 1, 2, 3);
179 ok(!lstrcmpW(L"123", out), "failed out=%s\n", wine_dbgstr_w(out));
180 ok(r==3,"failed: r=%ld\n", r);
181
182 /* a single digit with some spacing */
183 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1!4d!", 0, 0, out, ARRAY_SIZE(out), 1);
184 ok(!lstrcmpW(L" 1", out), "failed out=%s\n", wine_dbgstr_w(out));
185 ok(r==4,"failed: r=%ld\n", r);
186
187 /* a single digit, left justified */
188 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1!-4d!", 0, 0, out, ARRAY_SIZE(out), 1);
189 ok(!lstrcmpW(L"1 ", out), "failed out=%s\n", wine_dbgstr_w(out));
190 ok(r==4,"failed: r=%ld\n", r);
191
192 /* two digit decimal number */
193 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1!4d!", 0, 0, out, ARRAY_SIZE(out), 11);
194 ok(!lstrcmpW(L" 11", out), "failed out=%s\n", wine_dbgstr_w(out));
195 ok(r==4,"failed: r=%ld\n", r);
196
197 /* a hex number */
198 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1!4x!", 0, 0, out, ARRAY_SIZE(out), 11);
199 ok(!lstrcmpW(L" b", out), "failed out=%s\n", wine_dbgstr_w(out));
200 ok(r==4,"failed: r=%ld\n", r);
201
202 /* a hex number, upper case */
203 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1!4X!", 0, 0, out, ARRAY_SIZE(out), 11);
204 ok(!lstrcmpW(L" B", out), "failed out=%s\n", wine_dbgstr_w(out));
205 ok(r==4,"failed: r=%ld\n", r);
206
207 /* a hex number, upper case, left justified */
208 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1!-4X!", 0, 0, out, ARRAY_SIZE(out), 11);
209 ok(!lstrcmpW(L"B ", out), "failed out=%s\n", wine_dbgstr_w(out));
210 ok(r==4,"failed: r=%ld\n", r);
211
212 /* a long hex number, upper case */
213 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1!4X!", 0, 0, out, ARRAY_SIZE(out), 0x1ab);
214 ok(!lstrcmpW(L" 1AB", out), "failed out=%s\n", wine_dbgstr_w(out));
215 ok(r==4,"failed: r=%ld\n", r);
216
217 /* two percent... */
218 r = doitW(FORMAT_MESSAGE_FROM_STRING, L" %%%% ", 0, 0, out, ARRAY_SIZE(out));
219 ok(!lstrcmpW(L" %% ", out), "failed out=%s\n", wine_dbgstr_w(out));
220 ok(r==4,"failed: r=%ld\n", r);
221
222 /* periods are special cases */
223 r = doitW(FORMAT_MESSAGE_FROM_STRING, L" %.%. %1!d!", 0, 0, out, ARRAY_SIZE(out), 0x1ab);
224 ok(!lstrcmpW(L" .. 427", out), "failed out=%s\n", wine_dbgstr_w(out));
225 ok(r==8,"failed: r=%ld\n", r);
226
227 /* %0 ends the line */
228 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"test%0test", 0, 0, out, ARRAY_SIZE(out));
229 ok(!lstrcmpW(L"test", out), "failed out=%s\n", wine_dbgstr_w(out));
230 ok(r==4,"failed: r=%ld\n", r);
231
232 /* %! prints an exclamation */
233 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"yah%!%0 ", 0, 0, out, ARRAY_SIZE(out));
234 ok(!lstrcmpW(L"yah!", out), "failed out=%s\n", wine_dbgstr_w(out));
235 ok(r==4,"failed: r=%ld\n", r);
236
237 /* %space */
239 ok(!lstrcmpW(L" ", out), "failed out=%s\n", wine_dbgstr_w(out));
240 ok(r==4,"failed: r=%ld\n", r);
241
242 /* %n yields \r\n, %r yields \r, %t yields \t */
243 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%n%r%t", 0, 0, out, ARRAY_SIZE(out));
244 ok(!lstrcmpW(L"\r\n\r\t", out), "failed out=%s\n", wine_dbgstr_w(out));
245 ok(r==4,"failed: r=%ld\n", r);
246
247 /* line feed */
249 ok(!lstrcmpW(L"hi\r\n", out), "failed out=%s\n", wine_dbgstr_w(out));
250 ok(r==4,"failed: r=%ld\n", r);
251
252 /* carriage return line feed */
253 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"hi\r\n", 0, 0, out, ARRAY_SIZE(out));
254 ok(!lstrcmpW(L"hi\r\n", out), "failed out=%s\n", wine_dbgstr_w(out));
255 ok(r==4,"failed: r=%ld\n", r);
256
257 /* carriage return */
259 ok(!lstrcmpW(L"\r\n", out), "failed out=%s\n", wine_dbgstr_w(out));
260 ok(r==2,"failed: r=%ld\n", r);
261
262 /* double carriage return line feed */
263 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"\r\r\n", 0, 0, out, ARRAY_SIZE(out));
264 ok(!lstrcmpW(L"\r\n\r\n", out), "failed out=%s\n", wine_dbgstr_w(out));
265 ok(r==4,"failed: r=%ld\n", r);
266
267 /* null string as argument */
269 ok(!lstrcmpW(L"(null)", out),"failed out=[%s]\n", wine_dbgstr_w(out));
270 ok(r==6,"failed: r=%ld\n",r);
271
272 /* precision and width */
273
274 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1!3s!", 0, 0, out, ARRAY_SIZE(out), L"t" );
275 ok(!lstrcmpW(L" t", out),"failed out=[%s]\n", wine_dbgstr_w(out));
276 ok(r==3, "failed: r=%ld\n",r);
277 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1!*s!", 0, 0, out, ARRAY_SIZE(out), 4, L"t" );
278 ok(!lstrcmpW( L" t", out),"failed out=[%s]\n", wine_dbgstr_w(out));
279 ok(r==4,"failed: r=%ld\n",r);
280 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1!4.2u!", 0, 0, out, ARRAY_SIZE(out), 3 );
281 ok(!lstrcmpW( L" 03", out),"failed out=[%s]\n", wine_dbgstr_w(out));
282 ok(r==4,"failed: r=%ld\n",r);
283 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1!*.*u!", 0, 0, out, ARRAY_SIZE(out), 5, 3, 1 );
284 ok(!lstrcmpW( L" 001", out),"failed out=[%s]\n", wine_dbgstr_w(out));
285 ok(r==5,"failed: r=%ld\n",r);
286 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1!*.*u!,%1!*.*u!", 0, 0, out, ARRAY_SIZE(out),
287 5, 3, 1, 4, 2 );
288 ok(!lstrcmpW( L" 001, 0002", out),"failed out=[%s]\n", wine_dbgstr_w(out));
289 ok(r==11,"failed: r=%ld\n",r);
290 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1!*.*u!,%3!*.*u!", 0, 0, out, ARRAY_SIZE(out),
291 5, 3, 1, 6, 4, 2 );
292 ok(!lstrcmpW( L" 001, 0002", out) ||
293 broken(!lstrcmpW(L" 001,000004", out)), /* NT4/Win2k */
294 "failed out=[%s]\n", wine_dbgstr_w(out));
295 ok(r==12,"failed: r=%ld\n",r);
296 /* args are not counted the same way with an argument array */
297 {
298 ULONG_PTR args[] = { 6, 4, 2, 5, 3, 1 };
300 0, 0, out, ARRAY_SIZE(out), (va_list *)args );
301 ok(!lstrcmpW(L" 0002, 00003", out),"failed out=[%s]\n", wine_dbgstr_w(out));
302 ok(r==13,"failed: r=%ld\n",r);
304 0, 0, out, ARRAY_SIZE(out), (va_list *)args );
305 ok(!lstrcmpW(L" 0002, 001", out),"failed out=[%s]\n", wine_dbgstr_w(out));
306 ok(r==12,"failed: r=%ld\n",r);
307 }
308
309 /* change of pace... test the low byte of dwflags */
310
311 /* line feed */
313 0, out, ARRAY_SIZE(out));
314 ok(!lstrcmpW(L"hi ", out), "failed out=%s\n", wine_dbgstr_w(out));
315 ok(r==3,"failed: r=%ld\n", r);
316
317 /* carriage return line feed */
319 0, out, ARRAY_SIZE(out));
320 ok(!lstrcmpW(L"hi ", out), "failed out=%s\n", wine_dbgstr_w(out));
321 ok(r==3,"failed: r=%ld\n", r);
322
323 /* carriage return */
325 0, out, ARRAY_SIZE(out));
326 ok(!lstrcmpW(L" ", out), "failed out=%s\n", wine_dbgstr_w(out));
327 ok(r==1,"failed: r=%ld\n", r);
328
329 /* double carriage return line feed */
331 0, out, ARRAY_SIZE(out));
332 ok(!lstrcmpW(L" ", out), "failed out=%s\n", wine_dbgstr_w(out));
333 ok(r==2,"failed: r=%ld\n", r);
334}
335
337{
338 CHAR out[0x100] = {0};
339 DWORD r;
340 static const char init_buf[] = {'x', 'x', 'x', 'x', 'x', 'x'};
341
342 /* the basics */
344 ok(!strcmp("test", out),"failed out=[%s]\n",out);
345 ok(r==4,"failed: r=%ld\n",r);
346
347 /* null string, crashes on Windows */
348 if (0)
349 {
350 SetLastError(0xdeadbeef);
351 memcpy(out, init_buf, sizeof(init_buf));
353 }
354
355 /* empty string */
356 SetLastError(0xdeadbeef);
357 memcpy(out, init_buf, sizeof(init_buf));
359 ok(!memcmp(out, init_buf, sizeof(init_buf)), "Expected the buffer to be untouched\n");
360 ok(r==0, "succeeded: r=%ld\n", r);
361 ok(GetLastError() == ERROR_NO_WORK_DONE || broken(GetLastError() == 0xdeadbeef),
362 "last error %lu\n", GetLastError());
363
364 /* format placeholder with no specifier */
365 SetLastError(0xdeadbeef);
366 memcpy(out, init_buf, sizeof(init_buf));
368 ok(!memcmp(out, init_buf, sizeof(init_buf)),
369 "Expected the buffer to be untouched\n");
370 ok(r==0, "succeeded: r=%ld\n", r);
372 "last error %lu\n", GetLastError());
373
374 /* test string with format placeholder with no specifier */
375 SetLastError(0xdeadbeef);
376 memcpy(out, init_buf, sizeof(init_buf));
378 ok(!memcmp(out, init_buf, sizeof(init_buf)),
379 "Expected the buffer to be untouched\n");
380 ok(r==0, "succeeded: r=%ld\n", r);
382 "last error %lu\n", GetLastError());
383
384 /* insertion with no variadic arguments */
385 SetLastError(0xdeadbeef);
386 memcpy(out, init_buf, sizeof(init_buf));
388 ok(!memcmp(out, init_buf, sizeof(init_buf)), "Expected the buffer to be untouched\n");
389 ok(r==0, "succeeded: r=%ld\n", r);
390 ok(GetLastError()==ERROR_INVALID_PARAMETER, "last error %lu\n", GetLastError());
391
392 SetLastError(0xdeadbeef);
393 memcpy(out, init_buf, sizeof(init_buf));
395 0, out, ARRAY_SIZE(out), NULL);
396 ok(!memcmp(out, init_buf, sizeof(init_buf)), "Expected the buffer to be untouched\n");
397 ok(r==0, "succeeded: r=%ld\n", r);
398 ok(GetLastError()==ERROR_INVALID_PARAMETER, "last error %lu\n", GetLastError());
399
400 /* using the format feature */
401 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!s!", 0, 0, out, ARRAY_SIZE(out), "test");
402 ok(!strcmp("test", out),"failed out=[%s]\n",out);
403 ok(r==4,"failed: r=%ld\n",r);
404
405 /* no format */
406 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1", 0, 0, out, ARRAY_SIZE(out), "test");
407 ok(!strcmp("test", out),"failed out=[%s]\n",out);
408 ok(r==4,"failed: r=%ld\n",r);
409
410 /* two pieces */
411 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1%2", 0, 0, out, ARRAY_SIZE(out), "te","st");
412 ok(!strcmp("test", out),"failed out=[%s]\n",out);
413 ok(r==4,"failed: r=%ld\n",r);
414
415 /* three pieces */
416 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1%3%2%1", 0, 0, out, ARRAY_SIZE(out), "t","s","e");
417 ok(!strcmp("test", out),"failed out=[%s]\n",out);
418 ok(r==4,"failed: r=%ld\n",r);
419
420 /* s is ansi */
421 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!s!", 0, 0, out, ARRAY_SIZE(out), "test");
422 ok(!strcmp("test", out),"failed out=[%s]\n",out);
423 ok(r==4,"failed: r=%ld\n",r);
424
425 /* ls is unicode */
426 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!ls!", 0, 0, out, ARRAY_SIZE(out), L"test");
427 ok(!strcmp("test", out),"failed out=[%s]\n",out);
428 ok(r==4,"failed: r=%ld\n",r);
429
430 /* S is unicode */
431 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!S!", 0, 0, out, ARRAY_SIZE(out), L"test");
432 ok(!strcmp("test", out),"failed out=[%s]\n",out);
433 ok(r==4,"failed: r=%ld\n",r);
434
435 /* ws is unicode */
436 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!ws!", 0, 0, out, ARRAY_SIZE(out), L"test");
437 ok(!strcmp("test", out),"failed out=[%s]\n",out);
438 ok(r==4,"failed: r=%ld\n",r);
439
440 /* as characters */
441 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!c!%2!c!%3!c!%1!c!", 0, 0, out, ARRAY_SIZE(out),
442 't','e','s');
443 ok(!strcmp("test", out),"failed out=[%s]\n",out);
444 ok(r==4,"failed: r=%ld\n",r);
445
446 /* lc is unicode */
447 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!lc!%2!lc!%3!lc!%1!lc!", 0, 0, out, ARRAY_SIZE(out),
448 't','e','s');
449 ok(!strcmp("test", out),"failed out=[%s]\n",out);
450 ok(r==4,"failed: r=%ld\n",r);
451
452 /* wc is unicode */
453 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!wc!%2!wc!%3!wc!%1!wc!", 0, 0, out, ARRAY_SIZE(out),
454 't','e','s');
455 ok(!strcmp("test", out),"failed out=[%s]\n",out);
456 ok(r==4,"failed: r=%ld\n",r);
457
458 /* C is unicode */
459 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!C!%2!C!%3!C!%1!C!", 0, 0, out, ARRAY_SIZE(out),
460 't','e','s');
461 ok(!strcmp("test", out),"failed out=[%s]\n",out);
462 ok(r==4,"failed: r=%ld\n",r);
463
464 /* some numbers */
465 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!d!%2!d!%3!d!", 0, 0, out, ARRAY_SIZE(out), 1,2,3);
466 ok(!strcmp("123", out),"failed out=[%s]\n",out);
467 ok(r==3,"failed: r=%ld\n",r);
468
469 /* a single digit with some spacing */
470 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4d!", 0, 0, out, ARRAY_SIZE(out), 1);
471 ok(!strcmp(" 1", out),"failed out=[%s]\n",out);
472 ok(r==4,"failed: r=%ld\n",r);
473
474 /* a single digit, left justified */
475 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!-4d!", 0, 0, out, ARRAY_SIZE(out), 1);
476 ok(!strcmp("1 ", out),"failed out=[%s]\n",out);
477 ok(r==4,"failed: r=%ld\n",r);
478
479 /* two digit decimal number */
480 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4d!", 0, 0, out, ARRAY_SIZE(out), 11);
481 ok(!strcmp(" 11", out),"failed out=[%s]\n",out);
482 ok(r==4,"failed: r=%ld\n",r);
483
484 /* a hex number */
485 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4x!", 0, 0, out, ARRAY_SIZE(out), 11);
486 ok(!strcmp(" b", out),"failed out=[%s]\n",out);
487 ok(r==4,"failed: r=%ld\n",r);
488
489 /* a hex number, upper case */
490 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4X!", 0, 0, out, ARRAY_SIZE(out), 11);
491 ok(!strcmp(" B", out),"failed out=[%s]\n",out);
492 ok(r==4,"failed: r=%ld\n",r);
493
494 /* a hex number, upper case, left justified */
495 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!-4X!", 0, 0, out, ARRAY_SIZE(out), 11);
496 ok(!strcmp("B ", out),"failed out=[%s]\n",out);
497 ok(r==4,"failed: r=%ld\n",r);
498
499 /* a long hex number, upper case */
500 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4X!", 0, 0, out, ARRAY_SIZE(out), 0x1ab);
501 ok(!strcmp(" 1AB", out),"failed out=[%s]\n",out);
502 ok(r==4,"failed: r=%ld\n",r);
503
504 /* two percent... */
505 r = doit(FORMAT_MESSAGE_FROM_STRING, " %%%% ", 0, 0, out, ARRAY_SIZE(out));
506 ok(!strcmp(" %% ", out),"failed out=[%s]\n",out);
507 ok(r==4,"failed: r=%ld\n",r);
508
509 /* periods are special cases */
510 r = doit(FORMAT_MESSAGE_FROM_STRING, " %.%. %1!d!", 0, 0, out, ARRAY_SIZE(out), 0x1ab);
511 ok(!strcmp(" .. 427", out),"failed out=[%s]\n",out);
512 ok(r==7,"failed: r=%ld\n",r);
513
514 /* %0 ends the line */
515 r = doit(FORMAT_MESSAGE_FROM_STRING, "test%0test", 0, 0, out, ARRAY_SIZE(out));
516 ok(!strcmp("test", out),"failed out=[%s]\n",out);
517 ok(r==4,"failed: r=%ld\n",r);
518
519 /* %! prints an exclamation */
520 r = doit(FORMAT_MESSAGE_FROM_STRING, "yah%!%0 ", 0, 0, out, ARRAY_SIZE(out));
521 ok(!strcmp("yah!", out),"failed out=[%s]\n",out);
522 ok(r==4,"failed: r=%ld\n",r);
523
524 /* %space */
526 ok(!strcmp(" ", out),"failed out=[%s]\n",out);
527 ok(r==4,"failed: r=%ld\n",r);
528
529 /* %n yields \r\n, %r yields \r, %t yields \t */
530 r = doit(FORMAT_MESSAGE_FROM_STRING, "%n%r%t", 0, 0, out, ARRAY_SIZE(out));
531 ok(!strcmp("\r\n\r\t", out),"failed out=[%s]\n",out);
532 ok(r==4,"failed: r=%ld\n",r);
533
534 /* line feed */
536 ok(!strcmp("hi\r\n", out),"failed out=[%s]\n",out);
537 ok(r==4,"failed: r=%ld\n",r);
538
539 /* carriage return line feed */
540 r = doit(FORMAT_MESSAGE_FROM_STRING, "hi\r\n", 0, 0, out, ARRAY_SIZE(out));
541 ok(!strcmp("hi\r\n", out),"failed out=[%s]\n",out);
542 ok(r==4,"failed: r=%ld\n",r);
543
544 /* carriage return */
546 ok(!strcmp("\r\n", out),"failed out=[%s]\n",out);
547 ok(r==2,"failed: r=%ld\n",r);
548
549 /* double carriage return line feed */
550 r = doit(FORMAT_MESSAGE_FROM_STRING, "\r\r\n", 0, 0, out, ARRAY_SIZE(out));
551 ok(!strcmp("\r\n\r\n", out),"failed out=[%s]\n",out);
552 ok(r==4,"failed: r=%ld\n",r);
553
554 /* null string as argument */
556 ok(!strcmp("(null)", out),"failed out=[%s]\n",out);
557 ok(r==6,"failed: r=%ld\n",r);
558
559 /* precision and width */
560
562 0, 0, out, sizeof(out), "t" );
563 ok(!strcmp(" t", out),"failed out=[%s]\n",out);
564 ok(r==3, "failed: r=%ld\n",r);
566 0, 0, out, sizeof(out), 4, "t");
567 if (!strcmp("*s",out)) win_skip( "width/precision not supported\n" );
568 else
569 {
570 ok(!strcmp( " t", out),"failed out=[%s]\n",out);
571 ok(r==4,"failed: r=%ld\n",r);
572 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!4.2u!",
573 0, 0, out, sizeof(out), 3 );
574 ok(!strcmp( " 03", out),"failed out=[%s]\n",out);
575 ok(r==4,"failed: r=%ld\n",r);
576 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!*.*u!",
577 0, 0, out, sizeof(out), 5, 3, 1 );
578 ok(!strcmp( " 001", out),"failed out=[%s]\n",out);
579 ok(r==5,"failed: r=%ld\n",r);
580 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!*.*u!,%1!*.*u!",
581 0, 0, out, sizeof(out), 5, 3, 1, 4, 2 );
582 ok(!strcmp( " 001, 0002", out),"failed out=[%s]\n",out);
583 ok(r==11,"failed: r=%ld\n",r);
584 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!*.*u!,%3!*.*u!",
585 0, 0, out, sizeof(out), 5, 3, 1, 6, 4, 2 );
586 /* older Win versions marked as broken even though this is arguably the correct behavior */
587 /* but the new (brain-damaged) behavior is specified on MSDN */
588 ok(!strcmp( " 001, 0002", out) ||
589 broken(!strcmp(" 001,000004", out)), /* NT4/Win2k */
590 "failed out=[%s]\n",out);
591 ok(r==12,"failed: r=%ld\n",r);
592 /* args are not counted the same way with an argument array */
593 {
594 ULONG_PTR args[] = { 6, 4, 2, 5, 3, 1 };
596 "%1!*.*u!,%1!*.*u!", 0, 0, out, sizeof(out), (va_list *)args );
597 ok(!strcmp(" 0002, 00003", out),"failed out=[%s]\n",out);
598 ok(r==13,"failed: r=%ld\n",r);
600 "%1!*.*u!,%4!*.*u!", 0, 0, out, sizeof(out), (va_list *)args );
601 ok(!strcmp(" 0002, 001", out),"failed out=[%s]\n",out);
602 ok(r==12,"failed: r=%ld\n",r);
603 }
604 }
605
606 /* change of pace... test the low byte of dwflags */
607
608 /* line feed */
610 0, out, ARRAY_SIZE(out));
611 ok(!strcmp("hi ", out), "failed out=[%s]\n",out);
612 ok(r==3, "failed: r=%ld\n",r);
613
614 /* carriage return line feed */
616 0, out, ARRAY_SIZE(out));
617 ok(!strcmp("hi ", out),"failed out=[%s]\n",out);
618 ok(r==3,"failed: r=%ld\n",r);
619
620 /* carriage return */
622 0, out, ARRAY_SIZE(out));
623 ok(!strcmp(" ", out),"failed out=[%s]\n",out);
624 ok(r==1,"failed: r=%ld\n",r);
625
626 /* double carriage return line feed */
628 0, out, ARRAY_SIZE(out));
629 ok(!strcmp(" ", out),"failed out=[%s]\n",out);
630 ok(r==2,"failed: r=%ld\n",r);
631}
632
634{
635 static const char init_buf[] = {'x', 'x', 'x', 'x', 'x'};
636
637 DWORD ret;
638 CHAR out[256];
639
642 ok(ret == 4, "Expected FormatMessageA to return 4, got %ld\n", ret);
643 ok(!strcmp("test", out), "Expected output string \"test\", got %s\n", out);
644
645 /* The %0 escape sequence is handled. */
648 ok(ret == 4, "Expected FormatMessageA to return 4, got %ld\n", ret);
649 ok(!strcmp("test", out), "Expected output string \"test\", got %s\n", out);
650
653 ok(ret == 4, "Expected FormatMessageA to return 4, got %ld\n", ret);
654 ok(!strcmp("test", out), "Expected output string \"test\", got %s\n", out);
655
656 /* While FormatMessageA returns 0 in this case, no last error code is set. */
657 SetLastError(0xdeadbeef);
658 memcpy(out, init_buf, sizeof(init_buf));
661 ok(ret == 0, "Expected FormatMessageA to return 0, got %ld\n", ret);
662 ok(!memcmp(out, init_buf, sizeof(init_buf)), "Expected the output buffer to be untouched\n");
663 ok(GetLastError() == ERROR_NO_WORK_DONE || broken(GetLastError() == 0xdeadbeef),
664 "Expected GetLastError() to return ERROR_NO_WORK_DONE, got %lu\n", GetLastError());
665
666 /* Insert sequences are ignored. */
669 ok(ret == 17, "Expected FormatMessageA to return 17, got %ld\n", ret);
670 ok(!strcmp("test%1%2!*.*s!%99", out), "Expected output string \"test%%1%%2!*.*s!%%99\", got %s\n", out);
671
672 /* Only the "%n", "%r", and "%t" escape sequences are processed. */
675 ok(ret == 8, "Expected FormatMessageA to return 8, got %ld\n", ret);
676 ok(!strcmp("%%% %.%!", out), "Expected output string \"%%%%%% %%.%%!\", got %s\n", out);
677
680 ok(ret == 4, "Expected FormatMessageA to return 4, got %ld\n", ret);
681 ok(!strcmp("\r\n\r\t", out), "Expected output string \"\\r\\n\\r\\t\", got %s\n", out);
682
683 /* CRLF characters are processed normally. */
686 ok(ret == 4, "Expected FormatMessageA to return 4, got %ld\n", ret);
687 ok(!strcmp("hi\r\n", out), "Expected output string \"hi\\r\\n\", got %s\n", out);
688
691 ok(ret == 4, "Expected FormatMessageA to return 4, got %ld\n", ret);
692 ok(!strcmp("hi\r\n", out), "Expected output string \"hi\\r\\n\", got %s\n", out);
693
696 ok(ret == 2, "Expected FormatMessageA to return 2, got %ld\n", ret);
697 ok(!strcmp("\r\n", out), "Expected output string \"\\r\\n\", got %s\n", out);
698
701 ok(ret == 4, "Expected FormatMessageA to return 4, got %ld\n", ret);
702 ok(!strcmp("\r\n\r\n", out), "Expected output string \"\\r\\n\\r\\n\", got %s\n", out);
703
704 /* The width parameter is handled the same also. */
707 ok(!strcmp("hi ", out), "Expected output string \"hi \", got %s\n", out);
708 ok(ret == 3, "Expected FormatMessageA to return 3, got %ld\n", ret);
709
712 ok(ret == 3, "Expected FormatMessageA to return 3, got %ld\n", ret);
713 ok(!strcmp("hi ", out), "Expected output string \"hi \", got %s\n", out);
714
717 ok(ret == 1, "Expected FormatMessageA to return 1, got %ld\n", ret);
718 ok(!strcmp(" ", out), "Expected output string \" \", got %s\n", out);
719
722 ok(ret == 2, "Expected FormatMessageA to return 2, got %ld\n", ret);
723 ok(!strcmp(" ", out), "Expected output string \" \", got %s\n", out);
724}
725
727{
728 DWORD ret;
729 WCHAR out[256];
730
733 ok(ret == 4, "Expected FormatMessageW to return 4, got %ld\n", ret);
734 ok(!lstrcmpW(L"test", out), "Expected output string \"test\", got %s\n", wine_dbgstr_w(out));
735
736 /* The %0 escape sequence is handled. */
739 ok(ret == 4, "Expected FormatMessageW to return 4, got %ld\n", ret);
740 ok(!lstrcmpW(L"test", out), "Expected output string \"test\", got %s\n", wine_dbgstr_w(out));
741
744 ok(ret == 4, "Expected FormatMessageW to return 4, got %ld\n", ret);
745 ok(!lstrcmpW(L"test", out), "Expected output string \"test\", got %s\n", wine_dbgstr_w(out));
746
747 /* While FormatMessageA returns 0 in this case, no last error code is set. */
748 SetLastError(0xdeadbeef);
751 ok(ret == 0, "Expected FormatMessageW to return 0, got %ld\n", ret);
752 ok(!lstrcmpW(L"", out), "Expected the output buffer to be the empty string, got %s\n", wine_dbgstr_w(out));
753 ok(GetLastError() == ERROR_NO_WORK_DONE || broken(GetLastError() == 0xdeadbeef),
754 "Expected GetLastError() to return ERROR_NO_WORK_DONE, got %lu\n", GetLastError());
755
756 /* Insert sequences are ignored. */
759 ok(ret == 17, "Expected FormatMessageW to return 17, got %ld\n", ret);
760 ok(!lstrcmpW(L"test%1%2!*.*s!%99", out), "Expected output string \"test%%1%%2!*.*s!%%99\", got %s\n", wine_dbgstr_w(out));
761
762 /* Only the "%n", "%r", and "%t" escape sequences are processed. */
765 ok(ret == 8, "Expected FormatMessageW to return 8, got %ld\n", ret);
766 ok(!lstrcmpW(L"%%% %.%!", out), "Expected output string \"%%%%%% %%.%%!\", got %s\n", wine_dbgstr_w(out));
767
770 ok(ret == 4, "Expected FormatMessageW to return 4, got %ld\n", ret);
771 ok(!lstrcmpW(L"\r\n\r\t", out), "Expected output string \"\\r\\n\\r\\t\", got %s\n", wine_dbgstr_w(out));
772
773 /* CRLF characters are processed normally. */
776 ok(ret == 4, "Expected FormatMessageW to return 4, got %ld\n", ret);
777 ok(!lstrcmpW(L"hi\r\n", out), "Expected output string \"hi\\r\\n\", got %s\n", wine_dbgstr_w(out));
778
781 ok(ret == 4, "Expected FormatMessageW to return 4, got %ld\n", ret);
782 ok(!lstrcmpW(L"hi\r\n", out), "Expected output string \"hi\\r\\n\", got %s\n", wine_dbgstr_w(out));
783
786 ok(ret == 2, "Expected FormatMessageW to return 2, got %ld\n", ret);
787 ok(!lstrcmpW(L"\r\n", out), "Expected output string \"\\r\\n\", got %s\n", wine_dbgstr_w(out));
788
791 ok(ret == 4, "Expected FormatMessageW to return 4, got %ld\n", ret);
792 ok(!lstrcmpW(L"\r\n\r\n", out), "Expected output string \"\\r\\n\\r\\n\", got %s\n", wine_dbgstr_w(out));
793
794 /* The width parameter is handled the same also. */
796 FORMAT_MESSAGE_MAX_WIDTH_MASK, L"hi\n", 0, 0, out,
798 ok(ret == 3, "Expected FormatMessageW to return 3, got %ld\n", ret);
799 ok(!lstrcmpW(L"hi ", out), "Expected output string \"hi \", got %s\n", wine_dbgstr_w(out));
800
802 FORMAT_MESSAGE_MAX_WIDTH_MASK, L"hi\r\n", 0, 0, out,
804 ok(ret == 3, "Expected FormatMessageW to return 3, got %ld\n", ret);
805 ok(!lstrcmpW(L"hi ", out), "Expected output string \"hi \", got %s\n", wine_dbgstr_w(out));
806
809 ok(ret == 1, "Expected FormatMessageW to return 1, got %ld\n", ret);
810 ok(!lstrcmpW(L" ", out), "Expected output string \" \", got %s\n", wine_dbgstr_w(out));
811
813 FORMAT_MESSAGE_MAX_WIDTH_MASK, L"\r\r\n", 0, 0, out,
815 ok(ret == 2, "Expected FormatMessageW to return 2, got %ld\n", ret);
816 ok(!lstrcmpW(L" ", out), "Expected output string \" \", got %s\n", wine_dbgstr_w(out));
817}
818
819static void test_message_wrap(void)
820{
821 DWORD ret;
822 int i;
823 CHAR in[300], out[300], ref[300];
824
825 /* No need for wrapping */
827 "short long line", 0, 0, out, sizeof(out), NULL);
828 ok(ret == 15, "Expected FormatMessageW to return 15, got %ld\n", ret);
829 ok(!strcmp("short long line", out),"failed out=[%s]\n",out);
830
831 /* Wrap the last word */
833 "short long line", 0, 0, out, sizeof(out), NULL);
834 ok(ret == 16, "Expected FormatMessageW to return 16, got %ld\n", ret);
835 ok(!strcmp("short long\r\nline", out),"failed out=[%s]\n",out);
836
837 /* Wrap the very last word */
839 "short long long line", 0, 0, out, sizeof(out), NULL);
840 ok(ret == 21, "Expected FormatMessageW to return 21, got %ld\n", ret);
841 ok(!strcmp("short long long\r\nline", out),"failed out=[%s]\n",out);
842
843 /* Strictly less than 10 characters per line! */
845 "short long line", 0, 0, out, sizeof(out), NULL);
846 ok(ret == 16, "Expected FormatMessageW to return 16, got %ld\n", ret);
847 ok(!strcmp("short\r\nlong line", out),"failed out=[%s]\n",out);
848
849 /* Handling of duplicate spaces */
851 "short long line", 0, 0, out, sizeof(out), NULL);
852 ok(ret == 16, "Expected FormatMessageW to return 16, got %ld\n", ret);
853 ok(!strcmp("short long\r\nline", out),"failed out=[%s]\n",out);
854
856 "short long wordlongerthanaline", 0, 0, out, sizeof(out), NULL);
857 ok(ret == 33, "Expected FormatMessageW to return 33, got %ld\n", ret);
858 ok(!strcmp("short long\r\nwordlongerthanal\r\nine", out),"failed out=[%s]\n",out);
859
860 /* Breaking in the middle of spaces */
862 "short long line", 0, 0, out, sizeof(out), NULL);
863 ok(ret == 18, "Expected FormatMessageW to return 18, got %ld\n", ret);
864 ok(!strcmp("short long\r\n line", out),"failed out=[%s]\n",out);
865
867 "short long wordlongerthanaline", 0, 0, out, sizeof(out), NULL);
868 ok(ret == 35, "Expected FormatMessageW to return 35, got %ld\n", ret);
869 ok(!strcmp("short long\r\n\r\nwordlongerth\r\nanaline", out),"failed out=[%s]\n",out);
870
871 /* Handling of start-of-string spaces */
873 " short line", 0, 0, out, sizeof(out), NULL);
874 ok(ret == 13, "Expected FormatMessageW to return 13, got %ld\n", ret);
875 ok(!strcmp(" short line", out),"failed out=[%s]\n",out);
876
878 " shortlong line", 0, 0, out, sizeof(out), NULL);
879 ok(ret == 17, "Expected FormatMessageW to return 17, got %ld\n", ret);
880 ok(!strcmp("\r\nshortlong\r\nline", out),"failed out=[%s]\n",out);
881
882 /* Handling of start-of-line spaces */
884 "l1%n shortlong line", 0, 0, out, sizeof(out), NULL);
885 ok(ret == 21, "Expected FormatMessageW to return 21, got %ld\n", ret);
886 ok(!strcmp("l1\r\n\r\nshortlong\r\nline", out),"failed out=[%s]\n",out);
887
888 /* Pure space wrapping */
890 " ", 0, 0, out, sizeof(out), NULL);
891 ok(ret == 7, "Expected FormatMessageW to return 7, got %ld\n", ret);
892 ok(!strcmp("\r\n\r\n\r\n ", out),"failed out=[%s]\n",out);
893
894 /* Handling of trailing spaces */
896 "l1 ", 0, 0, out, sizeof(out), NULL);
897 ok(ret == 10, "Expected FormatMessageW to return 10, got %ld\n", ret);
898 ok(!strcmp("l1\r\n\r\n\r\n ", out),"failed out=[%s]\n",out);
899
900 /* Word that just fills the line */
902 "shortlon", 0, 0, out, sizeof(out), NULL);
903 ok(ret == 10, "Expected FormatMessageW to return 10, got %ld\n", ret);
904 ok(!strcmp("shortlon\r\n", out),"failed out=[%s]\n",out);
905
906 /* Word longer than the line */
908 "shortlongline", 0, 0, out, sizeof(out), NULL);
909 ok(ret == 15, "Expected FormatMessageW to return 15, got %ld\n", ret);
910 ok(!strcmp("shortlon\r\ngline", out),"failed out=[%s]\n",out);
911
912 /* Wrap the line multiple times */
914 "short long line", 0, 0, out, sizeof(out), NULL);
915 ok(ret == 17, "Expected FormatMessageW to return 17, got %ld\n", ret);
916 ok(!strcmp("short\r\nlong\r\nline", out),"failed out=[%s]\n",out);
917
918 /* '\n's in the source are ignored */
920 "short\nlong line", 0, 0, out, sizeof(out), NULL);
921 ok(ret == 16, "Expected FormatMessageW to return 16, got %ld\n", ret);
922 ok(!strcmp("short long\r\nline", out),"failed out=[%s]\n",out);
923
924 /* Wrap even before a '%n' */
926 "shortlon%n", 0, 0, out, sizeof(out), NULL);
927 ok(ret == 12, "Expected FormatMessageW to return 12, got %ld\n", ret);
928 ok(!strcmp("shortlon\r\n\r\n", out),"failed out=[%s]\n",out);
929
930 /* '%n's count as starting a new line and combine with line wrapping */
932 "short%nlong line", 0, 0, out, sizeof(out), NULL);
933 ok(ret == 16, "Expected FormatMessageW to return 16, got %ld\n", ret);
934 ok(!strcmp("short\r\nlong line", out),"failed out=[%s]\n",out);
935
937 "short%nlong line", 0, 0, out, sizeof(out), NULL);
938 ok(ret == 17, "Expected FormatMessageW to return 17, got %ld\n", ret);
939 ok(!strcmp("short\r\nlong\r\nline", out),"failed out=[%s]\n",out);
940
941 /* '%r's also count as starting a new line and all */
943 "short%rlong line", 0, 0, out, sizeof(out), NULL);
944 ok(ret == 15, "Expected FormatMessageW to return 15, got %ld\n", ret);
945 ok(!strcmp("short\rlong line", out),"failed out=[%s]\n",out);
946
948 "short%rlong line", 0, 0, out, sizeof(out), NULL);
949 ok(ret == 16, "Expected FormatMessageW to return 16, got %ld\n", ret);
950 ok(!strcmp("short\rlong\r\nline", out),"failed out=[%s]\n",out);
951
952 /* IGNORE_INSERTS does not prevent line wrapping or disable '%n' */
954 "short%nlong line%1", 0, 0, out, sizeof(out), NULL);
955 ok(ret == 19, "Expected FormatMessageW to return 19, got %ld\n", ret);
956 ok(!strcmp("short\r\nlong\r\nline%1", out),"failed out=[%s]\n",out);
957
958 /* MAX_WIDTH_MASK is the same as specifying an infinite line width */
959 strcpy(in, "first line%n");
960 strcpy(ref, "first line\r\n");
961 for (i=0; i < 26; i++)
962 {
963 strcat(in, "123456789 ");
964 strcat(ref, "123456789 ");
965 }
967 in, 0, 0, out, sizeof(out), NULL);
968 ok(ret == 272, "Expected FormatMessageW to return 272, got %ld\n", ret);
969 ok(!strcmp(ref, out),"failed out=[%s]\n",out);
970
971 /* Wrapping and non-space characters */
973 "short long\tline", 0, 0, out, sizeof(out), NULL);
974 ok(ret == 16, "Expected FormatMessageW to return 16, got %ld\n", ret);
975 ok(!strcmp("short\r\nlong\tline", out),"failed out=[%s]\n",out);
976
978 "short long-line", 0, 0, out, sizeof(out), NULL);
979 ok(ret == 16, "Expected FormatMessageW to return 16, got %ld\n", ret);
980 ok(!strcmp("short\r\nlong-line", out),"failed out=[%s]\n",out);
981
983 "short long_line", 0, 0, out, sizeof(out), NULL);
984 ok(ret == 16, "Expected FormatMessageW to return 16, got %ld\n", ret);
985 ok(!strcmp("short\r\nlong_line", out),"failed out=[%s]\n",out);
986
988 "short long.line", 0, 0, out, sizeof(out), NULL);
989 ok(ret == 16, "Expected FormatMessageW to return 16, got %ld\n", ret);
990 ok(!strcmp("short\r\nlong.line", out),"failed out=[%s]\n",out);
991
993 "short long,line", 0, 0, out, sizeof(out), NULL);
994 ok(ret == 16, "Expected FormatMessageW to return 16, got %ld\n", ret);
995 ok(!strcmp("short\r\nlong,line", out),"failed out=[%s]\n",out);
996
998 "short long!line", 0, 0, out, sizeof(out), NULL);
999 ok(ret == 16, "Expected FormatMessageW to return 16, got %ld\n", ret);
1000 ok(!strcmp("short\r\nlong!line", out),"failed out=[%s]\n",out);
1001
1003 "short long?line", 0, 0, out, sizeof(out), NULL);
1004 ok(ret == 16, "Expected FormatMessageW to return 16, got %ld\n", ret);
1005 ok(!strcmp("short\r\nlong?line", out),"failed out=[%s]\n",out);
1006}
1007
1008static void test_message_arg_eaten( const WCHAR *src, ... )
1009{
1010 DWORD ret;
1011 va_list list;
1012 WCHAR *arg, out[1];
1013
1014 out[0] = 0xcccc;
1015 va_start(list, src);
1016 SetLastError(0xdeadbeef);
1019 "Expected GetLastError() to return ERROR_INSUFFICIENT_BUFFER, got %lu\n", GetLastError());
1020 ok(ret == 0, "Expected FormatMessageW to return 0, got %lu\n", ret);
1021 ok(out[0] == 0, "Expected null, got %ls\n", out);
1022 arg = va_arg( list, WCHAR * );
1023#ifdef __REACTOS__
1024 ok(!!arg, "arg should not be NULL!\n");
1025 if (arg)
1026#endif
1027 ok(!wcscmp( L"unused", arg ), "Expected 'unused', got %s\n", wine_dbgstr_w(arg));
1028 va_end(list);
1029}
1030
1032{
1033 static const char init_buf[] = {'x', 'x', 'x', 'x', 'x'};
1034 static const char expected_buf[] = {'x', 'x', 'x', 'x', 'x'};
1035 DWORD ret, size, i;
1036 CHAR out[5];
1037
1038 SetLastError(0xdeadbeef);
1039 memcpy(out, init_buf, sizeof(init_buf));
1041 ok(ret == 0, "Expected FormatMessageA to return 0, got %lu\n", ret);
1043 "Expected GetLastError() to return ERROR_INSUFFICIENT_BUFFER, got %lu\n",
1044 GetLastError());
1045 ok(!memcmp(expected_buf, out, sizeof(expected_buf)),
1046 "Expected the buffer to be untouched\n");
1047
1048 SetLastError(0xdeadbeef);
1049 memcpy(out, init_buf, sizeof(init_buf));
1051 ok(ret == 0, "Expected FormatMessageA to return 0, got %lu\n", ret);
1053 "Expected GetLastError() to return ERROR_INSUFFICIENT_BUFFER, got %lu\n",
1054 GetLastError());
1055 ok(!memcmp(expected_buf, out, sizeof(expected_buf)),
1056 "Expected the buffer to be untouched\n");
1057
1058 SetLastError(0xdeadbeef);
1059 memcpy(out, init_buf, sizeof(init_buf));
1061 ok(ret == 0, "Expected FormatMessageA to return 0, got %lu\n", ret);
1063 "Expected GetLastError() to return ERROR_INSUFFICIENT_BUFFER, got %lu\n",
1064 GetLastError());
1065 ok(!memcmp(expected_buf, out, sizeof(expected_buf)),
1066 "Expected the buffer to be untouched\n");
1067
1068 for (size = 32700; size < 32800; size++)
1069 {
1070 char *tmp = HeapAlloc( GetProcessHeap(), 0, size );
1071 char *buf = HeapAlloc( GetProcessHeap(), 0, size );
1072
1073 for (i = 0; i < size; i++) tmp[i] = 'A' + i % 26;
1074 tmp[size - 1] = 0;
1075 SetLastError( 0xdeadbeef );
1077 if (size < 32768)
1078 {
1079 ok( ret == size - 1, "%lu: got %lu\n", size, ret );
1080 ok( !strcmp( tmp, buf ), "wrong buffer\n" );
1081 }
1082 else
1083 {
1084 ok( ret == 0, "%lu: got %lu\n", size, ret );
1085 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %lu\n", GetLastError() );
1086 }
1087
1088 SetLastError( 0xdeadbeef );
1089 ret = doit( FORMAT_MESSAGE_FROM_STRING, "%1", 0, 0, buf, size, tmp );
1090 if (size < 32768)
1091 {
1092 ok( ret == size - 1, "%lu: got %lu\n", size, ret );
1093 ok( !strcmp( tmp, buf ), "wrong buffer\n" );
1094 }
1095 else
1096 {
1097 ok( ret == 0, "%lu: got %lu\n", size, ret );
1099 "wrong error %lu\n", GetLastError() );
1100 }
1101 HeapFree( GetProcessHeap(), 0, buf );
1102
1103 SetLastError( 0xdeadbeef );
1105 tmp, 0, 0, (char *)&buf, size, NULL );
1106 if (size < 32768)
1107 {
1108 ok( ret == size - 1, "%lu: got %lu\n", size, ret );
1109 ok( !strcmp( tmp, buf ), "wrong buffer\n" );
1110 HeapFree( GetProcessHeap(), 0, buf );
1111 }
1112 else
1113 {
1114 ok( ret == 0, "%lu: got %lu\n", size, ret );
1115 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %lu\n", GetLastError() );
1116 }
1117 HeapFree( GetProcessHeap(), 0, tmp );
1118 }
1119}
1120
1122{
1123 DWORD ret, size, i;
1124 WCHAR out[8];
1125
1126 SetLastError(0xdeadbeef);
1127 lstrcpyW( out, L"xxxxxx" );
1129 ok(ret == 0, "Expected FormatMessageA to return 0, got %lu\n", ret);
1131 "Expected GetLastError() to return ERROR_INSUFFICIENT_BUFFER, got %lu\n",
1132 GetLastError());
1133 ok(!lstrcmpW( out, L"xxxxxx" ),
1134 "Expected the buffer to be untouched\n");
1135
1136 SetLastError(0xdeadbeef);
1137 lstrcpyW( out, L"xxxxxx" );
1139 ok(ret == 0, "Expected FormatMessageA to return 0, got %lu\n", ret);
1141 "Expected GetLastError() to return ERROR_INSUFFICIENT_BUFFER, got %lu\n",
1142 GetLastError());
1143 ok(!memcmp(out, L"\0xxxxx", 6 * sizeof(WCHAR)) ||
1144 broken(!lstrcmpW( out, L"xxxxxx" )), /* winxp */
1145 "Expected the buffer to be truncated\n");
1146
1147 SetLastError(0xdeadbeef);
1148 lstrcpyW( out, L"xxxxxx" );
1150 ok(ret == 0, "Expected FormatMessageA to return 0, got %lu\n", ret);
1152 "Expected GetLastError() to return ERROR_INSUFFICIENT_BUFFER, got %lu\n",
1153 GetLastError());
1154 ok(!memcmp(out, L"tes\0xx", 6 * sizeof(WCHAR)) ||
1155 broken(!lstrcmpW( out, L"xxxxxx" )), /* winxp */
1156 "Expected the buffer to be truncated\n");
1157
1158 for (size = 1000; size < 1000000; size += size / 10)
1159 {
1160 WCHAR *tmp = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) );
1161 WCHAR *buf = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) );
1162 for (i = 0; i < size; i++) tmp[i] = 'A' + i % 26;
1163 tmp[size - 1] = 0;
1165 ok(ret == size - 1 || broken(!ret), /* winxp */ "got %lu\n", ret);
1166 if (!ret) break;
1167 ok( !lstrcmpW( tmp, buf ), "wrong buffer\n" );
1168 ret = doitW( FORMAT_MESSAGE_FROM_STRING, L"%1", 0, 0, buf, size, tmp );
1169 ok(ret == size - 1, "got %lu\n", ret);
1170 ok( !lstrcmpW( tmp, buf ), "wrong buffer\n" );
1171 HeapFree( GetProcessHeap(), 0, buf );
1173 tmp, 0, 0, (WCHAR *)&buf, size, NULL );
1174 ok(ret == size - 1, "got %lu\n", ret);
1175 ok( !lstrcmpW( tmp, buf ), "wrong buffer\n" );
1176 HeapFree( GetProcessHeap(), 0, tmp );
1177 HeapFree( GetProcessHeap(), 0, buf );
1178 }
1179
1180 /* va_arg is eaten even in case of insufficient buffer */
1181 test_message_arg_eaten( L"%1!s! %2!s!", L"eaten", L"unused" );
1182}
1183
1185{
1186 DWORD ret, error;
1187
1188 /* Without FORMAT_MESSAGE_ALLOCATE_BUFFER, only the specified buffer size is checked. */
1189 SetLastError(0xdeadbeef);
1191 error = GetLastError();
1192 ok(!ret, "FormatMessageA returned %lu\n", ret);
1193 ok(error == ERROR_INSUFFICIENT_BUFFER, "last error %lu\n", error);
1194
1195 SetLastError(0xdeadbeef);
1197 error = GetLastError();
1198 ok(!ret, "FormatMessageA returned %lu\n", ret);
1199 ok(error == ERROR_INSUFFICIENT_BUFFER, "last error %lu\n", error);
1200
1201 if (0) /* crashes on Windows */
1202 {
1203 SetLastError(0xdeadbeef);
1205 }
1206
1207 SetLastError(0xdeadbeef);
1209 error = GetLastError();
1210 ok(!ret, "FormatMessageA returned %lu\n", ret);
1211 ok(error == ERROR_NOT_ENOUGH_MEMORY, "last error %lu\n", error);
1212
1213 SetLastError(0xdeadbeef);
1215 error = GetLastError();
1216 ok(!ret, "FormatMessageA returned %lu\n", ret);
1217 ok(error == ERROR_NOT_ENOUGH_MEMORY, "last error %lu\n", error);
1218
1219 SetLastError(0xdeadbeef);
1221 error = GetLastError();
1222 ok(!ret, "FormatMessageA returned %lu\n", ret);
1223 ok(error == ERROR_NOT_ENOUGH_MEMORY, "last error %lu\n", error);
1224}
1225
1227{
1228 DWORD ret, error;
1229
1230 SetLastError(0xdeadbeef);
1232 error = GetLastError();
1233 ok(!ret, "FormatMessageW returned %lu\n", ret);
1234 ok(error == ERROR_INVALID_PARAMETER, "last error %lu\n", error);
1235
1236 SetLastError(0xdeadbeef);
1238 error = GetLastError();
1239 ok(!ret, "FormatMessageW returned %lu\n", ret);
1240 ok(error == ERROR_INVALID_PARAMETER, "last error %lu\n", error);
1241
1242 SetLastError(0xdeadbeef);
1244 error = GetLastError();
1245 ok(!ret, "FormatMessageW returned %lu\n", ret);
1246 ok(error == ERROR_INVALID_PARAMETER, "last error %lu\n", error);
1247
1248 SetLastError(0xdeadbeef);
1250 error = GetLastError();
1251 ok(!ret, "FormatMessageW returned %lu\n", ret);
1252 ok(error == ERROR_INVALID_PARAMETER, "last error %lu\n", error);
1253
1254 SetLastError(0xdeadbeef);
1256 error = GetLastError();
1257 ok(!ret, "FormatMessageW returned %lu\n", ret);
1258 ok(error == ERROR_INVALID_PARAMETER, "last error %lu\n", error);
1259
1260 SetLastError(0xdeadbeef);
1262 error = GetLastError();
1263 ok(!ret, "FormatMessageW returned %lu\n", ret);
1264 ok(error == ERROR_INVALID_PARAMETER, "last error %lu\n", error);
1265}
1266
1268{
1269 DWORD ret;
1270 char *buf;
1271
1272 /* While MSDN suggests that FormatMessageA allocates a buffer whose size is
1273 * the larger of the output string and the requested buffer size, the tests
1274 * will not try to determine the actual size of the buffer allocated, as
1275 * the return value of LocalSize cannot be trusted for the purpose, and it should
1276 * in any case be safe for FormatMessageA to allocate in the manner that
1277 * MSDN suggests. */
1278
1279 SetLastError(0xdeadbeef);
1280 buf = (char *)0xdeadbeef;
1282 "", 0, 0, (char *)&buf, 0, NULL);
1283 ok(ret == 0, "Expected FormatMessageA to return 0, got %lu\n", ret);
1284 ok(buf == NULL, "Expected output buffer pointer to be NULL\n");
1285 ok(GetLastError() == ERROR_NO_WORK_DONE || broken(GetLastError() == 0xdeadbeef),
1286 "Expected GetLastError() to return ERROR_NO_WORK_DONE, got %lu\n", GetLastError());
1287
1288 buf = (char *)0xdeadbeef;
1290 "test", 0, 0, (char *)&buf, 0, NULL);
1291 ok(ret == 4, "Expected FormatMessageA to return 4, got %lu\n", ret);
1292 ok(buf != NULL && buf != (char *)0xdeadbeef,
1293 "Expected output buffer pointer to be valid\n");
1294 if (buf != NULL && buf != (char *)0xdeadbeef)
1295 {
1296 ok(!strcmp("test", buf),
1297 "Expected buffer to contain \"test\", got %s\n", buf);
1298 LocalFree(buf);
1299 }
1300
1301 buf = (char *)0xdeadbeef;
1303 "test", 0, 0, (char *)&buf, strlen("test"), NULL);
1304 ok(ret == 4, "Expected FormatMessageA to return 4, got %lu\n", ret);
1305 ok(buf != NULL && buf != (char *)0xdeadbeef,
1306 "Expected output buffer pointer to be valid\n");
1307 if (buf != NULL && buf != (char *)0xdeadbeef)
1308 {
1309 ok(!strcmp("test", buf),
1310 "Expected buffer to contain \"test\", got %s\n", buf);
1311 LocalFree(buf);
1312 }
1313
1314 buf = (char *)0xdeadbeef;
1316 "test", 0, 0, (char *)&buf, strlen("test") + 1, NULL);
1317 ok(ret == 4, "Expected FormatMessageA to return 4, got %lu\n", ret);
1318 ok(buf != NULL && buf != (char *)0xdeadbeef,
1319 "Expected output buffer pointer to be valid\n");
1320 if (buf != NULL && buf != (char *)0xdeadbeef)
1321 {
1322 ok(!strcmp("test", buf),
1323 "Expected buffer to contain \"test\", got %s\n", buf);
1324 LocalFree(buf);
1325 }
1326
1327 buf = (char *)0xdeadbeef;
1329 "test", 0, 0, (char *)&buf, strlen("test") + 2, NULL);
1330 ok(ret == 4, "Expected FormatMessageA to return 4, got %lu\n", ret);
1331 ok(buf != NULL && buf != (char *)0xdeadbeef,
1332 "Expected output buffer pointer to be valid\n");
1333 if (buf != NULL && buf != (char *)0xdeadbeef)
1334 {
1335 ok(!strcmp("test", buf),
1336 "Expected buffer to contain \"test\", got %s\n", buf);
1337 LocalFree(buf);
1338 }
1339
1340 buf = (char *)0xdeadbeef;
1342 "test", 0, 0, (char *)&buf, 1024, NULL);
1343 ok(ret == 4, "Expected FormatMessageA to return 4, got %lu\n", ret);
1344 ok(buf != NULL && buf != (char *)0xdeadbeef,
1345 "Expected output buffer pointer to be valid\n");
1346 if (buf != NULL && buf != (char *)0xdeadbeef)
1347 {
1348 ok(!strcmp("test", buf),
1349 "Expected buffer to contain \"test\", got %s\n", buf);
1350 LocalFree(buf);
1351 }
1352}
1353
1355{
1356 DWORD ret;
1357 WCHAR *buf;
1358
1359 /* While MSDN suggests that FormatMessageW allocates a buffer whose size is
1360 * the larger of the output string and the requested buffer size, the tests
1361 * will not try to determine the actual size of the buffer allocated, as
1362 * the return value of LocalSize cannot be trusted for the purpose, and it should
1363 * in any case be safe for FormatMessageW to allocate in the manner that
1364 * MSDN suggests. */
1365
1366 if (0) /* crashes on Windows */
1367 {
1368 buf = (WCHAR *)0xdeadbeef;
1370 NULL, 0, 0, (WCHAR *)&buf, 0, NULL);
1371 }
1372
1373 SetLastError(0xdeadbeef);
1374 buf = (WCHAR *)0xdeadbeef;
1376 L"", 0, 0, (WCHAR *)&buf, 0, NULL);
1377 ok(ret == 0, "Expected FormatMessageW to return 0, got %lu\n", ret);
1378 ok(buf == NULL, "Expected output buffer pointer to be NULL\n");
1379 ok(GetLastError() == ERROR_NO_WORK_DONE || broken(GetLastError() == 0xdeadbeef),
1380 "Expected GetLastError() to return ERROR_NO_WORK_DONE, got %lu\n", GetLastError());
1381
1382 SetLastError(0xdeadbeef);
1383 buf = (WCHAR *)0xdeadbeef;
1385 0, 0, (WCHAR *)&buf, 0, L"" );
1386 ok(ret == 0, "Expected FormatMessageW to return 0, got %lu\n", ret);
1387 ok(buf == NULL, "Expected output buffer pointer to be NULL\n");
1388 ok(GetLastError() == ERROR_NO_WORK_DONE || broken(GetLastError() == 0xdeadbeef),
1389 "Expected GetLastError() to return ERROR_NO_WORK_DONE, got %lu\n", GetLastError());
1390
1391 buf = (WCHAR *)0xdeadbeef;
1393 L"test", 0, 0, (WCHAR *)&buf, 0, NULL);
1394 ok(ret == 4, "Expected FormatMessageA to return 4, got %lu\n", ret);
1395 ok(buf != NULL && buf != (WCHAR *)0xdeadbeef,
1396 "Expected output buffer pointer to be valid\n");
1397 if (buf != NULL && buf != (WCHAR *)0xdeadbeef)
1398 {
1399 ok(!lstrcmpW(L"test", buf),
1400 "Expected buffer to contain \"test\", got %s\n", wine_dbgstr_w(buf));
1401 LocalFree(buf);
1402 }
1403
1404 buf = (WCHAR *)0xdeadbeef;
1406 L"test", 0, 0, (WCHAR *)&buf, 4, NULL);
1407 ok(ret == 4, "Expected FormatMessageA to return 4, got %lu\n", ret);
1408 ok(buf != NULL && buf != (WCHAR *)0xdeadbeef,
1409 "Expected output buffer pointer to be valid\n");
1410 if (buf != NULL && buf != (WCHAR *)0xdeadbeef)
1411 {
1412 ok(!lstrcmpW(L"test", buf),
1413 "Expected buffer to contain \"test\", got %s\n", wine_dbgstr_w(buf));
1414 LocalFree(buf);
1415 }
1416
1417 buf = (WCHAR *)0xdeadbeef;
1419 L"test", 0, 0, (WCHAR *)&buf, 5, NULL);
1420 ok(ret == 4, "Expected FormatMessageA to return 4, got %lu\n", ret);
1421 ok(buf != NULL && buf != (WCHAR *)0xdeadbeef,
1422 "Expected output buffer pointer to be valid\n");
1423 if (buf != NULL && buf != (WCHAR *)0xdeadbeef)
1424 {
1425 ok(!lstrcmpW(L"test", buf),
1426 "Expected buffer to contain \"test\", got %s\n", wine_dbgstr_w(buf));
1427 LocalFree(buf);
1428 }
1429
1430 buf = (WCHAR *)0xdeadbeef;
1432 L"test", 0, 0, (WCHAR *)&buf, 6, NULL);
1433 ok(ret == 4, "Expected FormatMessageA to return 4, got %lu\n", ret);
1434 ok(buf != NULL && buf != (WCHAR *)0xdeadbeef,
1435 "Expected output buffer pointer to be valid\n");
1436 if (buf != NULL && buf != (WCHAR *)0xdeadbeef)
1437 {
1438 ok(!lstrcmpW(L"test", buf),
1439 "Expected buffer to contain \"test\", got %s\n", wine_dbgstr_w(buf));
1440 LocalFree(buf);
1441 }
1442
1443 buf = (WCHAR *)0xdeadbeef;
1445 L"test", 0, 0, (WCHAR *)&buf, 1024, NULL);
1446 ok(ret == 4, "Expected FormatMessageA to return 4, got %lu\n", ret);
1447 ok(buf != NULL && buf != (WCHAR *)0xdeadbeef,
1448 "Expected output buffer pointer to be valid\n");
1449 if (buf != NULL && buf != (WCHAR *)0xdeadbeef)
1450 {
1451 ok(!lstrcmpW(L"test", buf),
1452 "Expected buffer to contain \"test\", got %s\n", wine_dbgstr_w(buf));
1453 LocalFree(buf);
1454 }
1455}
1456
1458{
1459 DWORD ret, error;
1460 HMODULE h;
1461 CHAR out[0x100] = {0};
1462
1463 h = GetModuleHandleA("kernel32.dll");
1464 ok(h != 0, "GetModuleHandle failed\n");
1465
1466 /*Test existing messageID; as the message strings from wine's kernel32 differ from windows' kernel32 we don't compare
1467 the strings but only test that FormatMessage doesn't return 0*/
1470 ok(ret != 0, "FormatMessageA returned 0\n");
1471
1472 /* Test HRESULT. It's not documented but in practice _com_error::ErrorMessage relies on this. */
1473 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, h, 0x80070005 /* E_ACCESSDENIED */,
1475 ok(ret != 0, "FormatMessageA returned 0\n");
1476
1479 ok(ret != 0, "FormatMessageA returned 0\n");
1480
1481 /* Test a message string with an insertion without passing any variadic arguments. */
1482 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, h, 193 /* ERROR_BAD_EXE_FORMAT */,
1484 ok(ret == 0, "FormatMessageA returned non-zero\n");
1485
1487 FORMAT_MESSAGE_ARGUMENT_ARRAY, h, 193 /* ERROR_BAD_EXE_FORMAT */,
1489 ok(ret == 0, "FormatMessageA returned non-zero\n");
1490
1491 /*Test nonexistent messageID with varying language IDs Note: FormatMessageW behaves the same*/
1492 SetLastError(0xdeadbeef);
1495 error = GetLastError();
1496 ok(ret == 0, "FormatMessageA returned %lu instead of 0\n", ret);
1499 error == ERROR_RESOURCE_TYPE_NOT_FOUND, "Unexpected last error %lu.\n", error);
1500
1501 SetLastError(0xdeadbeef);
1504 error = GetLastError();
1505 ok(ret == 0, "FormatMessageA returned %lu instead of 0\n", ret);
1508 error == ERROR_RESOURCE_TYPE_NOT_FOUND, "Unexpected last error %lu.\n", error);
1509
1510 SetLastError(0xdeadbeef);
1513 error = GetLastError();
1514 ok(ret == 0, "FormatMessageA returned %lu instead of 0\n", ret);
1517 error == ERROR_RESOURCE_TYPE_NOT_FOUND, "Unexpected last error %lu.\n", error);
1518
1519 SetLastError(0xdeadbeef);
1522 error = GetLastError();
1523 ok(ret == 0, "FormatMessageA returned %lu instead of 0\n", ret);
1529 "last error %lu\n", error);
1530
1531 SetLastError(0xdeadbeef);
1534 error = GetLastError();
1535 ok(ret == 0, "FormatMessageA returned %lu instead of 0\n", ret);
1541 "last error %lu\n", error);
1542}
1543
1545{
1546 static const char init_buf[] = {'x', 'x', 'x', 'x', 'x'};
1547
1548 DWORD ret;
1549 CHAR out[5];
1550 char *ptr;
1551
1552 SetLastError(0xdeadbeef);
1553 memcpy(out, init_buf, sizeof(init_buf));
1554 ret = FormatMessageA(0, "test", 0, 0, out, ARRAY_SIZE(out), NULL);
1555 ok(ret == 0, "Expected FormatMessageA to return 0, got %lu\n", ret);
1556 ok(!memcmp(out, init_buf, sizeof(init_buf)),
1557 "Expected the output buffer to be untouched\n");
1559 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %lu\n",
1560 GetLastError());
1561
1562 SetLastError(0xdeadbeef);
1563 ptr = (char *)0xdeadbeef;
1564 ret = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER, "test", 0, 0, (char *)&ptr, 0, NULL);
1565 ok(ret == 0, "Expected FormatMessageA to return 0, got %lu\n", ret);
1566 ok(ptr == NULL, "Expected output pointer to be initialized to NULL, got %p\n", ptr);
1568 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %lu\n",
1569 GetLastError());
1570
1571 SetLastError(0xdeadbeef);
1572 memcpy(out, init_buf, sizeof(init_buf));
1574 ok(ret == 0, "Expected FormatMessageA to return 0, got %lu\n", ret);
1575 ok(!memcmp(out, init_buf, sizeof(init_buf)),
1576 "Expected the output buffer to be untouched\n");
1578 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %lu\n",
1579 GetLastError());
1580
1581 SetLastError(0xdeadbeef);
1582 memcpy(out, init_buf, sizeof(init_buf));
1584 ok(ret == 0, "Expected FormatMessageA to return 0, got %lu\n", ret);
1585 ok(!memcmp(out, init_buf, sizeof(init_buf)),
1586 "Expected the output buffer to be untouched\n");
1588 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %lu\n",
1589 GetLastError());
1590
1591 SetLastError(0xdeadbeef);
1592 memcpy(out, init_buf, sizeof(init_buf));
1594 ok(ret == 0, "Expected FormatMessageA to return 0, got %lu\n", ret);
1595 ok(!memcmp(out, init_buf, sizeof(init_buf)),
1596 "Expected the output buffer to be untouched\n");
1598 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %lu\n",
1599 GetLastError());
1600
1601 /* Simultaneously setting FORMAT_MESSAGE_FROM_STRING with other source
1602 * flags is apparently permissible, and FORMAT_MESSAGE_FROM_STRING takes
1603 * precedence in this case. */
1604
1605 memcpy(out, init_buf, sizeof(init_buf));
1607 "test", 0, 0, out, ARRAY_SIZE(out), NULL);
1608 ok(ret == 4, "Expected FormatMessageA to return 4, got %lu\n", ret);
1609 ok(!strcmp("test", out),
1610 "Expected the output buffer to be untouched\n");
1611
1612 memcpy(out, init_buf, sizeof(init_buf));
1614 "test", 0, 0, out, ARRAY_SIZE(out), NULL);
1615 ok(ret == 4, "Expected FormatMessageA to return 4, got %lu\n", ret);
1616 ok(!strcmp("test", out),
1617 "Expected the output buffer to be untouched\n");
1618
1619 memcpy(out, init_buf, sizeof(init_buf));
1622 ok(ret == 4, "Expected FormatMessageA to return 4, got %lu\n", ret);
1623 ok(!strcmp("test", out),
1624 "Expected the output buffer to be untouched\n");
1625}
1626
1628{
1629 DWORD ret;
1630 WCHAR out[8];
1631 WCHAR *ptr;
1632
1633 SetLastError(0xdeadbeef);
1634 lstrcpyW( out, L"xxxxxx" );
1635 ret = FormatMessageW(0, L"test", 0, 0, out, ARRAY_SIZE(out), NULL);
1636 ok(ret == 0, "Expected FormatMessageW to return 0, got %lu\n", ret);
1637 ok(!lstrcmpW( out, L"xxxxxx" ),
1638 "Expected the output buffer to be untouched\n");
1640 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %lu\n",
1641 GetLastError());
1642
1643 SetLastError(0xdeadbeef);
1644 ptr = (WCHAR *)0xdeadbeef;
1646 ok(ret == 0, "Expected FormatMessageW to return 0, got %lu\n", ret);
1647 ok(ptr == NULL, "Expected output pointer to be initialized to NULL, got %p\n", ptr);
1649 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %lu\n",
1650 GetLastError());
1651
1652 SetLastError(0xdeadbeef);
1653 lstrcpyW( out, L"xxxxxx" );
1655 ok(ret == 0, "Expected FormatMessageW to return 0, got %lu\n", ret);
1656 ok(!lstrcmpW( out, L"xxxxxx" ),
1657 "Expected the output buffer to be untouched\n");
1659 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %lu\n",
1660 GetLastError());
1661
1662 SetLastError(0xdeadbeef);
1663 lstrcpyW( out, L"xxxxxx" );
1665 ok(ret == 0, "Expected FormatMessageW to return 0, got %lu\n", ret);
1666 ok(!lstrcmpW( out, L"xxxxxx" ),
1667 "Expected the output buffer to be untouched\n");
1669 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %lu\n",
1670 GetLastError());
1671
1672 SetLastError(0xdeadbeef);
1673 lstrcpyW( out, L"xxxxxx" );
1675 ok(ret == 0, "Expected FormatMessageW to return 0, got %lu\n", ret);
1676 ok(!lstrcmpW( out, L"xxxxxx" ),
1677 "Expected the output buffer to be untouched\n");
1679 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %lu\n",
1680 GetLastError());
1681
1682 /* Simultaneously setting FORMAT_MESSAGE_FROM_STRING with other source
1683 * flags is apparently permissible, and FORMAT_MESSAGE_FROM_STRING takes
1684 * precedence in this case. */
1685
1686 lstrcpyW( out, L"xxxxxx" );
1688 L"test", 0, 0, out, ARRAY_SIZE(out), NULL);
1689 ok(ret == 4, "Expected FormatMessageW to return 4, got %lu\n", ret);
1690 ok(!lstrcmpW(L"test", out),
1691 "Expected the output buffer to be untouched\n");
1692
1693 lstrcpyW( out, L"xxxxxx" );
1695 L"test", 0, 0, out, ARRAY_SIZE(out), NULL);
1696 ok(ret == 4, "Expected FormatMessageW to return 4, got %lu\n", ret);
1697 ok(!lstrcmpW(L"test", out),
1698 "Expected the output buffer to be untouched\n");
1699
1700 lstrcpyW( out, L"xxxxxx" );
1703 ok(ret == 4, "Expected FormatMessageW to return 4, got %lu\n", ret);
1704 ok(!lstrcmpW(L"test", out),
1705 "Expected the output buffer to be untouched\n");
1706}
1707
1709{
1710 WCHAR outW[0x100], expW[0x100];
1711 char outA[0x100];
1712 DWORD r;
1713 static const struct
1714 {
1715 UINT64 number;
1716 const char expected[32];
1717 int len;
1718 } unsigned_tests[] =
1719 {
1720 { 0, "0", 1 },
1721 { 1234567890, "1234567890", 10},
1722 { ULL(0xFFFFFFFF,0xFFFFFFFF), "18446744073709551615", 20 },
1723 { ULL(0x7FFFFFFF,0xFFFFFFFF), "9223372036854775807", 19 },
1724 };
1725 static const struct
1726 {
1727 INT64 number;
1728 const char expected[32];
1729 int len;
1730 } signed_tests[] =
1731 {
1732 { 0, "0" , 1},
1733 { 1234567890, "1234567890", 10 },
1734 { -1, "-1", 2},
1735 { ULL(0xFFFFFFFF,0xFFFFFFFF), "-1", 2},
1736 { ULL(0x7FFFFFFF,0xFFFFFFFF), "9223372036854775807", 19 },
1737 { -ULL(0x7FFFFFFF,0xFFFFFFFF), "-9223372036854775807", 20},
1738 };
1739 int i;
1740
1741 for (i = 0; i < ARRAY_SIZE(unsigned_tests); i++)
1742 {
1743 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1!I64u!", 0, 0, outW, ARRAY_SIZE(outW),
1744 unsigned_tests[i].number);
1745 MultiByteToWideChar(CP_ACP, 0, unsigned_tests[i].expected, -1, expW, ARRAY_SIZE(expW));
1746 ok(!lstrcmpW(outW, expW),"[%d] failed, expected %s, got %s\n", i,
1747 unsigned_tests[i].expected, wine_dbgstr_w(outW));
1748 ok(r == unsigned_tests[i].len,"[%d] failed: r=%ld\n", i, r);
1749 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!I64u!",
1750 0, 0, outA, sizeof(outA), unsigned_tests[i].number);
1751 ok(!strcmp(outA, unsigned_tests[i].expected),"[%d] failed, expected %s, got %s\n", i,
1752 unsigned_tests[i].expected, outA);
1753 ok(r == unsigned_tests[i].len,"[%d] failed: r=%ld\n", i, r);
1754 }
1755
1756 for (i = 0; i < ARRAY_SIZE(signed_tests); i++)
1757 {
1758 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1!I64d!", 0, 0, outW, ARRAY_SIZE(outW),
1759 signed_tests[i].number);
1760 MultiByteToWideChar(CP_ACP, 0, signed_tests[i].expected, -1, expW, ARRAY_SIZE(expW));
1761 ok(!lstrcmpW(outW, expW),"[%d] failed, expected %s, got %s\n", i,
1762 signed_tests[i].expected, wine_dbgstr_w(outW));
1763 ok(r == signed_tests[i].len,"[%d] failed: r=%ld\n", i, r);
1764 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!I64d!",
1765 0, 0, outA, sizeof(outA), signed_tests[i].number);
1766 ok(!strcmp(outA, signed_tests[i].expected),"[%d] failed, expected %s, got %s\n", i,
1767 signed_tests[i].expected, outA);
1768 ok(r == signed_tests[i].len,"[%d] failed: r=%ld\n", i, r);
1769 }
1770}
1771
1773{
1774 static const struct
1775 {
1777 BOOL broken;
1778 }
1779 tests[] =
1780 {
1781 {E_NOTIMPL},
1782 {E_FAIL},
1783 {DXGI_ERROR_INVALID_CALL, TRUE /* Available since Win8 */},
1784 {DXGI_ERROR_NOT_CURRENTLY_AVAILABLE, TRUE /* Available since Win8 */},
1785 };
1786
1787 char buffer[256];
1788 unsigned int i;
1789 DWORD len;
1790
1791 for (i = 0; i < ARRAY_SIZE(tests); ++i)
1792 {
1795 ok(len || broken(tests[i].broken), "Got zero len, code %#lx.\n", tests[i].error_code);
1796 }
1797}
1798
1799START_TEST(format_msg)
1800{
1801 DWORD ret;
1802
1811
1812 SetLastError(0xdeadbeef);
1815 {
1816 win_skip("FormatMessageW is not implemented\n");
1817 return;
1818 }
1819
1828}
unsigned long long UINT64
signed long long INT64
#define ok(value,...)
Definition: atltest.h:57
#define broken(x)
Definition: atltest.h:178
#define START_TEST(x)
Definition: atltest.h:75
#define ARRAY_SIZE(A)
Definition: main.h:20
Definition: list.h:37
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
#define ERROR_MORE_DATA
Definition: dderror.h:13
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10
#define E_NOTIMPL
Definition: ddrawi.h:99
#define E_FAIL
Definition: ddrawi.h:102
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define GetProcessHeap()
Definition: compat.h:736
#define ERROR_CALL_NOT_IMPLEMENTED
Definition: compat.h:102
#define ERROR_INVALID_PARAMETER
Definition: compat.h:101
#define CP_ACP
Definition: compat.h:109
#define SetLastError(x)
Definition: compat.h:752
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
#define lstrcpyW
Definition: compat.h:749
#define MultiByteToWideChar
Definition: compat.h:110
static const WCHAR expW[]
Definition: math.c:50
HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleA(LPCSTR lpModuleName)
Definition: loader.c:812
DWORD WINAPI FormatMessageW(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, LPWSTR lpBuffer, DWORD nSize, __ms_va_list *args)
Definition: format_msg.c:583
DWORD WINAPI FormatMessageA(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, LPSTR lpBuffer, DWORD nSize, __ms_va_list *args)
Definition: format_msg.c:483
int WINAPI lstrcmpW(LPCWSTR str1, LPCWSTR str2)
Definition: locale.c:4246
_ACRTIMP int __cdecl wcscmp(const wchar_t *, const wchar_t *)
Definition: wcs.c:1972
_ACRTIMP int __cdecl memcmp(const void *, const void *, size_t)
Definition: string.c:2802
#define va_end(v)
Definition: stdarg.h:28
#define va_arg(v, l)
Definition: stdarg.h:27
#define va_start(v, l)
Definition: stdarg.h:26
_ACRTIMP size_t __cdecl strlen(const char *)
Definition: string.c:1592
_ACRTIMP int __cdecl strcmp(const char *, const char *)
Definition: string.c:3319
char * va_list
Definition: vadefs.h:50
return ret
Definition: mutex.c:146
#define L(x)
Definition: resources.c:13
#define doit(a, b)
Definition: fieldoff.c:4
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
GLenum src
Definition: glext.h:6340
GLuint buffer
Definition: glext.h:5915
GLsizeiptr size
Definition: glext.h:5919
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLuint in
Definition: glext.h:9616
GLbitfield flags
Definition: glext.h:7161
GLuint GLenum GLenum GLenum GLenum outW
Definition: glext.h:9616
GLenum GLsizei len
Definition: glext.h:6722
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:7723
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1594
unsigned char size_t * outsize
Definition: jpeglib.h:981
#define wine_dbgstr_w
Definition: kernel32.h:34
#define win_skip
Definition: minitest.h:67
CONST void * LPCVOID
Definition: minwindef.h:164
#define error(str)
Definition: mkdosfs.c:1605
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
static PVOID ptr
Definition: dispmode.c:27
static struct test_info tests[]
msg_id
Definition: edit.c:43
BOOL expected
Definition: store.c:2000
static unsigned int number
Definition: dsound.c:1479
static DWORD WINAPIV doitW(DWORD flags, LPCVOID src, DWORD msg_id, DWORD lang_id, LPWSTR out, DWORD outsize,...)
Definition: format_msg.c:42
static void test_message_null_buffer(void)
Definition: format_msg.c:1184
static void test_message_allocate_buffer(void)
Definition: format_msg.c:1267
static void test_message_insufficient_buffer_wide(void)
Definition: format_msg.c:1121
static void test_message_ignore_inserts_wide(void)
Definition: format_msg.c:726
static void test_message_allocate_buffer_wide(void)
Definition: format_msg.c:1354
static void test_message_from_string_wide(void)
Definition: format_msg.c:55
#define ULL(a, b)
Definition: format_msg.c:27
static void test_message_ignore_inserts(void)
Definition: format_msg.c:633
static void test_message_null_buffer_wide(void)
Definition: format_msg.c:1226
static void test_message_from_64bit_number(void)
Definition: format_msg.c:1708
static void test_message_from_string(void)
Definition: format_msg.c:336
static void test_message_insufficient_buffer(void)
Definition: format_msg.c:1031
static void test_message_invalid_flags_wide(void)
Definition: format_msg.c:1627
static void test_message_arg_eaten(const WCHAR *src,...)
Definition: format_msg.c:1008
static void test_message_invalid_flags(void)
Definition: format_msg.c:1544
static void test_message_wrap(void)
Definition: format_msg.c:819
static void test_message_system_errors(void)
Definition: format_msg.c:1772
static void test_message_from_hmodule(void)
Definition: format_msg.c:1457
#define list
Definition: rosglue.h:35
#define WINAPIV
Definition: sdbpapi.h:64
#define LANG_NEUTRAL
Definition: nls.h:22
#define MAKELANGID(p, s)
Definition: nls.h:15
#define SUBLANG_ENGLISH_UK
Definition: nls.h:223
#define LANG_ENGLISH
Definition: nls.h:52
#define SUBLANG_NEUTRAL
Definition: nls.h:167
#define SUBLANG_DEFAULT
Definition: nls.h:168
#define SUBLANG_SYS_DEFAULT
Definition: nls.h:169
#define SUBLANG_ENGLISH_US
Definition: nls.h:222
strcat
Definition: string.h:92
strcpy
Definition: string.h:131
Definition: match.c:390
Definition: send.c:48
#define LANG_USER_DEFAULT
Definition: tnerror.cpp:50
uint32_t ULONG_PTR
Definition: typedefs.h:65
wchar_t tm const _CrtWcstime_Writes_and_advances_ptr_ count wchar_t ** out
Definition: wcsftime.cpp:383
static int error_code[8]
Definition: odbccp32.c:61
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define FORMAT_MESSAGE_FROM_STRING
Definition: winbase.h:398
#define FORMAT_MESSAGE_MAX_WIDTH_MASK
Definition: winbase.h:402
#define FORMAT_MESSAGE_IGNORE_INSERTS
Definition: winbase.h:397
#define FORMAT_MESSAGE_FROM_SYSTEM
Definition: winbase.h:400
#define FORMAT_MESSAGE_ALLOCATE_BUFFER
Definition: winbase.h:396
#define FORMAT_MESSAGE_ARGUMENT_ARRAY
Definition: winbase.h:401
#define FORMAT_MESSAGE_FROM_HMODULE
Definition: winbase.h:399
void * arg
Definition: msvc.h:10
#define ERROR_MUI_FILE_NOT_FOUND
Definition: winerror.h:3250
#define ERROR_NO_WORK_DONE
Definition: winerror.h:409
#define TRUST_E_NOSIGNATURE
Definition: winerror.h:4630
#define ERROR_MUI_FILE_NOT_LOADED
Definition: winerror.h:3255
#define DXGI_ERROR_INVALID_CALL
Definition: winerror.h:7120
#define ERROR_RESOURCE_TYPE_NOT_FOUND
Definition: winerror.h:1477
#define ERROR_RESOURCE_LANG_NOT_FOUND
Definition: winerror.h:1479
#define ERROR_MR_MID_NOT_FOUND
Definition: winerror.h:442
#define DXGI_ERROR_NOT_CURRENTLY_AVAILABLE
Definition: winerror.h:7140
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR * LPWSTR
Definition: xmlstorage.h:184
char * LPSTR
Definition: xmlstorage.h:182
char CHAR
Definition: xmlstorage.h:175