[MSI]
[reactos.git] / reactos / dll / win32 / msi / registry.c
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
3 *
4 * Copyright 2005 Mike McCormack for CodeWeavers
5 * Copyright 2005 Aric Stewart for CodeWeavers
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include <stdarg.h>
23
24 #define COBJMACROS
25 #define NONAMELESSUNION
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winreg.h"
30 #include "winnls.h"
31 #include "shlwapi.h"
32 #include "wine/debug.h"
33 #include "msi.h"
34 #include "msipriv.h"
35 #include "wincrypt.h"
36 #include "wine/unicode.h"
37 #include "winver.h"
38 #include "winuser.h"
39 #include "sddl.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(msi);
42
43 /*
44 * This module will be all the helper functions for registry access by the
45 * installer bits.
46 */
47
48 static const WCHAR szUserDataFeatures_fmt[] = {
49 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
50 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
51 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
52 '%','s','\\','P','r','o','d','u','c','t','s','\\','%','s','\\','F','e','a','t','u','r','e','s',0};
53
54 static const WCHAR szUserDataComp_fmt[] = {
55 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
56 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
57 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
58 '%','s','\\','C','o','m','p','o','n','e','n','t','s','\\','%','s',0};
59
60 static const WCHAR szUserDataComponents_fmt[] = {
61 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
62 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
63 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
64 '%','s','\\','C','o','m','p','o','n','e','n','t','s',0};
65
66 static const WCHAR szUserDataProd_fmt[] = {
67 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
68 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
69 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
70 '%','s','\\','P','r','o','d','u','c','t','s','\\','%','s',0};
71
72 static const WCHAR szUserDataProducts_fmt[] = {
73 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
74 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
75 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
76 '%','s','\\','P','r','o','d','u','c','t','s',0};
77
78 static const WCHAR szUserDataPatch_fmt[] = {
79 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
80 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
81 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
82 '%','s','\\','P','a','t','c','h','e','s','\\','%','s',0};
83
84 static const WCHAR szUserDataPatches_fmt[] = {
85 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
86 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
87 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
88 '%','s','\\','P','a','t','c','h','e','s',0};
89
90 static const WCHAR szUserDataProductPatches_fmt[] = {
91 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
92 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
93 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
94 '%','s','\\','P','r','o','d','u','c','t','s','\\','%','s','\\','P','a','t','c','h','e','s',0};
95
96 static const WCHAR szInstallProperties_fmt[] = {
97 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
98 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
99 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
100 '%','s','\\','P','r','o','d','u','c','t','s','\\','%','s','\\',
101 'I','n','s','t','a','l','l','P','r','o','p','e','r','t','i','e','s',0};
102
103 static const WCHAR szInstaller_LocalManaged_fmt[] = {
104 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
105 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
106 'I','n','s','t','a','l','l','e','r','\\','M','a','n','a','g','e','d','\\','%','s','\\',
107 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
108
109 static const WCHAR szInstaller_LocalManagedProd_fmt[] = {
110 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
111 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
112 'I','n','s','t','a','l','l','e','r','\\','M','a','n','a','g','e','d','\\','%','s','\\',
113 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s','\\','%','s',0};
114
115 static const WCHAR szInstaller_LocalManagedFeat_fmt[] = {
116 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
117 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
118 'I','n','s','t','a','l','l','e','r','\\','M','a','n','a','g','e','d','\\','%','s','\\',
119 'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s','\\','%','s',0};
120
121 static const WCHAR szInstaller_Products[] = {
122 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
123 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
124 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
125
126 static const WCHAR szInstaller_Patches[] = {
127 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
128 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
129 'I','n','s','t','a','l','l','e','r','\\','P','a','t','c','h','e','s',0};
130
131 static const WCHAR szInstaller_Components[] = {
132 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
133 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
134 'I','n','s','t','a','l','l','e','r','\\','C','o','m','p','o','n','e','n','t','s',0};
135
136 static const WCHAR szInstaller_LocalClassesProducts[] = {
137 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
138 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
139
140 static const WCHAR szInstaller_LocalClassesFeatures[] = {
141 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
142 'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s',0};
143
144 static const WCHAR szInstaller_LocalClassesProd[] = {
145 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
146 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s','\\',0};
147
148 static const WCHAR szInstaller_LocalClassesFeat[] = {
149 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
150 'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s','\\',0};
151
152 static const WCHAR szInstaller_ClassesUpgradeCode[] = {
153 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
154 'I','n','s','t','a','l','l','e','r','\\','U','p','g','r','a','d','e','C','o','d','e','s','\\',0};
155
156 static const WCHAR szInstaller_ClassesUpgradeCodes[] = {
157 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
158 'I','n','s','t','a','l','l','e','r','\\','U','p','g','r','a','d','e','C','o','d','e','s',0};
159
160 static const WCHAR szInstaller_Features[] = {
161 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
162 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
163 'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s','\\',0};
164
165 static const WCHAR szInstaller_UpgradeCodes[] = {
166 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
167 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
168 'I','n','s','t','a','l','l','e','r','\\','U','p','g','r','a','d','e','C','o','d','e','s','\\',0};
169
170 static const WCHAR szInstaller_UserUpgradeCodes[] = {
171 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
172 'I','n','s','t','a','l','l','e','r','\\','U','p','g','r','a','d','e','C','o','d','e','s','\\',0};
173
174 static const WCHAR szUninstall[] = {
175 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
176 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
177 'U','n','i','n','s','t','a','l','l','\\',0};
178
179 static const WCHAR szUninstall_32node[] = {
180 'S','o','f','t','w','a','r','e','\\','W','o','w','6','4','3','2','N','o','d','e','\\',
181 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
182 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','U','n','i','n','s','t','a','l','l','\\',0};
183
184 static const WCHAR szUserComponents[] = {
185 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
186 'I','n','s','t','a','l','l','e','r','\\','C','o','m','p','o','n','e','n','t','s','\\',0};
187
188 static const WCHAR szUserFeatures[] = {
189 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
190 'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s','\\',0};
191
192 static const WCHAR szUserProducts[] = {
193 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
194 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s','\\',0};
195
196 static const WCHAR szUserPatches[] = {
197 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
198 'I','n','s','t','a','l','l','e','r','\\','P','a','t','c','h','e','s','\\',0};
199
200 BOOL unsquash_guid(LPCWSTR in, LPWSTR out)
201 {
202 DWORD i,n=0;
203
204 if (lstrlenW(in) != 32)
205 return FALSE;
206
207 out[n++]='{';
208 for(i=0; i<8; i++)
209 out[n++] = in[7-i];
210 out[n++]='-';
211 for(i=0; i<4; i++)
212 out[n++] = in[11-i];
213 out[n++]='-';
214 for(i=0; i<4; i++)
215 out[n++] = in[15-i];
216 out[n++]='-';
217 for(i=0; i<2; i++)
218 {
219 out[n++] = in[17+i*2];
220 out[n++] = in[16+i*2];
221 }
222 out[n++]='-';
223 for( ; i<8; i++)
224 {
225 out[n++] = in[17+i*2];
226 out[n++] = in[16+i*2];
227 }
228 out[n++]='}';
229 out[n]=0;
230 return TRUE;
231 }
232
233 BOOL squash_guid(LPCWSTR in, LPWSTR out)
234 {
235 DWORD i,n=1;
236 GUID guid;
237
238 out[0] = 0;
239
240 if (FAILED(CLSIDFromString((LPCOLESTR)in, &guid)))
241 return FALSE;
242
243 for(i=0; i<8; i++)
244 out[7-i] = in[n++];
245 n++;
246 for(i=0; i<4; i++)
247 out[11-i] = in[n++];
248 n++;
249 for(i=0; i<4; i++)
250 out[15-i] = in[n++];
251 n++;
252 for(i=0; i<2; i++)
253 {
254 out[17+i*2] = in[n++];
255 out[16+i*2] = in[n++];
256 }
257 n++;
258 for( ; i<8; i++)
259 {
260 out[17+i*2] = in[n++];
261 out[16+i*2] = in[n++];
262 }
263 out[32]=0;
264 return TRUE;
265 }
266
267
268 /* tables for encoding and decoding base85 */
269 static const unsigned char table_dec85[0x80] = {
270 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
271 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
272 0xff,0x00,0xff,0xff,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0xff,
273 0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0xff,0xff,0xff,0x16,0xff,0x17,
274 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
275 0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0xff,0x34,0x35,0x36,
276 0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x42,0x43,0x44,0x45,0x46,
277 0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0xff,0x53,0x54,0xff,
278 };
279
280 static const char table_enc85[] =
281 "!$%&'()*+,-.0123456789=?@ABCDEFGHIJKLMNO"
282 "PQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwx"
283 "yz{}~";
284
285 /*
286 * Converts a base85 encoded guid into a GUID pointer
287 * Base85 encoded GUIDs should be 20 characters long.
288 *
289 * returns TRUE if successful, FALSE if not
290 */
291 BOOL decode_base85_guid( LPCWSTR str, GUID *guid )
292 {
293 DWORD i, val = 0, base = 1, *p;
294
295 if (!str)
296 return FALSE;
297
298 p = (DWORD*) guid;
299 for( i=0; i<20; i++ )
300 {
301 if( (i%5) == 0 )
302 {
303 val = 0;
304 base = 1;
305 }
306 val += table_dec85[str[i]] * base;
307 if( str[i] >= 0x80 )
308 return FALSE;
309 if( table_dec85[str[i]] == 0xff )
310 return FALSE;
311 if( (i%5) == 4 )
312 p[i/5] = val;
313 base *= 85;
314 }
315 return TRUE;
316 }
317
318 /*
319 * Encodes a base85 guid given a GUID pointer
320 * Caller should provide a 21 character buffer for the encoded string.
321 *
322 * returns TRUE if successful, FALSE if not
323 */
324 BOOL encode_base85_guid( GUID *guid, LPWSTR str )
325 {
326 unsigned int x, *p, i;
327
328 p = (unsigned int*) guid;
329 for( i=0; i<4; i++ )
330 {
331 x = p[i];
332 *str++ = table_enc85[x%85];
333 x = x/85;
334 *str++ = table_enc85[x%85];
335 x = x/85;
336 *str++ = table_enc85[x%85];
337 x = x/85;
338 *str++ = table_enc85[x%85];
339 x = x/85;
340 *str++ = table_enc85[x%85];
341 }
342 *str = 0;
343
344 return TRUE;
345 }
346
347 DWORD msi_version_str_to_dword(LPCWSTR p)
348 {
349 DWORD major, minor = 0, build = 0, version = 0;
350
351 if (!p)
352 return version;
353
354 major = atoiW(p);
355
356 p = strchrW(p, '.');
357 if (p)
358 {
359 minor = atoiW(p+1);
360 p = strchrW(p+1, '.');
361 if (p)
362 build = atoiW(p+1);
363 }
364
365 return MAKELONG(build, MAKEWORD(minor, major));
366 }
367
368 LONG msi_reg_set_val_str( HKEY hkey, LPCWSTR name, LPCWSTR value )
369 {
370 DWORD len;
371 if (!value) value = szEmpty;
372 len = (lstrlenW(value) + 1) * sizeof (WCHAR);
373 return RegSetValueExW( hkey, name, 0, REG_SZ, (const BYTE *)value, len );
374 }
375
376 LONG msi_reg_set_val_multi_str( HKEY hkey, LPCWSTR name, LPCWSTR value )
377 {
378 LPCWSTR p = value;
379 while (*p) p += lstrlenW(p) + 1;
380 return RegSetValueExW( hkey, name, 0, REG_MULTI_SZ,
381 (const BYTE *)value, (p + 1 - value) * sizeof(WCHAR) );
382 }
383
384 LONG msi_reg_set_val_dword( HKEY hkey, LPCWSTR name, DWORD val )
385 {
386 return RegSetValueExW( hkey, name, 0, REG_DWORD, (LPBYTE)&val, sizeof (DWORD) );
387 }
388
389 LONG msi_reg_set_subkey_val( HKEY hkey, LPCWSTR path, LPCWSTR name, LPCWSTR val )
390 {
391 HKEY hsubkey = 0;
392 LONG r;
393
394 r = RegCreateKeyW( hkey, path, &hsubkey );
395 if (r != ERROR_SUCCESS)
396 return r;
397 r = msi_reg_set_val_str( hsubkey, name, val );
398 RegCloseKey( hsubkey );
399 return r;
400 }
401
402 LPWSTR msi_reg_get_val_str( HKEY hkey, LPCWSTR name )
403 {
404 DWORD len = 0;
405 LPWSTR val;
406 LONG r;
407
408 r = RegQueryValueExW(hkey, name, NULL, NULL, NULL, &len);
409 if (r != ERROR_SUCCESS)
410 return NULL;
411
412 len += sizeof (WCHAR);
413 val = msi_alloc( len );
414 if (!val)
415 return NULL;
416 val[0] = 0;
417 RegQueryValueExW(hkey, name, NULL, NULL, (LPBYTE) val, &len);
418 return val;
419 }
420
421 BOOL msi_reg_get_val_dword( HKEY hkey, LPCWSTR name, DWORD *val)
422 {
423 DWORD type, len = sizeof (DWORD);
424 LONG r = RegQueryValueExW(hkey, name, NULL, &type, (LPBYTE) val, &len);
425 return r == ERROR_SUCCESS && type == REG_DWORD;
426 }
427
428 static WCHAR *get_user_sid(void)
429 {
430 HANDLE token;
431 DWORD size = 256;
432 TOKEN_USER *user;
433 WCHAR *ret;
434
435 if (!OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &token )) return NULL;
436 if (!(user = msi_alloc( size )))
437 {
438 CloseHandle( token );
439 return NULL;
440 }
441 if (!GetTokenInformation( token, TokenUser, user, size, &size ))
442 {
443 msi_free( user );
444 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER || !(user = msi_alloc( size )))
445 {
446 CloseHandle( token );
447 return NULL;
448 }
449 GetTokenInformation( token, TokenUser, user, size, &size );
450 }
451 CloseHandle( token );
452 if (!ConvertSidToStringSidW( user->User.Sid, &ret ))
453 {
454 msi_free( user );
455 return NULL;
456 }
457 msi_free( user );
458 return ret;
459 }
460
461 UINT MSIREG_OpenUninstallKey(const WCHAR *product, enum platform platform, HKEY *key, BOOL create)
462 {
463 WCHAR keypath[0x200];
464
465 TRACE("%s\n", debugstr_w(product));
466
467 if (is_64bit && platform == PLATFORM_INTEL)
468 {
469 strcpyW(keypath, szUninstall_32node);
470 strcatW(keypath, product);
471 }
472 else
473 {
474 strcpyW(keypath, szUninstall);
475 strcatW(keypath, product);
476 }
477 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, KEY_ALL_ACCESS, NULL, key, NULL);
478 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, KEY_ALL_ACCESS, key);
479 }
480
481 UINT MSIREG_DeleteUninstallKey(const WCHAR *product, enum platform platform)
482 {
483 WCHAR keypath[0x200];
484
485 TRACE("%s\n", debugstr_w(product));
486
487 if (is_64bit && platform == PLATFORM_INTEL)
488 {
489 strcpyW(keypath, szUninstall_32node);
490 strcatW(keypath, product);
491 }
492 else
493 {
494 strcpyW(keypath, szUninstall);
495 strcatW(keypath, product);
496 }
497 return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
498 }
499
500 UINT MSIREG_OpenProductKey(LPCWSTR szProduct, LPCWSTR szUserSid, MSIINSTALLCONTEXT context, HKEY *key, BOOL create)
501 {
502 LPWSTR usersid = NULL;
503 HKEY root = HKEY_LOCAL_MACHINE;
504 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
505 WCHAR squished_pc[GUID_SIZE], keypath[MAX_PATH];
506
507 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
508 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
509
510 if (context == MSIINSTALLCONTEXT_MACHINE)
511 {
512 strcpyW(keypath, szInstaller_LocalClassesProd);
513 strcatW(keypath, squished_pc);
514 }
515 else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
516 {
517 root = HKEY_CURRENT_USER;
518 strcpyW(keypath, szUserProducts);
519 strcatW(keypath, squished_pc);
520 }
521 else
522 {
523 if (!szUserSid)
524 {
525 if (!(usersid = get_user_sid()))
526 {
527 ERR("Failed to retrieve user SID\n");
528 return ERROR_FUNCTION_FAILED;
529 }
530 szUserSid = usersid;
531 }
532 sprintfW(keypath, szInstaller_LocalManagedProd_fmt, szUserSid, squished_pc);
533 LocalFree(usersid);
534 }
535 if (create) return RegCreateKeyExW(root, keypath, 0, NULL, 0, access, NULL, key, NULL);
536 return RegOpenKeyExW(root, keypath, 0, access, key);
537 }
538
539 UINT MSIREG_DeleteUserProductKey(LPCWSTR szProduct)
540 {
541 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
542
543 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
544 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
545
546 strcpyW(keypath, szUserProducts);
547 strcatW(keypath, squished_pc);
548 return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
549 }
550
551 UINT MSIREG_OpenUserPatchesKey(LPCWSTR szPatch, HKEY *key, BOOL create)
552 {
553 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
554
555 if (!squash_guid(szPatch, squished_pc)) return ERROR_FUNCTION_FAILED;
556 TRACE("%s squished %s\n", debugstr_w(szPatch), debugstr_w(squished_pc));
557
558 strcpyW(keypath, szUserPatches);
559 strcatW(keypath, squished_pc);
560
561 if (create) return RegCreateKeyW(HKEY_CURRENT_USER, keypath, key);
562 return RegOpenKeyW(HKEY_CURRENT_USER, keypath, key);
563 }
564
565 UINT MSIREG_OpenFeaturesKey(LPCWSTR szProduct, MSIINSTALLCONTEXT context, HKEY *key, BOOL create)
566 {
567 LPWSTR usersid;
568 HKEY root = HKEY_LOCAL_MACHINE;
569 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
570 WCHAR squished_pc[GUID_SIZE], keypath[MAX_PATH];
571
572 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
573 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
574
575 if (context == MSIINSTALLCONTEXT_MACHINE)
576 {
577 strcpyW(keypath, szInstaller_LocalClassesFeat);
578 strcatW(keypath, squished_pc);
579 }
580 else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
581 {
582 root = HKEY_CURRENT_USER;
583 strcpyW(keypath, szUserFeatures);
584 strcatW(keypath, squished_pc);
585 }
586 else
587 {
588 if (!(usersid = get_user_sid()))
589 {
590 ERR("Failed to retrieve user SID\n");
591 return ERROR_FUNCTION_FAILED;
592 }
593 sprintfW(keypath, szInstaller_LocalManagedFeat_fmt, usersid, squished_pc);
594 LocalFree(usersid);
595 }
596 if (create) return RegCreateKeyExW(root, keypath, 0, NULL, 0, access, NULL, key, NULL);
597 return RegOpenKeyExW(root, keypath, 0, access, key);
598 }
599
600 UINT MSIREG_DeleteUserFeaturesKey(LPCWSTR szProduct)
601 {
602 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
603
604 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
605 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
606
607 strcpyW(keypath, szUserFeatures);
608 strcatW(keypath, squished_pc);
609 return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
610 }
611
612 static UINT MSIREG_OpenInstallerFeaturesKey(LPCWSTR szProduct, HKEY *key, BOOL create)
613 {
614 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
615 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
616
617 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
618 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
619
620 strcpyW(keypath, szInstaller_Features);
621 strcatW(keypath, squished_pc);
622
623 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
624 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
625 }
626
627 UINT MSIREG_OpenUserDataFeaturesKey(LPCWSTR szProduct, MSIINSTALLCONTEXT context, HKEY *key, BOOL create)
628 {
629 LPWSTR usersid;
630 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
631 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
632
633 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
634 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
635
636 if (context == MSIINSTALLCONTEXT_MACHINE)
637 {
638 sprintfW(keypath, szUserDataFeatures_fmt, szLocalSid, squished_pc);
639 }
640 else
641 {
642 if (!(usersid = get_user_sid()))
643 {
644 ERR("Failed to retrieve user SID\n");
645 return ERROR_FUNCTION_FAILED;
646 }
647 sprintfW(keypath, szUserDataFeatures_fmt, usersid, squished_pc);
648 LocalFree(usersid);
649 }
650 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
651 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
652 }
653
654 UINT MSIREG_OpenUserComponentsKey(LPCWSTR szComponent, HKEY *key, BOOL create)
655 {
656 WCHAR squished_cc[GUID_SIZE], keypath[0x200];
657
658 if (!squash_guid(szComponent, squished_cc)) return ERROR_FUNCTION_FAILED;
659 TRACE("%s squished %s\n", debugstr_w(szComponent), debugstr_w(squished_cc));
660
661 strcpyW(keypath, szUserComponents);
662 strcatW(keypath, squished_cc);
663
664 if (create) return RegCreateKeyW(HKEY_CURRENT_USER, keypath, key);
665 return RegOpenKeyW(HKEY_CURRENT_USER, keypath, key);
666 }
667
668 UINT MSIREG_OpenUserDataComponentKey(LPCWSTR szComponent, LPCWSTR szUserSid, HKEY *key, BOOL create)
669 {
670 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
671 WCHAR comp[GUID_SIZE], keypath[0x200];
672 LPWSTR usersid;
673
674 if (!squash_guid(szComponent, comp)) return ERROR_FUNCTION_FAILED;
675 TRACE("%s squished %s\n", debugstr_w(szComponent), debugstr_w(comp));
676
677 if (!szUserSid)
678 {
679 if (!(usersid = get_user_sid()))
680 {
681 ERR("Failed to retrieve user SID\n");
682 return ERROR_FUNCTION_FAILED;
683 }
684 sprintfW(keypath, szUserDataComp_fmt, usersid, comp);
685 LocalFree(usersid);
686 }
687 else
688 sprintfW(keypath, szUserDataComp_fmt, szUserSid, comp);
689
690 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
691 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
692 }
693
694 UINT MSIREG_DeleteUserDataComponentKey(LPCWSTR szComponent, LPCWSTR szUserSid)
695 {
696 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
697 WCHAR comp[GUID_SIZE], keypath[0x200];
698 LPWSTR usersid;
699 HKEY hkey;
700 LONG r;
701
702 if (!squash_guid(szComponent, comp)) return ERROR_FUNCTION_FAILED;
703 TRACE("%s squished %s\n", debugstr_w(szComponent), debugstr_w(comp));
704
705 if (!szUserSid)
706 {
707 if (!(usersid = get_user_sid()))
708 {
709 ERR("Failed to retrieve user SID\n");
710 return ERROR_FUNCTION_FAILED;
711 }
712 sprintfW(keypath, szUserDataComponents_fmt, usersid);
713 LocalFree(usersid);
714 }
715 else
716 sprintfW(keypath, szUserDataComponents_fmt, szUserSid);
717
718 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, &hkey)) return ERROR_SUCCESS;
719 r = RegDeleteTreeW(hkey, comp);
720 RegCloseKey(hkey);
721 return r;
722 }
723
724 UINT MSIREG_OpenUserDataProductKey(LPCWSTR szProduct, MSIINSTALLCONTEXT dwContext, LPCWSTR szUserSid, HKEY *key, BOOL create)
725 {
726 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
727 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
728 LPWSTR usersid;
729
730 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
731 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
732
733 if (dwContext == MSIINSTALLCONTEXT_MACHINE)
734 sprintfW(keypath, szUserDataProd_fmt, szLocalSid, squished_pc);
735 else if (szUserSid)
736 sprintfW(keypath, szUserDataProd_fmt, szUserSid, squished_pc);
737 else
738 {
739 if (!(usersid = get_user_sid()))
740 {
741 ERR("Failed to retrieve user SID\n");
742 return ERROR_FUNCTION_FAILED;
743 }
744 sprintfW(keypath, szUserDataProd_fmt, usersid, squished_pc);
745 LocalFree(usersid);
746 }
747 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
748 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
749 }
750
751 UINT MSIREG_OpenUserDataPatchKey(LPCWSTR szPatch, MSIINSTALLCONTEXT dwContext, HKEY *key, BOOL create)
752 {
753 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
754 WCHAR squished_patch[GUID_SIZE], keypath[0x200];
755 LPWSTR usersid;
756
757 if (!squash_guid(szPatch, squished_patch)) return ERROR_FUNCTION_FAILED;
758 TRACE("%s squished %s\n", debugstr_w(szPatch), debugstr_w(squished_patch));
759
760 if (dwContext == MSIINSTALLCONTEXT_MACHINE)
761 sprintfW(keypath, szUserDataPatch_fmt, szLocalSid, squished_patch);
762 else
763 {
764 if (!(usersid = get_user_sid()))
765 {
766 ERR("Failed to retrieve user SID\n");
767 return ERROR_FUNCTION_FAILED;
768 }
769 sprintfW(keypath, szUserDataPatch_fmt, usersid, squished_patch);
770 LocalFree(usersid);
771 }
772 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
773 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
774 }
775
776 UINT MSIREG_DeleteUserDataPatchKey(LPCWSTR patch, MSIINSTALLCONTEXT context)
777 {
778 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
779 WCHAR squished_patch[GUID_SIZE], keypath[0x200];
780 LPWSTR usersid;
781 HKEY hkey;
782 LONG r;
783
784 if (!squash_guid(patch, squished_patch)) return ERROR_FUNCTION_FAILED;
785 TRACE("%s squished %s\n", debugstr_w(patch), debugstr_w(squished_patch));
786
787 if (context == MSIINSTALLCONTEXT_MACHINE)
788 sprintfW(keypath, szUserDataPatches_fmt, szLocalSid);
789 else
790 {
791 if (!(usersid = get_user_sid()))
792 {
793 ERR("Failed to retrieve user SID\n");
794 return ERROR_FUNCTION_FAILED;
795 }
796 sprintfW(keypath, szUserDataPatches_fmt, usersid);
797 LocalFree(usersid);
798 }
799 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, &hkey)) return ERROR_SUCCESS;
800 r = RegDeleteTreeW(hkey, squished_patch);
801 RegCloseKey(hkey);
802 return r;
803 }
804
805 UINT MSIREG_OpenUserDataProductPatchesKey(LPCWSTR product, MSIINSTALLCONTEXT context, HKEY *key, BOOL create)
806 {
807 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
808 WCHAR squished_product[GUID_SIZE], keypath[0x200];
809 LPWSTR usersid;
810
811 if (!squash_guid(product, squished_product)) return ERROR_FUNCTION_FAILED;
812 TRACE("%s squished %s\n", debugstr_w(product), debugstr_w(squished_product));
813
814 if (context == MSIINSTALLCONTEXT_MACHINE)
815 sprintfW(keypath, szUserDataProductPatches_fmt, szLocalSid, squished_product);
816 else
817 {
818 if (!(usersid = get_user_sid()))
819 {
820 ERR("Failed to retrieve user SID\n");
821 return ERROR_FUNCTION_FAILED;
822 }
823 sprintfW(keypath, szUserDataProductPatches_fmt, usersid, squished_product);
824 LocalFree(usersid);
825 }
826 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
827 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
828 }
829
830 UINT MSIREG_OpenInstallProps(LPCWSTR szProduct, MSIINSTALLCONTEXT dwContext, LPCWSTR szUserSid, HKEY *key, BOOL create)
831 {
832 LPWSTR usersid;
833 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
834 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
835
836 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
837 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
838
839 if (dwContext == MSIINSTALLCONTEXT_MACHINE)
840 sprintfW(keypath, szInstallProperties_fmt, szLocalSid, squished_pc);
841 else if (szUserSid)
842 sprintfW(keypath, szInstallProperties_fmt, szUserSid, squished_pc);
843 else
844 {
845 if (!(usersid = get_user_sid()))
846 {
847 ERR("Failed to retrieve user SID\n");
848 return ERROR_FUNCTION_FAILED;
849 }
850 sprintfW(keypath, szInstallProperties_fmt, usersid, squished_pc);
851 LocalFree(usersid);
852 }
853 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
854 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
855 }
856
857 UINT MSIREG_DeleteUserDataProductKey(LPCWSTR szProduct)
858 {
859 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
860 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
861 LPWSTR usersid;
862 HKEY hkey;
863 LONG r;
864
865 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
866 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
867
868 if (!(usersid = get_user_sid()))
869 {
870 ERR("Failed to retrieve user SID\n");
871 return ERROR_FUNCTION_FAILED;
872 }
873 sprintfW(keypath, szUserDataProducts_fmt, usersid);
874 LocalFree(usersid);
875
876 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, &hkey)) return ERROR_SUCCESS;
877 r = RegDeleteTreeW(hkey, squished_pc);
878 RegCloseKey(hkey);
879 return r;
880 }
881
882 UINT MSIREG_DeleteProductKey(LPCWSTR szProduct)
883 {
884 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
885 WCHAR squished_pc[GUID_SIZE];
886 HKEY hkey;
887 LONG r;
888
889 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
890 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
891
892 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_Products, 0, access, &hkey)) return ERROR_SUCCESS;
893 r = RegDeleteTreeW(hkey, squished_pc);
894 RegCloseKey(hkey);
895 return r;
896 }
897
898 UINT MSIREG_OpenPatchesKey(LPCWSTR szPatch, HKEY *key, BOOL create)
899 {
900 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
901 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
902
903 if (!squash_guid(szPatch, squished_pc)) return ERROR_FUNCTION_FAILED;
904 TRACE("%s squished %s\n", debugstr_w(szPatch), debugstr_w(squished_pc));
905
906 sprintfW(keypath, szInstaller_Patches, squished_pc);
907
908 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
909 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
910 }
911
912 UINT MSIREG_OpenUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY *key, BOOL create)
913 {
914 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
915 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
916
917 if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
918 TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
919
920 strcpyW(keypath, szInstaller_UpgradeCodes);
921 strcatW(keypath, squished_pc);
922
923 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
924 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
925 }
926
927 UINT MSIREG_OpenUserUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY* key, BOOL create)
928 {
929 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
930
931 if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
932 TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
933
934 strcpyW(keypath, szInstaller_UserUpgradeCodes);
935 strcatW(keypath, squished_pc);
936
937 if (create) return RegCreateKeyW(HKEY_CURRENT_USER, keypath, key);
938 return RegOpenKeyW(HKEY_CURRENT_USER, keypath, key);
939 }
940
941 UINT MSIREG_DeleteUserUpgradeCodesKey(LPCWSTR szUpgradeCode)
942 {
943 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
944
945 if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
946 TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
947
948 strcpyW(keypath, szInstaller_UserUpgradeCodes);
949 strcatW(keypath, squished_pc);
950 return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
951 }
952
953 UINT MSIREG_DeleteLocalClassesProductKey(LPCWSTR szProductCode)
954 {
955 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
956 WCHAR squished_pc[GUID_SIZE];
957 HKEY hkey;
958 LONG r;
959
960 if (!squash_guid(szProductCode, squished_pc)) return ERROR_FUNCTION_FAILED;
961 TRACE("%s squished %s\n", debugstr_w(szProductCode), debugstr_w(squished_pc));
962
963 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_LocalClassesProducts, 0, access, &hkey)) return ERROR_SUCCESS;
964 r = RegDeleteTreeW(hkey, squished_pc);
965 RegCloseKey(hkey);
966 return r;
967 }
968
969 UINT MSIREG_DeleteLocalClassesFeaturesKey(LPCWSTR szProductCode)
970 {
971 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
972 WCHAR squished_pc[GUID_SIZE];
973 HKEY hkey;
974 LONG r;
975
976 if (!squash_guid(szProductCode, squished_pc)) return ERROR_FUNCTION_FAILED;
977 TRACE("%s squished %s\n", debugstr_w(szProductCode), debugstr_w(squished_pc));
978
979 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_LocalClassesFeatures, 0, access, &hkey)) return ERROR_SUCCESS;
980 r = RegDeleteTreeW(hkey, squished_pc);
981 RegCloseKey(hkey);
982 return r;
983 }
984
985 UINT MSIREG_OpenClassesUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY *key, BOOL create)
986 {
987 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
988 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
989
990 if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
991 TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
992
993 strcpyW(keypath, szInstaller_ClassesUpgradeCode);
994 strcatW(keypath, squished_pc);
995
996 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
997 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
998 }
999
1000 UINT MSIREG_DeleteClassesUpgradeCodesKey(LPCWSTR szUpgradeCode)
1001 {
1002 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
1003 WCHAR squished_pc[GUID_SIZE];
1004 HKEY hkey;
1005 LONG r;
1006
1007 if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
1008 TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
1009
1010 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_ClassesUpgradeCodes, 0, access, &hkey)) return ERROR_SUCCESS;
1011 r = RegDeleteTreeW(hkey, squished_pc);
1012 RegCloseKey(hkey);
1013 return r;
1014 }
1015
1016 /*************************************************************************
1017 * MsiDecomposeDescriptorW [MSI.@]
1018 *
1019 * Decomposes an MSI descriptor into product, feature and component parts.
1020 * An MSI descriptor is a string of the form:
1021 * [base 85 guid] [feature code] '>' [base 85 guid]
1022 *
1023 * PARAMS
1024 * szDescriptor [I] the descriptor to decompose
1025 * szProduct [O] buffer of MAX_FEATURE_CHARS+1 for the product guid
1026 * szFeature [O] buffer of MAX_FEATURE_CHARS+1 for the feature code
1027 * szComponent [O] buffer of MAX_FEATURE_CHARS+1 for the component guid
1028 * pUsed [O] the length of the descriptor
1029 *
1030 * RETURNS
1031 * ERROR_SUCCESS if everything worked correctly
1032 * ERROR_INVALID_PARAMETER if the descriptor was invalid
1033 *
1034 */
1035 UINT WINAPI MsiDecomposeDescriptorW( LPCWSTR szDescriptor, LPWSTR szProduct,
1036 LPWSTR szFeature, LPWSTR szComponent, LPDWORD pUsed )
1037 {
1038 UINT r, len;
1039 LPWSTR p;
1040 GUID product, component;
1041
1042 TRACE("%s %p %p %p %p\n", debugstr_w(szDescriptor), szProduct,
1043 szFeature, szComponent, pUsed);
1044
1045 r = decode_base85_guid( szDescriptor, &product );
1046 if( !r )
1047 return ERROR_INVALID_PARAMETER;
1048
1049 TRACE("product %s\n", debugstr_guid( &product ));
1050
1051 p = strchrW(&szDescriptor[20],'>');
1052 if( !p )
1053 return ERROR_INVALID_PARAMETER;
1054
1055 len = (p - &szDescriptor[20]);
1056 if( len > MAX_FEATURE_CHARS )
1057 return ERROR_INVALID_PARAMETER;
1058
1059 TRACE("feature %s\n", debugstr_wn( &szDescriptor[20], len ));
1060
1061 r = decode_base85_guid( p+1, &component );
1062 if( !r )
1063 return ERROR_INVALID_PARAMETER;
1064
1065 TRACE("component %s\n", debugstr_guid( &component ));
1066
1067 if (szProduct)
1068 StringFromGUID2( &product, szProduct, MAX_FEATURE_CHARS+1 );
1069 if (szComponent)
1070 StringFromGUID2( &component, szComponent, MAX_FEATURE_CHARS+1 );
1071 if (szFeature)
1072 {
1073 memcpy( szFeature, &szDescriptor[20], len*sizeof(WCHAR) );
1074 szFeature[len] = 0;
1075 }
1076 len = ( &p[21] - szDescriptor );
1077
1078 TRACE("length = %d\n", len);
1079 if (pUsed) *pUsed = len;
1080
1081 return ERROR_SUCCESS;
1082 }
1083
1084 UINT WINAPI MsiDecomposeDescriptorA( LPCSTR szDescriptor, LPSTR szProduct,
1085 LPSTR szFeature, LPSTR szComponent, LPDWORD pUsed )
1086 {
1087 WCHAR product[MAX_FEATURE_CHARS+1];
1088 WCHAR feature[MAX_FEATURE_CHARS+1];
1089 WCHAR component[MAX_FEATURE_CHARS+1];
1090 LPWSTR str = NULL, p = NULL, f = NULL, c = NULL;
1091 UINT r;
1092
1093 TRACE("%s %p %p %p %p\n", debugstr_a(szDescriptor), szProduct,
1094 szFeature, szComponent, pUsed);
1095
1096 str = strdupAtoW( szDescriptor );
1097 if( szDescriptor && !str )
1098 return ERROR_OUTOFMEMORY;
1099
1100 if (szProduct)
1101 p = product;
1102 if (szFeature)
1103 f = feature;
1104 if (szComponent)
1105 c = component;
1106
1107 r = MsiDecomposeDescriptorW( str, p, f, c, pUsed );
1108
1109 if (r == ERROR_SUCCESS)
1110 {
1111 WideCharToMultiByte( CP_ACP, 0, p, -1,
1112 szProduct, MAX_FEATURE_CHARS+1, NULL, NULL );
1113 WideCharToMultiByte( CP_ACP, 0, f, -1,
1114 szFeature, MAX_FEATURE_CHARS+1, NULL, NULL );
1115 WideCharToMultiByte( CP_ACP, 0, c, -1,
1116 szComponent, MAX_FEATURE_CHARS+1, NULL, NULL );
1117 }
1118
1119 msi_free( str );
1120
1121 return r;
1122 }
1123
1124 UINT WINAPI MsiEnumProductsA(DWORD index, LPSTR lpguid)
1125 {
1126 DWORD r;
1127 WCHAR szwGuid[GUID_SIZE];
1128
1129 TRACE("%d %p\n", index, lpguid);
1130
1131 if (NULL == lpguid)
1132 return ERROR_INVALID_PARAMETER;
1133 r = MsiEnumProductsW(index, szwGuid);
1134 if( r == ERROR_SUCCESS )
1135 WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
1136
1137 return r;
1138 }
1139
1140 UINT WINAPI MsiEnumProductsW(DWORD index, LPWSTR lpguid)
1141 {
1142 TRACE("%d %p\n", index, lpguid);
1143
1144 if (NULL == lpguid)
1145 return ERROR_INVALID_PARAMETER;
1146
1147 return MsiEnumProductsExW( NULL, szAllSid, MSIINSTALLCONTEXT_ALL, index, lpguid,
1148 NULL, NULL, NULL );
1149 }
1150
1151 UINT WINAPI MsiEnumFeaturesA(LPCSTR szProduct, DWORD index,
1152 LPSTR szFeature, LPSTR szParent)
1153 {
1154 DWORD r;
1155 WCHAR szwFeature[GUID_SIZE], szwParent[GUID_SIZE];
1156 LPWSTR szwProduct = NULL;
1157
1158 TRACE("%s %d %p %p\n", debugstr_a(szProduct), index, szFeature, szParent);
1159
1160 if( szProduct )
1161 {
1162 szwProduct = strdupAtoW( szProduct );
1163 if( !szwProduct )
1164 return ERROR_OUTOFMEMORY;
1165 }
1166
1167 r = MsiEnumFeaturesW(szwProduct, index, szwFeature, szwParent);
1168 if( r == ERROR_SUCCESS )
1169 {
1170 WideCharToMultiByte(CP_ACP, 0, szwFeature, -1,
1171 szFeature, GUID_SIZE, NULL, NULL);
1172 WideCharToMultiByte(CP_ACP, 0, szwParent, -1,
1173 szParent, GUID_SIZE, NULL, NULL);
1174 }
1175
1176 msi_free( szwProduct);
1177
1178 return r;
1179 }
1180
1181 UINT WINAPI MsiEnumFeaturesW(LPCWSTR szProduct, DWORD index,
1182 LPWSTR szFeature, LPWSTR szParent)
1183 {
1184 HKEY hkeyProduct = 0;
1185 DWORD r, sz;
1186
1187 TRACE("%s %d %p %p\n", debugstr_w(szProduct), index, szFeature, szParent);
1188
1189 if( !szProduct )
1190 return ERROR_INVALID_PARAMETER;
1191
1192 r = MSIREG_OpenInstallerFeaturesKey(szProduct,&hkeyProduct,FALSE);
1193 if( r != ERROR_SUCCESS )
1194 return ERROR_NO_MORE_ITEMS;
1195
1196 sz = GUID_SIZE;
1197 r = RegEnumValueW(hkeyProduct, index, szFeature, &sz, NULL, NULL, NULL, NULL);
1198 RegCloseKey(hkeyProduct);
1199
1200 return r;
1201 }
1202
1203 UINT WINAPI MsiEnumComponentsA(DWORD index, LPSTR lpguid)
1204 {
1205 DWORD r;
1206 WCHAR szwGuid[GUID_SIZE];
1207
1208 TRACE("%u, %p\n", index, lpguid);
1209
1210 if (!lpguid) return ERROR_INVALID_PARAMETER;
1211
1212 r = MsiEnumComponentsW(index, szwGuid);
1213 if( r == ERROR_SUCCESS )
1214 WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
1215
1216 return r;
1217 }
1218
1219 UINT WINAPI MsiEnumComponentsW(DWORD index, LPWSTR lpguid)
1220 {
1221 TRACE("%u, %p\n", index, lpguid);
1222
1223 if (!lpguid) return ERROR_INVALID_PARAMETER;
1224
1225 return MsiEnumComponentsExW( szAllSid, MSIINSTALLCONTEXT_ALL, index, lpguid, NULL, NULL, NULL );
1226 }
1227
1228 UINT WINAPI MsiEnumComponentsExA( LPCSTR user_sid, DWORD ctx, DWORD index, CHAR guid[39],
1229 MSIINSTALLCONTEXT *installed_ctx, LPSTR sid, LPDWORD sid_len )
1230 {
1231 UINT r;
1232 WCHAR *user_sidW = NULL, *sidW = NULL, guidW[GUID_SIZE];
1233
1234 TRACE("%s, %u, %u, %p, %p, %p, %p\n", debugstr_a(user_sid), ctx, index, guid, installed_ctx,
1235 sid, sid_len);
1236
1237 if (sid && !sid_len) return ERROR_INVALID_PARAMETER;
1238 if (user_sid && !(user_sidW = strdupAtoW( user_sid ))) return ERROR_OUTOFMEMORY;
1239 if (sid && !(sidW = msi_alloc( *sid_len * sizeof(WCHAR) )))
1240 {
1241 msi_free( user_sidW );
1242 return ERROR_OUTOFMEMORY;
1243 }
1244 r = MsiEnumComponentsExW( user_sidW, ctx, index, guidW, installed_ctx, sidW, sid_len );
1245 if (r == ERROR_SUCCESS)
1246 {
1247 if (guid) WideCharToMultiByte( CP_ACP, 0, guidW, GUID_SIZE, guid, GUID_SIZE, NULL, NULL );
1248 if (sid) WideCharToMultiByte( CP_ACP, 0, sidW, *sid_len + 1, sid, *sid_len + 1, NULL, NULL );
1249 }
1250 msi_free( user_sidW );
1251 msi_free( sidW );
1252 return r;
1253 }
1254
1255 static UINT fetch_machine_component( DWORD ctx, DWORD index, DWORD *idx, WCHAR guid[39],
1256 MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid, LPDWORD sid_len )
1257 {
1258 static const WCHAR componentsW[] =
1259 {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1260 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1261 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
1262 'S','-','1','-','5','-','1','8','\\','C','o','m','p','o','n','e','n','t','s',0};
1263 UINT r = ERROR_SUCCESS;
1264 WCHAR component[GUID_SIZE];
1265 DWORD i = 0, len_component;
1266 REGSAM access = KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY;
1267 HKEY key_components;
1268
1269 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, componentsW, 0, access, &key_components ))
1270 return ERROR_NO_MORE_ITEMS;
1271
1272 len_component = sizeof(component)/sizeof(component[0]);
1273 while (!RegEnumKeyExW( key_components, i, component, &len_component, NULL, NULL, NULL, NULL ))
1274 {
1275 if (*idx == index) goto found;
1276 (*idx)++;
1277 len_component = sizeof(component)/sizeof(component[0]);
1278 i++;
1279 }
1280 RegCloseKey( key_components );
1281 return ERROR_NO_MORE_ITEMS;
1282
1283 found:
1284 if (sid_len)
1285 {
1286 if (*sid_len < 1)
1287 {
1288 *sid_len = 1;
1289 r = ERROR_MORE_DATA;
1290 }
1291 else if (sid)
1292 {
1293 *sid_len = 0;
1294 sid[0] = 0;
1295 }
1296 }
1297 if (guid) unsquash_guid( component, guid );
1298 if (installed_ctx) *installed_ctx = MSIINSTALLCONTEXT_MACHINE;
1299 RegCloseKey( key_components );
1300 return r;
1301 }
1302
1303 static UINT fetch_user_component( const WCHAR *usersid, DWORD ctx, DWORD index, DWORD *idx,
1304 WCHAR guid[39], MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid,
1305 LPDWORD sid_len )
1306 {
1307 static const WCHAR userdataW[] =
1308 {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1309 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1310 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a',0};
1311 static const WCHAR componentsW[] = {'\\','C','o','m','p','o','n','e','n','t','s',0};
1312 UINT r = ERROR_SUCCESS;
1313 WCHAR path[MAX_PATH], component[GUID_SIZE], user[128];
1314 DWORD i = 0, j = 0, len_component, len_user;
1315 REGSAM access = KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY;
1316 HKEY key_users, key_components;
1317
1318 if (ctx == MSIINSTALLCONTEXT_USERMANAGED) /* FIXME: were to find these? */
1319 return ERROR_NO_MORE_ITEMS;
1320
1321 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, userdataW, 0, access, &key_users ))
1322 return ERROR_NO_MORE_ITEMS;
1323
1324 len_user = sizeof(user)/sizeof(user[0]);
1325 while (!RegEnumKeyExW( key_users, i, user, &len_user, NULL, NULL, NULL, NULL ))
1326 {
1327 if ((strcmpW( usersid, szAllSid ) && strcmpW( usersid, user )) ||
1328 !strcmpW( szLocalSid, user ))
1329 {
1330 i++;
1331 len_user = sizeof(user)/sizeof(user[0]);
1332 continue;
1333 }
1334 strcpyW( path, user );
1335 strcatW( path, componentsW );
1336 if (RegOpenKeyExW( key_users, path, 0, access, &key_components ))
1337 {
1338 i++;
1339 len_user = sizeof(user)/sizeof(user[0]);
1340 continue;
1341 }
1342 len_component = sizeof(component)/sizeof(component[0]);
1343 while (!RegEnumKeyExW( key_components, j, component, &len_component, NULL, NULL, NULL, NULL ))
1344 {
1345 if (*idx == index) goto found;
1346 (*idx)++;
1347 len_component = sizeof(component)/sizeof(component[0]);
1348 j++;
1349 }
1350 RegCloseKey( key_components );
1351 len_user = sizeof(user)/sizeof(user[0]);
1352 i++;
1353 }
1354 RegCloseKey( key_users );
1355 return ERROR_NO_MORE_ITEMS;
1356
1357 found:
1358 if (sid_len)
1359 {
1360 if (*sid_len < len_user + 1)
1361 {
1362 *sid_len = len_user + 1;
1363 r = ERROR_MORE_DATA;
1364 }
1365 else if (sid)
1366 {
1367 *sid_len = len_user;
1368 strcpyW( sid, user );
1369 }
1370 }
1371 if (guid) unsquash_guid( component, guid );
1372 if (installed_ctx) *installed_ctx = ctx;
1373 RegCloseKey( key_components );
1374 RegCloseKey( key_users );
1375 return r;
1376 }
1377
1378 static UINT enum_components( const WCHAR *usersid, DWORD ctx, DWORD index, DWORD *idx, WCHAR guid[39],
1379 MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid, LPDWORD sid_len )
1380 {
1381 UINT r = ERROR_NO_MORE_ITEMS;
1382 WCHAR *user = NULL;
1383
1384 if (!usersid)
1385 {
1386 usersid = user = get_user_sid();
1387 if (!user) return ERROR_FUNCTION_FAILED;
1388 }
1389 if (ctx & MSIINSTALLCONTEXT_USERMANAGED)
1390 {
1391 r = fetch_user_component( usersid, MSIINSTALLCONTEXT_USERMANAGED, index, idx, guid,
1392 installed_ctx, sid, sid_len );
1393 if (r != ERROR_NO_MORE_ITEMS) goto done;
1394 }
1395 if (ctx & MSIINSTALLCONTEXT_USERUNMANAGED)
1396 {
1397 r = fetch_user_component( usersid, MSIINSTALLCONTEXT_USERUNMANAGED, index, idx, guid,
1398 installed_ctx, sid, sid_len );
1399 if (r != ERROR_NO_MORE_ITEMS) goto done;
1400 }
1401 if (ctx & MSIINSTALLCONTEXT_MACHINE)
1402 {
1403 r = fetch_machine_component( MSIINSTALLCONTEXT_MACHINE, index, idx, guid, installed_ctx,
1404 sid, sid_len );
1405 if (r != ERROR_NO_MORE_ITEMS) goto done;
1406 }
1407
1408 done:
1409 LocalFree( user );
1410 return r;
1411 }
1412
1413 UINT WINAPI MsiEnumComponentsExW( LPCWSTR user_sid, DWORD ctx, DWORD index, WCHAR guid[39],
1414 MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid, LPDWORD sid_len )
1415 {
1416 UINT r;
1417 DWORD idx = 0;
1418 static DWORD last_index;
1419
1420 TRACE("%s, %u, %u, %p, %p, %p, %p\n", debugstr_w(user_sid), ctx, index, guid, installed_ctx,
1421 sid, sid_len);
1422
1423 if ((sid && !sid_len) || !ctx || (user_sid && ctx == MSIINSTALLCONTEXT_MACHINE))
1424 return ERROR_INVALID_PARAMETER;
1425
1426 if (index && index - last_index != 1)
1427 return ERROR_INVALID_PARAMETER;
1428
1429 if (!index) last_index = 0;
1430
1431 r = enum_components( user_sid, ctx, index, &idx, guid, installed_ctx, sid, sid_len );
1432 if (r == ERROR_SUCCESS)
1433 last_index = index;
1434 else
1435 last_index = 0;
1436
1437 return r;
1438 }
1439
1440 UINT WINAPI MsiEnumClientsA(LPCSTR szComponent, DWORD index, LPSTR szProduct)
1441 {
1442 DWORD r;
1443 WCHAR szwProduct[GUID_SIZE];
1444 LPWSTR szwComponent = NULL;
1445
1446 TRACE("%s %d %p\n", debugstr_a(szComponent), index, szProduct);
1447
1448 if ( !szProduct )
1449 return ERROR_INVALID_PARAMETER;
1450
1451 if( szComponent )
1452 {
1453 szwComponent = strdupAtoW( szComponent );
1454 if( !szwComponent )
1455 return ERROR_OUTOFMEMORY;
1456 }
1457
1458 r = MsiEnumClientsW(szComponent?szwComponent:NULL, index, szwProduct);
1459 if( r == ERROR_SUCCESS )
1460 {
1461 WideCharToMultiByte(CP_ACP, 0, szwProduct, -1,
1462 szProduct, GUID_SIZE, NULL, NULL);
1463 }
1464
1465 msi_free( szwComponent);
1466
1467 return r;
1468 }
1469
1470 UINT WINAPI MsiEnumClientsW(LPCWSTR szComponent, DWORD index, LPWSTR szProduct)
1471 {
1472 HKEY hkeyComp = 0;
1473 DWORD r, sz;
1474 WCHAR szValName[SQUISH_GUID_SIZE];
1475
1476 TRACE("%s %d %p\n", debugstr_w(szComponent), index, szProduct);
1477
1478 if (!szComponent || !*szComponent || !szProduct)
1479 return ERROR_INVALID_PARAMETER;
1480
1481 if (MSIREG_OpenUserDataComponentKey(szComponent, NULL, &hkeyComp, FALSE) != ERROR_SUCCESS &&
1482 MSIREG_OpenUserDataComponentKey(szComponent, szLocalSid, &hkeyComp, FALSE) != ERROR_SUCCESS)
1483 return ERROR_UNKNOWN_COMPONENT;
1484
1485 /* see if there are any products at all */
1486 sz = SQUISH_GUID_SIZE;
1487 r = RegEnumValueW(hkeyComp, 0, szValName, &sz, NULL, NULL, NULL, NULL);
1488 if (r != ERROR_SUCCESS)
1489 {
1490 RegCloseKey(hkeyComp);
1491
1492 if (index != 0)
1493 return ERROR_INVALID_PARAMETER;
1494
1495 return ERROR_UNKNOWN_COMPONENT;
1496 }
1497
1498 sz = SQUISH_GUID_SIZE;
1499 r = RegEnumValueW(hkeyComp, index, szValName, &sz, NULL, NULL, NULL, NULL);
1500 if( r == ERROR_SUCCESS )
1501 {
1502 unsquash_guid(szValName, szProduct);
1503 TRACE("-> %s\n", debugstr_w(szProduct));
1504 }
1505 RegCloseKey(hkeyComp);
1506 return r;
1507 }
1508
1509 static UINT MSI_EnumComponentQualifiers( LPCWSTR szComponent, DWORD iIndex,
1510 awstring *lpQualBuf, LPDWORD pcchQual,
1511 awstring *lpAppBuf, LPDWORD pcchAppBuf )
1512 {
1513 DWORD name_sz, val_sz, name_max, val_max, type, ofs;
1514 LPWSTR name = NULL, val = NULL;
1515 UINT r, r2;
1516 HKEY key;
1517
1518 TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
1519 lpQualBuf, pcchQual, lpAppBuf, pcchAppBuf);
1520
1521 if (!szComponent)
1522 return ERROR_INVALID_PARAMETER;
1523
1524 r = MSIREG_OpenUserComponentsKey( szComponent, &key, FALSE );
1525 if (r != ERROR_SUCCESS)
1526 return ERROR_UNKNOWN_COMPONENT;
1527
1528 /* figure out how big the name is we want to return */
1529 name_max = 0x10;
1530 r = ERROR_OUTOFMEMORY;
1531 name = msi_alloc( name_max * sizeof(WCHAR) );
1532 if (!name)
1533 goto end;
1534
1535 val_max = 0x10;
1536 r = ERROR_OUTOFMEMORY;
1537 val = msi_alloc( val_max );
1538 if (!val)
1539 goto end;
1540
1541 /* loop until we allocate enough memory */
1542 while (1)
1543 {
1544 name_sz = name_max;
1545 val_sz = val_max;
1546 r = RegEnumValueW( key, iIndex, name, &name_sz,
1547 NULL, &type, (LPBYTE)val, &val_sz );
1548 if (r == ERROR_SUCCESS)
1549 break;
1550 if (r != ERROR_MORE_DATA)
1551 goto end;
1552
1553 if (type != REG_MULTI_SZ)
1554 {
1555 ERR("component data has wrong type (%d)\n", type);
1556 goto end;
1557 }
1558
1559 r = ERROR_OUTOFMEMORY;
1560 if ((name_sz+1) >= name_max)
1561 {
1562 name_max *= 2;
1563 msi_free( name );
1564 name = msi_alloc( name_max * sizeof (WCHAR) );
1565 if (!name)
1566 goto end;
1567 continue;
1568 }
1569 if (val_sz > val_max)
1570 {
1571 val_max = val_sz + sizeof (WCHAR);
1572 msi_free( val );
1573 val = msi_alloc( val_max * sizeof (WCHAR) );
1574 if (!val)
1575 goto end;
1576 continue;
1577 }
1578 ERR("should be enough data, but isn't %d %d\n", name_sz, val_sz );
1579 goto end;
1580 }
1581
1582 ofs = 0;
1583 r = MsiDecomposeDescriptorW( val, NULL, NULL, NULL, &ofs );
1584 if (r != ERROR_SUCCESS)
1585 goto end;
1586
1587 TRACE("Providing %s and %s\n", debugstr_w(name), debugstr_w(val+ofs));
1588
1589 r = msi_strcpy_to_awstring( name, lpQualBuf, pcchQual );
1590 r2 = msi_strcpy_to_awstring( val+ofs, lpAppBuf, pcchAppBuf );
1591
1592 if (r2 != ERROR_SUCCESS)
1593 r = r2;
1594
1595 end:
1596 msi_free(val);
1597 msi_free(name);
1598 RegCloseKey(key);
1599
1600 return r;
1601 }
1602
1603 /*************************************************************************
1604 * MsiEnumComponentQualifiersA [MSI.@]
1605 */
1606 UINT WINAPI MsiEnumComponentQualifiersA( LPCSTR szComponent, DWORD iIndex,
1607 LPSTR lpQualifierBuf, LPDWORD pcchQualifierBuf,
1608 LPSTR lpApplicationDataBuf, LPDWORD pcchApplicationDataBuf )
1609 {
1610 awstring qual, appdata;
1611 LPWSTR comp;
1612 UINT r;
1613
1614 TRACE("%s %08x %p %p %p %p\n", debugstr_a(szComponent), iIndex,
1615 lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
1616 pcchApplicationDataBuf);
1617
1618 comp = strdupAtoW( szComponent );
1619 if (szComponent && !comp)
1620 return ERROR_OUTOFMEMORY;
1621
1622 qual.unicode = FALSE;
1623 qual.str.a = lpQualifierBuf;
1624
1625 appdata.unicode = FALSE;
1626 appdata.str.a = lpApplicationDataBuf;
1627
1628 r = MSI_EnumComponentQualifiers( comp, iIndex,
1629 &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
1630 msi_free( comp );
1631 return r;
1632 }
1633
1634 /*************************************************************************
1635 * MsiEnumComponentQualifiersW [MSI.@]
1636 */
1637 UINT WINAPI MsiEnumComponentQualifiersW( LPCWSTR szComponent, DWORD iIndex,
1638 LPWSTR lpQualifierBuf, LPDWORD pcchQualifierBuf,
1639 LPWSTR lpApplicationDataBuf, LPDWORD pcchApplicationDataBuf )
1640 {
1641 awstring qual, appdata;
1642
1643 TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
1644 lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
1645 pcchApplicationDataBuf);
1646
1647 qual.unicode = TRUE;
1648 qual.str.w = lpQualifierBuf;
1649
1650 appdata.unicode = TRUE;
1651 appdata.str.w = lpApplicationDataBuf;
1652
1653 return MSI_EnumComponentQualifiers( szComponent, iIndex,
1654 &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
1655 }
1656
1657 /*************************************************************************
1658 * MsiEnumRelatedProductsW [MSI.@]
1659 *
1660 */
1661 UINT WINAPI MsiEnumRelatedProductsW(LPCWSTR szUpgradeCode, DWORD dwReserved,
1662 DWORD iProductIndex, LPWSTR lpProductBuf)
1663 {
1664 UINT r;
1665 HKEY hkey;
1666 DWORD dwSize = SQUISH_GUID_SIZE;
1667 WCHAR szKeyName[SQUISH_GUID_SIZE];
1668
1669 TRACE("%s %u %u %p\n", debugstr_w(szUpgradeCode), dwReserved,
1670 iProductIndex, lpProductBuf);
1671
1672 if (NULL == szUpgradeCode)
1673 return ERROR_INVALID_PARAMETER;
1674 if (NULL == lpProductBuf)
1675 return ERROR_INVALID_PARAMETER;
1676
1677 r = MSIREG_OpenUpgradeCodesKey(szUpgradeCode, &hkey, FALSE);
1678 if (r != ERROR_SUCCESS)
1679 return ERROR_NO_MORE_ITEMS;
1680
1681 r = RegEnumValueW(hkey, iProductIndex, szKeyName, &dwSize, NULL, NULL, NULL, NULL);
1682 if( r == ERROR_SUCCESS )
1683 unsquash_guid(szKeyName, lpProductBuf);
1684 RegCloseKey(hkey);
1685
1686 return r;
1687 }
1688
1689 /*************************************************************************
1690 * MsiEnumRelatedProductsA [MSI.@]
1691 *
1692 */
1693 UINT WINAPI MsiEnumRelatedProductsA(LPCSTR szUpgradeCode, DWORD dwReserved,
1694 DWORD iProductIndex, LPSTR lpProductBuf)
1695 {
1696 LPWSTR szwUpgradeCode = NULL;
1697 WCHAR productW[GUID_SIZE];
1698 UINT r;
1699
1700 TRACE("%s %u %u %p\n", debugstr_a(szUpgradeCode), dwReserved,
1701 iProductIndex, lpProductBuf);
1702
1703 if (szUpgradeCode)
1704 {
1705 szwUpgradeCode = strdupAtoW( szUpgradeCode );
1706 if( !szwUpgradeCode )
1707 return ERROR_OUTOFMEMORY;
1708 }
1709
1710 r = MsiEnumRelatedProductsW( szwUpgradeCode, dwReserved,
1711 iProductIndex, productW );
1712 if (r == ERROR_SUCCESS)
1713 {
1714 WideCharToMultiByte( CP_ACP, 0, productW, GUID_SIZE,
1715 lpProductBuf, GUID_SIZE, NULL, NULL );
1716 }
1717 msi_free( szwUpgradeCode);
1718 return r;
1719 }
1720
1721 /***********************************************************************
1722 * MsiEnumPatchesExA [MSI.@]
1723 */
1724 UINT WINAPI MsiEnumPatchesExA(LPCSTR szProductCode, LPCSTR szUserSid,
1725 DWORD dwContext, DWORD dwFilter, DWORD dwIndex, LPSTR szPatchCode,
1726 LPSTR szTargetProductCode, MSIINSTALLCONTEXT *pdwTargetProductContext,
1727 LPSTR szTargetUserSid, LPDWORD pcchTargetUserSid)
1728 {
1729 LPWSTR prodcode = NULL;
1730 LPWSTR usersid = NULL;
1731 LPWSTR targsid = NULL;
1732 WCHAR patch[GUID_SIZE];
1733 WCHAR targprod[GUID_SIZE];
1734 DWORD len;
1735 UINT r;
1736
1737 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n",
1738 debugstr_a(szProductCode), debugstr_a(szUserSid), dwContext, dwFilter,
1739 dwIndex, szPatchCode, szTargetProductCode, pdwTargetProductContext,
1740 szTargetUserSid, pcchTargetUserSid);
1741
1742 if (szTargetUserSid && !pcchTargetUserSid)
1743 return ERROR_INVALID_PARAMETER;
1744
1745 if (szProductCode) prodcode = strdupAtoW(szProductCode);
1746 if (szUserSid) usersid = strdupAtoW(szUserSid);
1747
1748 r = MsiEnumPatchesExW(prodcode, usersid, dwContext, dwFilter, dwIndex,
1749 patch, targprod, pdwTargetProductContext,
1750 NULL, &len);
1751 if (r != ERROR_SUCCESS)
1752 goto done;
1753
1754 WideCharToMultiByte(CP_ACP, 0, patch, -1, szPatchCode,
1755 GUID_SIZE, NULL, NULL);
1756 WideCharToMultiByte(CP_ACP, 0, targprod, -1, szTargetProductCode,
1757 GUID_SIZE, NULL, NULL);
1758
1759 if (!szTargetUserSid)
1760 {
1761 if (pcchTargetUserSid)
1762 *pcchTargetUserSid = len;
1763
1764 goto done;
1765 }
1766
1767 targsid = msi_alloc(++len * sizeof(WCHAR));
1768 if (!targsid)
1769 {
1770 r = ERROR_OUTOFMEMORY;
1771 goto done;
1772 }
1773
1774 r = MsiEnumPatchesExW(prodcode, usersid, dwContext, dwFilter, dwIndex,
1775 patch, targprod, pdwTargetProductContext,
1776 targsid, &len);
1777 if (r != ERROR_SUCCESS || !szTargetUserSid)
1778 goto done;
1779
1780 WideCharToMultiByte(CP_ACP, 0, targsid, -1, szTargetUserSid,
1781 *pcchTargetUserSid, NULL, NULL);
1782
1783 len = lstrlenW(targsid);
1784 if (*pcchTargetUserSid < len + 1)
1785 {
1786 r = ERROR_MORE_DATA;
1787 *pcchTargetUserSid = len * sizeof(WCHAR);
1788 }
1789 else
1790 *pcchTargetUserSid = len;
1791
1792 done:
1793 msi_free(prodcode);
1794 msi_free(usersid);
1795 msi_free(targsid);
1796
1797 return r;
1798 }
1799
1800 static UINT msi_get_patch_state(LPCWSTR prodcode, LPCWSTR usersid,
1801 MSIINSTALLCONTEXT context,
1802 LPWSTR patch, MSIPATCHSTATE *state)
1803 {
1804 DWORD type, val, size;
1805 HKEY prod, hkey = 0;
1806 HKEY udpatch = 0;
1807 LONG res;
1808 UINT r = ERROR_NO_MORE_ITEMS;
1809
1810 *state = MSIPATCHSTATE_INVALID;
1811
1812 r = MSIREG_OpenUserDataProductKey(prodcode, context,
1813 usersid, &prod, FALSE);
1814 if (r != ERROR_SUCCESS)
1815 return ERROR_NO_MORE_ITEMS;
1816
1817 res = RegOpenKeyExW(prod, szPatches, 0, KEY_READ, &hkey);
1818 if (res != ERROR_SUCCESS)
1819 goto done;
1820
1821 res = RegOpenKeyExW(hkey, patch, 0, KEY_READ, &udpatch);
1822 if (res != ERROR_SUCCESS)
1823 goto done;
1824
1825 size = sizeof(DWORD);
1826 res = RegGetValueW(udpatch, NULL, szState, RRF_RT_DWORD, &type, &val, &size);
1827 if (res != ERROR_SUCCESS ||
1828 val < MSIPATCHSTATE_APPLIED || val > MSIPATCHSTATE_REGISTERED)
1829 {
1830 r = ERROR_BAD_CONFIGURATION;
1831 goto done;
1832 }
1833
1834 *state = val;
1835 r = ERROR_SUCCESS;
1836
1837 done:
1838 RegCloseKey(udpatch);
1839 RegCloseKey(hkey);
1840 RegCloseKey(prod);
1841
1842 return r;
1843 }
1844
1845 static UINT msi_check_product_patches(LPCWSTR prodcode, LPCWSTR usersid,
1846 MSIINSTALLCONTEXT context, DWORD filter, DWORD index, DWORD *idx,
1847 LPWSTR patch, LPWSTR targetprod, MSIINSTALLCONTEXT *targetctx,
1848 LPWSTR targetsid, DWORD *sidsize, LPWSTR *transforms)
1849 {
1850 MSIPATCHSTATE state = MSIPATCHSTATE_INVALID;
1851 LPWSTR ptr, patches = NULL;
1852 HKEY prod, patchkey = 0;
1853 HKEY localprod = 0, localpatch = 0;
1854 DWORD type, size;
1855 LONG res;
1856 UINT temp, r = ERROR_NO_MORE_ITEMS;
1857
1858 if (MSIREG_OpenProductKey(prodcode, usersid, context,
1859 &prod, FALSE) != ERROR_SUCCESS)
1860 return ERROR_NO_MORE_ITEMS;
1861
1862 size = 0;
1863 res = RegGetValueW(prod, szPatches, szPatches, RRF_RT_ANY, &type, NULL,
1864 &size);
1865 if (res != ERROR_SUCCESS)
1866 goto done;
1867
1868 if (type != REG_MULTI_SZ)
1869 {
1870 r = ERROR_BAD_CONFIGURATION;
1871 goto done;
1872 }
1873
1874 patches = msi_alloc(size);
1875 if (!patches)
1876 {
1877 r = ERROR_OUTOFMEMORY;
1878 goto done;
1879 }
1880
1881 res = RegGetValueW(prod, szPatches, szPatches, RRF_RT_ANY, &type,
1882 patches, &size);
1883 if (res != ERROR_SUCCESS)
1884 goto done;
1885
1886 for (ptr = patches; *ptr && r == ERROR_NO_MORE_ITEMS; ptr += lstrlenW(ptr) + 1)
1887 {
1888 if (!unsquash_guid(ptr, patch))
1889 {
1890 r = ERROR_BAD_CONFIGURATION;
1891 goto done;
1892 }
1893
1894 size = 0;
1895 res = RegGetValueW(prod, szPatches, ptr, RRF_RT_REG_SZ,
1896 &type, NULL, &size);
1897 if (res != ERROR_SUCCESS)
1898 continue;
1899
1900 if (transforms)
1901 {
1902 *transforms = msi_alloc(size);
1903 if (!*transforms)
1904 {
1905 r = ERROR_OUTOFMEMORY;
1906 goto done;
1907 }
1908
1909 res = RegGetValueW(prod, szPatches, ptr, RRF_RT_REG_SZ,
1910 &type, *transforms, &size);
1911 if (res != ERROR_SUCCESS)
1912 continue;
1913 }
1914
1915 if (context == MSIINSTALLCONTEXT_USERMANAGED)
1916 {
1917 if (!(filter & MSIPATCHSTATE_APPLIED))
1918 {
1919 temp = msi_get_patch_state(prodcode, usersid, context,
1920 ptr, &state);
1921 if (temp == ERROR_BAD_CONFIGURATION)
1922 {
1923 r = ERROR_BAD_CONFIGURATION;
1924 goto done;
1925 }
1926
1927 if (temp != ERROR_SUCCESS || !(filter & state))
1928 continue;
1929 }
1930 }
1931 else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
1932 {
1933 if (!(filter & MSIPATCHSTATE_APPLIED))
1934 {
1935 temp = msi_get_patch_state(prodcode, usersid, context,
1936 ptr, &state);
1937 if (temp == ERROR_BAD_CONFIGURATION)
1938 {
1939 r = ERROR_BAD_CONFIGURATION;
1940 goto done;
1941 }
1942
1943 if (temp != ERROR_SUCCESS || !(filter & state))
1944 continue;
1945 }
1946 else
1947 {
1948 temp = MSIREG_OpenUserDataPatchKey(patch, context,
1949 &patchkey, FALSE);
1950 RegCloseKey(patchkey);
1951 if (temp != ERROR_SUCCESS)
1952 continue;
1953 }
1954 }
1955 else if (context == MSIINSTALLCONTEXT_MACHINE)
1956 {
1957 usersid = szEmpty;
1958
1959 if (MSIREG_OpenUserDataProductKey(prodcode, context, NULL, &localprod, FALSE) == ERROR_SUCCESS &&
1960 RegOpenKeyExW(localprod, szPatches, 0, KEY_READ, &localpatch) == ERROR_SUCCESS &&
1961 RegOpenKeyExW(localpatch, ptr, 0, KEY_READ, &patchkey) == ERROR_SUCCESS)
1962 {
1963 res = RegGetValueW(patchkey, NULL, szState, RRF_RT_REG_DWORD,
1964 &type, &state, &size);
1965
1966 if (!(filter & state))
1967 res = ERROR_NO_MORE_ITEMS;
1968
1969 RegCloseKey(patchkey);
1970 }
1971
1972 RegCloseKey(localpatch);
1973 RegCloseKey(localprod);
1974
1975 if (res != ERROR_SUCCESS)
1976 continue;
1977 }
1978
1979 if (*idx < index)
1980 {
1981 (*idx)++;
1982 continue;
1983 }
1984
1985 r = ERROR_SUCCESS;
1986 if (targetprod)
1987 lstrcpyW(targetprod, prodcode);
1988
1989 if (targetctx)
1990 *targetctx = context;
1991
1992 if (targetsid)
1993 {
1994 lstrcpynW(targetsid, usersid, *sidsize);
1995 if (lstrlenW(usersid) >= *sidsize)
1996 r = ERROR_MORE_DATA;
1997 }
1998
1999 if (sidsize)
2000 {
2001 *sidsize = lstrlenW(usersid);
2002 if (!targetsid)
2003 *sidsize *= sizeof(WCHAR);
2004 }
2005 }
2006
2007 done:
2008 RegCloseKey(prod);
2009 msi_free(patches);
2010
2011 return r;
2012 }
2013
2014 static UINT msi_enum_patches(LPCWSTR szProductCode, LPCWSTR szUserSid,
2015 DWORD dwContext, DWORD dwFilter, DWORD dwIndex, DWORD *idx,
2016 LPWSTR szPatchCode, LPWSTR szTargetProductCode,
2017 MSIINSTALLCONTEXT *pdwTargetProductContext, LPWSTR szTargetUserSid,
2018 LPDWORD pcchTargetUserSid, LPWSTR *szTransforms)
2019 {
2020 LPWSTR usersid = NULL;
2021 UINT r = ERROR_INVALID_PARAMETER;
2022
2023 if (!szUserSid)
2024 {
2025 szUserSid = usersid = get_user_sid();
2026 if (!usersid) return ERROR_FUNCTION_FAILED;
2027 }
2028
2029 if (dwContext & MSIINSTALLCONTEXT_USERMANAGED)
2030 {
2031 r = msi_check_product_patches(szProductCode, szUserSid,
2032 MSIINSTALLCONTEXT_USERMANAGED, dwFilter,
2033 dwIndex, idx, szPatchCode,
2034 szTargetProductCode,
2035 pdwTargetProductContext, szTargetUserSid,
2036 pcchTargetUserSid, szTransforms);
2037 if (r != ERROR_NO_MORE_ITEMS)
2038 goto done;
2039 }
2040
2041 if (dwContext & MSIINSTALLCONTEXT_USERUNMANAGED)
2042 {
2043 r = msi_check_product_patches(szProductCode, szUserSid,
2044 MSIINSTALLCONTEXT_USERUNMANAGED, dwFilter,
2045 dwIndex, idx, szPatchCode,
2046 szTargetProductCode,
2047 pdwTargetProductContext, szTargetUserSid,
2048 pcchTargetUserSid, szTransforms);
2049 if (r != ERROR_NO_MORE_ITEMS)
2050 goto done;
2051 }
2052
2053 if (dwContext & MSIINSTALLCONTEXT_MACHINE)
2054 {
2055 r = msi_check_product_patches(szProductCode, szUserSid,
2056 MSIINSTALLCONTEXT_MACHINE, dwFilter,
2057 dwIndex, idx, szPatchCode,
2058 szTargetProductCode,
2059 pdwTargetProductContext, szTargetUserSid,
2060 pcchTargetUserSid, szTransforms);
2061 if (r != ERROR_NO_MORE_ITEMS)
2062 goto done;
2063 }
2064
2065 done:
2066 LocalFree(usersid);
2067 return r;
2068 }
2069
2070 /***********************************************************************
2071 * MsiEnumPatchesExW [MSI.@]
2072 */
2073 UINT WINAPI MsiEnumPatchesExW(LPCWSTR szProductCode, LPCWSTR szUserSid,
2074 DWORD dwContext, DWORD dwFilter, DWORD dwIndex, LPWSTR szPatchCode,
2075 LPWSTR szTargetProductCode, MSIINSTALLCONTEXT *pdwTargetProductContext,
2076 LPWSTR szTargetUserSid, LPDWORD pcchTargetUserSid)
2077 {
2078 WCHAR squished_pc[GUID_SIZE];
2079 DWORD idx = 0;
2080 UINT r;
2081
2082 static DWORD last_index;
2083
2084 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n",
2085 debugstr_w(szProductCode), debugstr_w(szUserSid), dwContext, dwFilter,
2086 dwIndex, szPatchCode, szTargetProductCode, pdwTargetProductContext,
2087 szTargetUserSid, pcchTargetUserSid);
2088
2089 if (!szProductCode || !squash_guid(szProductCode, squished_pc))
2090 return ERROR_INVALID_PARAMETER;
2091
2092 if (szUserSid && !strcmpW( szUserSid, szLocalSid ))
2093 return ERROR_INVALID_PARAMETER;
2094
2095 if (dwContext & MSIINSTALLCONTEXT_MACHINE && szUserSid)
2096 return ERROR_INVALID_PARAMETER;
2097
2098 if (dwContext <= MSIINSTALLCONTEXT_NONE ||
2099 dwContext > MSIINSTALLCONTEXT_ALL)
2100 return ERROR_INVALID_PARAMETER;
2101
2102 if (dwFilter <= MSIPATCHSTATE_INVALID || dwFilter > MSIPATCHSTATE_ALL)
2103 return ERROR_INVALID_PARAMETER;
2104
2105 if (dwIndex && dwIndex - last_index != 1)
2106 return ERROR_INVALID_PARAMETER;
2107
2108 if (dwIndex == 0)
2109 last_index = 0;
2110
2111 r = msi_enum_patches(szProductCode, szUserSid, dwContext, dwFilter,
2112 dwIndex, &idx, szPatchCode, szTargetProductCode,
2113 pdwTargetProductContext, szTargetUserSid,
2114 pcchTargetUserSid, NULL);
2115
2116 if (r == ERROR_SUCCESS)
2117 last_index = dwIndex;
2118 else
2119 last_index = 0;
2120
2121 return r;
2122 }
2123
2124 /***********************************************************************
2125 * MsiEnumPatchesA [MSI.@]
2126 */
2127 UINT WINAPI MsiEnumPatchesA(LPCSTR szProduct, DWORD iPatchIndex,
2128 LPSTR lpPatchBuf, LPSTR lpTransformsBuf, LPDWORD pcchTransformsBuf)
2129 {
2130 LPWSTR product, transforms;
2131 WCHAR patch[GUID_SIZE];
2132 DWORD len;
2133 UINT r;
2134
2135 TRACE("(%s %d %p %p %p)\n", debugstr_a(szProduct), iPatchIndex,
2136 lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);
2137
2138 if (!szProduct || !lpPatchBuf || !lpTransformsBuf || !pcchTransformsBuf)
2139 return ERROR_INVALID_PARAMETER;
2140
2141 product = strdupAtoW(szProduct);
2142 if (!product)
2143 return ERROR_OUTOFMEMORY;
2144
2145 len = *pcchTransformsBuf;
2146 transforms = msi_alloc( len * sizeof(WCHAR) );
2147 if (!transforms)
2148 {
2149 r = ERROR_OUTOFMEMORY;
2150 goto done;
2151 }
2152
2153 r = MsiEnumPatchesW(product, iPatchIndex, patch, transforms, &len);
2154 if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
2155 goto done;
2156
2157 WideCharToMultiByte(CP_ACP, 0, patch, -1, lpPatchBuf,
2158 GUID_SIZE, NULL, NULL);
2159
2160 if (!WideCharToMultiByte(CP_ACP, 0, transforms, -1, lpTransformsBuf,
2161 *pcchTransformsBuf, NULL, NULL))
2162 r = ERROR_MORE_DATA;
2163
2164 if (r == ERROR_MORE_DATA)
2165 {
2166 lpTransformsBuf[*pcchTransformsBuf - 1] = '\0';
2167 *pcchTransformsBuf = len * 2;
2168 }
2169 else
2170 *pcchTransformsBuf = strlen( lpTransformsBuf );
2171
2172 done:
2173 msi_free(transforms);
2174 msi_free(product);
2175
2176 return r;
2177 }
2178
2179 /***********************************************************************
2180 * MsiEnumPatchesW [MSI.@]
2181 */
2182 UINT WINAPI MsiEnumPatchesW(LPCWSTR szProduct, DWORD iPatchIndex,
2183 LPWSTR lpPatchBuf, LPWSTR lpTransformsBuf, LPDWORD pcchTransformsBuf)
2184 {
2185 WCHAR squished_pc[GUID_SIZE];
2186 LPWSTR transforms = NULL;
2187 HKEY prod;
2188 DWORD idx = 0;
2189 UINT r;
2190
2191 TRACE("(%s %d %p %p %p)\n", debugstr_w(szProduct), iPatchIndex,
2192 lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);
2193
2194 if (!szProduct || !squash_guid(szProduct, squished_pc))
2195 return ERROR_INVALID_PARAMETER;
2196
2197 if (!lpPatchBuf || !lpTransformsBuf || !pcchTransformsBuf)
2198 return ERROR_INVALID_PARAMETER;
2199
2200 if (MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERMANAGED,
2201 &prod, FALSE) != ERROR_SUCCESS &&
2202 MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERUNMANAGED,
2203 &prod, FALSE) != ERROR_SUCCESS &&
2204 MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_MACHINE,
2205 &prod, FALSE) != ERROR_SUCCESS)
2206 return ERROR_UNKNOWN_PRODUCT;
2207
2208 RegCloseKey(prod);
2209
2210 r = msi_enum_patches(szProduct, NULL, MSIINSTALLCONTEXT_ALL,
2211 MSIPATCHSTATE_ALL, iPatchIndex, &idx, lpPatchBuf,
2212 NULL, NULL, NULL, NULL, &transforms);
2213 if (r != ERROR_SUCCESS)
2214 goto done;
2215
2216 lstrcpynW(lpTransformsBuf, transforms, *pcchTransformsBuf);
2217 if (*pcchTransformsBuf <= lstrlenW(transforms))
2218 {
2219 r = ERROR_MORE_DATA;
2220 *pcchTransformsBuf = lstrlenW(transforms);
2221 }
2222 else
2223 *pcchTransformsBuf = lstrlenW(transforms);
2224
2225 done:
2226 msi_free(transforms);
2227 return r;
2228 }
2229
2230 UINT WINAPI MsiEnumProductsExA( LPCSTR product, LPCSTR usersid, DWORD ctx, DWORD index,
2231 CHAR installed_product[GUID_SIZE],
2232 MSIINSTALLCONTEXT *installed_ctx, LPSTR sid, LPDWORD sid_len )
2233 {
2234 UINT r;
2235 WCHAR installed_productW[GUID_SIZE], *productW = NULL, *usersidW = NULL, *sidW = NULL;
2236
2237 TRACE("%s, %s, %u, %u, %p, %p, %p, %p\n", debugstr_a(product), debugstr_a(usersid),
2238 ctx, index, installed_product, installed_ctx, sid, sid_len);
2239
2240 if (sid && !sid_len) return ERROR_INVALID_PARAMETER;
2241 if (product && !(productW = strdupAtoW( product ))) return ERROR_OUTOFMEMORY;
2242 if (usersid && !(usersidW = strdupAtoW( usersid )))
2243 {
2244 msi_free( productW );
2245 return ERROR_OUTOFMEMORY;
2246 }
2247 if (sid && !(sidW = msi_alloc( *sid_len * sizeof(WCHAR) )))
2248 {
2249 msi_free( usersidW );
2250 msi_free( productW );
2251 return ERROR_OUTOFMEMORY;
2252 }
2253 r = MsiEnumProductsExW( productW, usersidW, ctx, index, installed_productW,
2254 installed_ctx, sidW, sid_len );
2255 if (r == ERROR_SUCCESS)
2256 {
2257 if (installed_product) WideCharToMultiByte( CP_ACP, 0, installed_productW, GUID_SIZE,
2258 installed_product, GUID_SIZE, NULL, NULL );
2259 if (sid) WideCharToMultiByte( CP_ACP, 0, sidW, *sid_len + 1, sid, *sid_len + 1, NULL, NULL );
2260 }
2261 msi_free( productW );
2262 msi_free( usersidW );
2263 msi_free( sidW );
2264 return r;
2265 }
2266
2267 static UINT fetch_machine_product( const WCHAR *match, DWORD index, DWORD *idx,
2268 WCHAR installed_product[GUID_SIZE],
2269 MSIINSTALLCONTEXT *installed_ctx, WCHAR *sid, DWORD *sid_len )
2270 {
2271 static const WCHAR productsW[] =
2272 {'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
2273 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
2274 UINT r;
2275 WCHAR product[GUID_SIZE];
2276 DWORD i = 0, len;
2277 REGSAM access = KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY;
2278 HKEY key;
2279
2280 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, productsW, 0, access, &key ))
2281 return ERROR_NO_MORE_ITEMS;
2282
2283 len = sizeof(product)/sizeof(product[0]);
2284 while (!RegEnumKeyExW( key, i, product, &len, NULL, NULL, NULL, NULL ))
2285 {
2286 if (match && strcmpW( match, product ))
2287 {
2288 i++;
2289 len = sizeof(product)/sizeof(product[0]);
2290 continue;
2291 }
2292 if (*idx == index) goto found;
2293 (*idx)++;
2294 len = sizeof(product)/sizeof(product[0]);
2295 i++;
2296 }
2297 RegCloseKey( key );
2298 return ERROR_NO_MORE_ITEMS;
2299
2300 found:
2301 if (sid_len && *sid_len < 1)
2302 {
2303 *sid_len = 1;
2304 r = ERROR_MORE_DATA;
2305 }
2306 else
2307 {
2308 if (installed_product) unsquash_guid( product, installed_product );
2309 if (installed_ctx) *installed_ctx = MSIINSTALLCONTEXT_MACHINE;
2310 if (sid)
2311 {
2312 sid[0] = 0;
2313 *sid_len = 0;
2314 }
2315 r = ERROR_SUCCESS;
2316 }
2317 RegCloseKey( key );
2318 return r;
2319 }
2320
2321 static UINT fetch_user_product( const WCHAR *match, const WCHAR *usersid, DWORD ctx, DWORD index,
2322 DWORD *idx, WCHAR installed_product[GUID_SIZE],
2323 MSIINSTALLCONTEXT *installed_ctx, WCHAR *sid, DWORD *sid_len )
2324 {
2325 static const WCHAR managedW[] =
2326 {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
2327 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s',
2328 'i','o','n','\\','I','n','s','t','a','l','l','e','r','\\','M','a','n','a','g','e','d',0};
2329 static const WCHAR managed_productsW[] =
2330 {'\\','I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
2331 static const WCHAR unmanaged_productsW[] =
2332 {'\\','S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
2333 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
2334 UINT r;
2335 const WCHAR *subkey;
2336 WCHAR path[MAX_PATH], product[GUID_SIZE], user[128];
2337 DWORD i = 0, j = 0, len_product, len_user;
2338 REGSAM access = KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY;
2339 HKEY key_users, key_products;
2340
2341 if (ctx == MSIINSTALLCONTEXT_USERMANAGED)
2342 {
2343 subkey = managed_productsW;
2344 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, managedW, 0, access, &key_users ))
2345 return ERROR_NO_MORE_ITEMS;
2346 }
2347 else if (ctx == MSIINSTALLCONTEXT_USERUNMANAGED)
2348 {
2349 subkey = unmanaged_productsW;
2350 if (RegOpenKeyExW( HKEY_USERS, NULL, 0, access, &key_users ))
2351 return ERROR_NO_MORE_ITEMS;
2352 }
2353 else return ERROR_INVALID_PARAMETER;
2354
2355 len_user = sizeof(user)/sizeof(user[0]);
2356 while (!RegEnumKeyExW( key_users, i, user, &len_user, NULL, NULL, NULL, NULL ))
2357 {
2358 if (strcmpW( usersid, user ) && strcmpW( usersid, szAllSid ))
2359 {
2360 i++;
2361 len_user = sizeof(user)/sizeof(user[0]);
2362 continue;
2363 }
2364 strcpyW( path, user );
2365 strcatW( path, subkey );
2366 if (RegOpenKeyExW( key_users, path, 0, access, &key_products ))
2367 {
2368 i++;
2369 len_user = sizeof(user)/sizeof(user[0]);
2370 continue;
2371 }
2372 len_product = sizeof(product)/sizeof(product[0]);
2373 while (!RegEnumKeyExW( key_products, j, product, &len_product, NULL, NULL, NULL, NULL ))
2374 {
2375 if (match && strcmpW( match, product ))
2376 {
2377 j++;
2378 len_product = sizeof(product)/sizeof(product[0]);
2379 continue;
2380 }
2381 if (*idx == index) goto found;
2382 (*idx)++;
2383 len_product = sizeof(product)/sizeof(product[0]);
2384 j++;
2385 }
2386 RegCloseKey( key_products );
2387 len_user = sizeof(user)/sizeof(user[0]);
2388 i++;
2389 }
2390 RegCloseKey( key_users );
2391 return ERROR_NO_MORE_ITEMS;
2392
2393 found:
2394 if (sid_len && *sid_len <= len_user)
2395 {
2396 *sid_len = len_user;
2397 r = ERROR_MORE_DATA;
2398 }
2399 else
2400 {
2401 if (installed_product) unsquash_guid( product, installed_product );
2402 if (installed_ctx) *installed_ctx = ctx;
2403 if (sid)
2404 {
2405 strcpyW( sid, user );
2406 *sid_len = len_user;
2407 }
2408 r = ERROR_SUCCESS;
2409 }
2410 RegCloseKey( key_products );
2411 RegCloseKey( key_users );
2412 return r;
2413 }
2414
2415 static UINT enum_products( const WCHAR *product, const WCHAR *usersid, DWORD ctx, DWORD index,
2416 DWORD *idx, WCHAR installed_product[GUID_SIZE],
2417 MSIINSTALLCONTEXT *installed_ctx, WCHAR *sid, DWORD *sid_len )
2418 {
2419 UINT r = ERROR_NO_MORE_ITEMS;
2420 WCHAR *user = NULL;
2421
2422 if (!usersid)
2423 {
2424 usersid = user = get_user_sid();
2425 if (!user) return ERROR_FUNCTION_FAILED;
2426 }
2427 if (ctx & MSIINSTALLCONTEXT_MACHINE)
2428 {
2429 r = fetch_machine_product( product, index, idx, installed_product, installed_ctx,
2430 sid, sid_len );
2431 if (r != ERROR_NO_MORE_ITEMS) goto done;
2432 }
2433 if (ctx & MSIINSTALLCONTEXT_USERUNMANAGED)
2434 {
2435 r = fetch_user_product( product, usersid, MSIINSTALLCONTEXT_USERUNMANAGED, index,
2436 idx, installed_product, installed_ctx, sid, sid_len );
2437 if (r != ERROR_NO_MORE_ITEMS) goto done;
2438 }
2439 if (ctx & MSIINSTALLCONTEXT_USERMANAGED)
2440 {
2441 r = fetch_user_product( product, usersid, MSIINSTALLCONTEXT_USERMANAGED, index,
2442 idx, installed_product, installed_ctx, sid, sid_len );
2443 if (r != ERROR_NO_MORE_ITEMS) goto done;
2444 }
2445
2446 done:
2447 LocalFree( user );
2448 return r;
2449 }
2450
2451 UINT WINAPI MsiEnumProductsExW( LPCWSTR product, LPCWSTR usersid, DWORD ctx, DWORD index,
2452 WCHAR installed_product[GUID_SIZE],
2453 MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid, LPDWORD sid_len )
2454 {
2455 UINT r;
2456 DWORD idx = 0;
2457 static DWORD last_index;
2458
2459 TRACE("%s, %s, %u, %u, %p, %p, %p, %p\n", debugstr_w(product), debugstr_w(usersid),
2460 ctx, index, installed_product, installed_ctx, sid, sid_len);
2461
2462 if ((sid && !sid_len) || !ctx || (usersid && ctx == MSIINSTALLCONTEXT_MACHINE))
2463 return ERROR_INVALID_PARAMETER;
2464
2465 if (index && index - last_index != 1)
2466 return ERROR_INVALID_PARAMETER;
2467
2468 if (!index) last_index = 0;
2469
2470 r = enum_products( product, usersid, ctx, index, &idx, installed_product, installed_ctx,
2471 sid, sid_len );
2472 if (r == ERROR_SUCCESS)
2473 last_index = index;
2474 else
2475 last_index = 0;
2476
2477 return r;
2478 }