xref: /reactos/base/applications/mmc/misc.c (revision 09dde2cf)
1 /*
2  * ReactOS Management Console
3  * Copyright (C) 2006 - 2007 Thomas Weidenmueller
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #include "precomp.h"
21 
22 INT
23 LengthOfStrResource(IN HINSTANCE hInst,
24                     IN UINT uID)
25 {
26     HRSRC hrSrc;
27     HGLOBAL hRes;
28     LPWSTR lpName, lpStr;
29 
30     if (hInst == NULL)
31     {
32         return -1;
33     }
34 
35     /* There are always blocks of 16 strings */
36     lpName = (LPWSTR)MAKEINTRESOURCE((uID >> 4) + 1);
37 
38     /* Find the string table block */
39     if ((hrSrc = FindResourceW(hInst, lpName, (LPWSTR)RT_STRING)) &&
40         (hRes = LoadResource(hInst, hrSrc)) &&
41         (lpStr = LockResource(hRes)))
42     {
43         UINT x;
44 
45         /* Find the string we're looking for */
46         uID &= 0xF; /* position in the block, same as % 16 */
47         for (x = 0; x < uID; x++)
48         {
49             lpStr += (*lpStr) + 1;
50         }
51 
52         /* Found the string */
53         return (int)(*lpStr);
54     }
55     return -1;
56 }
57 
58 INT
59 AllocAndLoadString(OUT LPTSTR *lpTarget,
60                    IN HINSTANCE hInst,
61                    IN UINT uID)
62 {
63     INT ln;
64 
65     ln = LengthOfStrResource(hInst,
66                              uID);
67     if (ln++ > 0)
68     {
69         (*lpTarget) = (LPWSTR)LocalAlloc(LMEM_FIXED,
70                                          ln * sizeof(TCHAR));
71         if ((*lpTarget) != NULL)
72         {
73             INT Ret;
74             if (!(Ret = LoadString(hInst, uID, *lpTarget, ln)))
75             {
76                 LocalFree((HLOCAL)(*lpTarget));
77             }
78             return Ret;
79         }
80     }
81     return 0;
82 }
83 
84 DWORD
85 LoadAndFormatString(IN HINSTANCE hInstance,
86                     IN UINT uID,
87                     OUT LPTSTR *lpTarget,
88                     ...)
89 {
90     DWORD Ret = 0;
91     LPWSTR lpFormat;
92     va_list lArgs;
93 
94     if (AllocAndLoadString(&lpFormat,
95                            hInstance,
96                            uID) != 0)
97     {
98         va_start(lArgs, lpTarget);
99         /* let's use FormatMessage to format it because it has the ability to allocate
100            memory automatically */
101         Ret = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
102                             lpFormat,
103                             0,
104                             0,
105                             (LPTSTR)lpTarget,
106                             0,
107                             &lArgs);
108         va_end(lArgs);
109 
110         LocalFree((HLOCAL)lpFormat);
111     }
112 
113     return Ret;
114 }
115