xref: /reactos/dll/cpl/desk/misc.c (revision c2c66aff)
1 #include "desk.h"
2 
3 static INT
LengthOfStrResource(IN HINSTANCE hInst,IN UINT uID)4 LengthOfStrResource(IN HINSTANCE hInst,
5                     IN UINT uID)
6 {
7     HRSRC hrSrc;
8     HGLOBAL hRes;
9     LPWSTR lpName, lpStr;
10 
11     if (hInst == NULL)
12     {
13         return -1;
14     }
15 
16     /* There are always blocks of 16 strings */
17     lpName = (LPWSTR)MAKEINTRESOURCE((uID >> 4) + 1);
18 
19     /* Find the string table block */
20     if ((hrSrc = FindResourceW(hInst, lpName, (LPWSTR)RT_STRING)) &&
21         (hRes = LoadResource(hInst, hrSrc)) &&
22         (lpStr = LockResource(hRes)))
23     {
24         UINT x;
25 
26         /* Find the string we're looking for */
27         uID &= 0xF; /* Position in the block, same as % 16 */
28         for (x = 0; x < uID; x++)
29         {
30             lpStr += (*lpStr) + 1;
31         }
32 
33         /* Found the string */
34         return (int)(*lpStr);
35     }
36     return -1;
37 }
38 
39 INT
AllocAndLoadString(OUT LPTSTR * lpTarget,IN HINSTANCE hInst,IN UINT uID)40 AllocAndLoadString(OUT LPTSTR *lpTarget,
41                    IN HINSTANCE hInst,
42                    IN UINT uID)
43 {
44     INT ln;
45 
46     ln = LengthOfStrResource(hInst,
47                              uID);
48     if (ln++ > 0)
49     {
50         (*lpTarget) = (LPTSTR)LocalAlloc(LMEM_FIXED,
51                                          ln * sizeof(TCHAR));
52         if ((*lpTarget) != NULL)
53         {
54             INT Ret;
55             if (!(Ret = LoadString(hInst, uID, *lpTarget, ln)))
56             {
57                 LocalFree((HLOCAL)(*lpTarget));
58             }
59             return Ret;
60         }
61     }
62     return 0;
63 }
64