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