Article ID: 196899
Article Last Modified on 7/11/2005
// 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;
}
Keywords: kbstring kbprb KB196899