2 * FormatMessage implementation
4 * Copyright 1996 Marcus Meissner
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 #include "wine/unicode.h"
36 #include "kernel_private.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(resource
);
42 /* Messages...used by FormatMessage32* (KERNEL32.something)
44 * They can be specified either directly or using a message ID and
45 * loading them from the resource.
47 * The resourcedata has following format:
49 * 0: DWORD nrofentries
50 * nrofentries * subentry:
53 * 8: DWORD offset from start to the stringentries
55 * (lastentry-firstentry) * stringentry:
56 * 0: WORD len (0 marks end) [ includes the 4 byte header length ]
59 * (stringentry i of a subentry refers to the ID 'firstentry+i')
61 * Yes, ANSI strings in win32 resources. Go figure.
64 const WCHAR PCNTFMTWSTR
[] = { '%','%','%','s',0 };
65 const WCHAR FMTWSTR
[] = { '%','s',0 };
67 /**********************************************************************
68 * load_messageW (internal)
70 static INT
load_messageW( HMODULE instance
, UINT id
, WORD lang
,
71 LPWSTR buffer
, INT buflen
)
73 const MESSAGE_RESOURCE_ENTRY
*mre
;
76 TRACE("instance = %08lx, id = %08lx, buffer = %p, length = %ld\n", (DWORD
)instance
, (DWORD
)id
, buffer
, (DWORD
)buflen
);
78 if (RtlFindMessage( instance
, RT_MESSAGETABLE
, lang
, id
, &mre
) != STATUS_SUCCESS
) return 0;
80 if (mre
->Flags
& MESSAGE_RESOURCE_UNICODE
)
81 slen
= mre
->Length
/ sizeof(WCHAR
);
85 TRACE(" - strlen=%d\n",slen
);
87 i
= min(buflen
- 1, slen
);
92 if (mre
->Flags
& MESSAGE_RESOURCE_UNICODE
)
93 lstrcpynW(buffer
, (LPWSTR
)mre
->Text
, i
);
95 MultiByteToWideChar( CP_ACP
, 0, mre
->Text
, -1, buffer
, i
);
104 TRACE("'%s' copied !\n", wine_dbgstr_w(buffer
));
109 /**********************************************************************
110 * load_messageA (internal)
112 static INT
load_messageA( HMODULE instance
, UINT id
, WORD lang
,
113 LPSTR buffer
, INT buflen
)
118 TRACE("instance = %08lx, id = %08lx, buffer = %p, length = %ld\n",
119 (DWORD
)instance
, (DWORD
)id
, buffer
, (DWORD
)buflen
);
122 return load_messageW(instance
, id
, lang
, NULL
, 0);
124 bufferW
= HeapAlloc(GetProcessHeap(), 0, buflen
* sizeof(WCHAR
));
128 ret
= load_messageW(instance
, id
, lang
, bufferW
, buflen
);
130 WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, buffer
, ret
, NULL
, NULL
);
137 /***********************************************************************
138 * FormatMessageA (KERNEL32.@)
139 * FIXME: missing wrap,
141 DWORD WINAPI
FormatMessageA(
150 LPDWORD args
=(LPDWORD
)_args
;
151 #if defined(__i386__) || defined(__sparc__)
152 /* This implementation is completely dependent on the format of the va_list on x86 CPUs */
156 DWORD width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
159 HMODULE hmodule
= (HMODULE
)lpSource
;
162 TRACE("(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
163 dwFlags
,lpSource
,dwMessageId
,dwLanguageId
,lpBuffer
,nSize
,args
);
164 if ((dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
165 &&((dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
)
166 || (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
))) return 0;
168 if (width
&& width
!= FORMAT_MESSAGE_MAX_WIDTH_MASK
)
169 FIXME("line wrapping (%lu) not supported.\n", width
);
171 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
173 from
= HeapAlloc( GetProcessHeap(), 0, strlen((LPSTR
)lpSource
)+1 );
174 strcpy( from
, (LPSTR
)lpSource
);
179 if (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
)
182 hmodule
= GetModuleHandleW(NULL
);
183 bufsize
=load_messageA(hmodule
,dwMessageId
,dwLanguageId
,NULL
,100);
185 if ((dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
) && (!bufsize
))
187 hmodule
= kernel32_handle
;
188 bufsize
=load_messageA(hmodule
,dwMessageId
,dwLanguageId
,NULL
,100);
192 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND
);
196 from
= HeapAlloc( GetProcessHeap(), 0, bufsize
+ 1 );
197 load_messageA(hmodule
,dwMessageId
,dwLanguageId
,from
,bufsize
+1);
199 target
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 100);
203 #define ADD_TO_T(c) do { \
205 if (t-target == talloced) {\
206 target = (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
207 t = target+talloced;\
214 if (dwFlags
& FORMAT_MESSAGE_IGNORE_INSERTS
) {
222 char *fmtstr
,*x
,*lastf
;
233 case '1':case '2':case '3':case '4':case '5':
234 case '6':case '7':case '8':case '9':
237 case '0':case '1':case '2':case '3':
238 case '4':case '5':case '6':case '7':
241 insertnr
=insertnr
*10+*f
-'0';
250 if (NULL
!=(x
=strchr(f
,'!'))) {
252 fmtstr
=HeapAlloc(GetProcessHeap(),0,strlen(f
)+2);
253 sprintf(fmtstr
,"%%%s",f
);
256 fmtstr
=HeapAlloc(GetProcessHeap(),0,strlen(f
)+2);
257 sprintf(fmtstr
,"%%%s",f
);
258 f
+=strlen(f
); /*at \0*/
262 fmtstr
= HeapAlloc(GetProcessHeap(),0,3);
263 strcpy( fmtstr
, "%s" );
269 if (dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)
270 argliststart
=args
+insertnr
-1;
272 argliststart
=(*(DWORD
**)args
)+insertnr
-1;
274 /* FIXME: precision and width components are not handled correctly */
275 if ( (strcmp(fmtstr
, "%ls") == 0) || (strcmp(fmtstr
,"%S") == 0) ) {
276 sz
= WideCharToMultiByte( CP_ACP
, 0, *(WCHAR
**)argliststart
, -1, NULL
, 0, NULL
, NULL
);
277 b
= HeapAlloc(GetProcessHeap(), 0, sz
);
278 WideCharToMultiByte( CP_ACP
, 0, *(WCHAR
**)argliststart
, -1, b
, sz
, NULL
, NULL
);
280 b
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sz
= 1000);
281 /* CMF - This makes a BIG assumption about va_list */
282 TRACE("A BIG assumption\n");
283 vsnprintf(b
, sz
, fmtstr
, (va_list) argliststart
);
285 for (x
=b
; *x
; x
++) ADD_TO_T(*x
);
287 HeapFree(GetProcessHeap(),0,b
);
289 /* NULL args - copy formatstr
292 while ((lastf
<f
)&&(*lastf
)) {
296 HeapFree(GetProcessHeap(),0,fmtstr
);
343 talloced
= strlen(target
)+1;
344 if (nSize
&& talloced
<nSize
) {
345 target
= (char*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,target
,nSize
);
347 TRACE("-- %s\n",debugstr_a(target
));
348 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) {
349 *((LPVOID
*)lpBuffer
) = (LPVOID
)LocalAlloc(GMEM_ZEROINIT
,max(nSize
, talloced
));
350 memcpy(*(LPSTR
*)lpBuffer
,target
,talloced
);
352 lstrcpynA(lpBuffer
,target
,nSize
);
354 HeapFree(GetProcessHeap(),0,target
);
355 if (from
) HeapFree(GetProcessHeap(),0,from
);
356 TRACE("-- returning %d\n", (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ? strlen(*(LPSTR
*)lpBuffer
):strlen(lpBuffer
));
357 return (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ?
358 strlen(*(LPSTR
*)lpBuffer
):
362 #endif /* __i386__ */
367 /***********************************************************************
368 * FormatMessageW (KERNEL32.@)
370 DWORD WINAPI
FormatMessageW(
379 LPDWORD args
=(LPDWORD
)_args
;
380 #if defined(__i386__) || defined(__sparc__)
381 /* This implementation is completely dependent on the format of the va_list on x86 CPUs */
385 DWORD width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
388 HMODULE hmodule
= (HMODULE
)lpSource
;
391 TRACE("(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
392 dwFlags
,lpSource
,dwMessageId
,dwLanguageId
,lpBuffer
,nSize
,args
);
393 if ((dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
394 &&((dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
)
395 || (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
))) return 0;
397 if (width
&& width
!= FORMAT_MESSAGE_MAX_WIDTH_MASK
)
398 FIXME("line wrapping not supported.\n");
400 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
) {
401 from
= HeapAlloc( GetProcessHeap(), 0, (strlenW((LPWSTR
)lpSource
) + 1) *
403 strcpyW( from
, (LPWSTR
)lpSource
);
408 if (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
)
411 hmodule
= GetModuleHandleW(NULL
);
412 bufsize
=load_messageW(hmodule
,dwMessageId
,dwLanguageId
,NULL
,100);
414 if ((dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
) && (!bufsize
))
416 hmodule
= kernel32_handle
;
417 bufsize
=load_messageW(hmodule
,dwMessageId
,dwLanguageId
,NULL
,100);
421 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND
);
425 from
= HeapAlloc( GetProcessHeap(), 0, (bufsize
+ 1) * sizeof(WCHAR
) );
426 load_messageW(hmodule
,dwMessageId
,dwLanguageId
,from
,bufsize
+1);
428 target
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 100 * sizeof(WCHAR
) );
432 #define ADD_TO_T(c) do {\
434 if (t-target == talloced) {\
435 target = (WCHAR*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2*sizeof(WCHAR));\
436 t = target+talloced;\
443 if (dwFlags
& FORMAT_MESSAGE_IGNORE_INSERTS
) {
451 WCHAR
*fmtstr
,*sprintfbuf
,*x
;
462 case '1':case '2':case '3':case '4':case '5':
463 case '6':case '7':case '8':case '9':
466 case '0':case '1':case '2':case '3':
467 case '4':case '5':case '6':case '7':
470 insertnr
=insertnr
*10+*f
-'0';
479 if (NULL
!=(x
=strchrW(f
,'!'))) {
481 fmtstr
=HeapAlloc( GetProcessHeap(), 0,(strlenW(f
)+2)*sizeof(WCHAR
));
482 sprintfW(fmtstr
,PCNTFMTWSTR
,f
);
485 fmtstr
=HeapAlloc(GetProcessHeap(),0,(strlenW(f
)+2)*sizeof(WCHAR
));
486 sprintfW(fmtstr
,PCNTFMTWSTR
,f
);
487 f
+=strlenW(f
); /*at \0*/
491 fmtstr
= HeapAlloc( GetProcessHeap(),0,3*sizeof(WCHAR
));
492 strcpyW( fmtstr
, FMTWSTR
);
494 if (dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)
495 argliststart
=args
+insertnr
-1;
497 argliststart
=(*(DWORD
**)args
)+insertnr
-1;
499 if (fmtstr
[strlenW(fmtstr
)-1]=='s' && argliststart
[0]) {
502 xarr
[0]=*(argliststart
+0);
503 /* possible invalid pointers */
504 xarr
[1]=*(argliststart
+1);
505 xarr
[2]=*(argliststart
+2);
506 sprintfbuf
=HeapAlloc(GetProcessHeap(),0,(strlenW((LPWSTR
)argliststart
[0])*2+1)*sizeof(WCHAR
));
508 /* CMF - This makes a BIG assumption about va_list */
509 vsprintfW(sprintfbuf
, fmtstr
, (va_list) xarr
);
511 sprintfbuf
=HeapAlloc(GetProcessHeap(),0,100);
513 /* CMF - This makes a BIG assumption about va_list */
514 vsprintfW(sprintfbuf
, fmtstr
, (va_list) argliststart
);
520 HeapFree(GetProcessHeap(),0,sprintfbuf
);
521 HeapFree(GetProcessHeap(),0,fmtstr
);
568 talloced
= strlenW(target
)+1;
569 if (nSize
&& talloced
<nSize
)
570 target
= (WCHAR
*)HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,target
,nSize
*sizeof(WCHAR
));
571 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) {
572 /* nSize is the MINIMUM size */
573 DWORD len
= strlenW(target
) + 1;
574 *((LPVOID
*)lpBuffer
) = (LPVOID
)LocalAlloc(GMEM_ZEROINIT
,len
*sizeof(WCHAR
));
575 strcpyW(*(LPWSTR
*)lpBuffer
, target
);
577 else lstrcpynW(lpBuffer
, target
, nSize
);
579 HeapFree(GetProcessHeap(),0,target
);
580 if (from
) HeapFree(GetProcessHeap(),0,from
);
581 TRACE("ret=%s\n", wine_dbgstr_w((dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ?
582 *(LPWSTR
*)lpBuffer
: lpBuffer
));
583 return (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ?
584 strlenW(*(LPWSTR
*)lpBuffer
):
588 #endif /* __i386__ */