Sync to trunk (r44371)
[reactos.git] / reactos / dll / win32 / kernel32 / misc / errormsg.c
index ad1bf12..966d887 100644 (file)
@@ -21,7 +21,7 @@
  *
  * 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>
@@ -76,13 +76,12 @@ __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 );
@@ -90,30 +89,36 @@ static LPSTR load_messageA( HMODULE module, UINT id, WORD lang )
     if (!module) module = GetModuleHandleW( NULL );
     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 );
@@ -121,24 +126,28 @@ static LPWSTR load_messageW( HMODULE module, UINT id, WORD lang )
     if (!module) module = GetModuleHandleW( NULL );
     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,
@@ -199,7 +208,6 @@ DWORD WINAPI FormatMessageA(
 
         if (!from)
         {
-            SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND);
             return 0;
         }
     }
@@ -457,7 +465,6 @@ DWORD WINAPI FormatMessageW(
 
         if (!from)
         {
-            SetLastError (ERROR_RESOURCE_LANG_NOT_FOUND);
             return 0;
         }
     }
@@ -641,7 +648,7 @@ 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);