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
) 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
) 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;
177 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
181 if (width
&& width
!= FORMAT_MESSAGE_MAX_WIDTH_MASK
)
182 FIXME("line wrapping (%lu) not supported.\n", width
);
184 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
186 from
= HeapAlloc( GetProcessHeap(), 0, strlen((LPCSTR
)lpSource
)+1 );
191 strcpy( from
, (LPCSTR
)lpSource
);
195 if (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
)
196 from
= load_messageA( (HMODULE
)lpSource
, dwMessageId
, (WORD
)dwLanguageId
);
197 if (!from
&& (dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
))
198 from
= load_messageA( kernel32_handle
, dwMessageId
, (WORD
)dwLanguageId
);
202 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND
);
206 target
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 100);
209 HeapFree(GetProcessHeap(),0,from
);
210 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
216 #define ADD_TO_T(c) do { \
218 if (t-target == talloced) {\
219 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
220 t = target+talloced;\
227 if (dwFlags
& FORMAT_MESSAGE_IGNORE_INSERTS
) {
235 char *fmtstr
,*x
,*lastf
;
246 case '1':case '2':case '3':case '4':case '5':
247 case '6':case '7':case '8':case '9':
250 case '0':case '1':case '2':case '3':
251 case '4':case '5':case '6':case '7':
254 insertnr
=insertnr
*10+*f
-'0';
263 if (NULL
!=(x
=strchr(f
,'!'))) {
265 fmtstr
=HeapAlloc(GetProcessHeap(),0,strlen(f
)+2);
268 HeapFree(GetProcessHeap(),0,from
);
269 HeapFree(GetProcessHeap(),0,target
);
270 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
273 sprintf(fmtstr
,"%%%s",f
);
276 fmtstr
=HeapAlloc(GetProcessHeap(),0,strlen(f
)+2);
279 HeapFree(GetProcessHeap(),0,from
);
280 HeapFree(GetProcessHeap(),0,target
);
281 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
284 sprintf(fmtstr
,"%%%s",f
);
285 f
+=strlen(f
); /*at \0*/
289 fmtstr
= HeapAlloc(GetProcessHeap(),0,3);
292 HeapFree(GetProcessHeap(),0,from
);
293 HeapFree(GetProcessHeap(),0,target
);
294 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
297 strcpy( fmtstr
, "%s" );
303 if (dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)
304 argliststart
=args
+insertnr
-1;
306 argliststart
=(*(DWORD
**)args
)+insertnr
-1;
312 HeapFree(GetProcessHeap(), 0, b
);
315 b
= HeapAlloc(GetProcessHeap(), 0, sz
);
316 /* CMF - This makes a BIG assumption about va_list */
317 } while (0 > _vsnprintf(b
, sz
, fmtstr
, (va_list) argliststart
));
322 HeapFree(GetProcessHeap(),0,b
);
324 /* NULL args - copy formatstr
327 while ((lastf
<f
)&&(*lastf
)) {
331 HeapFree(GetProcessHeap(),0,fmtstr
);
378 talloced
= strlen(target
)+1;
379 if (nSize
&& talloced
<nSize
) {
380 target
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,target
,nSize
);
382 //TRACE("-- %s\n",debugstr_a(target));
383 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) {
384 *((LPVOID
*)lpBuffer
) = (LPVOID
)LocalAlloc(LMEM_ZEROINIT
,max(nSize
, talloced
));
385 memcpy(*(LPSTR
*)lpBuffer
,target
,talloced
);
387 lstrcpynA(lpBuffer
,target
,nSize
);
389 HeapFree(GetProcessHeap(),0,target
);
390 HeapFree(GetProcessHeap(),0,from
);
391 TRACE("-- returning %d\n", (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ? strlen(*(LPSTR
*)lpBuffer
):strlen(lpBuffer
));
392 return (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ?
393 strlen(*(LPSTR
*)lpBuffer
):
397 #endif /* __i386__ */
401 /***********************************************************************
402 * FormatMessageW (KERNEL32.@)
406 DWORD WINAPI
FormatMessageW(
415 HMODULE kernel32_handle
= GetModuleHandleW(kernel32W
);
416 LPDWORD args
=(LPDWORD
)_args
;
417 #if defined(__i386__) || defined(__sparc__)
418 /* This implementation is completely dependent on the format of the va_list on x86 CPUs */
422 DWORD width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
426 TRACE("(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
427 dwFlags
,lpSource
,dwMessageId
,dwLanguageId
,lpBuffer
,nSize
,args
);
428 if ((dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
429 &&((dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
)
430 || (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
))) return 0;
434 SetLastError(ERROR_INVALID_PARAMETER
);
438 if (width
&& width
!= FORMAT_MESSAGE_MAX_WIDTH_MASK
)
439 FIXME("line wrapping not supported.\n");
441 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
) {
442 from
= HeapAlloc( GetProcessHeap(), 0, (strlenW((LPCWSTR
)lpSource
) + 1) *
446 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
449 strcpyW( from
, (LPCWSTR
)lpSource
);
453 if (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
)
454 from
= load_messageW( (HMODULE
)lpSource
, dwMessageId
, (WORD
)dwLanguageId
);
455 if (!from
&& (dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
))
456 from
= load_messageW( kernel32_handle
, dwMessageId
,(WORD
)dwLanguageId
);
460 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND
);
465 target
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 100 * sizeof(WCHAR
) );
468 HeapFree(GetProcessHeap(),0,from
);
469 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
475 #define ADD_TO_T(c) do {\
477 if (t-target == talloced) {\
478 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2*sizeof(WCHAR));\
479 t = target+talloced;\
486 if (dwFlags
& FORMAT_MESSAGE_IGNORE_INSERTS
) {
494 WCHAR
*fmtstr
,*sprintfbuf
,*x
,*lastf
;
506 case '1':case '2':case '3':case '4':case '5':
507 case '6':case '7':case '8':case '9':
510 case '0':case '1':case '2':case '3':
511 case '4':case '5':case '6':case '7':
514 insertnr
=insertnr
*10+*f
-'0';
523 if (NULL
!=(x
=strchrW(f
,'!'))) {
525 fmtstr
=HeapAlloc( GetProcessHeap(), 0,(strlenW(f
)+2)*sizeof(WCHAR
));
528 HeapFree(GetProcessHeap(),0,from
);
529 HeapFree(GetProcessHeap(),0,target
);
530 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
533 sprintfW(fmtstr
,PCNTFMTWSTR
,f
);
536 fmtstr
=HeapAlloc(GetProcessHeap(),0,(strlenW(f
)+2)*sizeof(WCHAR
));
539 HeapFree(GetProcessHeap(),0,from
);
540 HeapFree(GetProcessHeap(),0,target
);
541 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
544 sprintfW(fmtstr
,PCNTFMTWSTR
,f
);
545 f
+=strlenW(f
); /*at \0*/
549 fmtstr
= HeapAlloc( GetProcessHeap(),0,3*sizeof(WCHAR
));
552 HeapFree(GetProcessHeap(),0,from
);
553 HeapFree(GetProcessHeap(),0,target
);
554 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
557 strcpyW( fmtstr
, FMTWSTR
);
561 if (dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)
562 argliststart
=args
+insertnr
-1;
564 argliststart
=(*(DWORD
**)args
)+insertnr
-1;
570 HeapFree(GetProcessHeap(),0,sprintfbuf
);
573 sprintfbuf
=HeapAlloc(GetProcessHeap(),0,len
*sizeof(WCHAR
));
574 /* CMF - This makes a BIG assumption about va_list */
575 } while (0 > _vsnwprintf(sprintfbuf
, len
, fmtstr
, (va_list) argliststart
));
580 HeapFree(GetProcessHeap(),0,sprintfbuf
);
583 /* NULL args - copy formatstr
586 while ((lastf
<f
)&&(*lastf
)) {
591 HeapFree(GetProcessHeap(),0,fmtstr
);
638 talloced
= strlenW(target
)+1;
639 if (nSize
&& talloced
<nSize
)
640 target
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,target
,nSize
*sizeof(WCHAR
));
641 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) {
642 /* nSize is the MINIMUM size */
643 DWORD len
= strlenW(target
) + 1;
644 *((LPVOID
*)lpBuffer
) = (LPVOID
)LocalAlloc(LMEM_ZEROINIT
,len
*sizeof(WCHAR
));
645 strcpyW(*(LPWSTR
*)lpBuffer
, target
);
647 else lstrcpynW(lpBuffer
, target
, nSize
);
649 HeapFree(GetProcessHeap(),0,target
);
650 HeapFree(GetProcessHeap(),0,from
);
651 //TRACE("ret=%s\n", wine_dbgstr_w((dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
652 // *(LPWSTR*)lpBuffer : lpBuffer));
653 return (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ?
654 strlenW(*(LPWSTR
*)lpBuffer
):
658 #endif /* __i386__ */