[PATCH] make LoadStringW pass tests

Christopher Berner raccoonone at procyongames.com
Wed Mar 5 14:28:54 CST 2008


---
 dlls/crypt32/oid.c           |    7 ++++---
 dlls/hhctrl.ocx/help.c       |   10 +++++-----
 dlls/mpr/wnet.c              |   10 ++++++----
 dlls/user32/resource.c       |   14 +++++++++++++-
 dlls/user32/tests/resource.c |   29 +++++++++++------------------
 5 files changed, 39 insertions(+), 31 deletions(-)

diff --git a/dlls/crypt32/oid.c b/dlls/crypt32/oid.c
index 5d17997..229deff 100644
--- a/dlls/crypt32/oid.c
+++ b/dlls/crypt32/oid.c
@@ -1402,8 +1402,9 @@ static void init_oid_info(HINSTANCE hinst)
         }
         else
         {
+            LPWSTR stringresource;
             int len = LoadStringW(hinst, (UINT_PTR)oidInfoConstructors[i].pwszName,
-             NULL, 0);
+             (LPWSTR)&stringresource, 0);
 
             if (len)
             {
@@ -1419,8 +1420,8 @@ static void init_oid_info(HINSTANCE hinst)
                      (LPWSTR)((LPBYTE)info + sizeof(struct OIDInfo));
                     info->info.dwGroupId = oidInfoConstructors[i].dwGroupId;
                     info->info.u.Algid = oidInfoConstructors[i].Algid;
-                    LoadStringW(hinst, (UINT_PTR)oidInfoConstructors[i].pwszName,
-                     (LPWSTR)info->info.pwszName, len + 1);
+                    memcpy((LPWSTR)info->info.pwszName, stringresource, len*sizeof(WCHAR));
+                    ((LPWSTR)info->info.pwszName)[len] = (WCHAR)0; /* string resources are not null terminated, so add null */
                     if (oidInfoConstructors[i].blob)
                     {
                         info->info.ExtraInfo.cbData =
diff --git a/dlls/hhctrl.ocx/help.c b/dlls/hhctrl.ocx/help.c
index 3f6a080..dd4fdef 100644
--- a/dlls/hhctrl.ocx/help.c
+++ b/dlls/hhctrl.ocx/help.c
@@ -50,14 +50,14 @@ static const WCHAR szEmpty[] = {0};
 /* Loads a string from the resource file */
 static LPWSTR HH_LoadString(DWORD dwID)
 {
-    LPWSTR string = NULL;
+    LPWSTR string = NULL, stringresource;
     int iSize;
 
-    iSize = LoadStringW(hhctrl_hinstance, dwID, NULL, 0);
-    iSize += 2; /* some strings (tab text) needs double-null termination */
+    iSize = LoadStringW(hhctrl_hinstance, dwID, (LPWSTR)&stringresource, 0);
 
-    string = heap_alloc(iSize * sizeof(WCHAR));
-    LoadStringW(hhctrl_hinstance, dwID, string, iSize);
+    string = heap_alloc((iSize + 2) * sizeof(WCHAR)); /* some strings (tab text) needs double-null termination */
+    memcpy(string, stringresource, iSize*sizeof(WCHAR));
+    string[iSize] = (WCHAR)0; /* string resources are not null terminated, so add null */
 
     return string;
 }
diff --git a/dlls/mpr/wnet.c b/dlls/mpr/wnet.c
index 17e8195..588e761 100644
--- a/dlls/mpr/wnet.c
+++ b/dlls/mpr/wnet.c
@@ -279,16 +279,18 @@ void wnetInit(HINSTANCE hInstDll)
                     {
                         PWSTR ptrPrev;
                         int entireNetworkLen;
+                        LPWSTR stringresource;
 
                         entireNetworkLen = LoadStringW(hInstDll,
-                         IDS_ENTIRENETWORK, NULL, 0);
+                         IDS_ENTIRENETWORK, (LPWSTR)&stringresource, 0);
                         providerTable->entireNetwork = HeapAlloc(
                          GetProcessHeap(), 0, (entireNetworkLen + 1) *
                          sizeof(WCHAR));
                         if (providerTable->entireNetwork)
-                            LoadStringW(hInstDll, IDS_ENTIRENETWORK,
-                             providerTable->entireNetwork,
-                             entireNetworkLen + 1);
+                        {
+                            memcpy(providerTable->entireNetwork, stringresource, entireNetworkLen*sizeof(WCHAR));
+                            providerTable->entireNetwork[entireNetworkLen] = (WCHAR) 0; /* add null, since string resources aren't null terminated */
+                        }
                         providerTable->numAllocated = numToAllocate;
                         for (ptr = providers; ptr; )
                         {
diff --git a/dlls/user32/resource.c b/dlls/user32/resource.c
index 9eed0e8..7813310 100644
--- a/dlls/user32/resource.c
+++ b/dlls/user32/resource.c
@@ -364,10 +364,14 @@ INT WINAPI LoadStringW( HINSTANCE instance, UINT resource_id,
     WCHAR *p;
     int string_num;
     int i;
+    int length;
 
     TRACE("instance = %p, id = %04x, buffer = %p, length = %d\n",
           instance, resource_id, buffer, buflen);
 
+    if(buffer == NULL)
+        return 0;
+
     /* Use loword (incremented by 1) as resourceid */
     hrsrc = FindResourceW( instance, MAKEINTRESOURCEW((LOWORD(resource_id) >> 4) + 1),
                            (LPWSTR)RT_STRING );
@@ -381,8 +385,16 @@ INT WINAPI LoadStringW( HINSTANCE instance, UINT resource_id,
 	p += *p + 1;
 
     TRACE("strlen = %d\n", (int)*p );
+    length = (int)*p;
+
+    /*if buflen == 0, then return a read-only pointer to the resource itself in buffer
+    it is assumed that buffer is actually a (LPWSTR *) */
+    if(buflen == 0)
+    {
+        *((LPWSTR *)buffer) = p + 1;
+        return length;
+    }
 
-    if (buffer == NULL) return *p;
     i = min(buflen - 1, *p);
     if (i > 0) {
 	memcpy(buffer, p + 1, i * sizeof (WCHAR));
diff --git a/dlls/user32/tests/resource.c b/dlls/user32/tests/resource.c
index 7be82ad..99fc24f 100644
--- a/dlls/user32/tests/resource.c
+++ b/dlls/user32/tests/resource.c
@@ -43,13 +43,10 @@ static void test_LoadStringW(void)
     length1 = LoadStringW(hInst, 2, (WCHAR *) &resourcepointer, 0); /* get pointer to resource. */
     length2 = LoadStringW(hInst, 2, returnedstringw, sizeof(returnedstringw) /sizeof(WCHAR)); /* get resource string */
     ok(length2 > 0, "LoadStringW failed to load resource 2, ret %d, err %d\n", length2, GetLastError());
-    todo_wine
-    {
-        ok(length1 == length2, "LoadStringW returned different values dependent on buflen. ret1 %d, ret2 %d\n",
-            length1, length2);
-        ok(length1 > 0 && resourcepointer != NULL, "LoadStringW failed to get pointer to resource 2, ret %d, err %d\n",
-            length1, GetLastError());
-    }
+    ok(length1 == length2, "LoadStringW returned different values dependent on buflen. ret1 %d, ret2 %d\n",
+        length1, length2);
+    ok(length1 > 0 && resourcepointer != NULL, "LoadStringW failed to get pointer to resource 2, ret %d, err %d\n",
+        length1, GetLastError());
 
     /* Copy the resource since it is not '\0' terminated, and add '\0' to the end */
     if(resourcepointer != NULL) /* Check that the resource pointer was loaded to avoid access violation */
@@ -62,17 +59,13 @@ static void test_LoadStringW(void)
         ok(!memcmp(copiedstringw, returnedstringw, (length2 + 1)*sizeof(WCHAR)),
            "strings don't match: returnedstring = %s, copiedstring = %s\n", returnedstring, copiedstring);
     }
-    todo_wine
-    {
-        /* check that calling LoadStringW with buffer = NULL returns zero */
-        retvalue = LoadStringW(hInst, 2, NULL, 0);
-        ok(!retvalue, "LoadStringW returned a non-zero value when called with buffer = NULL, retvalue = %d\n",
-            retvalue);
-        /* check again, with a different buflen value, that calling LoadStringW with buffer = NULL returns zero */
-        retvalue = LoadStringW(hInst, 2, NULL, 128);
-        ok(!retvalue, "LoadStringW returned a non-zero value when called with buffer = NULL, retvalue = %d\n",
-            retvalue);
-    }
+
+    /* check that calling LoadStringW with buffer = NULL returns zero */
+    retvalue = LoadStringW(hInst, 2, NULL, 0);
+    ok(!retvalue, "LoadStringW returned a non-zero value when called with buffer = NULL, retvalue = %d\n", retvalue);
+    /* check again, with a different buflen value, that calling LoadStringW with buffer = NULL returns zero */
+    retvalue = LoadStringW(hInst, 2, NULL, 128);
+    ok(!retvalue, "LoadStringW returned a non-zero value when called with buffer = NULL, retvalue = %d\n", retvalue);
 }
 
 static void test_LoadStringA (void)
-- 
1.5.2.5


--------------080109060005010307000600--



More information about the wine-patches mailing list