3 * reactos/lib/kernel32/misc/errormsg.c
7 * FormatMessage implementation
9 * Copyright 1996 Marcus Meissner
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "../include/debug.h"
31 #include "../include/wine/unicode.h"
36 static const WCHAR PCNTFMTWSTR
[] = { '%','%','%','s',0 };
37 static const WCHAR FMTWSTR
[] = { '%','s',0 };
38 static const WCHAR kernel32W
[] = {'k','e','r','n','e','l','3','2',0};
41 /* DO NOT USE IT!! it will go away soon */
43 __inline
static LPSTR
HEAP_strdupWtoA( HANDLE heap
, DWORD flags
, LPCWSTR str
)
48 if (!str
) return NULL
;
49 len
= WideCharToMultiByte( CP_ACP
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
50 ret
= RtlAllocateHeap(RtlGetProcessHeap(), flags
, len
);
51 if(ret
) WideCharToMultiByte( CP_ACP
, 0, str
, -1, ret
, len
, NULL
, NULL
);
55 /* Messages...used by FormatMessage32* (KERNEL32.something)
57 * They can be specified either directly or using a message ID and
58 * loading them from the resource.
60 * The resourcedata has following format:
62 * 0: DWORD nrofentries
63 * nrofentries * subentry:
66 * 8: DWORD offset from start to the stringentries
68 * (lastentry-firstentry) * stringentry:
69 * 0: WORD len (0 marks end) [ includes the 4 byte header length ]
72 * (stringentry i of a subentry refers to the ID 'firstentry+i')
74 * Yes, ANSI strings in win32 resources. Go figure.
77 /**********************************************************************
78 * load_messageA (internal)
81 static LPSTR
load_messageA( 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
) RT_MESSAGETABLE
, lang
, id
, &mre
);
91 if (!NT_SUCCESS(Status
))
94 if (mre
->Flags
& MESSAGE_RESOURCE_UNICODE
)
96 int len
= WideCharToMultiByte( CP_ACP
, 0, (const WCHAR
*)mre
->Text
, -1, NULL
, 0, NULL
, NULL
);
97 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
))) return NULL
;
98 WideCharToMultiByte( CP_ACP
, 0, (const WCHAR
*)mre
->Text
, -1, buffer
, len
, NULL
, NULL
);
102 int len
= strlen((const char*)mre
->Text
) + 1;
103 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
))) return NULL
;
104 memcpy( buffer
, mre
->Text
, len
);
106 TRACE("returning %s\n", wine_dbgstr_a(buffer
));
112 static LPWSTR
load_messageW( HMODULE module
, UINT id
, WORD lang
)
114 PRTL_MESSAGE_RESOURCE_ENTRY mre
;
118 TRACE("module = %p, id = %08x\n", module
, id
);
120 if (!module
) module
= GetModuleHandleW( NULL
);
121 Status
= RtlFindMessage( module
, (ULONG
) RT_MESSAGETABLE
, lang
, id
, &mre
);
122 if (!NT_SUCCESS(Status
))
125 if (mre
->Flags
& MESSAGE_RESOURCE_UNICODE
)
127 int len
= (strlenW( (const WCHAR
*)mre
->Text
) + 1) * sizeof(WCHAR
);
128 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
))) return NULL
;
129 memcpy( buffer
, mre
->Text
, len
);
133 int len
= MultiByteToWideChar( CP_ACP
, 0, (const char *)mre
->Text
, -1, NULL
, 0 );
134 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) ))) return NULL
;
135 MultiByteToWideChar( CP_ACP
, 0, (const char*)mre
->Text
, -1, buffer
, len
);
137 TRACE("returning %s\n", wine_dbgstr_w(buffer
));
141 /***********************************************************************
142 * FormatMessageA (KERNEL32.@)
143 * FIXME: missing wrap,
147 DWORD WINAPI
FormatMessageA(
156 LPDWORD args
=(LPDWORD
)_args
;
157 HMODULE kernel32_handle
= GetModuleHandleW(kernel32W
);
159 #if defined(__i386__) || defined(__sparc__)
160 /* This implementation is completely dependent on the format of the va_list on x86 CPUs */
164 DWORD width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
168 TRACE("(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
169 dwFlags
,lpSource
,dwMessageId
,dwLanguageId
,lpBuffer
,nSize
,args
);
170 if ((dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
171 &&((dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
)
172 || (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
))) return 0;
174 if (width
&& width
!= FORMAT_MESSAGE_MAX_WIDTH_MASK
)
175 FIXME("line wrapping (%lu) not supported.\n", width
);
177 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
179 from
= HeapAlloc( GetProcessHeap(), 0, strlen((LPCSTR
)lpSource
)+1 );
184 strcpy( from
, (LPCSTR
)lpSource
);
188 if (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
)
189 from
= load_messageA( (HMODULE
)lpSource
, dwMessageId
, dwLanguageId
);
190 if (!from
&& (dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
))
191 from
= load_messageA( kernel32_handle
, dwMessageId
, dwLanguageId
);
195 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND
);
199 target
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 100);
203 #define ADD_TO_T(c) do { \
205 if (t-target == talloced) {\
206 target = 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;
278 HeapFree(GetProcessHeap(), 0, b
);
281 b
= HeapAlloc(GetProcessHeap(), 0, sz
);
282 /* CMF - This makes a BIG assumption about va_list */
283 } while (0 > _vsnprintf(b
, sz
, fmtstr
, (va_list) argliststart
));
288 HeapFree(GetProcessHeap(),0,b
);
290 /* NULL args - copy formatstr
293 while ((lastf
<f
)&&(*lastf
)) {
297 HeapFree(GetProcessHeap(),0,fmtstr
);
344 talloced
= strlen(target
)+1;
345 if (nSize
&& talloced
<nSize
) {
346 target
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,target
,nSize
);
348 TRACE("-- %s\n",debugstr_a(target
));
349 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) {
350 *((LPVOID
*)lpBuffer
) = (LPVOID
)LocalAlloc(LMEM_ZEROINIT
,max(nSize
, talloced
));
351 memcpy(*(LPSTR
*)lpBuffer
,target
,talloced
);
353 lstrcpynA(lpBuffer
,target
,nSize
);
355 HeapFree(GetProcessHeap(),0,target
);
356 HeapFree(GetProcessHeap(),0,from
);
357 TRACE("-- returning %d\n", (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ? strlen(*(LPSTR
*)lpBuffer
):strlen(lpBuffer
));
358 return (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ?
359 strlen(*(LPSTR
*)lpBuffer
):
363 #endif /* __i386__ */
367 /***********************************************************************
368 * FormatMessageW (KERNEL32.@)
372 DWORD WINAPI
FormatMessageW(
381 HMODULE kernel32_handle
= GetModuleHandleW(kernel32W
);
382 LPDWORD args
=(LPDWORD
)_args
;
383 #if defined(__i386__) || defined(__sparc__)
384 /* This implementation is completely dependent on the format of the va_list on x86 CPUs */
388 DWORD width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
392 TRACE("(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
393 dwFlags
,lpSource
,dwMessageId
,dwLanguageId
,lpBuffer
,nSize
,args
);
394 if ((dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
395 &&((dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
)
396 || (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
))) return 0;
398 if (width
&& width
!= FORMAT_MESSAGE_MAX_WIDTH_MASK
)
399 FIXME("line wrapping not supported.\n");
401 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
) {
402 from
= HeapAlloc( GetProcessHeap(), 0, (strlenW((LPCWSTR
)lpSource
) + 1) *
404 strcpyW( from
, (LPCWSTR
)lpSource
);
408 if (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
)
409 from
= load_messageW( (HMODULE
)lpSource
, dwMessageId
, dwLanguageId
);
410 if (!from
&& (dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
))
411 from
= load_messageW( kernel32_handle
, dwMessageId
, dwLanguageId
);
415 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND
);
420 target
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 100 * sizeof(WCHAR
) );
424 #define ADD_TO_T(c) do {\
426 if (t-target == talloced) {\
427 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2*sizeof(WCHAR));\
428 t = target+talloced;\
435 if (dwFlags
& FORMAT_MESSAGE_IGNORE_INSERTS
) {
443 WCHAR
*fmtstr
,*sprintfbuf
,*x
,*lastf
;
455 case '1':case '2':case '3':case '4':case '5':
456 case '6':case '7':case '8':case '9':
459 case '0':case '1':case '2':case '3':
460 case '4':case '5':case '6':case '7':
463 insertnr
=insertnr
*10+*f
-'0';
472 if (NULL
!=(x
=strchrW(f
,'!'))) {
474 fmtstr
=HeapAlloc( GetProcessHeap(), 0,(strlenW(f
)+2)*sizeof(WCHAR
));
475 sprintfW(fmtstr
,PCNTFMTWSTR
,f
);
478 fmtstr
=HeapAlloc(GetProcessHeap(),0,(strlenW(f
)+2)*sizeof(WCHAR
));
479 sprintfW(fmtstr
,PCNTFMTWSTR
,f
);
480 f
+=strlenW(f
); /*at \0*/
484 fmtstr
= HeapAlloc( GetProcessHeap(),0,3*sizeof(WCHAR
));
485 strcpyW( fmtstr
, FMTWSTR
);
489 if (dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)
490 argliststart
=args
+insertnr
-1;
492 argliststart
=(*(DWORD
**)args
)+insertnr
-1;
498 HeapFree(GetProcessHeap(),0,sprintfbuf
);
501 sprintfbuf
=HeapAlloc(GetProcessHeap(),0,len
*sizeof(WCHAR
));
502 /* CMF - This makes a BIG assumption about va_list */
503 } while (0 > _vsnwprintf(sprintfbuf
, len
, fmtstr
, (va_list) argliststart
));
508 HeapFree(GetProcessHeap(),0,sprintfbuf
);
511 /* NULL args - copy formatstr
514 while ((lastf
<f
)&&(*lastf
)) {
519 HeapFree(GetProcessHeap(),0,fmtstr
);
566 talloced
= strlenW(target
)+1;
567 if (nSize
&& talloced
<nSize
)
568 target
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,target
,nSize
*sizeof(WCHAR
));
569 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) {
570 /* nSize is the MINIMUM size */
571 DWORD len
= strlenW(target
) + 1;
572 *((LPVOID
*)lpBuffer
) = (LPVOID
)LocalAlloc(LMEM_ZEROINIT
,len
*sizeof(WCHAR
));
573 strcpyW(*(LPWSTR
*)lpBuffer
, target
);
575 else lstrcpynW(lpBuffer
, target
, nSize
);
577 HeapFree(GetProcessHeap(),0,target
);
578 HeapFree(GetProcessHeap(),0,from
);
579 TRACE("ret=%s\n", wine_dbgstr_w((dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ?
580 *(LPWSTR
*)lpBuffer
: lpBuffer
));
581 return (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ?
582 strlenW(*(LPWSTR
*)lpBuffer
):
586 #endif /* __i386__ */