Sync to trunk (r44371)
[reactos.git] / reactos / dll / win32 / kernel32 / misc / errormsg.c
index 658a689..966d887 100644 (file)
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
 #include <k32.h>
 
 #define NDEBUG
 
-#include "../include/debug.h"
+#include <debug.h>
 #include "wine/unicode.h"
 
 #define TRACE DPRINT
@@ -76,69 +76,78 @@ __inline static LPSTR HEAP_strdupWtoA( HANDLE heap, DWORD flags, LPCWSTR str )
  */
 
 /**********************************************************************
- *      load_messageA           (internal)
+ *     load_messageW           (internal)
  */
-
-static LPSTR load_messageA( HMODULE module, UINT id, WORD lang )
+static LPWSTR load_messageW( HMODULE module, UINT id, WORD lang )
 {
     PRTL_MESSAGE_RESOURCE_ENTRY mre;
-    char *buffer;
+    WCHAR *buffer;
     NTSTATUS Status;
 
     TRACE("module = %p, id = %08x\n", module, id );
 
     if (!module) module = GetModuleHandleW( NULL );
-    Status = RtlFindMessage( module, (ULONG) RT_MESSAGETABLE, lang, id, &mre );
+    Status = RtlFindMessage( module, (ULONG_PTR) RT_MESSAGETABLE, lang, id, &mre );
     if (!NT_SUCCESS(Status))
+    {
+        SetLastError( RtlNtStatusToDosError(Status) );
         return NULL;
+    }
 
     if (mre->Flags & MESSAGE_RESOURCE_UNICODE)
     {
-        int len = WideCharToMultiByte( CP_ACP, 0, (const WCHAR *)mre->Text, -1, NULL, 0, NULL, NULL );
+        int len = (strlenW( (const WCHAR *)mre->Text ) + 1) * sizeof(WCHAR);
         if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len ))) return NULL;
-        WideCharToMultiByte( CP_ACP, 0, (const WCHAR *)mre->Text, -1, buffer, len, NULL, NULL );
+        memcpy( buffer, mre->Text, len );
     }
     else
     {
-        int len = strlen((const char*)mre->Text) + 1;
-        if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len ))) return NULL;
-        memcpy( buffer, mre->Text, len );
+        int len = MultiByteToWideChar( CP_ACP, 0, (const char *)mre->Text, -1, NULL, 0 );
+        if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return NULL;
+        MultiByteToWideChar( CP_ACP, 0, (const char*)mre->Text, -1, buffer, len );
     }
-    TRACE("returning %s\n", wine_dbgstr_a(buffer));
+    //TRACE("returning %s\n", wine_dbgstr_w(buffer));
     return buffer;
 }
 
 
+/**********************************************************************
+ *      load_messageA           (internal)
+ */
 
-static LPWSTR load_messageW( HMODULE module, UINT id, WORD lang )
+static LPSTR load_messageA( HMODULE module, UINT id, WORD lang )
 {
     PRTL_MESSAGE_RESOURCE_ENTRY mre;
-    WCHAR *buffer;
+    char *buffer;
     NTSTATUS Status;
 
     TRACE("module = %p, id = %08x\n", module, id );
 
     if (!module) module = GetModuleHandleW( NULL );
-    Status = RtlFindMessage( module, (ULONG) RT_MESSAGETABLE, lang, id, &mre );
+    Status = RtlFindMessage( module, (ULONG_PTR) RT_MESSAGETABLE, lang, id, &mre );
     if (!NT_SUCCESS(Status))
+    {
+        SetLastError( RtlNtStatusToDosError(Status) );
         return NULL;
+    }
 
     if (mre->Flags & MESSAGE_RESOURCE_UNICODE)
     {
-        int len = (strlenW( (const WCHAR *)mre->Text ) + 1) * sizeof(WCHAR);
+        int len = WideCharToMultiByte( CP_ACP, 0, (const WCHAR *)mre->Text, -1, NULL, 0, NULL, NULL );
         if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len ))) return NULL;
-        memcpy( buffer, mre->Text, len );
+        WideCharToMultiByte( CP_ACP, 0, (const WCHAR *)mre->Text, -1, buffer, len, NULL, NULL );
     }
     else
     {
-        int len = MultiByteToWideChar( CP_ACP, 0, (const char *)mre->Text, -1, NULL, 0 );
-        if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return NULL;
-        MultiByteToWideChar( CP_ACP, 0, (const char*)mre->Text, -1, buffer, len );
+        int len = strlen((const char*)mre->Text) + 1;
+        if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len ))) return NULL;
+        memcpy( buffer, mre->Text, len );
     }
