66fcfa88ae8639df8c69a977979ad026d92fc03e
[reactos.git] / reactos / lib / devmgr / misc.c
1 /*
2 * ReactOS Device Manager Applet
3 * Copyright (C) 2004 - 2005 ReactOS Team
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 /* $Id: devmgr.c 12852 2005-01-06 13:58:04Z mf $
20 *
21 * PROJECT: ReactOS devmgr.dll
22 * FILE: lib/devmgr/misc.c
23 * PURPOSE: ReactOS Device Manager
24 * PROGRAMMER: Thomas Weidenmueller <w3seek@reactos.com>
25 * UPDATE HISTORY:
26 * 2005/11/24 Created
27 */
28 #include <precomp.h>
29
30 HINSTANCE hDllInstance = NULL;
31
32 static INT
33 LengthOfStrResource(IN HINSTANCE hInst,
34 IN UINT uID)
35 {
36 HRSRC hrSrc;
37 HGLOBAL hRes;
38 LPWSTR lpName, lpStr;
39
40 if (hInst == NULL)
41 {
42 return -1;
43 }
44
45 /* There are always blocks of 16 strings */
46 lpName = (LPWSTR)MAKEINTRESOURCE((uID >> 4) + 1);
47
48 /* Find the string table block */
49 if ((hrSrc = FindResourceW(hInst, lpName, (LPWSTR)RT_STRING)) &&
50 (hRes = LoadResource(hInst, hrSrc)) &&
51 (lpStr = LockResource(hRes)))
52 {
53 UINT x;
54
55 /* Find the string we're looking for */
56 uID &= 0xF; /* position in the block, same as % 16 */
57 for (x = 0; x < uID; x++)
58 {
59 lpStr += (*lpStr) + 1;
60 }
61
62 /* Found the string */
63 return (int)(*lpStr);
64 }
65 return -1;
66 }
67
68 static INT
69 AllocAndLoadString(OUT LPWSTR *lpTarget,
70 IN HINSTANCE hInst,
71 IN UINT uID)
72 {
73 INT ln;
74
75 ln = LengthOfStrResource(hInst,
76 uID);
77 if (ln++ > 0)
78 {
79 (*lpTarget) = (LPWSTR)LocalAlloc(LMEM_FIXED,
80 ln * sizeof(WCHAR));
81 if ((*lpTarget) != NULL)
82 {
83 INT Ret;
84 if (!(Ret = LoadStringW(hInst, uID, *lpTarget, ln)))
85 {
86 LocalFree((HLOCAL)(*lpTarget));
87 }
88 return Ret;
89 }
90 }
91 return 0;
92 }
93
94 DWORD
95 LoadAndFormatString(IN HINSTANCE hInstance,
96 IN UINT uID,
97 OUT LPWSTR *lpTarget,
98 ...)
99 {
100 DWORD Ret = 0;
101 LPWSTR lpFormat;
102 va_list lArgs;
103
104 if (AllocAndLoadString(&lpFormat,
105 hInstance,
106 uID) > 0)
107 {
108 va_start(lArgs, lpTarget);
109 /* let's use FormatMessage to format it because it has the ability to allocate
110 memory automatically */
111 Ret = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
112 lpFormat,
113 0,
114 0,
115 (LPWSTR)lpTarget,
116 0,
117 &lArgs);
118 va_end(lArgs);
119
120 LocalFree((HLOCAL)lpFormat);
121 }
122
123 return Ret;
124 }
125
126 LPARAM
127 ListViewGetSelectedItemData(IN HWND hwnd)
128 {
129 int Index;
130
131 Index = ListView_GetNextItem(hwnd,
132 -1,
133 LVNI_SELECTED);
134 if (Index != -1)
135 {
136 LVITEM li;
137
138 li.mask = LVIF_PARAM;
139 li.iItem = Index;
140 li.iSubItem = 0;
141
142 if (ListView_GetItem(hwnd,
143 &li))
144 {
145 return li.lParam;
146 }
147 }
148
149 return 0;
150 }
151
152 BOOL
153 STDCALL
154 DllMain(IN HINSTANCE hinstDLL,
155 IN DWORD dwReason,
156 IN LPVOID lpvReserved)
157 {
158 switch (dwReason)
159 {
160 case DLL_PROCESS_ATTACH:
161 DisableThreadLibraryCalls(hinstDLL);
162 hDllInstance = hinstDLL;
163 break;
164 }
165
166 return TRUE;
167 }