[SHELL32] Fix Control_RunDLLW (#5400)
[reactos.git] / dll / win32 / batt / batt.c
1 /*
2 * PROJECT: ReactOS system libraries
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll\win32\batt\batt.c
5 * PURPOSE: Battery Class installers
6 * PROGRAMMERS: Copyright 2010 Eric Kohl
7 */
8
9
10 #define WIN32_NO_STATUS
11 #include <stdarg.h>
12 #include <windef.h>
13 #include <winbase.h>
14 #include <winreg.h>
15 #include <winuser.h>
16 #include <setupapi.h>
17
18 #include <initguid.h>
19 #include <devguid.h>
20
21 #define NDEBUG
22 #include <debug.h>
23
24 static
25 DWORD
26 InstallCompositeBattery(
27 _In_ HDEVINFO DeviceInfoSet,
28 _In_opt_ PSP_DEVINFO_DATA DeviceInfoData,
29 _In_ PSP_DEVINSTALL_PARAMS_W DeviceInstallParams)
30 {
31 WCHAR szDeviceId[32];
32 SP_DRVINFO_DATA DriverInfoData;
33 HDEVINFO NewDeviceInfoSet = INVALID_HANDLE_VALUE;
34 PSP_DEVINFO_DATA NewDeviceInfoData = NULL;
35 BOOL bDeviceRegistered = FALSE, bHaveDriverInfoList = FALSE;
36 DWORD dwError = ERROR_SUCCESS;
37
38 DPRINT("InstallCompositeBattery(%p %p %p)\n",
39 DeviceInfoSet, DeviceInfoData, DeviceInstallParams);
40
41 NewDeviceInfoSet = SetupDiCreateDeviceInfoList(&GUID_DEVCLASS_SYSTEM,
42 DeviceInstallParams->hwndParent);
43 if (NewDeviceInfoSet == INVALID_HANDLE_VALUE)
44 {
45 DPRINT1("SetupDiCreateDeviceInfoList() failed (Error %lu)\n", GetLastError());
46 return GetLastError();
47 }
48
49 NewDeviceInfoData = HeapAlloc(GetProcessHeap(),
50 HEAP_ZERO_MEMORY,
51 sizeof(SP_DEVINFO_DATA));
52 if (NewDeviceInfoData == NULL)
53 {
54 dwError = ERROR_OUTOFMEMORY;
55 goto done;
56 }
57
58 NewDeviceInfoData->cbSize = sizeof(SP_DEVINFO_DATA);
59 if (!SetupDiCreateDeviceInfoW(NewDeviceInfoSet,
60 L"Root\\COMPOSITE_BATTERY\\0000",
61 &GUID_DEVCLASS_SYSTEM,
62 NULL,
63 DeviceInstallParams->hwndParent,
64 0,
65 NewDeviceInfoData))
66 {
67 dwError = GetLastError();
68 if (dwError == ERROR_DEVINST_ALREADY_EXISTS)
69 {
70 dwError = ERROR_SUCCESS;
71 goto done;
72 }
73
74 DPRINT1("SetupDiCreateDeviceInfoW() failed (Error %lu 0x%08lx)\n", dwError, dwError);
75 goto done;
76 }
77
78 if (!SetupDiRegisterDeviceInfo(NewDeviceInfoSet,
79 NewDeviceInfoData,
80 0,
81 NULL,
82 NULL,
83 NULL))
84 {
85 dwError = GetLastError();
86 DPRINT1("SetupDiRegisterDeviceInfo() failed (Error %lu 0x%08lx)\n", dwError, dwError);
87 goto done;
88 }
89
90 bDeviceRegistered = TRUE;
91
92 ZeroMemory(szDeviceId, sizeof(szDeviceId));
93 wcscpy(szDeviceId, L"COMPOSITE_BATTERY");
94
95 if (!SetupDiSetDeviceRegistryPropertyW(NewDeviceInfoSet,
96 NewDeviceInfoData,
97 SPDRP_HARDWAREID,
98 (PBYTE)szDeviceId,
99 (wcslen(szDeviceId) + 2) * sizeof(WCHAR)))
100 {
101 dwError = GetLastError();
102 DPRINT1("SetupDiSetDeviceRegistryPropertyW() failed (Error %lu 0x%08lx)\n", dwError, dwError);
103 goto done;
104 }
105
106 if (!SetupDiBuildDriverInfoList(NewDeviceInfoSet,
107 NewDeviceInfoData,
108 SPDIT_COMPATDRIVER))
109 {
110 dwError = GetLastError();
111 DPRINT1("SetupDiBuildDriverInfoList() failed (Error %lu 0x%08lx)\n", dwError, dwError);
112 goto done;
113 }
114
115 bHaveDriverInfoList = TRUE;
116
117 DriverInfoData.cbSize = sizeof(SP_DRVINFO_DATA);
118 if (!SetupDiEnumDriverInfo(NewDeviceInfoSet,
119 NewDeviceInfoData,
120 SPDIT_COMPATDRIVER,
121 0,
122 &DriverInfoData))
123 {
124 dwError = GetLastError();
125 DPRINT1("SetupDiEnumDriverInfo() failed (Error %lu 0x%08lx)\n", dwError, dwError);
126 goto done;
127 }
128
129 if (!SetupDiSetSelectedDriver(NewDeviceInfoSet,
130 NewDeviceInfoData,
131 &DriverInfoData))
132 {
133 dwError = GetLastError();
134 DPRINT1("SetupDiSetSelectedDriver() failed (Error %lu 0x%08lx)\n", dwError, dwError);
135 goto done;
136 }
137
138 if (!SetupDiInstallDevice(NewDeviceInfoSet,
139 NewDeviceInfoData))
140 {
141 dwError = GetLastError();
142 DPRINT1("SetupDiInstallDevice() failed (Error %lu 0x%08lx)\n", dwError, dwError);
143 goto done;
144 }
145
146 dwError = ERROR_SUCCESS;
147
148 done:
149 if (bHaveDriverInfoList)
150 SetupDiDestroyDriverInfoList(NewDeviceInfoSet,
151 NewDeviceInfoData,
152 SPDIT_COMPATDRIVER);
153
154 if (bDeviceRegistered)
155 SetupDiDeleteDeviceInfo(NewDeviceInfoSet,
156 NewDeviceInfoData);
157
158 if (NewDeviceInfoData != NULL)
159 HeapFree(GetProcessHeap(), 0, NewDeviceInfoData);
160
161 if (NewDeviceInfoSet != INVALID_HANDLE_VALUE)
162 SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
163
164 return dwError;
165 }
166
167
168 DWORD
169 WINAPI
170 BatteryClassInstall(
171 _In_ DI_FUNCTION InstallFunction,
172 _In_ HDEVINFO DeviceInfoSet,
173 _In_opt_ PSP_DEVINFO_DATA DeviceInfoData)
174 {
175 SP_DEVINSTALL_PARAMS_W DeviceInstallParams;
176 DWORD dwError;
177
178 DPRINT("BatteryClassInstall(%u %p %p)\n",
179 InstallFunction, DeviceInfoSet, DeviceInfoData);
180
181 if (InstallFunction != DIF_INSTALLDEVICE)
182 return ERROR_DI_DO_DEFAULT;
183
184 DeviceInstallParams.cbSize = sizeof(DeviceInstallParams);
185 if (!SetupDiGetDeviceInstallParamsW(DeviceInfoSet,
186 DeviceInfoData,
187 &DeviceInstallParams))
188 {
189 DPRINT1("SetupDiGetDeviceInstallParamsW() failed (Error %lu)\n", GetLastError());
190 return GetLastError();
191 }
192
193 /* Install the composite battery device */
194 dwError = InstallCompositeBattery(DeviceInfoSet,
195 DeviceInfoData,
196 &DeviceInstallParams);
197 if (dwError == ERROR_SUCCESS)
198 {
199 /* Install the battery device */
200 dwError = ERROR_DI_DO_DEFAULT;
201 }
202
203 return dwError;
204 }
205
206
207 DWORD
208 WINAPI
209 BatteryClassCoInstaller(IN DI_FUNCTION InstallFunction,
210 IN HDEVINFO DeviceInfoSet,
211 IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
212 IN OUT PCOINSTALLER_CONTEXT_DATA Context)
213 {
214 switch (InstallFunction)
215 {
216 default:
217 DPRINT("Install function %u ignored\n", InstallFunction);
218 return ERROR_DI_DO_DEFAULT;
219 }
220 }
221
222
223 BOOL
224 WINAPI
225 DllMain(
226 _In_ HINSTANCE hinstDll,
227 _In_ DWORD dwReason,
228 _In_ LPVOID reserved)
229 {
230 switch (dwReason)
231 {
232 case DLL_PROCESS_ATTACH:
233 DisableThreadLibraryCalls(hinstDll);
234 break;
235
236 case DLL_PROCESS_DETACH:
237 break;
238 }
239
240 return TRUE;
241 }
242
243 /* EOF */