-    TRACE("returning %s\n", wine_dbgstr_w(buffer));
+    //TRACE("returning %s\n", wine_dbgstr_a(buffer));
     return buffer;
 }
 
+
 /***********************************************************************
  *           FormatMessageA   (KERNEL32.@)
  * FIXME: missing wrap,
@@ -172,6 +181,12 @@ DWORD WINAPI FormatMessageA(
         &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
            || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
 
+    if (!lpBuffer)
+    {
+        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+        return 0;
+    }
+
     if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
         FIXME("line wrapping (%lu) not supported.\n", width);
     from = NULL;
@@ -187,13 +202,12 @@ DWORD WINAPI FormatMessageA(
     else {
         from = NULL;
         if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)
-            from = load_messageA( (HMODULE)lpSource, dwMessageId, dwLanguageId );
+            from = load_messageA( (HMODULE)lpSource, dwMessageId, (WORD)dwLanguageId );
         if (!from && (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM))
-            from = load_messageA( kernel32_handle, dwMessageId, dwLanguageId );
+            from = load_messageA( kernel32_handle, dwMessageId, (WORD)dwLanguageId );
 
         if (!from)
         {
-            SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND);
             return 0;
         }
     }
@@ -373,7 +387,7 @@ DWORD WINAPI FormatMessageA(
     if (nSize && talloced<nSize) {
         target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,nSize);
     }
-    TRACE("-- %s\n",debugstr_a(target));
+    //TRACE("-- %s\n",debugstr_a(target));
     if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
         *((LPVOID*)lpBuffer) = (LPVOID)LocalAlloc(LMEM_ZEROINIT,max(nSize, talloced));
         memcpy(*(LPSTR*)lpBuffer,target,talloced);
@@ -423,6 +437,12 @@ DWORD WINAPI FormatMessageW(
         &&((dwFlags & FORMAT_MESSAGE_FROM_SYSTEM)
            || (dwFlags & FORMAT_MESSAGE_FROM_HMODULE))) return 0;
 
+    if (!lpBuffer)
+    {
+        SetLastError(ERROR_INVALID_PARAMETER);
+        return 0;
+    }
+
     if (width && width != FORMAT_MESSAGE_MAX_WIDTH_MASK)
         FIXME("line wrapping not supported.\n");
     from = NULL;
@@ -439,13 +459,12 @@ DWORD WINAPI FormatMessageW(
     else {
         from = NULL;
         if (dwFlags & FORMAT_MESSAGE_FROM_HMODULE)
-            from = load_messageW( (HMODULE)lpSource, dwMessageId, dwLanguageId );
+            from = load_messageW( (HMODULE)lpSource, dwMessageId, (WORD)dwLanguageId );
         if (!from && (dwFlags & FORMAT_MESSAGE_FROM_SYSTEM))
-            from = load_messageW( kernel32_handle, dwMessageId, dwLanguageId );
+            from = load_messageW( kernel32_handle, dwMessageId,(WORD)dwLanguageId );
 
         if (!from)
         {
-            SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND);
             return 0;
         }
     }
@@ -557,7 +576,7 @@ DWORD WINAPI FormatMessageW(
                                 if (sprintfbuf) {
                                     HeapFree(GetProcessHeap(),0,sprintfbuf);
                                 }
-                                len += 256; 
+                                len += 256;
                                 sprintfbuf=HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
                                 /* CMF - This makes a BIG assumption about va_list */
                             } while (0 > _vsnwprintf(sprintfbuf, len, fmtstr, (va_list) argliststart));
@@ -629,15 +648,15 @@ DWORD WINAPI FormatMessageW(
     if (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) {
         /* nSize is the MINIMUM size */
         DWORD len = strlenW(target) + 1;
-        *((LPVOID*)lpBuffer) = (LPVOID)LocalAlloc(LMEM_ZEROINIT,len*sizeof(WCHAR));
+        *((LPVOID*)lpBuffer) = LocalAlloc(LMEM_ZEROINIT,len*sizeof(WCHAR));
         strcpyW(*(LPWSTR*)lpBuffer, target);
     }
     else lstrcpynW(lpBuffer, target, nSize);
 
     HeapFree(GetProcessHeap(),0,target);
     HeapFree(GetProcessHeap(),0,from);
-    TRACE("ret=%s\n", wine_dbgstr_w((dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
-        *(LPWSTR*)lpBuffer : lpBuffer));
+    //TRACE("ret=%s\n", wine_dbgstr_w((dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
+      //  *(LPWSTR*)lpBuffer : lpBuffer));
     return (dwFlags & FORMAT_MESSAGE_ALLOCATE_BUFFER) ?
         strlenW(*(LPWSTR*)lpBuffer):
             strlenW(lpBuffer);