winex11.drv: Map coordinates before calling send_mouse_input.
[wine/zf.git] / dlls / kernel32 / tests / format_msg.c
blob4997f8991bd0381365f2f722256425a7217b0d33
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 #define ULL(a,b) (((ULONG64)(a) << 32) | (b))
29 static DWORD WINAPIV doit(DWORD flags, LPCVOID src, DWORD msg_id, DWORD lang_id,
30 LPSTR out, DWORD outsize, ... )
32 __ms_va_list list;
33 DWORD r;
35 __ms_va_start(list, outsize);
36 r = FormatMessageA(flags, src, msg_id,
37 lang_id, out, outsize, &list);
38 __ms_va_end(list);
39 return r;
42 static DWORD WINAPIV doitW(DWORD flags, LPCVOID src, DWORD msg_id, DWORD lang_id,
43 LPWSTR out, DWORD outsize, ... )
45 __ms_va_list list;
46 DWORD r;
48 __ms_va_start(list, outsize);
49 r = FormatMessageW(flags, src, msg_id,
50 lang_id, out, outsize, &list);
51 __ms_va_end(list);
52 return r;
55 static void test_message_from_string_wide(void)
57 WCHAR out[0x100] = {0};
58 DWORD r, error;
60 /* the basics */
61 r = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, L"test", 0, 0, out, ARRAY_SIZE(out), NULL);
62 ok(!lstrcmpW(L"test", out), "failed out=%s\n", wine_dbgstr_w(out));
63 ok(r==4, "failed: r=%d\n", r);
65 /* null string, crashes on Windows */
66 if (0)
68 SetLastError(0xdeadbeef);
69 lstrcpyW( out, L"xxxxxx" );
70 FormatMessageW(FORMAT_MESSAGE_FROM_STRING, NULL, 0, 0, out, ARRAY_SIZE(out), NULL);
73 /* empty string */
74 SetLastError(0xdeadbeef);
75 lstrcpyW( out, L"xxxxxx" );
76 r = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, L"", 0, 0, out, ARRAY_SIZE(out), NULL);
77 error = GetLastError();
78 ok(!lstrcmpW(L"", out), "failed out=%s\n", wine_dbgstr_w(out));
79 ok(r==0, "succeeded: r=%d\n", r);
80 ok(error == ERROR_NO_WORK_DONE || broken(error == 0xdeadbeef), "last error %u\n", error);
82 /* format placeholder with no specifier */
83 SetLastError(0xdeadbeef);
84 lstrcpyW( out, L"xxxxxx" );
85 r = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, L"%", 0, 0, out, ARRAY_SIZE(out), NULL);
86 error = GetLastError();
87 ok(!lstrcmpW( out, L"xxxxxx" ),
88 "Expected the buffer to be unchanged\n");
89 ok(r==0, "succeeded: r=%d\n", r);
90 ok(error==ERROR_INVALID_PARAMETER, "last error %u\n", error);
92 /* test string with format placeholder with no specifier */
93 SetLastError(0xdeadbeef);
94 lstrcpyW( out, L"xxxxxx" );
95 r = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, L"test%", 0, 0, out, ARRAY_SIZE(out), NULL);
96 error = GetLastError();
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=%d\n", r);
100 ok(error==ERROR_INVALID_PARAMETER, "last error %u\n", error);
102 /* insertion with no variadic arguments */
103 SetLastError(0xdeadbeef);
104 lstrcpyW( out, L"xxxxxx" );
105 r = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, L"%1", 0, 0, out, ARRAY_SIZE(out), NULL);
106 error = GetLastError();
107 ok(!lstrcmpW( out, L"xxxxxx" ),
108 "Expected the buffer to be unchanged\n");
109 ok(r==0, "succeeded: r=%d\n", r);
110 ok(error==ERROR_INVALID_PARAMETER, "last error %u\n", error);
112 SetLastError(0xdeadbeef);
113 lstrcpyW( out, L"xxxxxx" );
114 r = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY, L"%1", 0,
115 0, out, ARRAY_SIZE(out), NULL);
116 error = GetLastError();
117 ok(!lstrcmpW( out, L"xxxxxx" ),
118 "Expected the buffer to be unchanged\n");
119 ok(r==0, "succeeded: r=%d\n", r);
120 ok(error==ERROR_INVALID_PARAMETER, "last error %u\n", error);
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=%d\n", r);
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=%d\n", r);
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=%d\n", r);
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=%d\n", r);
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=%d\n", r);
147 /* S is ascii */
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=%d\n", r);
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=%d\n", r);
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=%d\n", r);
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=%d\n", r);
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=%d\n", r);
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=%d\n", r);
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=%d\n", r);
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=%d\n", r);
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=%d\n", r);
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=%d\n", r);
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=%d\n", r);
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=%d\n", r);
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=%d\n", r);
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=%d\n", r);
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=%d\n", r);
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=%d\n", r);
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=%d\n", r);
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=%d\n", r);
237 /* %space */
238 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"% % ", 0, 0, out, ARRAY_SIZE(out));
239 ok(!lstrcmpW(L" ", out), "failed out=%s\n", wine_dbgstr_w(out));
240 ok(r==4,"failed: r=%d\n", r);
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=%d\n", r);
247 /* line feed */
248 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"hi\n", 0, 0, out, ARRAY_SIZE(out));
249 ok(!lstrcmpW(L"hi\r\n", out), "failed out=%s\n", wine_dbgstr_w(out));
250 ok(r==4,"failed: r=%d\n", r);
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=%d\n", r);
257 /* carriage return */
258 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"\r", 0, 0, out, ARRAY_SIZE(out));
259 ok(!lstrcmpW(L"\r\n", out), "failed out=%s\n", wine_dbgstr_w(out));
260 ok(r==2,"failed: r=%d\n", r);
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=%d\n", r);
267 /* null string as argument */
268 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1", 0, 0, out, ARRAY_SIZE(out), NULL);
269 ok(!lstrcmpW(L"(null)", out),"failed out=[%s]\n", wine_dbgstr_w(out));
270 ok(r==6,"failed: r=%d\n",r);
272 /* precision and width */
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=%d\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=%d\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=%d\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=%d\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=%d\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=%d\n",r);
296 /* args are not counted the same way with an argument array */
298 ULONG_PTR args[] = { 6, 4, 2, 5, 3, 1 };
299 r = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY, L"%1!*.*u!,%1!*.*u!",
300 0, 0, out, ARRAY_SIZE(out), (__ms_va_list *)args );
301 ok(!lstrcmpW(L" 0002, 00003", out),"failed out=[%s]\n", wine_dbgstr_w(out));
302 ok(r==13,"failed: r=%d\n",r);
303 r = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY, L"%1!*.*u!,%4!*.*u!",
304 0, 0, out, ARRAY_SIZE(out), (__ms_va_list *)args );
305 ok(!lstrcmpW(L" 0002, 001", out),"failed out=[%s]\n", wine_dbgstr_w(out));
306 ok(r==12,"failed: r=%d\n",r);
309 /* change of pace... test the low byte of dwflags */
311 /* line feed */
312 r = doitW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, L"hi\n", 0,
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=%d\n", r);
317 /* carriage return line feed */
318 r = doitW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, L"hi\r\n", 0,
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=%d\n", r);
323 /* carriage return */
324 r = doitW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, L"\r", 0,
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=%d\n", r);
329 /* double carriage return line feed */
330 r = doitW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, L"\r\r\n", 0,
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=%d\n", r);
336 static void test_message_from_string(void)
338 CHAR out[0x100] = {0};
339 DWORD r;
340 static const char init_buf[] = {'x', 'x', 'x', 'x', 'x', 'x'};
342 /* the basics */
343 r = FormatMessageA(FORMAT_MESSAGE_FROM_STRING, "test", 0, 0, out, ARRAY_SIZE(out),NULL);
344 ok(!strcmp("test", out),"failed out=[%s]\n",out);
345 ok(r==4,"failed: r=%d\n",r);
347 /* null string, crashes on Windows */
348 if (0)
350 SetLastError(0xdeadbeef);
351 memcpy(out, init_buf, sizeof(init_buf));
352 FormatMessageA(FORMAT_MESSAGE_FROM_STRING, NULL, 0, 0, out, ARRAY_SIZE(out), NULL);
355 /* empty string */
356 SetLastError(0xdeadbeef);
357 memcpy(out, init_buf, sizeof(init_buf));
358 r = FormatMessageA(FORMAT_MESSAGE_FROM_STRING, "", 0, 0, out, ARRAY_SIZE(out), NULL);
359 ok(!memcmp(out, init_buf, sizeof(init_buf)), "Expected the buffer to be untouched\n");
360 ok(r==0, "succeeded: r=%d\n", r);
361 ok(GetLastError() == ERROR_NO_WORK_DONE || broken(GetLastError() == 0xdeadbeef),
362 "last error %u\n", GetLastError());
364 /* format placeholder with no specifier */
365 SetLastError(0xdeadbeef);
366 memcpy(out, init_buf, sizeof(init_buf));
367 r = FormatMessageA(FORMAT_MESSAGE_FROM_STRING, "%", 0, 0, out, ARRAY_SIZE(out), NULL);
368 ok(!memcmp(out, init_buf, sizeof(init_buf)),
369 "Expected the buffer to be untouched\n");
370 ok(r==0, "succeeded: r=%d\n", r);
371 ok(GetLastError()==ERROR_INVALID_PARAMETER,
372 "last error %u\n", GetLastError());
374 /* test string with format placeholder with no specifier */
375 SetLastError(0xdeadbeef);
376 memcpy(out, init_buf, sizeof(init_buf));
377 r = FormatMessageA(FORMAT_MESSAGE_FROM_STRING, "test%", 0, 0, out, ARRAY_SIZE(out), NULL);
378 ok(!memcmp(out, init_buf, sizeof(init_buf)),
379 "Expected the buffer to be untouched\n");
380 ok(r==0, "succeeded: r=%d\n", r);
381 ok(GetLastError()==ERROR_INVALID_PARAMETER,
382 "last error %u\n", GetLastError());
384 /* insertion with no variadic arguments */
385 SetLastError(0xdeadbeef);
386 memcpy(out, init_buf, sizeof(init_buf));
387 r = FormatMessageA(FORMAT_MESSAGE_FROM_STRING, "%1", 0, 0, out, ARRAY_SIZE(out), NULL);
388 ok(!memcmp(out, init_buf, sizeof(init_buf)), "Expected the buffer to be untouched\n");
389 ok(r==0, "succeeded: r=%d\n", r);
390 ok(GetLastError()==ERROR_INVALID_PARAMETER, "last error %u\n", GetLastError());
392 SetLastError(0xdeadbeef);
393 memcpy(out, init_buf, sizeof(init_buf));
394 r = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY, "%1", 0,
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=%d\n", r);
398 ok(GetLastError()==ERROR_INVALID_PARAMETER, "last error %u\n", GetLastError());
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=%d\n",r);
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=%d\n",r);
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=%d\n",r);
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=%d\n",r);
420 /* s is ascii */
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=%d\n",r);
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=%d\n",r);
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=%d\n",r);
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=%d\n",r);
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=%d\n",r);
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=%d\n",r);
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=%d\n",r);
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=%d\n",r);
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=%d\n",r);
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=%d\n",r);
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=%d\n",r);
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=%d\n",r);
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=%d\n",r);
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=%d\n",r);
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=%d\n",r);
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=%d\n",r);
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=%d\n",r);
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=%d\n",r);
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=%d\n",r);
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=%d\n",r);
524 /* %space */
525 r = doit(FORMAT_MESSAGE_FROM_STRING, "% % ", 0, 0, out, ARRAY_SIZE(out));
526 ok(!strcmp(" ", out),"failed out=[%s]\n",out);
527 ok(r==4,"failed: r=%d\n",r);
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=%d\n",r);
534 /* line feed */
535 r = doit(FORMAT_MESSAGE_FROM_STRING, "hi\n", 0, 0, out, ARRAY_SIZE(out));
536 ok(!strcmp("hi\r\n", out),"failed out=[%s]\n",out);
537 ok(r==4,"failed: r=%d\n",r);
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=%d\n",r);
544 /* carriage return */
545 r = doit(FORMAT_MESSAGE_FROM_STRING, "\r", 0, 0, out, ARRAY_SIZE(out));
546 ok(!strcmp("\r\n", out),"failed out=[%s]\n",out);
547 ok(r==2,"failed: r=%d\n",r);
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=%d\n",r);
554 /* null string as argument */
555 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1", 0, 0, out, ARRAY_SIZE(out), NULL);
556 ok(!strcmp("(null)", out),"failed out=[%s]\n",out);
557 ok(r==6,"failed: r=%d\n",r);
559 /* precision and width */
561 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!3s!",
562 0, 0, out, sizeof(out), "t" );
563 ok(!strcmp(" t", out),"failed out=[%s]\n",out);
564 ok(r==3, "failed: r=%d\n",r);
565 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!*s!",
566 0, 0, out, sizeof(out), 4, "t");
567 if (!strcmp("*s",out)) win_skip( "width/precision not supported\n" );
568 else
570 ok(!strcmp( " t", out),"failed out=[%s]\n",out);
571 ok(r==4,"failed: r=%d\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=%d\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=%d\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=%d\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=%d\n",r);
592 /* args are not counted the same way with an argument array */
594 ULONG_PTR args[] = { 6, 4, 2, 5, 3, 1 };
595 r = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY,
596 "%1!*.*u!,%1!*.*u!", 0, 0, out, sizeof(out), (__ms_va_list *)args );
597 ok(!strcmp(" 0002, 00003", out),"failed out=[%s]\n",out);
598 ok(r==13,"failed: r=%d\n",r);
599 r = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY,
600 "%1!*.*u!,%4!*.*u!", 0, 0, out, sizeof(out), (__ms_va_list *)args );
601 ok(!strcmp(" 0002, 001", out),"failed out=[%s]\n",out);
602 ok(r==12,"failed: r=%d\n",r);
606 /* change of pace... test the low byte of dwflags */
608 /* line feed */
609 r = doit(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, "hi\n", 0,
610 0, out, ARRAY_SIZE(out));
611 ok(!strcmp("hi ", out), "failed out=[%s]\n",out);
612 ok(r==3, "failed: r=%d\n",r);
614 /* carriage return line feed */
615 r = doit(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, "hi\r\n", 0,
616 0, out, ARRAY_SIZE(out));
617 ok(!strcmp("hi ", out),"failed out=[%s]\n",out);
618 ok(r==3,"failed: r=%d\n",r);
620 /* carriage return */
621 r = doit(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, "\r", 0,
622 0, out, ARRAY_SIZE(out));
623 ok(!strcmp(" ", out),"failed out=[%s]\n",out);
624 ok(r==1,"failed: r=%d\n",r);
626 /* double carriage return line feed */
627 r = doit(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK, "\r\r\n", 0,
628 0, out, ARRAY_SIZE(out));
629 ok(!strcmp(" ", out),"failed out=[%s]\n",out);
630 ok(r==2,"failed: r=%d\n",r);
633 static void test_message_ignore_inserts(void)
635 static const char init_buf[] = {'x', 'x', 'x', 'x', 'x'};
637 DWORD ret;
638 CHAR out[256];
640 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, "test", 0, 0, out,
641 ARRAY_SIZE(out), NULL);
642 ok(ret == 4, "Expected FormatMessageA to return 4, got %d\n", ret);
643 ok(!strcmp("test", out), "Expected output string \"test\", got %s\n", out);
645 /* The %0 escape sequence is handled. */
646 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, "test%0", 0, 0, out,
647 ARRAY_SIZE(out), NULL);
648 ok(ret == 4, "Expected FormatMessageA to return 4, got %d\n", ret);
649 ok(!strcmp("test", out), "Expected output string \"test\", got %s\n", out);
651 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, "test%0test", 0, 0, out,
652 ARRAY_SIZE(out), NULL);
653 ok(ret == 4, "Expected FormatMessageA to return 4, got %d\n", ret);
654 ok(!strcmp("test", out), "Expected output string \"test\", got %s\n", out);
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));
659 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, "%0test", 0, 0, out,
660 ARRAY_SIZE(out), NULL);
661 ok(ret == 0, "Expected FormatMessageA to return 0, got %d\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 %u\n", GetLastError());
666 /* Insert sequences are ignored. */
667 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, "test%1%2!*.*s!%99", 0, 0, out,
668 ARRAY_SIZE(out), NULL);
669 ok(ret == 17, "Expected FormatMessageA to return 17, got %d\n", ret);
670 ok(!strcmp("test%1%2!*.*s!%99", out), "Expected output string \"test%%1%%2!*.*s!%%99\", got %s\n", out);
672 /* Only the "%n", "%r", and "%t" escape sequences are processed. */
673 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, "%%% %.%!", 0, 0, out,
674 ARRAY_SIZE(out), NULL);
675 ok(ret == 8, "Expected FormatMessageA to return 8, got %d\n", ret);
676 ok(!strcmp("%%% %.%!", out), "Expected output string \"%%%%%% %%.%%!\", got %s\n", out);
678 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, "%n%r%t", 0, 0, out,
679 ARRAY_SIZE(out), NULL);
680 ok(ret == 4, "Expected FormatMessageA to return 4, got %d\n", ret);
681 ok(!strcmp("\r\n\r\t", out), "Expected output string \"\\r\\n\\r\\t\", got %s\n", out);
683 /* CRLF characters are processed normally. */
684 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, "hi\n", 0, 0, out,
685 ARRAY_SIZE(out), NULL);
686 ok(ret == 4, "Expected FormatMessageA to return 4, got %d\n", ret);
687 ok(!strcmp("hi\r\n", out), "Expected output string \"hi\\r\\n\", got %s\n", out);
689 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, "hi\r\n", 0, 0, out,
690 ARRAY_SIZE(out), NULL);
691 ok(ret == 4, "Expected FormatMessageA to return 4, got %d\n", ret);
692 ok(!strcmp("hi\r\n", out), "Expected output string \"hi\\r\\n\", got %s\n", out);
694 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, "\r", 0, 0, out,
695 ARRAY_SIZE(out), NULL);
696 ok(ret == 2, "Expected FormatMessageA to return 2, got %d\n", ret);
697 ok(!strcmp("\r\n", out), "Expected output string \"\\r\\n\", got %s\n", out);
699 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, "\r\r\n", 0, 0, out,
700 ARRAY_SIZE(out), NULL);
701 ok(ret == 4, "Expected FormatMessageA to return 4, got %d\n", ret);
702 ok(!strcmp("\r\n\r\n", out), "Expected output string \"\\r\\n\\r\\n\", got %s\n", out);
704 /* The width parameter is handled the same also. */
705 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS |
706 FORMAT_MESSAGE_MAX_WIDTH_MASK, "hi\n", 0, 0, out, ARRAY_SIZE(out), NULL);
707 ok(!strcmp("hi ", out), "Expected output string \"hi \", got %s\n", out);
708 ok(ret == 3, "Expected FormatMessageA to return 3, got %d\n", ret);
710 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS |
711 FORMAT_MESSAGE_MAX_WIDTH_MASK, "hi\r\n", 0, 0, out, ARRAY_SIZE(out), NULL);
712 ok(ret == 3, "Expected FormatMessageA to return 3, got %d\n", ret);
713 ok(!strcmp("hi ", out), "Expected output string \"hi \", got %s\n", out);
715 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS |
716 FORMAT_MESSAGE_MAX_WIDTH_MASK, "\r", 0, 0, out, ARRAY_SIZE(out), NULL);
717 ok(ret == 1, "Expected FormatMessageA to return 1, got %d\n", ret);
718 ok(!strcmp(" ", out), "Expected output string \" \", got %s\n", out);
720 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS |
721 FORMAT_MESSAGE_MAX_WIDTH_MASK, "\r\r\n", 0, 0, out, ARRAY_SIZE(out), NULL);
722 ok(ret == 2, "Expected FormatMessageA to return 2, got %d\n", ret);
723 ok(!strcmp(" ", out), "Expected output string \" \", got %s\n", out);
726 static void test_message_ignore_inserts_wide(void)
728 DWORD ret;
729 WCHAR out[256];
731 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, L"test", 0, 0, out,
732 ARRAY_SIZE(out), NULL);
733 ok(ret == 4, "Expected FormatMessageW to return 4, got %d\n", ret);
734 ok(!lstrcmpW(L"test", out), "Expected output string \"test\", got %s\n", wine_dbgstr_w(out));
736 /* The %0 escape sequence is handled. */
737 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, L"test%0", 0, 0, out,
738 ARRAY_SIZE(out), NULL);
739 ok(ret == 4, "Expected FormatMessageW to return 4, got %d\n", ret);
740 ok(!lstrcmpW(L"test", out), "Expected output string \"test\", got %s\n", wine_dbgstr_w(out));
742 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, L"test%0test", 0, 0, out,
743 ARRAY_SIZE(out), NULL);
744 ok(ret == 4, "Expected FormatMessageW to return 4, got %d\n", ret);
745 ok(!lstrcmpW(L"test", out), "Expected output string \"test\", got %s\n", wine_dbgstr_w(out));
747 /* While FormatMessageA returns 0 in this case, no last error code is set. */
748 SetLastError(0xdeadbeef);
749 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, L"%0test", 0, 0, out,
750 ARRAY_SIZE(out), NULL);
751 ok(ret == 0, "Expected FormatMessageW to return 0, got %d\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 %u\n", GetLastError());
756 /* Insert sequences are ignored. */
757 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, L"test%1%2!*.*s!%99", 0, 0, out,
758 ARRAY_SIZE(out), NULL);
759 ok(ret == 17, "Expected FormatMessageW to return 17, got %d\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));
762 /* Only the "%n", "%r", and "%t" escape sequences are processed. */
763 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, L"%%% %.%!", 0, 0, out,
764 ARRAY_SIZE(out), NULL);
765 ok(ret == 8, "Expected FormatMessageW to return 8, got %d\n", ret);
766 ok(!lstrcmpW(L"%%% %.%!", out), "Expected output string \"%%%%%% %%.%%!\", got %s\n", wine_dbgstr_w(out));
768 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, L"%n%r%t", 0, 0, out,
769 ARRAY_SIZE(out), NULL);
770 ok(ret == 4, "Expected FormatMessageW to return 4, got %d\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));
773 /* CRLF characters are processed normally. */
774 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, L"hi\n", 0, 0, out,
775 ARRAY_SIZE(out), NULL);
776 ok(ret == 4, "Expected FormatMessageW to return 4, got %d\n", ret);
777 ok(!lstrcmpW(L"hi\r\n", out), "Expected output string \"hi\\r\\n\", got %s\n", wine_dbgstr_w(out));
779 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, L"hi\r\n", 0, 0, out,
780 ARRAY_SIZE(out), NULL);
781 ok(ret == 4, "Expected FormatMessageW to return 4, got %d\n", ret);
782 ok(!lstrcmpW(L"hi\r\n", out), "Expected output string \"hi\\r\\n\", got %s\n", wine_dbgstr_w(out));
784 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, L"\r", 0, 0, out,
785 ARRAY_SIZE(out), NULL);
786 ok(ret == 2, "Expected FormatMessageW to return 2, got %d\n", ret);
787 ok(!lstrcmpW(L"\r\n", out), "Expected output string \"\\r\\n\", got %s\n", wine_dbgstr_w(out));
789 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, L"\r\r\n", 0, 0, out,
790 ARRAY_SIZE(out), NULL);
791 ok(ret == 4, "Expected FormatMessageW to return 4, got %d\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));
794 /* The width parameter is handled the same also. */
795 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS |
796 FORMAT_MESSAGE_MAX_WIDTH_MASK, L"hi\n", 0, 0, out,
797 ARRAY_SIZE(out), NULL);
798 ok(ret == 3, "Expected FormatMessageW to return 3, got %d\n", ret);
799 ok(!lstrcmpW(L"hi ", out), "Expected output string \"hi \", got %s\n", wine_dbgstr_w(out));
801 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS |
802 FORMAT_MESSAGE_MAX_WIDTH_MASK, L"hi\r\n", 0, 0, out,
803 ARRAY_SIZE(out), NULL);
804 ok(ret == 3, "Expected FormatMessageW to return 3, got %d\n", ret);
805 ok(!lstrcmpW(L"hi ", out), "Expected output string \"hi \", got %s\n", wine_dbgstr_w(out));
807 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS |
808 FORMAT_MESSAGE_MAX_WIDTH_MASK, L"\r", 0, 0, out, ARRAY_SIZE(out), NULL);
809 ok(ret == 1, "Expected FormatMessageW to return 1, got %d\n", ret);
810 ok(!lstrcmpW(L" ", out), "Expected output string \" \", got %s\n", wine_dbgstr_w(out));
812 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS |
813 FORMAT_MESSAGE_MAX_WIDTH_MASK, L"\r\r\n", 0, 0, out,
814 ARRAY_SIZE(out), NULL);
815 ok(ret == 2, "Expected FormatMessageW to return 2, got %d\n", ret);
816 ok(!lstrcmpW(L" ", out), "Expected output string \" \", got %s\n", wine_dbgstr_w(out));
819 static void test_message_wrap(void)
821 DWORD ret;
822 int i;
823 CHAR in[300], out[300], ref[300];
825 /* No need for wrapping */
826 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 20,
827 "short long line", 0, 0, out, sizeof(out), NULL);
828 ok(ret == 15, "Expected FormatMessageW to return 15, got %d\n", ret);
829 ok(!strcmp("short long line", out),"failed out=[%s]\n",out);
831 /* Wrap the last word */
832 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 11,
833 "short long line", 0, 0, out, sizeof(out), NULL);
834 ok(ret == 16, "Expected FormatMessageW to return 16, got %d\n", ret);
835 ok(!strcmp("short long\r\nline", out),"failed out=[%s]\n",out);
837 /* Wrap the very last word */
838 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 20,
839 "short long long line", 0, 0, out, sizeof(out), NULL);
840 ok(ret == 21, "Expected FormatMessageW to return 21, got %d\n", ret);
841 ok(!strcmp("short long long\r\nline", out),"failed out=[%s]\n",out);
843 /* Strictly less than 10 characters per line! */
844 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 10,
845 "short long line", 0, 0, out, sizeof(out), NULL);
846 ok(ret == 16, "Expected FormatMessageW to return 16, got %d\n", ret);
847 ok(!strcmp("short\r\nlong line", out),"failed out=[%s]\n",out);
849 /* Handling of duplicate spaces */
850 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 16,
851 "short long line", 0, 0, out, sizeof(out), NULL);
852 ok(ret == 16, "Expected FormatMessageW to return 16, got %d\n", ret);
853 ok(!strcmp("short long\r\nline", out),"failed out=[%s]\n",out);
855 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 16,
856 "short long wordlongerthanaline", 0, 0, out, sizeof(out), NULL);
857 ok(ret == 33, "Expected FormatMessageW to return 33, got %d\n", ret);
858 ok(!strcmp("short long\r\nwordlongerthanal\r\nine", out),"failed out=[%s]\n",out);
860 /* Breaking in the middle of spaces */
861 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 12,
862 "short long line", 0, 0, out, sizeof(out), NULL);
863 ok(ret == 18, "Expected FormatMessageW to return 18, got %d\n", ret);
864 ok(!strcmp("short long\r\n line", out),"failed out=[%s]\n",out);
866 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 12,
867 "short long wordlongerthanaline", 0, 0, out, sizeof(out), NULL);
868 ok(ret == 35, "Expected FormatMessageW to return 35, got %d\n", ret);
869 ok(!strcmp("short long\r\n\r\nwordlongerth\r\nanaline", out),"failed out=[%s]\n",out);
871 /* Handling of start-of-string spaces */
872 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 15,
873 " short line", 0, 0, out, sizeof(out), NULL);
874 ok(ret == 13, "Expected FormatMessageW to return 13, got %d\n", ret);
875 ok(!strcmp(" short line", out),"failed out=[%s]\n",out);
877 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 11,
878 " shortlong line", 0, 0, out, sizeof(out), NULL);
879 ok(ret == 17, "Expected FormatMessageW to return 17, got %d\n", ret);
880 ok(!strcmp("\r\nshortlong\r\nline", out),"failed out=[%s]\n",out);
882 /* Handling of start-of-line spaces */
883 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 11,
884 "l1%n shortlong line", 0, 0, out, sizeof(out), NULL);
885 ok(ret == 21, "Expected FormatMessageW to return 21, got %d\n", ret);
886 ok(!strcmp("l1\r\n\r\nshortlong\r\nline", out),"failed out=[%s]\n",out);
888 /* Pure space wrapping */
889 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 5,
890 " ", 0, 0, out, sizeof(out), NULL);
891 ok(ret == 7, "Expected FormatMessageW to return 7, got %d\n", ret);
892 ok(!strcmp("\r\n\r\n\r\n ", out),"failed out=[%s]\n",out);
894 /* Handling of trailing spaces */
895 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 5,
896 "l1 ", 0, 0, out, sizeof(out), NULL);
897 ok(ret == 10, "Expected FormatMessageW to return 10, got %d\n", ret);
898 ok(!strcmp("l1\r\n\r\n\r\n ", out),"failed out=[%s]\n",out);
900 /* Word that just fills the line */
901 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 8,
902 "shortlon", 0, 0, out, sizeof(out), NULL);
903 ok(ret == 10, "Expected FormatMessageW to return 10, got %d\n", ret);
904 ok(!strcmp("shortlon\r\n", out),"failed out=[%s]\n",out);
906 /* Word longer than the line */
907 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 8,
908 "shortlongline", 0, 0, out, sizeof(out), NULL);
909 ok(ret == 15, "Expected FormatMessageW to return 15, got %d\n", ret);
910 ok(!strcmp("shortlon\r\ngline", out),"failed out=[%s]\n",out);
912 /* Wrap the line multiple times */
913 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 7,
914 "short long line", 0, 0, out, sizeof(out), NULL);
915 ok(ret == 17, "Expected FormatMessageW to return 17, got %d\n", ret);
916 ok(!strcmp("short\r\nlong\r\nline", out),"failed out=[%s]\n",out);
918 /* '\n's in the source are ignored */
919 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 11,
920 "short\nlong line", 0, 0, out, sizeof(out), NULL);
921 ok(ret == 16, "Expected FormatMessageW to return 16, got %d\n", ret);
922 ok(!strcmp("short long\r\nline", out),"failed out=[%s]\n",out);
924 /* Wrap even before a '%n' */
925 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 8,
926 "shortlon%n", 0, 0, out, sizeof(out), NULL);
927 ok(ret == 12, "Expected FormatMessageW to return 12, got %d\n", ret);
928 ok(!strcmp("shortlon\r\n\r\n", out),"failed out=[%s]\n",out);
930 /* '%n's count as starting a new line and combine with line wrapping */
931 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 10,
932 "short%nlong line", 0, 0, out, sizeof(out), NULL);
933 ok(ret == 16, "Expected FormatMessageW to return 16, got %d\n", ret);
934 ok(!strcmp("short\r\nlong line", out),"failed out=[%s]\n",out);
936 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 8,
937 "short%nlong line", 0, 0, out, sizeof(out), NULL);
938 ok(ret == 17, "Expected FormatMessageW to return 17, got %d\n", ret);
939 ok(!strcmp("short\r\nlong\r\nline", out),"failed out=[%s]\n",out);
941 /* '%r's also count as starting a new line and all */
942 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 10,
943 "short%rlong line", 0, 0, out, sizeof(out), NULL);
944 ok(ret == 15, "Expected FormatMessageW to return 15, got %d\n", ret);
945 ok(!strcmp("short\rlong line", out),"failed out=[%s]\n",out);
947 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 8,
948 "short%rlong line", 0, 0, out, sizeof(out), NULL);
949 ok(ret == 16, "Expected FormatMessageW to return 16, got %d\n", ret);
950 ok(!strcmp("short\rlong\r\nline", out),"failed out=[%s]\n",out);
952 /* IGNORE_INSERTS does not prevent line wrapping or disable '%n' */
953 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS | 8,
954 "short%nlong line%1", 0, 0, out, sizeof(out), NULL);
955 ok(ret == 19, "Expected FormatMessageW to return 19, got %d\n", ret);
956 ok(!strcmp("short\r\nlong\r\nline%1", out),"failed out=[%s]\n",out);
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++)
963 strcat(in, "123456789 ");
964 strcat(ref, "123456789 ");
966 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_MAX_WIDTH_MASK,
967 in, 0, 0, out, sizeof(out), NULL);
968 ok(ret == 272, "Expected FormatMessageW to return 272, got %d\n", ret);
969 ok(!strcmp(ref, out),"failed out=[%s]\n",out);
971 /* Wrapping and non-space characters */
972 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 11,
973 "short long\tline", 0, 0, out, sizeof(out), NULL);
974 ok(ret == 16, "Expected FormatMessageW to return 16, got %d\n", ret);
975 ok(!strcmp("short\r\nlong\tline", out),"failed out=[%s]\n",out);
977 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 11,
978 "short long-line", 0, 0, out, sizeof(out), NULL);
979 ok(ret == 16, "Expected FormatMessageW to return 16, got %d\n", ret);
980 ok(!strcmp("short\r\nlong-line", out),"failed out=[%s]\n",out);
982 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 11,
983 "short long_line", 0, 0, out, sizeof(out), NULL);
984 ok(ret == 16, "Expected FormatMessageW to return 16, got %d\n", ret);
985 ok(!strcmp("short\r\nlong_line", out),"failed out=[%s]\n",out);
987 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 11,
988 "short long.line", 0, 0, out, sizeof(out), NULL);
989 ok(ret == 16, "Expected FormatMessageW to return 16, got %d\n", ret);
990 ok(!strcmp("short\r\nlong.line", out),"failed out=[%s]\n",out);
992 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 11,
993 "short long,line", 0, 0, out, sizeof(out), NULL);
994 ok(ret == 16, "Expected FormatMessageW to return 16, got %d\n", ret);
995 ok(!strcmp("short\r\nlong,line", out),"failed out=[%s]\n",out);
997 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 11,
998 "short long!line", 0, 0, out, sizeof(out), NULL);
999 ok(ret == 16, "Expected FormatMessageW to return 16, got %d\n", ret);
1000 ok(!strcmp("short\r\nlong!line", out),"failed out=[%s]\n",out);
1002 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | 11,
1003 "short long?line", 0, 0, out, sizeof(out), NULL);
1004 ok(ret == 16, "Expected FormatMessageW to return 16, got %d\n", ret);
1005 ok(!strcmp("short\r\nlong?line", out),"failed out=[%s]\n",out);
1008 static void test_message_insufficient_buffer(void)
1010 static const char init_buf[] = {'x', 'x', 'x', 'x', 'x'};
1011 static const char expected_buf[] = {'x', 'x', 'x', 'x', 'x'};
1012 DWORD ret, size, i;
1013 CHAR out[5];
1015 SetLastError(0xdeadbeef);
1016 memcpy(out, init_buf, sizeof(init_buf));
1017 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING, "test", 0, 0, out, 0, NULL);
1018 ok(ret == 0, "Expected FormatMessageA to return 0, got %u\n", ret);
1019 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1020 "Expected GetLastError() to return ERROR_INSUFFICIENT_BUFFER, got %u\n",
1021 GetLastError());
1022 ok(!memcmp(expected_buf, out, sizeof(expected_buf)),
1023 "Expected the buffer to be untouched\n");
1025 SetLastError(0xdeadbeef);
1026 memcpy(out, init_buf, sizeof(init_buf));
1027 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING, "test", 0, 0, out, 1, NULL);
1028 ok(ret == 0, "Expected FormatMessageA to return 0, got %u\n", ret);
1029 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1030 "Expected GetLastError() to return ERROR_INSUFFICIENT_BUFFER, got %u\n",
1031 GetLastError());
1032 ok(!memcmp(expected_buf, out, sizeof(expected_buf)),
1033 "Expected the buffer to be untouched\n");
1035 SetLastError(0xdeadbeef);
1036 memcpy(out, init_buf, sizeof(init_buf));
1037 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING, "test", 0, 0, out, ARRAY_SIZE(out) - 1, NULL);
1038 ok(ret == 0, "Expected FormatMessageA to return 0, got %u\n", ret);
1039 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1040 "Expected GetLastError() to return ERROR_INSUFFICIENT_BUFFER, got %u\n",
1041 GetLastError());
1042 ok(!memcmp(expected_buf, out, sizeof(expected_buf)),
1043 "Expected the buffer to be untouched\n");
1045 for (size = 32700; size < 32800; size++)
1047 char *tmp = HeapAlloc( GetProcessHeap(), 0, size );
1048 char *buf = HeapAlloc( GetProcessHeap(), 0, size );
1050 for (i = 0; i < size; i++) tmp[i] = 'A' + i % 26;
1051 tmp[size - 1] = 0;
1052 SetLastError( 0xdeadbeef );
1053 ret = FormatMessageA( FORMAT_MESSAGE_FROM_STRING, tmp, 0, 0, buf, size, NULL );
1054 if (size < 32768)
1056 ok( ret == size - 1, "%u: got %u\n", size, ret );
1057 ok( !strcmp( tmp, buf ), "wrong buffer\n" );
1059 else
1061 ok( ret == 0, "%u: got %u\n", size, ret );
1062 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1065 SetLastError( 0xdeadbeef );
1066 ret = doit( FORMAT_MESSAGE_FROM_STRING, "%1", 0, 0, buf, size, tmp );
1067 if (size < 32768)
1069 ok( ret == size - 1, "%u: got %u\n", size, ret );
1070 ok( !strcmp( tmp, buf ), "wrong buffer\n" );
1072 else
1074 ok( ret == 0, "%u: got %u\n", size, ret );
1075 ok( GetLastError() == ERROR_INVALID_PARAMETER || broken(GetLastError() == ERROR_MORE_DATA), /* winxp */
1076 "wrong error %u\n", GetLastError() );
1078 HeapFree( GetProcessHeap(), 0, buf );
1080 SetLastError( 0xdeadbeef );
1081 ret = FormatMessageA( FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1082 tmp, 0, 0, (char *)&buf, size, NULL );
1083 if (size < 32768)
1085 ok( ret == size - 1, "%u: got %u\n", size, ret );
1086 ok( !strcmp( tmp, buf ), "wrong buffer\n" );
1087 HeapFree( GetProcessHeap(), 0, buf );
1089 else
1091 ok( ret == 0, "%u: got %u\n", size, ret );
1092 ok( GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %u\n", GetLastError() );
1094 HeapFree( GetProcessHeap(), 0, tmp );
1098 static void test_message_insufficient_buffer_wide(void)
1100 DWORD ret, size, i;
1101 WCHAR out[8];
1103 SetLastError(0xdeadbeef);
1104 lstrcpyW( out, L"xxxxxx" );
1105 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, L"test", 0, 0, out, 0, NULL);
1106 ok(ret == 0, "Expected FormatMessageA to return 0, got %u\n", ret);
1107 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1108 "Expected GetLastError() to return ERROR_INSUFFICIENT_BUFFER, got %u\n",
1109 GetLastError());
1110 ok(!lstrcmpW( out, L"xxxxxx" ),
1111 "Expected the buffer to be untouched\n");
1113 SetLastError(0xdeadbeef);
1114 lstrcpyW( out, L"xxxxxx" );
1115 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, L"test", 0, 0, out, 1, NULL);
1116 ok(ret == 0, "Expected FormatMessageA to return 0, got %u\n", ret);
1117 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1118 "Expected GetLastError() to return ERROR_INSUFFICIENT_BUFFER, got %u\n",
1119 GetLastError());
1120 ok(!memcmp(out, L"\0xxxxx", 6 * sizeof(WCHAR)) ||
1121 broken(!lstrcmpW( out, L"xxxxxx" )), /* winxp */
1122 "Expected the buffer to be truncated\n");
1124 SetLastError(0xdeadbeef);
1125 lstrcpyW( out, L"xxxxxx" );
1126 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, L"test", 0, 0, out, 4, NULL);
1127 ok(ret == 0, "Expected FormatMessageA to return 0, got %u\n", ret);
1128 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1129 "Expected GetLastError() to return ERROR_INSUFFICIENT_BUFFER, got %u\n",
1130 GetLastError());
1131 ok(!memcmp(out, L"tes\0xx", 6 * sizeof(WCHAR)) ||
1132 broken(!lstrcmpW( out, L"xxxxxx" )), /* winxp */
1133 "Expected the buffer to be truncated\n");
1135 for (size = 1000; size < 1000000; size += size / 10)
1137 WCHAR *tmp = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) );
1138 WCHAR *buf = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) );
1139 for (i = 0; i < size; i++) tmp[i] = 'A' + i % 26;
1140 tmp[size - 1] = 0;
1141 ret = FormatMessageW( FORMAT_MESSAGE_FROM_STRING, tmp, 0, 0, buf, size, NULL );
1142 ok(ret == size - 1 || broken(!ret), /* winxp */ "got %u\n", ret);
1143 if (!ret) break;
1144 ok( !lstrcmpW( tmp, buf ), "wrong buffer\n" );
1145 ret = doitW( FORMAT_MESSAGE_FROM_STRING, L"%1", 0, 0, buf, size, tmp );
1146 ok(ret == size - 1, "got %u\n", ret);
1147 ok( !lstrcmpW( tmp, buf ), "wrong buffer\n" );
1148 HeapFree( GetProcessHeap(), 0, buf );
1149 ret = FormatMessageW( FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1150 tmp, 0, 0, (WCHAR *)&buf, size, NULL );
1151 ok(ret == size - 1, "got %u\n", ret);
1152 ok( !lstrcmpW( tmp, buf ), "wrong buffer\n" );
1153 HeapFree( GetProcessHeap(), 0, tmp );
1154 HeapFree( GetProcessHeap(), 0, buf );
1158 static void test_message_null_buffer(void)
1160 DWORD ret, error;
1162 /* Without FORMAT_MESSAGE_ALLOCATE_BUFFER, only the specified buffer size is checked. */
1163 SetLastError(0xdeadbeef);
1164 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, 0, 0, NULL, 0, NULL);
1165 error = GetLastError();
1166 ok(!ret, "FormatMessageA returned %u\n", ret);
1167 ok(error == ERROR_INSUFFICIENT_BUFFER, "last error %u\n", error);
1169 SetLastError(0xdeadbeef);
1170 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, 0, 0, NULL, 1, NULL);
1171 error = GetLastError();
1172 ok(!ret, "FormatMessageA returned %u\n", ret);
1173 ok(error == ERROR_INSUFFICIENT_BUFFER, "last error %u\n", error);
1175 if (0) /* crashes on Windows */
1177 SetLastError(0xdeadbeef);
1178 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, 0, 0, NULL, 256, NULL);
1181 SetLastError(0xdeadbeef);
1182 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, 0, 0, NULL, 0, NULL);
1183 error = GetLastError();
1184 ok(!ret, "FormatMessageA returned %u\n", ret);
1185 ok(error == ERROR_NOT_ENOUGH_MEMORY, "last error %u\n", error);
1187 SetLastError(0xdeadbeef);
1188 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, 0, 0, NULL, 1, NULL);
1189 error = GetLastError();
1190 ok(!ret, "FormatMessageA returned %u\n", ret);
1191 ok(error == ERROR_NOT_ENOUGH_MEMORY, "last error %u\n", error);
1193 SetLastError(0xdeadbeef);
1194 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, 0, 0, NULL, 256, NULL);
1195 error = GetLastError();
1196 ok(!ret, "FormatMessageA returned %u\n", ret);
1197 ok(error == ERROR_NOT_ENOUGH_MEMORY, "last error %u\n", error);
1200 static void test_message_null_buffer_wide(void)
1202 DWORD ret, error;
1204 SetLastError(0xdeadbeef);
1205 ret = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, NULL, 0, 0, NULL, 0, NULL);
1206 error = GetLastError();
1207 ok(!ret, "FormatMessageW returned %u\n", ret);
1208 ok(error == ERROR_INVALID_PARAMETER, "last error %u\n", error);
1210 SetLastError(0xdeadbeef);
1211 ret = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, NULL, 0, 0, NULL, 1, NULL);
1212 error = GetLastError();
1213 ok(!ret, "FormatMessageW returned %u\n", ret);
1214 ok(error == ERROR_INVALID_PARAMETER, "last error %u\n", error);
1216 SetLastError(0xdeadbeef);
1217 ret = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, NULL, 0, 0, NULL, 256, NULL);
1218 error = GetLastError();
1219 ok(!ret, "FormatMessageW returned %u\n", ret);
1220 ok(error == ERROR_INVALID_PARAMETER, "last error %u\n", error);
1222 SetLastError(0xdeadbeef);
1223 ret = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, 0, 0, NULL, 0, NULL);
1224 error = GetLastError();
1225 ok(!ret, "FormatMessageW returned %u\n", ret);
1226 ok(error == ERROR_INVALID_PARAMETER, "last error %u\n", error);
1228 SetLastError(0xdeadbeef);
1229 ret = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, 0, 0, NULL, 1, NULL);
1230 error = GetLastError();
1231 ok(!ret, "FormatMessageW returned %u\n", ret);
1232 ok(error == ERROR_INVALID_PARAMETER, "last error %u\n", error);
1234 SetLastError(0xdeadbeef);
1235 ret = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, 0, 0, NULL, 256, NULL);
1236 error = GetLastError();
1237 ok(!ret, "FormatMessageW returned %u\n", ret);
1238 ok(error == ERROR_INVALID_PARAMETER, "last error %u\n", error);
1241 static void test_message_allocate_buffer(void)
1243 DWORD ret;
1244 char *buf;
1246 /* While MSDN suggests that FormatMessageA allocates a buffer whose size is
1247 * the larger of the output string and the requested buffer size, the tests
1248 * will not try to determine the actual size of the buffer allocated, as
1249 * the return value of LocalSize cannot be trusted for the purpose, and it should
1250 * in any case be safe for FormatMessageA to allocate in the manner that
1251 * MSDN suggests. */
1253 SetLastError(0xdeadbeef);
1254 buf = (char *)0xdeadbeef;
1255 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1256 "", 0, 0, (char *)&buf, 0, NULL);
1257 ok(ret == 0, "Expected FormatMessageA to return 0, got %u\n", ret);
1258 ok(buf == NULL, "Expected output buffer pointer to be NULL\n");
1259 ok(GetLastError() == ERROR_NO_WORK_DONE || broken(GetLastError() == 0xdeadbeef),
1260 "Expected GetLastError() to return ERROR_NO_WORK_DONE, got %u\n", GetLastError());
1262 buf = (char *)0xdeadbeef;
1263 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1264 "test", 0, 0, (char *)&buf, 0, NULL);
1265 ok(ret == 4, "Expected FormatMessageA to return 4, got %u\n", ret);
1266 ok(buf != NULL && buf != (char *)0xdeadbeef,
1267 "Expected output buffer pointer to be valid\n");
1268 if (buf != NULL && buf != (char *)0xdeadbeef)
1270 ok(!strcmp("test", buf),
1271 "Expected buffer to contain \"test\", got %s\n", buf);
1272 LocalFree(buf);
1275 buf = (char *)0xdeadbeef;
1276 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1277 "test", 0, 0, (char *)&buf, strlen("test"), NULL);
1278 ok(ret == 4, "Expected FormatMessageA to return 4, got %u\n", ret);
1279 ok(buf != NULL && buf != (char *)0xdeadbeef,
1280 "Expected output buffer pointer to be valid\n");
1281 if (buf != NULL && buf != (char *)0xdeadbeef)
1283 ok(!strcmp("test", buf),
1284 "Expected buffer to contain \"test\", got %s\n", buf);
1285 LocalFree(buf);
1288 buf = (char *)0xdeadbeef;
1289 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1290 "test", 0, 0, (char *)&buf, strlen("test") + 1, NULL);
1291 ok(ret == 4, "Expected FormatMessageA to return 4, got %u\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)
1296 ok(!strcmp("test", buf),
1297 "Expected buffer to contain \"test\", got %s\n", buf);
1298 LocalFree(buf);
1301 buf = (char *)0xdeadbeef;
1302 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1303 "test", 0, 0, (char *)&buf, strlen("test") + 2, NULL);
1304 ok(ret == 4, "Expected FormatMessageA to return 4, got %u\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)
1309 ok(!strcmp("test", buf),
1310 "Expected buffer to contain \"test\", got %s\n", buf);
1311 LocalFree(buf);
1314 buf = (char *)0xdeadbeef;
1315 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1316 "test", 0, 0, (char *)&buf, 1024, NULL);
1317 ok(ret == 4, "Expected FormatMessageA to return 4, got %u\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)
1322 ok(!strcmp("test", buf),
1323 "Expected buffer to contain \"test\", got %s\n", buf);
1324 LocalFree(buf);
1328 static void test_message_allocate_buffer_wide(void)
1330 DWORD ret;
1331 WCHAR *buf;
1333 /* While MSDN suggests that FormatMessageW allocates a buffer whose size is
1334 * the larger of the output string and the requested buffer size, the tests
1335 * will not try to determine the actual size of the buffer allocated, as
1336 * the return value of LocalSize cannot be trusted for the purpose, and it should
1337 * in any case be safe for FormatMessageW to allocate in the manner that
1338 * MSDN suggests. */
1340 if (0) /* crashes on Windows */
1342 buf = (WCHAR *)0xdeadbeef;
1343 FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1344 NULL, 0, 0, (WCHAR *)&buf, 0, NULL);
1347 SetLastError(0xdeadbeef);
1348 buf = (WCHAR *)0xdeadbeef;
1349 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1350 L"", 0, 0, (WCHAR *)&buf, 0, NULL);
1351 ok(ret == 0, "Expected FormatMessageW to return 0, got %u\n", ret);
1352 ok(buf == NULL, "Expected output buffer pointer to be NULL\n");
1353 ok(GetLastError() == ERROR_NO_WORK_DONE || broken(GetLastError() == 0xdeadbeef),
1354 "Expected GetLastError() to return ERROR_NO_WORK_DONE, got %u\n", GetLastError());
1356 SetLastError(0xdeadbeef);
1357 buf = (WCHAR *)0xdeadbeef;
1358 ret = doitW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER, L"%1",
1359 0, 0, (WCHAR *)&buf, 0, L"" );
1360 ok(ret == 0, "Expected FormatMessageW to return 0, got %u\n", ret);
1361 ok(buf == NULL, "Expected output buffer pointer to be NULL\n");
1362 ok(GetLastError() == ERROR_NO_WORK_DONE || broken(GetLastError() == 0xdeadbeef),
1363 "Expected GetLastError() to return ERROR_NO_WORK_DONE, got %u\n", GetLastError());
1365 buf = (WCHAR *)0xdeadbeef;
1366 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1367 L"test", 0, 0, (WCHAR *)&buf, 0, NULL);
1368 ok(ret == 4, "Expected FormatMessageA to return 4, got %u\n", ret);
1369 ok(buf != NULL && buf != (WCHAR *)0xdeadbeef,
1370 "Expected output buffer pointer to be valid\n");
1371 if (buf != NULL && buf != (WCHAR *)0xdeadbeef)
1373 ok(!lstrcmpW(L"test", buf),
1374 "Expected buffer to contain \"test\", got %s\n", wine_dbgstr_w(buf));
1375 LocalFree(buf);
1378 buf = (WCHAR *)0xdeadbeef;
1379 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1380 L"test", 0, 0, (WCHAR *)&buf, 4, NULL);
1381 ok(ret == 4, "Expected FormatMessageA to return 4, got %u\n", ret);
1382 ok(buf != NULL && buf != (WCHAR *)0xdeadbeef,
1383 "Expected output buffer pointer to be valid\n");
1384 if (buf != NULL && buf != (WCHAR *)0xdeadbeef)
1386 ok(!lstrcmpW(L"test", buf),
1387 "Expected buffer to contain \"test\", got %s\n", wine_dbgstr_w(buf));
1388 LocalFree(buf);
1391 buf = (WCHAR *)0xdeadbeef;
1392 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1393 L"test", 0, 0, (WCHAR *)&buf, 5, NULL);
1394 ok(ret == 4, "Expected FormatMessageA to return 4, got %u\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)
1399 ok(!lstrcmpW(L"test", buf),
1400 "Expected buffer to contain \"test\", got %s\n", wine_dbgstr_w(buf));
1401 LocalFree(buf);
1404 buf = (WCHAR *)0xdeadbeef;
1405 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1406 L"test", 0, 0, (WCHAR *)&buf, 6, NULL);
1407 ok(ret == 4, "Expected FormatMessageA to return 4, got %u\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)
1412 ok(!lstrcmpW(L"test", buf),
1413 "Expected buffer to contain \"test\", got %s\n", wine_dbgstr_w(buf));
1414 LocalFree(buf);
1417 buf = (WCHAR *)0xdeadbeef;
1418 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1419 L"test", 0, 0, (WCHAR *)&buf, 1024, NULL);
1420 ok(ret == 4, "Expected FormatMessageA to return 4, got %u\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)
1425 ok(!lstrcmpW(L"test", buf),
1426 "Expected buffer to contain \"test\", got %s\n", wine_dbgstr_w(buf));
1427 LocalFree(buf);
1431 static void test_message_from_hmodule(void)
1433 DWORD ret, error;
1434 HMODULE h;
1435 CHAR out[0x100] = {0};
1437 h = GetModuleHandleA("kernel32.dll");
1438 ok(h != 0, "GetModuleHandle failed\n");
1440 /*Test existing messageID; as the message strings from wine's kernel32 differ from windows' kernel32 we don't compare
1441 the strings but only test that FormatMessage doesn't return 0*/
1442 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, h, 7/*=ERROR_ARENA_TRASHED*/,
1443 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), out, ARRAY_SIZE(out), NULL);
1444 ok(ret != 0, "FormatMessageA returned 0\n");
1446 /* Test HRESULT. It's not documented but in practice _com_error::ErrorMessage relies on this. */
1447 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, h, 0x80070005 /* E_ACCESSDENIED */,
1448 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), out, ARRAY_SIZE(out), NULL);
1449 ok(ret != 0, "FormatMessageA returned 0\n");
1451 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, h, TRUST_E_NOSIGNATURE,
1452 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), out, ARRAY_SIZE(out), NULL);
1453 ok(ret != 0, "FormatMessageA returned 0\n");
1455 /* Test a message string with an insertion without passing any variadic arguments. */
1456 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, h, 193 /* ERROR_BAD_EXE_FORMAT */,
1457 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), out, ARRAY_SIZE(out), NULL);
1458 ok(ret == 0, "FormatMessageA returned non-zero\n");
1460 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE |
1461 FORMAT_MESSAGE_ARGUMENT_ARRAY, h, 193 /* ERROR_BAD_EXE_FORMAT */,
1462 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), out, ARRAY_SIZE(out), NULL);
1463 ok(ret == 0, "FormatMessageA returned non-zero\n");
1465 /*Test nonexistent messageID with varying language IDs Note: FormatMessageW behaves the same*/
1466 SetLastError(0xdeadbeef);
1467 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, h, 3044,
1468 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), out, ARRAY_SIZE(out), NULL);
1469 error = GetLastError();
1470 ok(ret == 0, "FormatMessageA returned %u instead of 0\n", ret);
1471 ok(error == ERROR_MR_MID_NOT_FOUND ||
1472 error == ERROR_MUI_FILE_NOT_FOUND ||
1473 error == ERROR_RESOURCE_TYPE_NOT_FOUND, "Unexpected last error %u.\n", error);
1475 SetLastError(0xdeadbeef);
1476 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, h, 3044,
1477 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), out, ARRAY_SIZE(out), NULL);
1478 error = GetLastError();
1479 ok(ret == 0, "FormatMessageA returned %u instead of 0\n", ret);
1480 ok(error == ERROR_MR_MID_NOT_FOUND ||
1481 error == ERROR_MUI_FILE_NOT_LOADED ||
1482 error == ERROR_RESOURCE_TYPE_NOT_FOUND, "Unexpected last error %u.\n", error);
1484 SetLastError(0xdeadbeef);
1485 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, h, 3044,
1486 MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT), out, ARRAY_SIZE(out), NULL);
1487 error = GetLastError();
1488 ok(ret == 0, "FormatMessageA returned %u instead of 0\n", ret);
1489 ok(error == ERROR_MR_MID_NOT_FOUND ||
1490 error == ERROR_MUI_FILE_NOT_LOADED ||
1491 error == ERROR_RESOURCE_TYPE_NOT_FOUND, "Unexpected last error %u.\n", error);
1493 SetLastError(0xdeadbeef);
1494 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, h, 3044,
1495 MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), out, ARRAY_SIZE(out), NULL);
1496 error = GetLastError();
1497 ok(ret == 0, "FormatMessageA returned %u instead of 0\n", ret);
1498 ok(error == ERROR_RESOURCE_LANG_NOT_FOUND ||
1499 error == ERROR_RESOURCE_TYPE_NOT_FOUND ||
1500 error == ERROR_MR_MID_NOT_FOUND ||
1501 error == ERROR_MUI_FILE_NOT_FOUND ||
1502 error == ERROR_MUI_FILE_NOT_LOADED,
1503 "last error %u\n", error);
1505 SetLastError(0xdeadbeef);
1506 ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, h, 3044,
1507 MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_UK), out, ARRAY_SIZE(out), NULL);
1508 error = GetLastError();
1509 ok(ret == 0, "FormatMessageA returned %u instead of 0\n", ret);
1510 ok(error == ERROR_RESOURCE_LANG_NOT_FOUND ||
1511 error == ERROR_RESOURCE_TYPE_NOT_FOUND ||
1512 error == ERROR_MR_MID_NOT_FOUND ||
1513 error == ERROR_MUI_FILE_NOT_FOUND ||
1514 error == ERROR_MUI_FILE_NOT_LOADED,
1515 "last error %u\n", error);
1518 static void test_message_invalid_flags(void)
1520 static const char init_buf[] = {'x', 'x', 'x', 'x', 'x'};
1522 DWORD ret;
1523 CHAR out[5];
1524 char *ptr;
1526 SetLastError(0xdeadbeef);
1527 memcpy(out, init_buf, sizeof(init_buf));
1528 ret = FormatMessageA(0, "test", 0, 0, out, ARRAY_SIZE(out), NULL);
1529 ok(ret == 0, "Expected FormatMessageA to return 0, got %u\n", ret);
1530 ok(!memcmp(out, init_buf, sizeof(init_buf)),
1531 "Expected the output buffer to be untouched\n");
1532 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1533 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %u\n",
1534 GetLastError());
1536 SetLastError(0xdeadbeef);
1537 ptr = (char *)0xdeadbeef;
1538 ret = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER, "test", 0, 0, (char *)&ptr, 0, NULL);
1539 ok(ret == 0, "Expected FormatMessageA to return 0, got %u\n", ret);
1540 ok(ptr == NULL, "Expected output pointer to be initialized to NULL, got %p\n", ptr);
1541 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1542 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %u\n",
1543 GetLastError());
1545 SetLastError(0xdeadbeef);
1546 memcpy(out, init_buf, sizeof(init_buf));
1547 ret = FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS, "test", 0, 0, out, ARRAY_SIZE(out), NULL);
1548 ok(ret == 0, "Expected FormatMessageA to return 0, got %u\n", ret);
1549 ok(!memcmp(out, init_buf, sizeof(init_buf)),
1550 "Expected the output buffer to be untouched\n");
1551 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1552 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %u\n",
1553 GetLastError());
1555 SetLastError(0xdeadbeef);
1556 memcpy(out, init_buf, sizeof(init_buf));
1557 ret = FormatMessageA(FORMAT_MESSAGE_ARGUMENT_ARRAY, "test", 0, 0, out, ARRAY_SIZE(out), NULL);
1558 ok(ret == 0, "Expected FormatMessageA to return 0, got %u\n", ret);
1559 ok(!memcmp(out, init_buf, sizeof(init_buf)),
1560 "Expected the output buffer to be untouched\n");
1561 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1562 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %u\n",
1563 GetLastError());
1565 SetLastError(0xdeadbeef);
1566 memcpy(out, init_buf, sizeof(init_buf));
1567 ret = FormatMessageA(FORMAT_MESSAGE_MAX_WIDTH_MASK, "test", 0, 0, out, ARRAY_SIZE(out), NULL);
1568 ok(ret == 0, "Expected FormatMessageA to return 0, got %u\n", ret);
1569 ok(!memcmp(out, init_buf, sizeof(init_buf)),
1570 "Expected the output buffer to be untouched\n");
1571 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1572 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %u\n",
1573 GetLastError());
1575 /* Simultaneously setting FORMAT_MESSAGE_FROM_STRING with other source
1576 * flags is apparently permissible, and FORMAT_MESSAGE_FROM_STRING takes
1577 * precedence in this case. */
1579 memcpy(out, init_buf, sizeof(init_buf));
1580 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_FROM_SYSTEM,
1581 "test", 0, 0, out, ARRAY_SIZE(out), NULL);
1582 ok(ret == 4, "Expected FormatMessageA to return 4, got %u\n", ret);
1583 ok(!strcmp("test", out),
1584 "Expected the output buffer to be untouched\n");
1586 memcpy(out, init_buf, sizeof(init_buf));
1587 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_FROM_HMODULE,
1588 "test", 0, 0, out, ARRAY_SIZE(out), NULL);
1589 ok(ret == 4, "Expected FormatMessageA to return 4, got %u\n", ret);
1590 ok(!strcmp("test", out),
1591 "Expected the output buffer to be untouched\n");
1593 memcpy(out, init_buf, sizeof(init_buf));
1594 ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_FROM_HMODULE |
1595 FORMAT_MESSAGE_FROM_SYSTEM, "test", 0, 0, out, ARRAY_SIZE(out), NULL);
1596 ok(ret == 4, "Expected FormatMessageA to return 4, got %u\n", ret);
1597 ok(!strcmp("test", out),
1598 "Expected the output buffer to be untouched\n");
1601 static void test_message_invalid_flags_wide(void)
1603 DWORD ret;
1604 WCHAR out[8];
1605 WCHAR *ptr;
1607 SetLastError(0xdeadbeef);
1608 lstrcpyW( out, L"xxxxxx" );
1609 ret = FormatMessageW(0, L"test", 0, 0, out, ARRAY_SIZE(out), NULL);
1610 ok(ret == 0, "Expected FormatMessageW to return 0, got %u\n", ret);
1611 ok(!lstrcmpW( out, L"xxxxxx" ),
1612 "Expected the output buffer to be untouched\n");
1613 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1614 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %u\n",
1615 GetLastError());
1617 SetLastError(0xdeadbeef);
1618 ptr = (WCHAR *)0xdeadbeef;
1619 ret = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER, L"test", 0, 0, (WCHAR *)&ptr, 0, NULL);
1620 ok(ret == 0, "Expected FormatMessageW to return 0, got %u\n", ret);
1621 ok(ptr == NULL, "Expected output pointer to be initialized to NULL, got %p\n", ptr);
1622 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1623 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %u\n",
1624 GetLastError());
1626 SetLastError(0xdeadbeef);
1627 lstrcpyW( out, L"xxxxxx" );
1628 ret = FormatMessageW(FORMAT_MESSAGE_IGNORE_INSERTS, L"test", 0, 0, out, ARRAY_SIZE(out), NULL);
1629 ok(ret == 0, "Expected FormatMessageW to return 0, got %u\n", ret);
1630 ok(!lstrcmpW( out, L"xxxxxx" ),
1631 "Expected the output buffer to be untouched\n");
1632 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1633 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %u\n",
1634 GetLastError());
1636 SetLastError(0xdeadbeef);
1637 lstrcpyW( out, L"xxxxxx" );
1638 ret = FormatMessageW(FORMAT_MESSAGE_ARGUMENT_ARRAY, L"test", 0, 0, out, ARRAY_SIZE(out), NULL);
1639 ok(ret == 0, "Expected FormatMessageW to return 0, got %u\n", ret);
1640 ok(!lstrcmpW( out, L"xxxxxx" ),
1641 "Expected the output buffer to be untouched\n");
1642 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1643 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %u\n",
1644 GetLastError());
1646 SetLastError(0xdeadbeef);
1647 lstrcpyW( out, L"xxxxxx" );
1648 ret = FormatMessageW(FORMAT_MESSAGE_MAX_WIDTH_MASK, L"test", 0, 0, out, ARRAY_SIZE(out), NULL);
1649 ok(ret == 0, "Expected FormatMessageW to return 0, got %u\n", ret);
1650 ok(!lstrcmpW( out, L"xxxxxx" ),
1651 "Expected the output buffer to be untouched\n");
1652 ok(GetLastError() == ERROR_INVALID_PARAMETER,
1653 "Expected GetLastError() to return ERROR_INVALID_PARAMETER, got %u\n",
1654 GetLastError());
1656 /* Simultaneously setting FORMAT_MESSAGE_FROM_STRING with other source
1657 * flags is apparently permissible, and FORMAT_MESSAGE_FROM_STRING takes
1658 * precedence in this case. */
1660 lstrcpyW( out, L"xxxxxx" );
1661 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_FROM_SYSTEM,
1662 L"test", 0, 0, out, ARRAY_SIZE(out), NULL);
1663 ok(ret == 4, "Expected FormatMessageW to return 4, got %u\n", ret);
1664 ok(!lstrcmpW(L"test", out),
1665 "Expected the output buffer to be untouched\n");
1667 lstrcpyW( out, L"xxxxxx" );
1668 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_FROM_HMODULE,
1669 L"test", 0, 0, out, ARRAY_SIZE(out), NULL);
1670 ok(ret == 4, "Expected FormatMessageW to return 4, got %u\n", ret);
1671 ok(!lstrcmpW(L"test", out),
1672 "Expected the output buffer to be untouched\n");
1674 lstrcpyW( out, L"xxxxxx" );
1675 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_FROM_HMODULE |
1676 FORMAT_MESSAGE_FROM_SYSTEM, L"test", 0, 0, out, ARRAY_SIZE(out), NULL);
1677 ok(ret == 4, "Expected FormatMessageW to return 4, got %u\n", ret);
1678 ok(!lstrcmpW(L"test", out),
1679 "Expected the output buffer to be untouched\n");
1682 static void test_message_from_64bit_number(void)
1684 WCHAR outW[0x100], expW[0x100];
1685 char outA[0x100];
1686 DWORD r;
1687 static const struct
1689 UINT64 number;
1690 const char expected[32];
1691 int len;
1692 } unsigned_tests[] =
1694 { 0, "0", 1 },
1695 { 1234567890, "1234567890", 10},
1696 { ULL(0xFFFFFFFF,0xFFFFFFFF), "18446744073709551615", 20 },
1697 { ULL(0x7FFFFFFF,0xFFFFFFFF), "9223372036854775807", 19 },
1699 static const struct
1701 INT64 number;
1702 const char expected[32];
1703 int len;
1704 } signed_tests[] =
1706 { 0, "0" , 1},
1707 { 1234567890, "1234567890", 10 },
1708 { -1, "-1", 2},
1709 { ULL(0xFFFFFFFF,0xFFFFFFFF), "-1", 2},
1710 { ULL(0x7FFFFFFF,0xFFFFFFFF), "9223372036854775807", 19 },
1711 { -ULL(0x7FFFFFFF,0xFFFFFFFF), "-9223372036854775807", 20},
1713 int i;
1715 for (i = 0; i < ARRAY_SIZE(unsigned_tests); i++)
1717 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1!I64u!", 0, 0, outW, ARRAY_SIZE(outW),
1718 unsigned_tests[i].number);
1719 MultiByteToWideChar(CP_ACP, 0, unsigned_tests[i].expected, -1, expW, ARRAY_SIZE(expW));
1720 ok(!lstrcmpW(outW, expW),"[%d] failed, expected %s, got %s\n", i,
1721 unsigned_tests[i].expected, wine_dbgstr_w(outW));
1722 ok(r == unsigned_tests[i].len,"[%d] failed: r=%d\n", i, r);
1723 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!I64u!",
1724 0, 0, outA, sizeof(outA), unsigned_tests[i].number);
1725 ok(!strcmp(outA, unsigned_tests[i].expected),"[%d] failed, expected %s, got %s\n", i,
1726 unsigned_tests[i].expected, outA);
1727 ok(r == unsigned_tests[i].len,"[%d] failed: r=%d\n", i, r);
1730 for (i = 0; i < ARRAY_SIZE(signed_tests); i++)
1732 r = doitW(FORMAT_MESSAGE_FROM_STRING, L"%1!I64d!", 0, 0, outW, ARRAY_SIZE(outW),
1733 signed_tests[i].number);
1734 MultiByteToWideChar(CP_ACP, 0, signed_tests[i].expected, -1, expW, ARRAY_SIZE(expW));
1735 ok(!lstrcmpW(outW, expW),"[%d] failed, expected %s, got %s\n", i,
1736 signed_tests[i].expected, wine_dbgstr_w(outW));
1737 ok(r == signed_tests[i].len,"[%d] failed: r=%d\n", i, r);
1738 r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!I64d!",
1739 0, 0, outA, sizeof(outA), signed_tests[i].number);
1740 ok(!strcmp(outA, signed_tests[i].expected),"[%d] failed, expected %s, got %s\n", i,
1741 signed_tests[i].expected, outA);
1742 ok(r == signed_tests[i].len,"[%d] failed: r=%d\n", i, r);
1746 static void test_message_system_errors(void)
1748 static const struct
1750 DWORD error_code;
1751 BOOL broken;
1753 tests[] =
1755 {E_NOTIMPL},
1756 {E_FAIL},
1757 {DXGI_ERROR_INVALID_CALL, TRUE /* Available since Win8 */},
1758 {DXGI_ERROR_NOT_CURRENTLY_AVAILABLE, TRUE /* Available since Win8 */},
1761 char buffer[256];
1762 unsigned int i;
1763 DWORD len;
1765 for (i = 0; i < ARRAY_SIZE(tests); ++i)
1767 len = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, tests[i].error_code,
1768 LANG_USER_DEFAULT, buffer, ARRAY_SIZE(buffer), NULL);
1769 ok(len || broken(tests[i].broken), "Got zero len, code %#x.\n", tests[i].error_code);
1773 START_TEST(format_msg)
1775 DWORD ret;
1777 test_message_from_string();
1778 test_message_ignore_inserts();
1779 test_message_wrap();
1780 test_message_insufficient_buffer();
1781 test_message_null_buffer();
1782 test_message_allocate_buffer();
1783 test_message_from_hmodule();
1784 test_message_invalid_flags();
1786 SetLastError(0xdeadbeef);
1787 ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, NULL, 0, 0, NULL, 0, NULL);
1788 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1790 win_skip("FormatMessageW is not implemented\n");
1791 return;
1794 test_message_from_string_wide();
1795 test_message_ignore_inserts_wide();
1796 test_message_insufficient_buffer_wide();
1797 test_message_null_buffer_wide();
1798 test_message_allocate_buffer_wide();
1799 test_message_invalid_flags_wide();
1800 test_message_from_64bit_number();
1801 test_message_system_errors();