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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_messageW (internal)
81 static LPWSTR
load_messageW( HMODULE module
, UINT id
, WORD lang
)
83 PRTL_MESSAGE_RESOURCE_ENTRY mre
;
87 TRACE("module = %p, id = %08x\n", module
, id
);
89 if (!module
) module
= GetModuleHandleW( NULL
);
90 Status
= RtlFindMessage( module
, (ULONG_PTR
) RT_MESSAGETABLE
, lang
, id
, &mre
);
91 if (!NT_SUCCESS(Status
))
93 SetLastError( RtlNtStatusToDosError(Status
) );
97 if (mre
->Flags
& MESSAGE_RESOURCE_UNICODE
)
99 int len
= (strlenW( (const WCHAR
*)mre
->Text
) + 1) * sizeof(WCHAR
);
100 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
))) return NULL
;
101 memcpy( buffer
, mre
->Text
, len
);
105 int len
= MultiByteToWideChar( CP_ACP
, 0, (const char *)mre
->Text
, -1, NULL
, 0 );
106 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) ))) return NULL
;
107 MultiByteToWideChar( CP_ACP
, 0, (const char*)mre
->Text
, -1, buffer
, len
);
109 //TRACE("returning %s\n", wine_dbgstr_w(buffer));
114 /**********************************************************************
115 * load_messageA (internal)
118 static LPSTR
load_messageA( HMODULE module
, UINT id
, WORD lang
)
120 PRTL_MESSAGE_RESOURCE_ENTRY mre
;
124 TRACE("module = %p, id = %08x\n", module
, id
);
126 if (!module
) module
= GetModuleHandleW( NULL
);
127 Status
= RtlFindMessage( module
, (ULONG_PTR
) RT_MESSAGETABLE
, lang
, id
, &mre
);
128 if (!NT_SUCCESS(Status
))
130 SetLastError( RtlNtStatusToDosError(Status
) );
134 if (mre
->Flags
& MESSAGE_RESOURCE_UNICODE
)
136 int len
= WideCharToMultiByte( CP_ACP
, 0, (const WCHAR
*)mre
->Text
, -1, NULL
, 0, NULL
, NULL
);
137 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
))) return NULL
;
138 WideCharToMultiByte( CP_ACP
, 0, (const WCHAR
*)mre
->Text
, -1, buffer
, len
, NULL
, NULL
);
142 int len
= strlen((const char*)mre
->Text
) + 1;
143 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
))) return NULL
;
144 memcpy( buffer
, mre
->Text
, len
);
146 //TRACE("returning %s\n", wine_dbgstr_a(buffer));
151 /***********************************************************************
152 * FormatMessageA (KERNEL32.@)
153 * FIXME: missing wrap,
157 DWORD WINAPI
FormatMessageA(
166 LPDWORD args
=(LPDWORD
)_args
;
167 HMODULE kernel32_handle
= GetModuleHandleW(kernel32W
);
169 #if defined(__i386__) || defined(__sparc__)
170 /* This implementation is completely dependent on the format of the va_list on x86 CPUs */
174 DWORD width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
178 TRACE("(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
179 dwFlags
,lpSource
,dwMessageId
,dwLanguageId
,lpBuffer
,nSize
,args
);
180 if ((dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
181 &&((dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
)
182 || (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
))) return 0;
186 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
190 if (width
&& width
!= FORMAT_MESSAGE_MAX_WIDTH_MASK
)
191 FIXME("line wrapping (%lu) not supported.\n", width
);
193 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
195 from
= HeapAlloc( GetProcessHeap(), 0, strlen((LPCSTR
)lpSource
)+1 );
200 strcpy( from
, (LPCSTR
)lpSource
);
204 if (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
)
205 from
= load_messageA( (HMODULE
)lpSource
, dwMessageId
, (WORD
)dwLanguageId
);
206 if (!from
&& (dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
))
207 from
= load_messageA( kernel32_handle
, dwMessageId
, (WORD
)dwLanguageId
);
214 target
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 100);
217 HeapFree(GetProcessHeap(),0,from
);
218 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
224 #define ADD_TO_T(c) do { \
226 if (t-target == talloced) {\
227 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
228 t = target+talloced;\
235 if (dwFlags
& FORMAT_MESSAGE_IGNORE_INSERTS
) {
243 char *fmtstr
,*x
,*lastf
;
254 case '1':case '2':case '3':case '4':case '5':
255 case '6':case '7':case '8':case '9':
258 case '0':case '1':case '2':case '3':
259 case '4':case '5':case '6':case '7':
262 insertnr
=insertnr
*10+*f
-'0';
271 if (NULL
!=(x
=strchr(f
,'!'))) {
273 fmtstr
=HeapAlloc(GetProcessHeap(),0,strlen(f
)+2);
276 HeapFree(GetProcessHeap(),0,from
);
277 HeapFree(GetProcessHeap(),0,target
);
278 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
281 sprintf(fmtstr
,"%%%s",f
);
284 fmtstr
=HeapAlloc(GetProcessHeap(),0,strlen(f
)+2);
287 HeapFree(GetProcessHeap(),0,from
);
288 HeapFree(GetProcessHeap(),0,target
);
289 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
292 sprintf(fmtstr
,"%%%s",f
);
293 f
+=strlen(f
); /*at \0*/
297 fmtstr
= HeapAlloc(GetProcessHeap(),0,3);
300 HeapFree(GetProcessHeap(),0,from
);
301 HeapFree(GetProcessHeap(),0,target
);
302 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
305 strcpy( fmtstr
, "%s" );
311 if (dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)
312 argliststart
=args
+insertnr
-1;
314 argliststart
=(*(DWORD
**)args
)+insertnr
-1;
320 HeapFree(GetProcessHeap(), 0, b
);
323 b
= HeapAlloc(GetProcessHeap(), 0, sz
);
324 /* CMF - This makes a BIG assumption about va_list */
325 } while (0 > _vsnprintf(b
, sz
, fmtstr
, (va_list) argliststart
));
330 HeapFree(GetProcessHeap(),0,b
);
332 /* NULL args - copy formatstr
335 while ((lastf
<f
)&&(*lastf
)) {
339 HeapFree(GetProcessHeap(),0,fmtstr
);
386 talloced
= strlen(target
)+1;
387 if (nSize
&& talloced
<nSize
) {
388 target
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,target
,nSize
);
390 //TRACE("-- %s\n",debugstr_a(target));
391 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) {
392 *((LPVOID
*)lpBuffer
) = (LPVOID
)LocalAlloc(LMEM_ZEROINIT
,max(nSize
, talloced
));
393 memcpy(*(LPSTR
*)lpBuffer
,target
,talloced
);
395 lstrcpynA(lpBuffer
,target
,nSize
);
397 HeapFree(GetProcessHeap(),0,target
);
398 HeapFree(GetProcessHeap(),0,from
);
399 TRACE("-- returning %d\n", (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ? strlen(*(LPSTR
*)lpBuffer
):strlen(lpBuffer
));
400 return (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ?
401 strlen(*(LPSTR
*)lpBuffer
):
405 #endif /* __i386__ */
409 /***********************************************************************
410 * FormatMessageW (KERNEL32.@)
414 DWORD WINAPI
FormatMessageW(
423 HMODULE kernel32_handle
= GetModuleHandleW(kernel32W
);
424 LPDWORD args
=(LPDWORD
)_args
;
425 #if defined(__i386__) || defined(__sparc__)
426 /* This implementation is completely dependent on the format of the va_list on x86 CPUs */
430 DWORD width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
434 TRACE("(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
435 dwFlags
,lpSource
,dwMessageId
,dwLanguageId
,lpBuffer
,nSize
,args
);
436 if ((dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
437 &&((dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
)
438 || (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
))) return 0;
442 SetLastError(ERROR_INVALID_PARAMETER
);
446 if (width
&& width
!= FORMAT_MESSAGE_MAX_WIDTH_MASK
)
447 FIXME("line wrapping not supported.\n");
449 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
) {
450 from
= HeapAlloc( GetProcessHeap(), 0, (strlenW((LPCWSTR
)lpSource
) + 1) *
454 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
457 strcpyW( from
, (LPCWSTR
)lpSource
);
461 if (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
)
462 from
= load_messageW( (HMODULE
)lpSource
, dwMessageId
, (WORD
)dwLanguageId
);
463 if (!from
&& (dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
))
464 from
= load_messageW( kernel32_handle
, dwMessageId
,(WORD
)dwLanguageId
);
472 target
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 100 * sizeof(WCHAR
) );
475 HeapFree(GetProcessHeap(),0,from
);
476 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
482 #define ADD_TO_T(c) do {\
484 if (t-target == talloced) {\
485 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2*sizeof(WCHAR));\
486 t = target+talloced;\
493 if (dwFlags
& FORMAT_MESSAGE_IGNORE_INSERTS
) {
501 WCHAR
*fmtstr
,*sprintfbuf
,*x
,*lastf
;
513 case '1':case '2':case '3':case '4':case '5':
514 case '6':case '7':case '8':case '9':
517 case '0':case '1':case '2':case '3':
518 case '4':case '5':case '6':case '7':
521 insertnr
=insertnr
*10+*f
-'0';
530 if (NULL
!=(x
=strchrW(f
,'!'))) {
532 fmtstr
=HeapAlloc( GetProcessHeap(), 0,(strlenW(f
)+2)*sizeof(WCHAR
));
535 HeapFree(GetProcessHeap(),0,from
);
536 HeapFree(GetProcessHeap(),0,target
);
537 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
540 sprintfW(fmtstr
,PCNTFMTWSTR
,f
);
543 fmtstr
=HeapAlloc(GetProcessHeap(),0,(strlenW(f
)+2)*sizeof(WCHAR
));
546 HeapFree(GetProcessHeap(),0,from
);
547 HeapFree(GetProcessHeap(),0,target
);
548 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
551 sprintfW(fmtstr
,PCNTFMTWSTR
,f
);
552 f
+=strlenW(f
); /*at \0*/
556 fmtstr
= HeapAlloc( GetProcessHeap(),0,3*sizeof(WCHAR
));
559 HeapFree(GetProcessHeap(),0,from
);
560 HeapFree(GetProcessHeap(),0,target
);
561 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
564 strcpyW( fmtstr
, FMTWSTR
);
568 if (dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)
569 argliststart
=args
+insertnr
-1;
571 argliststart
=(*(DWORD
**)args
)+insertnr
-1;
577 HeapFree(GetProcessHeap(),0,sprintfbuf
);
580 sprintfbuf
=HeapAlloc(GetProcessHeap(),0,len
*sizeof(WCHAR
));
581 /* CMF - This makes a BIG assumption about va_list */
582 } while (0 > _vsnwprintf(sprintfbuf
, len
, fmtstr
, (va_list) argliststart
));
587 HeapFree(GetProcessHeap(),0,sprintfbuf
);
590 /* NULL args - copy formatstr
593 while ((lastf
<f
)&&(*lastf
)) {
598 HeapFree(GetProcessHeap(),0,fmtstr
);
645 talloced
= strlenW(target
)+1;
646 if (nSize
&& talloced
<nSize
)
647 target
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,target
,nSize
*sizeof(WCHAR
));
648 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) {
649 /* nSize is the MINIMUM size */
650 DWORD len
= strlenW(target
) + 1;
651 *((LPVOID
*)lpBuffer
) = LocalAlloc(LMEM_ZEROINIT
,len
*sizeof(WCHAR
));
652 strcpyW(*(LPWSTR
*)lpBuffer
, target
);
654 else lstrcpynW(lpBuffer
, target
, nSize
);
656 HeapFree(GetProcessHeap(),0,target
);
657 HeapFree(GetProcessHeap(),0,from
);
658 //TRACE("ret=%s\n", wine_dbgstr_w((dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
659 // *(LPWSTR*)lpBuffer : lpBuffer));
660 return (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ?
661 strlenW(*(LPWSTR
*)lpBuffer
):
665 #endif /* __i386__ */