[NTDLL]
[reactos.git] / reactos / base / applications / mscutils / devmgmt / misc.c
1 /*
2 * PROJECT: ReactOS Device Managment
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/system/devmgmt/misc.c
5 * PURPOSE: miscallanous functions
6 * COPYRIGHT: Copyright 2006 Ged Murphy <gedmurphy@gmail.com>
7 *
8 */
9
10 #include "precomp.h"
11
12 static INT
13 LengthOfStrResource(IN HINSTANCE hInst,
14 IN UINT uID)
15 {
16 HRSRC hrSrc;
17 HGLOBAL hRes;
18 LPWSTR lpName, lpStr;
19
20 if (hInst == NULL)
21 {
22 return -1;
23 }
24
25 /* There are always blocks of 16 strings */
26 lpName = (LPWSTR)MAKEINTRESOURCE((uID >> 4) + 1);
27
28 /* Find the string table block */
29 if ((hrSrc = FindResourceW(hInst, lpName, (LPWSTR)RT_STRING)) &&
30 (hRes = LoadResource(hInst, hrSrc)) &&
31 (lpStr = (LPWSTR)LockResource(hRes)))
32 {
33 UINT x;
34
35 /* Find the string we're looking for */
36 uID &= 0xF; /* position in the block, same as % 16 */
37 for (x = 0; x < uID; x++)
38 {
39 lpStr += (*lpStr) + 1;
40 }
41
42 /* Found the string */
43 return (int)(*lpStr);
44 }
45 return -1;
46 }
47
48 INT
49 AllocAndLoadString(OUT LPTSTR *lpTarget,
50 IN HINSTANCE hInst,
51 IN UINT uID)
52 {
53 INT ln;
54
55 ln = LengthOfStrResource(hInst,
56 uID);
57 if (ln++ > 0)
58 {
59 (*lpTarget) = (LPTSTR)LocalAlloc(LMEM_FIXED,
60 ln * sizeof(TCHAR));
61 if ((*lpTarget) != NULL)
62 {
63 INT Ret;
64 if (!(Ret = LoadString(hInst, uID, *lpTarget, ln)))
65 {
66 LocalFree((HLOCAL)(*lpTarget));
67 }
68 return Ret;
69 }
70 }
71 return 0;
72 }
73
74 DWORD
75 LoadAndFormatString(IN HINSTANCE hInstance,
76 IN UINT uID,
77 OUT LPTSTR *lpTarget,
78 ...)
79 {
80 DWORD Ret = 0;
81 LPTSTR lpFormat;
82 va_list lArgs;
83
84 if (AllocAndLoadString(&lpFormat,
85 hInstance,
86 uID) > 0)
87 {
88 va_start(lArgs, lpTarget);
89 /* let's use FormatMessage to format it because it has the ability to allocate
90 memory automatically */
91 Ret = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
92 lpFormat,
93 0,
94 0,
95 (LPTSTR)lpTarget,
96 0,
97 &lArgs);
98 va_end(lArgs);
99
100 LocalFree((HLOCAL)lpFormat);
101 }
102
103 return Ret;
104 }
105
106 BOOL
107 StatusBarLoadAndFormatString(IN HWND hStatusBar,
108 IN INT PartId,
109 IN HINSTANCE hInstance,
110 IN UINT uID,
111 ...)
112 {
113 BOOL Ret = FALSE;
114 LPTSTR lpFormat, lpStr;
115 va_list lArgs;
116
117 if (AllocAndLoadString(&lpFormat,
118 hInstance,
119 uID) > 0)
120 {
121 va_start(lArgs, uID);
122 /* let's use FormatMessage to format it because it has the ability to allocate
123 memory automatically */
124 Ret = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
125 lpFormat,
126 0,
127 0,
128 (LPTSTR)&lpStr,
129 0,
130 &lArgs);
131 va_end(lArgs);
132
133 if (lpStr != NULL)
134 {
135 Ret = (BOOL)SendMessage(hStatusBar,
136 SB_SETTEXT,
137 (WPARAM)PartId,
138 (LPARAM)lpStr);
139 LocalFree((HLOCAL)lpStr);
140 }
141
142 LocalFree((HLOCAL)lpFormat);
143 }
144
145 return Ret;
146 }
147
148 BOOL
149 StatusBarLoadString(IN HWND hStatusBar,
150 IN INT PartId,
151 IN HINSTANCE hInstance,
152 IN UINT uID)
153 {
154 BOOL Ret = FALSE;
155 LPTSTR lpStr;
156
157 if (AllocAndLoadString(&lpStr,
158 hInstance,
159 uID) > 0)
160 {
161 Ret = (BOOL)SendMessage(hStatusBar,
162 SB_SETTEXT,
163 (WPARAM)PartId,
164 (LPARAM)lpStr);
165 LocalFree((HLOCAL)lpStr);
166 }
167
168 return Ret;
169 }
170
171
172 INT
173 GetTextFromEdit(OUT LPTSTR lpString,
174 IN HWND hDlg,
175 IN UINT Res)
176 {
177 INT len = GetWindowTextLength(GetDlgItem(hDlg, Res));
178 if(len > 0)
179 {
180 GetDlgItemText(hDlg,
181 Res,
182 lpString,
183 len + 1);
184 }
185 else
186 lpString = NULL;
187
188 return len;
189 }
190
191
192 HIMAGELIST
193 InitImageList(UINT StartResource,
194 UINT EndResource,
195 UINT Width,
196 UINT Height)
197 {
198 HBITMAP hBitmap;
199 HIMAGELIST hImageList;
200 UINT i;
201 INT Ret;
202
203 /* Create the toolbar icon image list */
204 hImageList = ImageList_Create(Width,
205 Height,
206 ILC_MASK | ILC_COLOR24,
207 EndResource - StartResource,
208 0);
209 if (hImageList == NULL)
210 return NULL;
211
212 /* Add all icons to the image list */
213 for (i = StartResource; i <= EndResource; i++)
214 {
215 hBitmap = (HBITMAP)LoadImage(hInstance,
216 MAKEINTRESOURCE(i),
217 IMAGE_BITMAP,
218 Width,
219 Height,
220 LR_LOADTRANSPARENT);
221 if (hBitmap == NULL)
222 return NULL;
223
224 Ret = ImageList_AddMasked(hImageList,
225 hBitmap,
226 RGB(255, 0, 128));
227 if (Ret == -1)
228 return NULL;
229
230 DeleteObject(hBitmap);
231 }
232
233 return hImageList;
234 }
235
236
237 VOID GetError(VOID)
238 {
239 LPVOID lpMsgBuf;
240
241 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
242 FORMAT_MESSAGE_FROM_SYSTEM |
243 FORMAT_MESSAGE_IGNORE_INSERTS,
244 NULL,
245 GetLastError(),
246 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
247 (LPTSTR) &lpMsgBuf,
248 0,
249 NULL );
250
251 MessageBox(NULL, lpMsgBuf, _T("Error!"), MB_OK | MB_ICONERROR);
252
253 LocalFree(lpMsgBuf);
254 }
255
256
257 VOID DisplayString(LPTSTR Msg)
258 {
259 MessageBox(NULL, Msg, _T("Note!"), MB_ICONEXCLAMATION|MB_OK);
260 }