PRB: LoadStringW Returns NULL on Windows 95 and Windows 98 (196899)



The information in this article applies to:

  • Microsoft Platform Software Development Kit (SDK) 1.0, when used with:
    • the operating system: Microsoft Windows 95
    • the operating system: Microsoft Windows 98

This article was previously published under Q196899

SYMPTOMS

A call to LoadStringW returns NULL on Windows 95 or Windows 98, even though the string is in the resource.

CAUSE

Strings are stored in resource files in Unicode. LoadStringA converts the strings it reads to Multibyte Character Set (MBCS) before returning them. How this conversion takes place depends on the current code page. LoadStringW, on Windows NT, returns the exact string stored in the resource. On Windows 95 and Windows 98, this function is a stub that returns NULL. This is true of most W functions on Windows 95 and Windows 98, with MessageBoxW being a notable exception.

RESOLUTION

You can emulate the behavior of LoadStringW through calls to FindResourceEx and LoadResource. Strings are stored in resources in blocks of 16. First, you must locate the block containing the desired string and load it into memory. Then you must locate the string within the block of memory and copy it out. The following code shows how to do this:
   // Called as follows: If IDS_TEST is the string you wish to load and
   // WCHAR Buffer[128] is your buffer, the call would be
   // MyLoadStringW(IDS_TEST,Buffer,128);
   // If it succeeds, the function returns the number of characters copied
   // into the buffer, not including the NULL terminating character, or
   // zero if the string resource does not exist.
   int MyLoadStringW(UINT wID, LPWSTR wzBuf, int cchBuf)
   {
      UINT    block, num;
      int     len = 0;
      HRSRC   hRC = NULL;
      HGLOBAL hgl = NULL;
      LPWSTR  str = NULL;
      UINT i;

      wzBuf[0] = L'\0';

      block = (wID >> 4) + 1;   // Compute block number.
      num = wID & 0xf;      // Compute offset into block.

      hRC = FindResourceEx(NULL,
         RT_STRING,
         MAKEINTRESOURCE(block),
         MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));
      hgl = LoadResource(NULL, hRC);

      str = (LPWSTR)LockResource(hgl);
      if (str)
      {
         for (i = 0; i < num; i++)
         {
            str += *str + 1;
         }
         wcsncpy(wzBuf, str + 1, min(cchBuf - 1, *str));

      wzBuf[min(cchBuf-1, *str) ] = '\0';

      len = *str;
      }
      return len;
   }
				

STATUS

This behavior is by design.

Modification Type:MinorLast Reviewed:7/11/2005
Keywords:kbprb kbString KB196899