3 * reactos/lib/kernel32/misc/errormsg.c
4 * Wine calls this file now as kernel/format_msg.c
8 * FormatMessage implementation
10 * Copyright 1996 Marcus Meissner
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include "wine/unicode.h"
37 static const WCHAR PCNTFMTWSTR
[] = { '%','%','%','s',0 };
38 static const WCHAR FMTWSTR
[] = { '%','s',0 };
39 static const WCHAR kernel32W
[] = {'k','e','r','n','e','l','3','2',0};
42 /* DO NOT USE IT!! it will go away soon */
44 __inline
static LPSTR
HEAP_strdupWtoA( HANDLE heap
, DWORD flags
, LPCWSTR str
)
49 if (!str
) return NULL
;
50 len
= WideCharToMultiByte( CP_ACP
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
51 ret
= RtlAllocateHeap(RtlGetProcessHeap(), flags
, len
);
52 if(ret
) WideCharToMultiByte( CP_ACP
, 0, str
, -1, ret
, len
, NULL
, NULL
);
56 /* Messages...used by FormatMessage32* (KERNEL32.something)
58 * They can be specified either directly or using a message ID and
59 * loading them from the resource.
61 * The resourcedata has following format:
63 * 0: DWORD nrofentries
64 * nrofentries * subentry:
67 * 8: DWORD offset from start to the stringentries
69 * (lastentry-firstentry) * stringentry:
70 * 0: WORD len (0 marks end) [ includes the 4 byte header length ]
73 * (stringentry i of a subentry refers to the ID 'firstentry+i')
75 * Yes, ANSI strings in win32 resources. Go figure.
78 /**********************************************************************
79 * load_messageA (internal)
82 static LPSTR
load_messageA( HMODULE module
, UINT id
, WORD lang
)
84 PRTL_MESSAGE_RESOURCE_ENTRY mre
;
88 TRACE("module = %p, id = %08x\n", module
, id
);
90 if (!module
) module
= GetModuleHandleW( NULL
);
91 Status
= RtlFindMessage( module
, (ULONG_PTR
) RT_MESSAGETABLE
, lang
, id
, &mre
);
92 if (!NT_SUCCESS(Status
))
95 if (mre
->Flags
& MESSAGE_RESOURCE_UNICODE
)
97 int len
= WideCharToMultiByte( CP_ACP
, 0, (const WCHAR
*)mre
->Text
, -1, NULL
, 0, NULL
, NULL
);
98 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
))) return NULL
;
99 WideCharToMultiByte( CP_ACP
, 0, (const WCHAR
*)mre
->Text
, -1, buffer
, len
, NULL
, NULL
);
103 int len
= strlen((const char*)mre
->Text
) + 1;
104 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
))) return NULL
;
105 memcpy( buffer
, mre
->Text
, len
);
107 //TRACE("returning %s\n", wine_dbgstr_a(buffer));
113 static LPWSTR
load_messageW( HMODULE module
, UINT id
, WORD lang
)
115 PRTL_MESSAGE_RESOURCE_ENTRY mre
;
119 TRACE("module = %p, id = %08x\n", module
, id
);
121 if (!module
) module
= GetModuleHandleW( NULL
);
122 Status
= RtlFindMessage( module
, (ULONG_PTR
) RT_MESSAGETABLE
, lang
, id
, &mre
);
123 if (!NT_SUCCESS(Status
))
126 if (mre
->Flags
& MESSAGE_RESOURCE_UNICODE
)
128 int len
= (strlenW( (const WCHAR
*)mre
->Text
) + 1) * sizeof(WCHAR
);
129 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
))) return NULL
;
130 memcpy( buffer
, mre
->Text
, len
);
134 int len
= MultiByteToWideChar( CP_ACP
, 0, (const char *)mre
->Text
, -1, NULL
, 0 );
135 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) ))) return NULL
;
136 MultiByteToWideChar( CP_ACP
, 0, (const char*)mre
->Text
, -1, buffer
, len
);
138 //TRACE("returning %s\n", wine_dbgstr_w(buffer));
142 /***********************************************************************
143 * FormatMessageA (KERNEL32.@)
144 * FIXME: missing wrap,
148 DWORD WINAPI
FormatMessageA(
157 LPDWORD args
=(LPDWORD
)_args
;
158 HMODULE kernel32_handle
= GetModuleHandleW(kernel32W
);
160 #if defined(__i386__) || defined(__sparc__)
161 /* This implementation is completely dependent on the format of the va_list on x86 CPUs */
165 DWORD width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
169 TRACE("(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
170 dwFlags
,lpSource
,dwMessageId
,dwLanguageId
,lpBuffer
,nSize
,args
);
171 if ((dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
172 &&((dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
)
173 || (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
))) return 0;
175 if (width
&& width
!= FORMAT_MESSAGE_MAX_WIDTH_MASK
)
176 FIXME("line wrapping (%lu) not supported.\n", width
);
178 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
180 from
= HeapAlloc( GetProcessHeap(), 0, strlen((LPCSTR
)lpSource
)+1 );
185 strcpy( from
, (LPCSTR
)lpSource
);
189 if (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
)
190 from
= load_messageA( (HMODULE
)lpSource
, dwMessageId
, (WORD
)dwLanguageId
);
191 if (!from
&& (dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
))
192 from
= load_messageA( kernel32_handle
, dwMessageId
, (WORD
)dwLanguageId
);
196 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND
);
200 target
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 100);
203 HeapFree(GetProcessHeap(),0,from
);
204 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
210 #define ADD_TO_T(c) do { \
212 if (t-target == talloced) {\
213 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
214 t = target+talloced;\
221 if (dwFlags
& FORMAT_MESSAGE_IGNORE_INSERTS
) {
229 char *fmtstr
,*x
,*lastf
;
240 case '1':case '2':case '3':case '4':case '5':
241 case '6':case '7':case '8':case '9':
244 case '0':case '1':case '2':case '3':
245 case '4':case '5':case '6':case '7':
248 insertnr
=insertnr
*10+*f
-'0';
257 if (NULL
!=(x
=strchr(f
,'!'))) {
259 fmtstr
=HeapAlloc(GetProcessHeap(),0,strlen(f
)+2);
262 HeapFree(GetProcessHeap(),0,from
);
263 HeapFree(GetProcessHeap(),0,target
);
264 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
267 sprintf(fmtstr
,"%%%s",f
);
270 fmtstr
=HeapAlloc(GetProcessHeap(),0,strlen(f
)+2);
273 HeapFree(GetProcessHeap(),0,from
);
274 HeapFree(GetProcessHeap(),0,target
);
275 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
278 sprintf(fmtstr
,"%%%s",f
);
279 f
+=strlen(f
); /*at \0*/
283 fmtstr
= HeapAlloc(GetProcessHeap(),0,3);
286 HeapFree(GetProcessHeap(),0,from
);
287 HeapFree(GetProcessHeap(),0,target
);
288 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
291 strcpy( fmtstr
, "%s" );
297 if (dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)
298 argliststart
=args
+insertnr
-1;
300 argliststart
=(*(DWORD
**)args
)+insertnr
-1;
306 HeapFree(GetProcessHeap(), 0, b
);
309 b
= HeapAlloc(GetProcessHeap(), 0, sz
);
310 /* CMF - This makes a BIG assumption about va_list */
311 } while (0 > _vsnprintf(b
, sz
, fmtstr
, (va_list) argliststart
));
316 HeapFree(GetProcessHeap(),0,b
);
318 /* NULL args - copy formatstr
321 while ((lastf
<f
)&&(*lastf
)) {
325 HeapFree(GetProcessHeap(),0,fmtstr
);
372 talloced
= strlen(target
)+1;
373 if (nSize
&& talloced
<nSize
) {
374 target
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,target
,nSize
);
376 //TRACE("-- %s\n",debugstr_a(target));
377 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) {
378 *((LPVOID
*)lpBuffer
) = (LPVOID
)LocalAlloc(LMEM_ZEROINIT
,max(nSize
, talloced
));
379 memcpy(*(LPSTR
*)lpBuffer
,target
,talloced
);
381 lstrcpynA(lpBuffer
,target
,nSize
);
383 HeapFree(GetProcessHeap(),0,target
);
384 HeapFree(GetProcessHeap(),0,from
);
385 TRACE("-- returning %d\n", (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ? strlen(*(LPSTR
*)lpBuffer
):strlen(lpBuffer
));
386 return (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ?
387 strlen(*(LPSTR
*)lpBuffer
):
391 #endif /* __i386__ */
395 /***********************************************************************
396 * FormatMessageW (KERNEL32.@)
400 DWORD WINAPI
FormatMessageW(
409 HMODULE kernel32_handle
= GetModuleHandleW(kernel32W
);
410 LPDWORD args
=(LPDWORD
)_args
;
411 #if defined(__i386__) || defined(__sparc__)
412 /* This implementation is completely dependent on the format of the va_list on x86 CPUs */
416 DWORD width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
420 TRACE("(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
421 dwFlags
,lpSource
,dwMessageId
,dwLanguageId
,lpBuffer
,nSize
,args
);
422 if ((dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
423 &&((dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
)
424 || (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
))) return 0;
426 if (width
&& width
!= FORMAT_MESSAGE_MAX_WIDTH_MASK
)
427 FIXME("line wrapping not supported.\n");
429 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
) {
430 from
= HeapAlloc( GetProcessHeap(), 0, (strlenW((LPCWSTR
)lpSource
) + 1) *
434 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
437 strcpyW( from
, (LPCWSTR
)lpSource
);
441 if (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
)
442 from
= load_messageW( (HMODULE
)lpSource
, dwMessageId
, (WORD
)dwLanguageId
);
443 if (!from
&& (dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
))
444 from
= load_messageW( kernel32_handle
, dwMessageId
,(WORD
)dwLanguageId
);
448 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND
);
453 target
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 100 * sizeof(WCHAR
) );
456 HeapFree(GetProcessHeap(),0,from
);
457 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
463 #define ADD_TO_T(c) do {\
465 if (t-target == talloced) {\
466 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2*sizeof(WCHAR));\
467 t = target+talloced;\
474 if (dwFlags
& FORMAT_MESSAGE_IGNORE_INSERTS
) {
482 WCHAR
*fmtstr
,*sprintfbuf
,*x
,*lastf
;
494 case '1':case '2':case '3':case '4':case '5':
495 case '6':case '7':case '8':case '9':
498 case '0':case '1':case '2':case '3':
499 case '4':case '5':case '6':case '7':
502 insertnr
=insertnr
*10+*f
-'0';
511 if (NULL
!=(x
=strchrW(f
,'!'))) {
513 fmtstr
=HeapAlloc( GetProcessHeap(), 0,(strlenW(f
)+2)*sizeof(WCHAR
));
516 HeapFree(GetProcessHeap(),0,from
);
517 HeapFree(GetProcessHeap(),0,target
);
518 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
521 sprintfW(fmtstr
,PCNTFMTWSTR
,f
);
524 fmtstr
=HeapAlloc(GetProcessHeap(),0,(strlenW(f
)+2)*sizeof(WCHAR
));
527 HeapFree(GetProcessHeap(),0,from
);
528 HeapFree(GetProcessHeap(),0,target
);
529 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
532 sprintfW(fmtstr
,PCNTFMTWSTR
,f
);
533 f
+=strlenW(f
); /*at \0*/
537 fmtstr
= HeapAlloc( GetProcessHeap(),0,3*sizeof(WCHAR
));
540 HeapFree(GetProcessHeap(),0,from
);
541 HeapFree(GetProcessHeap(),0,target
);
542 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
545 strcpyW( fmtstr
, FMTWSTR
);
549 if (dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)
550 argliststart
=args
+insertnr
-1;
552 argliststart
=(*(DWORD
**)args
)+insertnr
-1;
558 HeapFree(GetProcessHeap(),0,sprintfbuf
);
561 sprintfbuf
=HeapAlloc(GetProcessHeap(),0,len
*sizeof(WCHAR
));
562 /* CMF - This makes a BIG assumption about va_list */
563 } while (0 > _vsnwprintf(sprintfbuf
, len
, fmtstr
, (va_list) argliststart
));
568 HeapFree(GetProcessHeap(),0,sprintfbuf
);
571 /* NULL args - copy formatstr
574 while ((lastf
<f
)&&(*lastf
)) {
579 HeapFree(GetProcessHeap(),0,fmtstr
);
626 talloced
= strlenW(target
)+1;
627 if (nSize
&& talloced
<nSize
)
628 target
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,target
,nSize
*sizeof(WCHAR
));
629 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) {
630 /* nSize is the MINIMUM size */
631 DWORD len
= strlenW(target
) + 1;
632 *((LPVOID
*)lpBuffer
) = (LPVOID
)LocalAlloc(LMEM_ZEROINIT
,len
*sizeof(WCHAR
));
633 strcpyW(*(LPWSTR
*)lpBuffer
, target
);
635 else lstrcpynW(lpBuffer
, target
, nSize
);
637 HeapFree(GetProcessHeap(),0,target
);
638 HeapFree(GetProcessHeap(),0,from
);
639 //TRACE("ret=%s\n", wine_dbgstr_w((dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
640 // *(LPWSTR*)lpBuffer : lpBuffer));
641 return (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ?
642 strlenW(*(LPWSTR
*)lpBuffer
):
646 #endif /* __i386__ */