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 );
180 strcpy( from
, (LPCSTR
)lpSource
);
184 if (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
)
185 from
= load_messageA( (HMODULE
)lpSource
, dwMessageId
, dwLanguageId
);
186 if (!from
&& (dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
))
187 from
= load_messageA( kernel32_handle
, dwMessageId
, dwLanguageId
);
191 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND
);
195 target
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 100);
199 #define ADD_TO_T(c) do { \
201 if (t-target == talloced) {\
202 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2);\
203 t = target+talloced;\
210 if (dwFlags
& FORMAT_MESSAGE_IGNORE_INSERTS
) {
218 char *fmtstr
,*x
,*lastf
;
229 case '1':case '2':case '3':case '4':case '5':
230 case '6':case '7':case '8':case '9':
233 case '0':case '1':case '2':case '3':
234 case '4':case '5':case '6':case '7':
237 insertnr
=insertnr
*10+*f
-'0';
246 if (NULL
!=(x
=strchr(f
,'!'))) {
248 fmtstr
=HeapAlloc(GetProcessHeap(),0,strlen(f
)+2);
249 sprintf(fmtstr
,"%%%s",f
);
252 fmtstr
=HeapAlloc(GetProcessHeap(),0,strlen(f
)+2);
253 sprintf(fmtstr
,"%%%s",f
);
254 f
+=strlen(f
); /*at \0*/
258 fmtstr
= HeapAlloc(GetProcessHeap(),0,3);
259 strcpy( fmtstr
, "%s" );
265 if (dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)
266 argliststart
=args
+insertnr
-1;
268 argliststart
=(*(DWORD
**)args
)+insertnr
-1;
274 HeapFree(GetProcessHeap(), 0, b
);
277 b
= HeapAlloc(GetProcessHeap(), 0, sz
);
278 /* CMF - This makes a BIG assumption about va_list */
279 } while (0 > _vsnprintf(b
, sz
, fmtstr
, (va_list) argliststart
));
284 HeapFree(GetProcessHeap(),0,b
);
286 /* NULL args - copy formatstr
289 while ((lastf
<f
)&&(*lastf
)) {
293 HeapFree(GetProcessHeap(),0,fmtstr
);
340 talloced
= strlen(target
)+1;
341 if (nSize
&& talloced
<nSize
) {
342 target
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,target
,nSize
);
344 TRACE("-- %s\n",debugstr_a(target
));
345 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) {
346 *((LPVOID
*)lpBuffer
) = (LPVOID
)LocalAlloc(LMEM_ZEROINIT
,max(nSize
, talloced
));
347 memcpy(*(LPSTR
*)lpBuffer
,target
,talloced
);
349 lstrcpynA(lpBuffer
,target
,nSize
);
351 HeapFree(GetProcessHeap(),0,target
);
352 HeapFree(GetProcessHeap(),0,from
);
353 TRACE("-- returning %d\n", (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ? strlen(*(LPSTR
*)lpBuffer
):strlen(lpBuffer
));
354 return (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ?
355 strlen(*(LPSTR
*)lpBuffer
):
359 #endif /* __i386__ */
363 /***********************************************************************
364 * FormatMessageW (KERNEL32.@)
368 DWORD WINAPI
FormatMessageW(
377 HMODULE kernel32_handle
= GetModuleHandleW(kernel32W
);
378 LPDWORD args
=(LPDWORD
)_args
;
379 #if defined(__i386__) || defined(__sparc__)
380 /* This implementation is completely dependent on the format of the va_list on x86 CPUs */
384 DWORD width
= dwFlags
& FORMAT_MESSAGE_MAX_WIDTH_MASK
;
388 TRACE("(0x%lx,%p,%ld,0x%lx,%p,%ld,%p)\n",
389 dwFlags
,lpSource
,dwMessageId
,dwLanguageId
,lpBuffer
,nSize
,args
);
390 if ((dwFlags
& FORMAT_MESSAGE_FROM_STRING
)
391 &&((dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
)
392 || (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
))) return 0;
394 if (width
&& width
!= FORMAT_MESSAGE_MAX_WIDTH_MASK
)
395 FIXME("line wrapping not supported.\n");
397 if (dwFlags
& FORMAT_MESSAGE_FROM_STRING
) {
398 from
= HeapAlloc( GetProcessHeap(), 0, (strlenW((LPCWSTR
)lpSource
) + 1) *
400 strcpyW( from
, (LPCWSTR
)lpSource
);
404 if (dwFlags
& FORMAT_MESSAGE_FROM_HMODULE
)
405 from
= load_messageW( (HMODULE
)lpSource
, dwMessageId
, dwLanguageId
);
406 if (!from
&& (dwFlags
& FORMAT_MESSAGE_FROM_SYSTEM
))
407 from
= load_messageW( kernel32_handle
, dwMessageId
, dwLanguageId
);
411 SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND
);
416 target
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 100 * sizeof(WCHAR
) );
420 #define ADD_TO_T(c) do {\
422 if (t-target == talloced) {\
423 target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2*sizeof(WCHAR));\
424 t = target+talloced;\
431 if (dwFlags
& FORMAT_MESSAGE_IGNORE_INSERTS
) {
439 WCHAR
*fmtstr
,*sprintfbuf
,*x
,*lastf
;
451 case '1':case '2':case '3':case '4':case '5':
452 case '6':case '7':case '8':case '9':
455 case '0':case '1':case '2':case '3':
456 case '4':case '5':case '6':case '7':
459 insertnr
=insertnr
*10+*f
-'0';
468 if (NULL
!=(x
=strchrW(f
,'!'))) {
470 fmtstr
=HeapAlloc( GetProcessHeap(), 0,(strlenW(f
)+2)*sizeof(WCHAR
));
471 sprintfW(fmtstr
,PCNTFMTWSTR
,f
);
474 fmtstr
=HeapAlloc(GetProcessHeap(),0,(strlenW(f
)+2)*sizeof(WCHAR
));
475 sprintfW(fmtstr
,PCNTFMTWSTR
,f
);
476 f
+=strlenW(f
); /*at \0*/
480 fmtstr
= HeapAlloc( GetProcessHeap(),0,3*sizeof(WCHAR
));
481 strcpyW( fmtstr
, FMTWSTR
);
485 if (dwFlags
& FORMAT_MESSAGE_ARGUMENT_ARRAY
)
486 argliststart
=args
+insertnr
-1;
488 argliststart
=(*(DWORD
**)args
)+insertnr
-1;
494 HeapFree(GetProcessHeap(),0,sprintfbuf
);
497 sprintfbuf
=HeapAlloc(GetProcessHeap(),0,len
*sizeof(WCHAR
));
498 /* CMF - This makes a BIG assumption about va_list */
499 } while (0 > _vsnwprintf(sprintfbuf
, len
, fmtstr
, (va_list) argliststart
));
504 HeapFree(GetProcessHeap(),0,sprintfbuf
);
507 /* NULL args - copy formatstr
510 while ((lastf
<f
)&&(*lastf
)) {
515 HeapFree(GetProcessHeap(),0,fmtstr
);
562 talloced
= strlenW(target
)+1;
563 if (nSize
&& talloced
<nSize
)
564 target
= HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,target
,nSize
*sizeof(WCHAR
));
565 if (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) {
566 /* nSize is the MINIMUM size */
567 DWORD len
= strlenW(target
) + 1;
568 *((LPVOID
*)lpBuffer
) = (LPVOID
)LocalAlloc(LMEM_ZEROINIT
,len
*sizeof(WCHAR
));
569 strcpyW(*(LPWSTR
*)lpBuffer
, target
);
571 else lstrcpynW(lpBuffer
, target
, nSize
);
573 HeapFree(GetProcessHeap(),0,target
);
574 HeapFree(GetProcessHeap(),0,from
);
575 TRACE("ret=%s\n", wine_dbgstr_w((dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ?
576 *(LPWSTR
*)lpBuffer
: lpBuffer
));
577 return (dwFlags
& FORMAT_MESSAGE_ALLOCATE_BUFFER
) ?
578 strlenW(*(LPWSTR
*)lpBuffer
):
582 #endif /* __i386__ */