winemac: Simplify format_for_type() and callers since there's at most one format...
[wine.git] / dlls / msi / registry.c
blobf3b1ddf2444dcc90edef4750fabe4f25ed84ded3
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2005 Mike McCormack for CodeWeavers
5 * Copyright 2005 Aric Stewart for CodeWeavers
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.
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.
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
22 #include <stdarg.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winnls.h"
30 #include "shlwapi.h"
31 #include "wine/debug.h"
32 #include "msi.h"
33 #include "msipriv.h"
34 #include "wincrypt.h"
35 #include "wine/unicode.h"
36 #include "winver.h"
37 #include "winuser.h"
38 #include "sddl.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msi);
42 /*
43 * This module will be all the helper functions for registry access by the
44 * installer bits.
47 static const WCHAR szUserDataFeatures_fmt[] = {
48 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
49 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
50 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
51 '%','s','\\','P','r','o','d','u','c','t','s','\\','%','s','\\','F','e','a','t','u','r','e','s',0};
53 static const WCHAR szUserDataComp_fmt[] = {
54 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
55 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
56 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
57 '%','s','\\','C','o','m','p','o','n','e','n','t','s','\\','%','s',0};
59 static const WCHAR szUserDataComponents_fmt[] = {
60 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
61 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
62 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
63 '%','s','\\','C','o','m','p','o','n','e','n','t','s',0};
65 static const WCHAR szUserDataProd_fmt[] = {
66 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
67 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
68 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
69 '%','s','\\','P','r','o','d','u','c','t','s','\\','%','s',0};
71 static const WCHAR szUserDataProducts_fmt[] = {
72 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
73 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
74 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
75 '%','s','\\','P','r','o','d','u','c','t','s',0};
77 static const WCHAR szUserDataPatch_fmt[] = {
78 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
79 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
80 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
81 '%','s','\\','P','a','t','c','h','e','s','\\','%','s',0};
83 static const WCHAR szUserDataPatches_fmt[] = {
84 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
85 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
86 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
87 '%','s','\\','P','a','t','c','h','e','s',0};
89 static const WCHAR szUserDataProductPatches_fmt[] = {
90 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
91 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
92 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
93 '%','s','\\','P','r','o','d','u','c','t','s','\\','%','s','\\','P','a','t','c','h','e','s',0};
95 static const WCHAR szInstallProperties_fmt[] = {
96 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
97 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
98 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
99 '%','s','\\','P','r','o','d','u','c','t','s','\\','%','s','\\',
100 'I','n','s','t','a','l','l','P','r','o','p','e','r','t','i','e','s',0};
102 static const WCHAR szInstaller_LocalManagedProd_fmt[] = {
103 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
104 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
105 'I','n','s','t','a','l','l','e','r','\\','M','a','n','a','g','e','d','\\','%','s','\\',
106 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s','\\','%','s',0};
108 static const WCHAR szInstaller_LocalManagedFeat_fmt[] = {
109 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
110 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
111 'I','n','s','t','a','l','l','e','r','\\','M','a','n','a','g','e','d','\\','%','s','\\',
112 'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s','\\','%','s',0};
114 static const WCHAR szInstaller_Products[] = {
115 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
116 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
117 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
119 static const WCHAR szInstaller_Patches[] = {
120 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
121 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
122 'I','n','s','t','a','l','l','e','r','\\','P','a','t','c','h','e','s',0};
124 static const WCHAR szInstaller_LocalClassesProducts[] = {
125 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
126 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
128 static const WCHAR szInstaller_LocalClassesFeatures[] = {
129 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
130 'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s',0};
132 static const WCHAR szInstaller_LocalClassesProd[] = {
133 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
134 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s','\\',0};
136 static const WCHAR szInstaller_LocalClassesFeat[] = {
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','\\','F','e','a','t','u','r','e','s','\\',0};
140 static const WCHAR szInstaller_ClassesUpgradeCode[] = {
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','\\','U','p','g','r','a','d','e','C','o','d','e','s','\\',0};
144 static const WCHAR szInstaller_ClassesUpgradeCodes[] = {
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','\\','U','p','g','r','a','d','e','C','o','d','e','s',0};
148 static const WCHAR szInstaller_Features[] = {
149 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
150 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
151 'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s','\\',0};
153 static const WCHAR szInstaller_UpgradeCodes[] = {
154 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
155 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
156 'I','n','s','t','a','l','l','e','r','\\','U','p','g','r','a','d','e','C','o','d','e','s','\\',0};
158 static const WCHAR szInstaller_UserUpgradeCodes[] = {
159 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
160 'I','n','s','t','a','l','l','e','r','\\','U','p','g','r','a','d','e','C','o','d','e','s','\\',0};
162 static const WCHAR szUninstall[] = {
163 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
164 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
165 'U','n','i','n','s','t','a','l','l','\\',0};
167 static const WCHAR szUninstall_32node[] = {
168 'S','o','f','t','w','a','r','e','\\','W','o','w','6','4','3','2','N','o','d','e','\\',
169 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
170 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','U','n','i','n','s','t','a','l','l','\\',0};
172 static const WCHAR szUserComponents[] = {
173 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
174 'I','n','s','t','a','l','l','e','r','\\','C','o','m','p','o','n','e','n','t','s','\\',0};
176 static const WCHAR szInstaller_Components[] = {
177 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
178 'I','n','s','t','a','l','l','e','r','\\','C','o','m','p','o','n','e','n','t','s','\\',0};
180 static const WCHAR szUserFeatures[] = {
181 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
182 'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s','\\',0};
184 static const WCHAR szUserProducts[] = {
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','\\','P','r','o','d','u','c','t','s','\\',0};
188 static const WCHAR szUserPatches[] = {
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','\\','P','a','t','c','h','e','s','\\',0};
192 BOOL unsquash_guid(LPCWSTR in, LPWSTR out)
194 DWORD i,n=0;
196 if (lstrlenW(in) != 32)
197 return FALSE;
199 out[n++]='{';
200 for(i=0; i<8; i++)
201 out[n++] = in[7-i];
202 out[n++]='-';
203 for(i=0; i<4; i++)
204 out[n++] = in[11-i];
205 out[n++]='-';
206 for(i=0; i<4; i++)
207 out[n++] = in[15-i];
208 out[n++]='-';
209 for(i=0; i<2; i++)
211 out[n++] = in[17+i*2];
212 out[n++] = in[16+i*2];
214 out[n++]='-';
215 for( ; i<8; i++)
217 out[n++] = in[17+i*2];
218 out[n++] = in[16+i*2];
220 out[n++]='}';
221 out[n]=0;
222 return TRUE;
225 BOOL squash_guid(LPCWSTR in, LPWSTR out)
227 DWORD i,n=1;
228 GUID guid;
230 out[0] = 0;
232 if (FAILED(CLSIDFromString((LPCOLESTR)in, &guid)))
233 return FALSE;
235 for(i=0; i<8; i++)
236 out[7-i] = in[n++];
237 n++;
238 for(i=0; i<4; i++)
239 out[11-i] = in[n++];
240 n++;
241 for(i=0; i<4; i++)
242 out[15-i] = in[n++];
243 n++;
244 for(i=0; i<2; i++)
246 out[17+i*2] = in[n++];
247 out[16+i*2] = in[n++];
249 n++;
250 for( ; i<8; i++)
252 out[17+i*2] = in[n++];
253 out[16+i*2] = in[n++];
255 out[32]=0;
256 return TRUE;
260 /* tables for encoding and decoding base85 */
261 static const unsigned char table_dec85[0x80] = {
262 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
263 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
264 0xff,0x00,0xff,0xff,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0xff,
265 0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0xff,0xff,0xff,0x16,0xff,0x17,
266 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
267 0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0xff,0x34,0x35,0x36,
268 0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x42,0x43,0x44,0x45,0x46,
269 0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0xff,0x53,0x54,0xff,
272 static const char table_enc85[] =
273 "!$%&'()*+,-.0123456789=?@ABCDEFGHIJKLMNO"
274 "PQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwx"
275 "yz{}~";
278 * Converts a base85 encoded guid into a GUID pointer
279 * Base85 encoded GUIDs should be 20 characters long.
281 * returns TRUE if successful, FALSE if not
283 BOOL decode_base85_guid( LPCWSTR str, GUID *guid )
285 DWORD i, val = 0, base = 1, *p;
287 if (!str)
288 return FALSE;
290 p = (DWORD*) guid;
291 for( i=0; i<20; i++ )
293 if( (i%5) == 0 )
295 val = 0;
296 base = 1;
298 val += table_dec85[str[i]] * base;
299 if( str[i] >= 0x80 )
300 return FALSE;
301 if( table_dec85[str[i]] == 0xff )
302 return FALSE;
303 if( (i%5) == 4 )
304 p[i/5] = val;
305 base *= 85;
307 return TRUE;
311 * Encodes a base85 guid given a GUID pointer
312 * Caller should provide a 21 character buffer for the encoded string.
314 * returns TRUE if successful, FALSE if not
316 BOOL encode_base85_guid( GUID *guid, LPWSTR str )
318 unsigned int x, *p, i;
320 p = (unsigned int*) guid;
321 for( i=0; i<4; i++ )
323 x = p[i];
324 *str++ = table_enc85[x%85];
325 x = x/85;
326 *str++ = table_enc85[x%85];
327 x = x/85;
328 *str++ = table_enc85[x%85];
329 x = x/85;
330 *str++ = table_enc85[x%85];
331 x = x/85;
332 *str++ = table_enc85[x%85];
334 *str = 0;
336 return TRUE;
339 DWORD msi_version_str_to_dword(LPCWSTR p)
341 DWORD major, minor = 0, build = 0, version = 0;
343 if (!p)
344 return version;
346 major = atoiW(p);
348 p = strchrW(p, '.');
349 if (p)
351 minor = atoiW(p+1);
352 p = strchrW(p+1, '.');
353 if (p)
354 build = atoiW(p+1);
357 return MAKELONG(build, MAKEWORD(minor, major));
360 LONG msi_reg_set_val_str( HKEY hkey, LPCWSTR name, LPCWSTR value )
362 DWORD len;
363 if (!value) value = szEmpty;
364 len = (lstrlenW(value) + 1) * sizeof (WCHAR);
365 return RegSetValueExW( hkey, name, 0, REG_SZ, (const BYTE *)value, len );
368 LONG msi_reg_set_val_multi_str( HKEY hkey, LPCWSTR name, LPCWSTR value )
370 LPCWSTR p = value;
371 while (*p) p += lstrlenW(p) + 1;
372 return RegSetValueExW( hkey, name, 0, REG_MULTI_SZ,
373 (const BYTE *)value, (p + 1 - value) * sizeof(WCHAR) );
376 LONG msi_reg_set_val_dword( HKEY hkey, LPCWSTR name, DWORD val )
378 return RegSetValueExW( hkey, name, 0, REG_DWORD, (LPBYTE)&val, sizeof (DWORD) );
381 LONG msi_reg_set_subkey_val( HKEY hkey, LPCWSTR path, LPCWSTR name, LPCWSTR val )
383 HKEY hsubkey = 0;
384 LONG r;
386 r = RegCreateKeyW( hkey, path, &hsubkey );
387 if (r != ERROR_SUCCESS)
388 return r;
389 r = msi_reg_set_val_str( hsubkey, name, val );
390 RegCloseKey( hsubkey );
391 return r;
394 LPWSTR msi_reg_get_val_str( HKEY hkey, LPCWSTR name )
396 DWORD len = 0;
397 LPWSTR val;
398 LONG r;
400 r = RegQueryValueExW(hkey, name, NULL, NULL, NULL, &len);
401 if (r != ERROR_SUCCESS)
402 return NULL;
404 len += sizeof (WCHAR);
405 val = msi_alloc( len );
406 if (!val)
407 return NULL;
408 val[0] = 0;
409 RegQueryValueExW(hkey, name, NULL, NULL, (LPBYTE) val, &len);
410 return val;
413 BOOL msi_reg_get_val_dword( HKEY hkey, LPCWSTR name, DWORD *val)
415 DWORD type, len = sizeof (DWORD);
416 LONG r = RegQueryValueExW(hkey, name, NULL, &type, (LPBYTE) val, &len);
417 return r == ERROR_SUCCESS && type == REG_DWORD;
420 static WCHAR *get_user_sid(void)
422 HANDLE token;
423 DWORD size = 256;
424 TOKEN_USER *user;
425 WCHAR *ret;
427 if (!OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &token )) return NULL;
428 if (!(user = msi_alloc( size )))
430 CloseHandle( token );
431 return NULL;
433 if (!GetTokenInformation( token, TokenUser, user, size, &size ))
435 msi_free( user );
436 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER || !(user = msi_alloc( size )))
438 CloseHandle( token );
439 return NULL;
441 GetTokenInformation( token, TokenUser, user, size, &size );
443 CloseHandle( token );
444 if (!ConvertSidToStringSidW( user->User.Sid, &ret ))
446 msi_free( user );
447 return NULL;
449 msi_free( user );
450 return ret;
453 UINT MSIREG_OpenUninstallKey(const WCHAR *product, enum platform platform, HKEY *key, BOOL create)
455 WCHAR keypath[0x200];
457 TRACE("%s\n", debugstr_w(product));
459 if (is_64bit && platform == PLATFORM_INTEL)
461 strcpyW(keypath, szUninstall_32node);
462 strcatW(keypath, product);
464 else
466 strcpyW(keypath, szUninstall);
467 strcatW(keypath, product);
469 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, KEY_ALL_ACCESS, NULL, key, NULL);
470 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, KEY_ALL_ACCESS, key);
473 UINT MSIREG_DeleteUninstallKey(const WCHAR *product, enum platform platform)
475 WCHAR keypath[0x200];
477 TRACE("%s\n", debugstr_w(product));
479 if (is_64bit && platform == PLATFORM_INTEL)
481 strcpyW(keypath, szUninstall_32node);
482 strcatW(keypath, product);
484 else
486 strcpyW(keypath, szUninstall);
487 strcatW(keypath, product);
489 return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
492 UINT MSIREG_OpenProductKey(LPCWSTR szProduct, LPCWSTR szUserSid, MSIINSTALLCONTEXT context, HKEY *key, BOOL create)
494 LPWSTR usersid = NULL;
495 HKEY root = HKEY_LOCAL_MACHINE;
496 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
497 WCHAR squished_pc[GUID_SIZE], keypath[MAX_PATH];
499 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
500 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
502 if (context == MSIINSTALLCONTEXT_MACHINE)
504 strcpyW(keypath, szInstaller_LocalClassesProd);
505 strcatW(keypath, squished_pc);
507 else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
509 root = HKEY_CURRENT_USER;
510 strcpyW(keypath, szUserProducts);
511 strcatW(keypath, squished_pc);
513 else
515 if (!szUserSid)
517 if (!(usersid = get_user_sid()))
519 ERR("Failed to retrieve user SID\n");
520 return ERROR_FUNCTION_FAILED;
522 szUserSid = usersid;
524 sprintfW(keypath, szInstaller_LocalManagedProd_fmt, szUserSid, squished_pc);
525 LocalFree(usersid);
527 if (create) return RegCreateKeyExW(root, keypath, 0, NULL, 0, access, NULL, key, NULL);
528 return RegOpenKeyExW(root, keypath, 0, access, key);
531 UINT MSIREG_DeleteUserProductKey(LPCWSTR szProduct)
533 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
535 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
536 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
538 strcpyW(keypath, szUserProducts);
539 strcatW(keypath, squished_pc);
540 return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
543 UINT MSIREG_OpenUserPatchesKey(LPCWSTR szPatch, HKEY *key, BOOL create)
545 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
547 if (!squash_guid(szPatch, squished_pc)) return ERROR_FUNCTION_FAILED;
548 TRACE("%s squished %s\n", debugstr_w(szPatch), debugstr_w(squished_pc));
550 strcpyW(keypath, szUserPatches);
551 strcatW(keypath, squished_pc);
553 if (create) return RegCreateKeyW(HKEY_CURRENT_USER, keypath, key);
554 return RegOpenKeyW(HKEY_CURRENT_USER, keypath, key);
557 UINT MSIREG_OpenFeaturesKey(LPCWSTR szProduct, LPCWSTR szUserSid, MSIINSTALLCONTEXT context,
558 HKEY *key, BOOL create)
560 HKEY root = HKEY_LOCAL_MACHINE;
561 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
562 WCHAR squished_pc[GUID_SIZE], keypath[MAX_PATH], *usersid = NULL;
564 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
565 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
567 if (context == MSIINSTALLCONTEXT_MACHINE)
569 strcpyW(keypath, szInstaller_LocalClassesFeat);
570 strcatW(keypath, squished_pc);
572 else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
574 root = HKEY_CURRENT_USER;
575 strcpyW(keypath, szUserFeatures);
576 strcatW(keypath, squished_pc);
578 else
580 if (!szUserSid)
582 if (!(usersid = get_user_sid()))
584 ERR("Failed to retrieve user SID\n");
585 return ERROR_FUNCTION_FAILED;
587 szUserSid = usersid;
589 sprintfW(keypath, szInstaller_LocalManagedFeat_fmt, szUserSid, squished_pc);
590 LocalFree(usersid);
592 if (create) return RegCreateKeyExW(root, keypath, 0, NULL, 0, access, NULL, key, NULL);
593 return RegOpenKeyExW(root, keypath, 0, access, key);
596 UINT MSIREG_DeleteUserFeaturesKey(LPCWSTR szProduct)
598 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
600 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
601 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
603 strcpyW(keypath, szUserFeatures);
604 strcatW(keypath, squished_pc);
605 return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
608 static UINT MSIREG_OpenInstallerFeaturesKey(LPCWSTR szProduct, HKEY *key, BOOL create)
610 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
611 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
613 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
614 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
616 strcpyW(keypath, szInstaller_Features);
617 strcatW(keypath, squished_pc);
619 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
620 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
623 UINT MSIREG_OpenUserDataFeaturesKey(LPCWSTR szProduct, LPCWSTR szUserSid, MSIINSTALLCONTEXT context,
624 HKEY *key, BOOL create)
626 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
627 WCHAR squished_pc[GUID_SIZE], keypath[0x200], *usersid = NULL;
629 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
630 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
632 if (context == MSIINSTALLCONTEXT_MACHINE)
634 sprintfW(keypath, szUserDataFeatures_fmt, szLocalSid, squished_pc);
636 else
638 if (!szUserSid)
640 if (!(usersid = get_user_sid()))
642 ERR("Failed to retrieve user SID\n");
643 return ERROR_FUNCTION_FAILED;
645 szUserSid = usersid;
647 sprintfW(keypath, szUserDataFeatures_fmt, szUserSid, squished_pc);
648 LocalFree(usersid);
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);
654 UINT MSIREG_OpenUserComponentsKey(LPCWSTR szComponent, HKEY *key, BOOL create)
656 WCHAR squished_cc[GUID_SIZE], keypath[0x200];
657 UINT ret;
659 if (!squash_guid(szComponent, squished_cc)) return ERROR_FUNCTION_FAILED;
660 TRACE("%s squished %s\n", debugstr_w(szComponent), debugstr_w(squished_cc));
662 strcpyW(keypath, szUserComponents);
663 strcatW(keypath, squished_cc);
665 if (create) return RegCreateKeyW(HKEY_CURRENT_USER, keypath, key);
666 ret = RegOpenKeyW(HKEY_CURRENT_USER, keypath, key);
667 if (ret != ERROR_FILE_NOT_FOUND) return ret;
669 strcpyW(keypath, szInstaller_Components);
670 strcatW(keypath, squished_cc);
671 return RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
674 UINT MSIREG_OpenUserDataComponentKey(LPCWSTR szComponent, LPCWSTR szUserSid, HKEY *key, BOOL create)
676 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
677 WCHAR comp[GUID_SIZE], keypath[0x200];
678 LPWSTR usersid;
680 if (!squash_guid(szComponent, comp)) return ERROR_FUNCTION_FAILED;
681 TRACE("%s squished %s\n", debugstr_w(szComponent), debugstr_w(comp));
683 if (!szUserSid)
685 if (!(usersid = get_user_sid()))
687 ERR("Failed to retrieve user SID\n");
688 return ERROR_FUNCTION_FAILED;
690 sprintfW(keypath, szUserDataComp_fmt, usersid, comp);
691 LocalFree(usersid);
693 else
694 sprintfW(keypath, szUserDataComp_fmt, szUserSid, comp);
696 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
697 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
700 UINT MSIREG_DeleteUserDataComponentKey(LPCWSTR szComponent, LPCWSTR szUserSid)
702 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
703 WCHAR comp[GUID_SIZE], keypath[0x200];
704 LPWSTR usersid;
705 HKEY hkey;
706 LONG r;
708 if (!squash_guid(szComponent, comp)) return ERROR_FUNCTION_FAILED;
709 TRACE("%s squished %s\n", debugstr_w(szComponent), debugstr_w(comp));
711 if (!szUserSid)
713 if (!(usersid = get_user_sid()))
715 ERR("Failed to retrieve user SID\n");
716 return ERROR_FUNCTION_FAILED;
718 sprintfW(keypath, szUserDataComponents_fmt, usersid);
719 LocalFree(usersid);
721 else
722 sprintfW(keypath, szUserDataComponents_fmt, szUserSid);
724 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, &hkey)) return ERROR_SUCCESS;
725 r = RegDeleteTreeW(hkey, comp);
726 RegCloseKey(hkey);
727 return r;
730 UINT MSIREG_OpenUserDataProductKey(LPCWSTR szProduct, MSIINSTALLCONTEXT dwContext, LPCWSTR szUserSid, HKEY *key, BOOL create)
732 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
733 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
734 LPWSTR usersid;
736 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
737 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
739 if (dwContext == MSIINSTALLCONTEXT_MACHINE)
740 sprintfW(keypath, szUserDataProd_fmt, szLocalSid, squished_pc);
741 else if (szUserSid)
742 sprintfW(keypath, szUserDataProd_fmt, szUserSid, squished_pc);
743 else
745 if (!(usersid = get_user_sid()))
747 ERR("Failed to retrieve user SID\n");
748 return ERROR_FUNCTION_FAILED;
750 sprintfW(keypath, szUserDataProd_fmt, usersid, squished_pc);
751 LocalFree(usersid);
753 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
754 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
757 UINT MSIREG_OpenUserDataPatchKey(LPCWSTR szPatch, MSIINSTALLCONTEXT dwContext, HKEY *key, BOOL create)
759 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
760 WCHAR squished_patch[GUID_SIZE], keypath[0x200];
761 LPWSTR usersid;
763 if (!squash_guid(szPatch, squished_patch)) return ERROR_FUNCTION_FAILED;
764 TRACE("%s squished %s\n", debugstr_w(szPatch), debugstr_w(squished_patch));
766 if (dwContext == MSIINSTALLCONTEXT_MACHINE)
767 sprintfW(keypath, szUserDataPatch_fmt, szLocalSid, squished_patch);
768 else
770 if (!(usersid = get_user_sid()))
772 ERR("Failed to retrieve user SID\n");
773 return ERROR_FUNCTION_FAILED;
775 sprintfW(keypath, szUserDataPatch_fmt, usersid, squished_patch);
776 LocalFree(usersid);
778 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
779 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
782 UINT MSIREG_DeleteUserDataPatchKey(LPCWSTR patch, MSIINSTALLCONTEXT context)
784 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
785 WCHAR squished_patch[GUID_SIZE], keypath[0x200];
786 LPWSTR usersid;
787 HKEY hkey;
788 LONG r;
790 if (!squash_guid(patch, squished_patch)) return ERROR_FUNCTION_FAILED;
791 TRACE("%s squished %s\n", debugstr_w(patch), debugstr_w(squished_patch));
793 if (context == MSIINSTALLCONTEXT_MACHINE)
794 sprintfW(keypath, szUserDataPatches_fmt, szLocalSid);
795 else
797 if (!(usersid = get_user_sid()))
799 ERR("Failed to retrieve user SID\n");
800 return ERROR_FUNCTION_FAILED;
802 sprintfW(keypath, szUserDataPatches_fmt, usersid);
803 LocalFree(usersid);
805 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, &hkey)) return ERROR_SUCCESS;
806 r = RegDeleteTreeW(hkey, squished_patch);
807 RegCloseKey(hkey);
808 return r;
811 UINT MSIREG_OpenUserDataProductPatchesKey(LPCWSTR product, MSIINSTALLCONTEXT context, HKEY *key, BOOL create)
813 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
814 WCHAR squished_product[GUID_SIZE], keypath[0x200];
815 LPWSTR usersid;
817 if (!squash_guid(product, squished_product)) return ERROR_FUNCTION_FAILED;
818 TRACE("%s squished %s\n", debugstr_w(product), debugstr_w(squished_product));
820 if (context == MSIINSTALLCONTEXT_MACHINE)
821 sprintfW(keypath, szUserDataProductPatches_fmt, szLocalSid, squished_product);
822 else
824 if (!(usersid = get_user_sid()))
826 ERR("Failed to retrieve user SID\n");
827 return ERROR_FUNCTION_FAILED;
829 sprintfW(keypath, szUserDataProductPatches_fmt, usersid, squished_product);
830 LocalFree(usersid);
832 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
833 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
836 UINT MSIREG_OpenInstallProps(LPCWSTR szProduct, MSIINSTALLCONTEXT dwContext, LPCWSTR szUserSid, HKEY *key, BOOL create)
838 LPWSTR usersid;
839 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
840 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
842 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
843 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
845 if (dwContext == MSIINSTALLCONTEXT_MACHINE)
846 sprintfW(keypath, szInstallProperties_fmt, szLocalSid, squished_pc);
847 else if (szUserSid)
848 sprintfW(keypath, szInstallProperties_fmt, szUserSid, squished_pc);
849 else
851 if (!(usersid = get_user_sid()))
853 ERR("Failed to retrieve user SID\n");
854 return ERROR_FUNCTION_FAILED;
856 sprintfW(keypath, szInstallProperties_fmt, usersid, squished_pc);
857 LocalFree(usersid);
859 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
860 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
863 UINT MSIREG_DeleteUserDataProductKey(LPCWSTR szProduct)
865 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
866 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
867 LPWSTR usersid;
868 HKEY hkey;
869 LONG r;
871 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
872 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
874 if (!(usersid = get_user_sid()))
876 ERR("Failed to retrieve user SID\n");
877 return ERROR_FUNCTION_FAILED;
879 sprintfW(keypath, szUserDataProducts_fmt, usersid);
880 LocalFree(usersid);
882 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, &hkey)) return ERROR_SUCCESS;
883 r = RegDeleteTreeW(hkey, squished_pc);
884 RegCloseKey(hkey);
885 return r;
888 UINT MSIREG_DeleteProductKey(LPCWSTR szProduct)
890 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
891 WCHAR squished_pc[GUID_SIZE];
892 HKEY hkey;
893 LONG r;
895 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
896 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
898 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_Products, 0, access, &hkey)) return ERROR_SUCCESS;
899 r = RegDeleteTreeW(hkey, squished_pc);
900 RegCloseKey(hkey);
901 return r;
904 UINT MSIREG_OpenPatchesKey(LPCWSTR szPatch, HKEY *key, BOOL create)
906 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
907 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
909 if (!squash_guid(szPatch, squished_pc)) return ERROR_FUNCTION_FAILED;
910 TRACE("%s squished %s\n", debugstr_w(szPatch), debugstr_w(squished_pc));
912 sprintfW(keypath, szInstaller_Patches, squished_pc);
914 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
915 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
918 UINT MSIREG_OpenUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY *key, BOOL create)
920 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
921 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
923 if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
924 TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
926 strcpyW(keypath, szInstaller_UpgradeCodes);
927 strcatW(keypath, squished_pc);
929 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
930 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
933 UINT MSIREG_OpenUserUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY* key, BOOL create)
935 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
937 if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
938 TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
940 strcpyW(keypath, szInstaller_UserUpgradeCodes);
941 strcatW(keypath, squished_pc);
943 if (create) return RegCreateKeyW(HKEY_CURRENT_USER, keypath, key);
944 return RegOpenKeyW(HKEY_CURRENT_USER, keypath, key);
947 UINT MSIREG_DeleteUserUpgradeCodesKey(LPCWSTR szUpgradeCode)
949 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
951 if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
952 TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
954 strcpyW(keypath, szInstaller_UserUpgradeCodes);
955 strcatW(keypath, squished_pc);
956 return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
959 UINT MSIREG_DeleteLocalClassesProductKey(LPCWSTR szProductCode)
961 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
962 WCHAR squished_pc[GUID_SIZE];
963 HKEY hkey;
964 LONG r;
966 if (!squash_guid(szProductCode, squished_pc)) return ERROR_FUNCTION_FAILED;
967 TRACE("%s squished %s\n", debugstr_w(szProductCode), debugstr_w(squished_pc));
969 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_LocalClassesProducts, 0, access, &hkey)) return ERROR_SUCCESS;
970 r = RegDeleteTreeW(hkey, squished_pc);
971 RegCloseKey(hkey);
972 return r;
975 UINT MSIREG_DeleteLocalClassesFeaturesKey(LPCWSTR szProductCode)
977 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
978 WCHAR squished_pc[GUID_SIZE];
979 HKEY hkey;
980 LONG r;
982 if (!squash_guid(szProductCode, squished_pc)) return ERROR_FUNCTION_FAILED;
983 TRACE("%s squished %s\n", debugstr_w(szProductCode), debugstr_w(squished_pc));
985 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_LocalClassesFeatures, 0, access, &hkey)) return ERROR_SUCCESS;
986 r = RegDeleteTreeW(hkey, squished_pc);
987 RegCloseKey(hkey);
988 return r;
991 UINT MSIREG_OpenClassesUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY *key, BOOL create)
993 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
994 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
996 if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
997 TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
999 strcpyW(keypath, szInstaller_ClassesUpgradeCode);
1000 strcatW(keypath, squished_pc);
1002 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
1003 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
1006 UINT MSIREG_DeleteClassesUpgradeCodesKey(LPCWSTR szUpgradeCode)
1008 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
1009 WCHAR squished_pc[GUID_SIZE];
1010 HKEY hkey;
1011 LONG r;
1013 if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
1014 TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
1016 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_ClassesUpgradeCodes, 0, access, &hkey)) return ERROR_SUCCESS;
1017 r = RegDeleteTreeW(hkey, squished_pc);
1018 RegCloseKey(hkey);
1019 return r;
1022 /*************************************************************************
1023 * MsiDecomposeDescriptorW [MSI.@]
1025 * Decomposes an MSI descriptor into product, feature and component parts.
1026 * An MSI descriptor is a string of the form:
1027 * [base 85 guid] [feature code] '>' [base 85 guid]
1029 * PARAMS
1030 * szDescriptor [I] the descriptor to decompose
1031 * szProduct [O] buffer of MAX_FEATURE_CHARS+1 for the product guid
1032 * szFeature [O] buffer of MAX_FEATURE_CHARS+1 for the feature code
1033 * szComponent [O] buffer of MAX_FEATURE_CHARS+1 for the component guid
1034 * pUsed [O] the length of the descriptor
1036 * RETURNS
1037 * ERROR_SUCCESS if everything worked correctly
1038 * ERROR_INVALID_PARAMETER if the descriptor was invalid
1041 UINT WINAPI MsiDecomposeDescriptorW( LPCWSTR szDescriptor, LPWSTR szProduct,
1042 LPWSTR szFeature, LPWSTR szComponent, LPDWORD pUsed )
1044 UINT r, len;
1045 LPWSTR p;
1046 GUID product, component;
1048 TRACE("%s %p %p %p %p\n", debugstr_w(szDescriptor), szProduct,
1049 szFeature, szComponent, pUsed);
1051 r = decode_base85_guid( szDescriptor, &product );
1052 if( !r )
1053 return ERROR_INVALID_PARAMETER;
1055 TRACE("product %s\n", debugstr_guid( &product ));
1057 p = strchrW(&szDescriptor[20],'>');
1058 if( !p )
1059 return ERROR_INVALID_PARAMETER;
1061 len = (p - &szDescriptor[20]);
1062 if( len > MAX_FEATURE_CHARS )
1063 return ERROR_INVALID_PARAMETER;
1065 TRACE("feature %s\n", debugstr_wn( &szDescriptor[20], len ));
1067 r = decode_base85_guid( p+1, &component );
1068 if( !r )
1069 return ERROR_INVALID_PARAMETER;
1071 TRACE("component %s\n", debugstr_guid( &component ));
1073 if (szProduct)
1074 StringFromGUID2( &product, szProduct, MAX_FEATURE_CHARS+1 );
1075 if (szComponent)
1076 StringFromGUID2( &component, szComponent, MAX_FEATURE_CHARS+1 );
1077 if (szFeature)
1079 memcpy( szFeature, &szDescriptor[20], len*sizeof(WCHAR) );
1080 szFeature[len] = 0;
1082 len = ( &p[21] - szDescriptor );
1084 TRACE("length = %d\n", len);
1085 if (pUsed) *pUsed = len;
1087 return ERROR_SUCCESS;
1090 UINT WINAPI MsiDecomposeDescriptorA( LPCSTR szDescriptor, LPSTR szProduct,
1091 LPSTR szFeature, LPSTR szComponent, LPDWORD pUsed )
1093 WCHAR product[MAX_FEATURE_CHARS+1];
1094 WCHAR feature[MAX_FEATURE_CHARS+1];
1095 WCHAR component[MAX_FEATURE_CHARS+1];
1096 LPWSTR str = NULL, p = NULL, f = NULL, c = NULL;
1097 UINT r;
1099 TRACE("%s %p %p %p %p\n", debugstr_a(szDescriptor), szProduct,
1100 szFeature, szComponent, pUsed);
1102 str = strdupAtoW( szDescriptor );
1103 if( szDescriptor && !str )
1104 return ERROR_OUTOFMEMORY;
1106 if (szProduct)
1107 p = product;
1108 if (szFeature)
1109 f = feature;
1110 if (szComponent)
1111 c = component;
1113 r = MsiDecomposeDescriptorW( str, p, f, c, pUsed );
1115 if (r == ERROR_SUCCESS)
1117 WideCharToMultiByte( CP_ACP, 0, p, -1,
1118 szProduct, MAX_FEATURE_CHARS+1, NULL, NULL );
1119 WideCharToMultiByte( CP_ACP, 0, f, -1,
1120 szFeature, MAX_FEATURE_CHARS+1, NULL, NULL );
1121 WideCharToMultiByte( CP_ACP, 0, c, -1,
1122 szComponent, MAX_FEATURE_CHARS+1, NULL, NULL );
1125 msi_free( str );
1127 return r;
1130 UINT WINAPI MsiEnumProductsA(DWORD index, LPSTR lpguid)
1132 DWORD r;
1133 WCHAR szwGuid[GUID_SIZE];
1135 TRACE("%d %p\n", index, lpguid);
1137 if (NULL == lpguid)
1138 return ERROR_INVALID_PARAMETER;
1139 r = MsiEnumProductsW(index, szwGuid);
1140 if( r == ERROR_SUCCESS )
1141 WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
1143 return r;
1146 UINT WINAPI MsiEnumProductsW(DWORD index, LPWSTR lpguid)
1148 TRACE("%d %p\n", index, lpguid);
1150 if (NULL == lpguid)
1151 return ERROR_INVALID_PARAMETER;
1153 return MsiEnumProductsExW( NULL, szAllSid, MSIINSTALLCONTEXT_ALL, index, lpguid,
1154 NULL, NULL, NULL );
1157 UINT WINAPI MsiEnumFeaturesA(LPCSTR szProduct, DWORD index,
1158 LPSTR szFeature, LPSTR szParent)
1160 DWORD r;
1161 WCHAR szwFeature[GUID_SIZE], szwParent[GUID_SIZE];
1162 LPWSTR szwProduct = NULL;
1164 TRACE("%s %d %p %p\n", debugstr_a(szProduct), index, szFeature, szParent);
1166 if( szProduct )
1168 szwProduct = strdupAtoW( szProduct );
1169 if( !szwProduct )
1170 return ERROR_OUTOFMEMORY;
1173 r = MsiEnumFeaturesW(szwProduct, index, szwFeature, szwParent);
1174 if( r == ERROR_SUCCESS )
1176 WideCharToMultiByte(CP_ACP, 0, szwFeature, -1,
1177 szFeature, GUID_SIZE, NULL, NULL);
1178 WideCharToMultiByte(CP_ACP, 0, szwParent, -1,
1179 szParent, GUID_SIZE, NULL, NULL);
1182 msi_free( szwProduct);
1184 return r;
1187 UINT WINAPI MsiEnumFeaturesW(LPCWSTR szProduct, DWORD index,
1188 LPWSTR szFeature, LPWSTR szParent)
1190 HKEY hkeyProduct = 0;
1191 DWORD r, sz;
1193 TRACE("%s %d %p %p\n", debugstr_w(szProduct), index, szFeature, szParent);
1195 if( !szProduct )
1196 return ERROR_INVALID_PARAMETER;
1198 r = MSIREG_OpenInstallerFeaturesKey(szProduct,&hkeyProduct,FALSE);
1199 if( r != ERROR_SUCCESS )
1200 return ERROR_NO_MORE_ITEMS;
1202 sz = GUID_SIZE;
1203 r = RegEnumValueW(hkeyProduct, index, szFeature, &sz, NULL, NULL, NULL, NULL);
1204 RegCloseKey(hkeyProduct);
1206 return r;
1209 UINT WINAPI MsiEnumComponentsA(DWORD index, LPSTR lpguid)
1211 DWORD r;
1212 WCHAR szwGuid[GUID_SIZE];
1214 TRACE("%u, %p\n", index, lpguid);
1216 if (!lpguid) return ERROR_INVALID_PARAMETER;
1218 r = MsiEnumComponentsW(index, szwGuid);
1219 if( r == ERROR_SUCCESS )
1220 WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
1222 return r;
1225 UINT WINAPI MsiEnumComponentsW(DWORD index, LPWSTR lpguid)
1227 TRACE("%u, %p\n", index, lpguid);
1229 if (!lpguid) return ERROR_INVALID_PARAMETER;
1231 return MsiEnumComponentsExW( szAllSid, MSIINSTALLCONTEXT_ALL, index, lpguid, NULL, NULL, NULL );
1234 UINT WINAPI MsiEnumComponentsExA( LPCSTR user_sid, DWORD ctx, DWORD index, CHAR guid[39],
1235 MSIINSTALLCONTEXT *installed_ctx, LPSTR sid, LPDWORD sid_len )
1237 UINT r;
1238 WCHAR *user_sidW = NULL, *sidW = NULL, guidW[GUID_SIZE];
1240 TRACE("%s, %u, %u, %p, %p, %p, %p\n", debugstr_a(user_sid), ctx, index, guid, installed_ctx,
1241 sid, sid_len);
1243 if (sid && !sid_len) return ERROR_INVALID_PARAMETER;
1244 if (user_sid && !(user_sidW = strdupAtoW( user_sid ))) return ERROR_OUTOFMEMORY;
1245 if (sid && !(sidW = msi_alloc( *sid_len * sizeof(WCHAR) )))
1247 msi_free( user_sidW );
1248 return ERROR_OUTOFMEMORY;
1250 r = MsiEnumComponentsExW( user_sidW, ctx, index, guidW, installed_ctx, sidW, sid_len );
1251 if (r == ERROR_SUCCESS)
1253 if (guid) WideCharToMultiByte( CP_ACP, 0, guidW, GUID_SIZE, guid, GUID_SIZE, NULL, NULL );
1254 if (sid) WideCharToMultiByte( CP_ACP, 0, sidW, *sid_len + 1, sid, *sid_len + 1, NULL, NULL );
1256 msi_free( user_sidW );
1257 msi_free( sidW );
1258 return r;
1261 static UINT fetch_machine_component( DWORD ctx, DWORD index, DWORD *idx, WCHAR guid[39],
1262 MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid, LPDWORD sid_len )
1264 static const WCHAR componentsW[] =
1265 {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1266 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1267 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
1268 'S','-','1','-','5','-','1','8','\\','C','o','m','p','o','n','e','n','t','s',0};
1269 UINT r = ERROR_SUCCESS;
1270 WCHAR component[GUID_SIZE];
1271 DWORD i = 0, len_component;
1272 REGSAM access = KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY;
1273 HKEY key_components;
1275 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, componentsW, 0, access, &key_components ))
1276 return ERROR_NO_MORE_ITEMS;
1278 len_component = sizeof(component)/sizeof(component[0]);
1279 while (!RegEnumKeyExW( key_components, i, component, &len_component, NULL, NULL, NULL, NULL ))
1281 if (*idx == index) goto found;
1282 (*idx)++;
1283 len_component = sizeof(component)/sizeof(component[0]);
1284 i++;
1286 RegCloseKey( key_components );
1287 return ERROR_NO_MORE_ITEMS;
1289 found:
1290 if (sid_len)
1292 if (*sid_len < 1)
1294 *sid_len = 1;
1295 r = ERROR_MORE_DATA;
1297 else if (sid)
1299 *sid_len = 0;
1300 sid[0] = 0;
1303 if (guid) unsquash_guid( component, guid );
1304 if (installed_ctx) *installed_ctx = MSIINSTALLCONTEXT_MACHINE;
1305 RegCloseKey( key_components );
1306 return r;
1309 static UINT fetch_user_component( const WCHAR *usersid, DWORD ctx, DWORD index, DWORD *idx,
1310 WCHAR guid[39], MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid,
1311 LPDWORD sid_len )
1313 static const WCHAR userdataW[] =
1314 {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1315 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1316 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a',0};
1317 static const WCHAR componentsW[] = {'\\','C','o','m','p','o','n','e','n','t','s',0};
1318 UINT r = ERROR_SUCCESS;
1319 WCHAR path[MAX_PATH], component[GUID_SIZE], user[128];
1320 DWORD i = 0, j = 0, len_component, len_user;
1321 REGSAM access = KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY;
1322 HKEY key_users, key_components;
1324 if (ctx == MSIINSTALLCONTEXT_USERMANAGED) /* FIXME: where to find these? */
1325 return ERROR_NO_MORE_ITEMS;
1327 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, userdataW, 0, access, &key_users ))
1328 return ERROR_NO_MORE_ITEMS;
1330 len_user = sizeof(user)/sizeof(user[0]);
1331 while (!RegEnumKeyExW( key_users, i, user, &len_user, NULL, NULL, NULL, NULL ))
1333 if ((strcmpW( usersid, szAllSid ) && strcmpW( usersid, user )) ||
1334 !strcmpW( szLocalSid, user ))
1336 i++;
1337 len_user = sizeof(user)/sizeof(user[0]);
1338 continue;
1340 strcpyW( path, user );
1341 strcatW( path, componentsW );
1342 if (RegOpenKeyExW( key_users, path, 0, access, &key_components ))
1344 i++;
1345 len_user = sizeof(user)/sizeof(user[0]);
1346 continue;
1348 len_component = sizeof(component)/sizeof(component[0]);
1349 while (!RegEnumKeyExW( key_components, j, component, &len_component, NULL, NULL, NULL, NULL ))
1351 if (*idx == index) goto found;
1352 (*idx)++;
1353 len_component = sizeof(component)/sizeof(component[0]);
1354 j++;
1356 RegCloseKey( key_components );
1357 len_user = sizeof(user)/sizeof(user[0]);
1358 i++;
1360 RegCloseKey( key_users );
1361 return ERROR_NO_MORE_ITEMS;
1363 found:
1364 if (sid_len)
1366 if (*sid_len < len_user + 1)
1368 *sid_len = len_user + 1;
1369 r = ERROR_MORE_DATA;
1371 else if (sid)
1373 *sid_len = len_user;
1374 strcpyW( sid, user );
1377 if (guid) unsquash_guid( component, guid );
1378 if (installed_ctx) *installed_ctx = ctx;
1379 RegCloseKey( key_components );
1380 RegCloseKey( key_users );
1381 return r;
1384 static UINT enum_components( const WCHAR *usersid, DWORD ctx, DWORD index, DWORD *idx, WCHAR guid[39],
1385 MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid, LPDWORD sid_len )
1387 UINT r = ERROR_NO_MORE_ITEMS;
1388 WCHAR *user = NULL;
1390 if (!usersid)
1392 usersid = user = get_user_sid();
1393 if (!user) return ERROR_FUNCTION_FAILED;
1395 if (ctx & MSIINSTALLCONTEXT_USERMANAGED)
1397 r = fetch_user_component( usersid, MSIINSTALLCONTEXT_USERMANAGED, index, idx, guid,
1398 installed_ctx, sid, sid_len );
1399 if (r != ERROR_NO_MORE_ITEMS) goto done;
1401 if (ctx & MSIINSTALLCONTEXT_USERUNMANAGED)
1403 r = fetch_user_component( usersid, MSIINSTALLCONTEXT_USERUNMANAGED, index, idx, guid,
1404 installed_ctx, sid, sid_len );
1405 if (r != ERROR_NO_MORE_ITEMS) goto done;
1407 if (ctx & MSIINSTALLCONTEXT_MACHINE)
1409 r = fetch_machine_component( MSIINSTALLCONTEXT_MACHINE, index, idx, guid, installed_ctx,
1410 sid, sid_len );
1411 if (r != ERROR_NO_MORE_ITEMS) goto done;
1414 done:
1415 LocalFree( user );
1416 return r;
1419 UINT WINAPI MsiEnumComponentsExW( LPCWSTR user_sid, DWORD ctx, DWORD index, WCHAR guid[39],
1420 MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid, LPDWORD sid_len )
1422 UINT r;
1423 DWORD idx = 0;
1424 static DWORD last_index;
1426 TRACE("%s, %u, %u, %p, %p, %p, %p\n", debugstr_w(user_sid), ctx, index, guid, installed_ctx,
1427 sid, sid_len);
1429 if ((sid && !sid_len) || !ctx || (user_sid && ctx == MSIINSTALLCONTEXT_MACHINE))
1430 return ERROR_INVALID_PARAMETER;
1432 if (index && index - last_index != 1)
1433 return ERROR_INVALID_PARAMETER;
1435 if (!index) last_index = 0;
1437 r = enum_components( user_sid, ctx, index, &idx, guid, installed_ctx, sid, sid_len );
1438 if (r == ERROR_SUCCESS)
1439 last_index = index;
1440 else
1441 last_index = 0;
1443 return r;
1446 UINT WINAPI MsiEnumClientsA(LPCSTR szComponent, DWORD index, LPSTR szProduct)
1448 DWORD r;
1449 WCHAR szwProduct[GUID_SIZE];
1450 LPWSTR szwComponent = NULL;
1452 TRACE("%s %d %p\n", debugstr_a(szComponent), index, szProduct);
1454 if ( !szProduct )
1455 return ERROR_INVALID_PARAMETER;
1457 if( szComponent )
1459 szwComponent = strdupAtoW( szComponent );
1460 if( !szwComponent )
1461 return ERROR_OUTOFMEMORY;
1464 r = MsiEnumClientsW(szComponent?szwComponent:NULL, index, szwProduct);
1465 if( r == ERROR_SUCCESS )
1467 WideCharToMultiByte(CP_ACP, 0, szwProduct, -1,
1468 szProduct, GUID_SIZE, NULL, NULL);
1471 msi_free( szwComponent);
1473 return r;
1476 UINT WINAPI MsiEnumClientsW(LPCWSTR szComponent, DWORD index, LPWSTR szProduct)
1478 HKEY hkeyComp = 0;
1479 DWORD r, sz;
1480 WCHAR szValName[SQUISH_GUID_SIZE];
1482 TRACE("%s %d %p\n", debugstr_w(szComponent), index, szProduct);
1484 if (!szComponent || !*szComponent || !szProduct)
1485 return ERROR_INVALID_PARAMETER;
1487 if (MSIREG_OpenUserDataComponentKey(szComponent, NULL, &hkeyComp, FALSE) != ERROR_SUCCESS &&
1488 MSIREG_OpenUserDataComponentKey(szComponent, szLocalSid, &hkeyComp, FALSE) != ERROR_SUCCESS)
1489 return ERROR_UNKNOWN_COMPONENT;
1491 /* see if there are any products at all */
1492 sz = SQUISH_GUID_SIZE;
1493 r = RegEnumValueW(hkeyComp, 0, szValName, &sz, NULL, NULL, NULL, NULL);
1494 if (r != ERROR_SUCCESS)
1496 RegCloseKey(hkeyComp);
1498 if (index != 0)
1499 return ERROR_INVALID_PARAMETER;
1501 return ERROR_UNKNOWN_COMPONENT;
1504 sz = SQUISH_GUID_SIZE;
1505 r = RegEnumValueW(hkeyComp, index, szValName, &sz, NULL, NULL, NULL, NULL);
1506 if( r == ERROR_SUCCESS )
1508 unsquash_guid(szValName, szProduct);
1509 TRACE("-> %s\n", debugstr_w(szProduct));
1511 RegCloseKey(hkeyComp);
1512 return r;
1515 UINT WINAPI MsiEnumClientsExA(LPCSTR component, LPCSTR usersid, DWORD ctx, DWORD index,
1516 CHAR installed_product[GUID_SIZE],
1517 MSIINSTALLCONTEXT *installed_ctx, LPSTR sid, LPDWORD sid_len)
1519 FIXME("%s, %s, %u, %u, %p, %p, %p, %p\n", debugstr_a(component), debugstr_a(usersid),
1520 ctx, index, installed_product, installed_ctx, sid, sid_len);
1521 return ERROR_ACCESS_DENIED;
1524 UINT WINAPI MsiEnumClientsExW(LPCWSTR component, LPCWSTR usersid, DWORD ctx, DWORD index,
1525 WCHAR installed_product[GUID_SIZE],
1526 MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid, LPDWORD sid_len)
1528 FIXME("%s, %s, %u, %u, %p, %p, %p, %p\n", debugstr_w(component), debugstr_w(usersid),
1529 ctx, index, installed_product, installed_ctx, sid, sid_len);
1530 return ERROR_ACCESS_DENIED;
1533 static UINT MSI_EnumComponentQualifiers( LPCWSTR szComponent, DWORD iIndex,
1534 awstring *lpQualBuf, LPDWORD pcchQual,
1535 awstring *lpAppBuf, LPDWORD pcchAppBuf )
1537 DWORD name_sz, val_sz, name_max, val_max, type, ofs;
1538 LPWSTR name = NULL, val = NULL;
1539 UINT r, r2;
1540 HKEY key;
1542 TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
1543 lpQualBuf, pcchQual, lpAppBuf, pcchAppBuf);
1545 if (!szComponent)
1546 return ERROR_INVALID_PARAMETER;
1548 r = MSIREG_OpenUserComponentsKey( szComponent, &key, FALSE );
1549 if (r != ERROR_SUCCESS)
1550 return ERROR_UNKNOWN_COMPONENT;
1552 /* figure out how big the name is we want to return */
1553 name_max = 0x10;
1554 r = ERROR_OUTOFMEMORY;
1555 name = msi_alloc( name_max * sizeof(WCHAR) );
1556 if (!name)
1557 goto end;
1559 val_max = 0x10;
1560 r = ERROR_OUTOFMEMORY;
1561 val = msi_alloc( val_max );
1562 if (!val)
1563 goto end;
1565 /* loop until we allocate enough memory */
1566 while (1)
1568 name_sz = name_max;
1569 val_sz = val_max;
1570 r = RegEnumValueW( key, iIndex, name, &name_sz,
1571 NULL, &type, (LPBYTE)val, &val_sz );
1572 if (r == ERROR_SUCCESS)
1573 break;
1574 if (r != ERROR_MORE_DATA)
1575 goto end;
1577 if (type != REG_MULTI_SZ)
1579 ERR("component data has wrong type (%d)\n", type);
1580 goto end;
1583 r = ERROR_OUTOFMEMORY;
1584 if (name_sz + 1 >= name_max)
1586 name_max *= 2;
1587 msi_free( name );
1588 name = msi_alloc( name_max * sizeof (WCHAR) );
1589 if (!name)
1590 goto end;
1591 continue;
1593 if (val_sz > val_max)
1595 val_max = val_sz + sizeof (WCHAR);
1596 msi_free( val );
1597 val = msi_alloc( val_max * sizeof (WCHAR) );
1598 if (!val)
1599 goto end;
1600 continue;
1602 ERR("should be enough data, but isn't %d %d\n", name_sz, val_sz );
1603 goto end;
1606 ofs = 0;
1607 r = MsiDecomposeDescriptorW( val, NULL, NULL, NULL, &ofs );
1608 if (r != ERROR_SUCCESS)
1609 goto end;
1611 TRACE("Providing %s and %s\n", debugstr_w(name), debugstr_w(val+ofs));
1613 r = msi_strcpy_to_awstring( name, -1, lpQualBuf, pcchQual );
1614 r2 = msi_strcpy_to_awstring( val+ofs, -1, lpAppBuf, pcchAppBuf );
1616 if (r2 != ERROR_SUCCESS)
1617 r = r2;
1619 end:
1620 msi_free(val);
1621 msi_free(name);
1622 RegCloseKey(key);
1623 return r;
1626 /*************************************************************************
1627 * MsiEnumComponentQualifiersA [MSI.@]
1629 UINT WINAPI MsiEnumComponentQualifiersA( LPCSTR szComponent, DWORD iIndex,
1630 LPSTR lpQualifierBuf, LPDWORD pcchQualifierBuf,
1631 LPSTR lpApplicationDataBuf, LPDWORD pcchApplicationDataBuf )
1633 awstring qual, appdata;
1634 LPWSTR comp;
1635 UINT r;
1637 TRACE("%s %08x %p %p %p %p\n", debugstr_a(szComponent), iIndex,
1638 lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
1639 pcchApplicationDataBuf);
1641 comp = strdupAtoW( szComponent );
1642 if (szComponent && !comp)
1643 return ERROR_OUTOFMEMORY;
1645 qual.unicode = FALSE;
1646 qual.str.a = lpQualifierBuf;
1648 appdata.unicode = FALSE;
1649 appdata.str.a = lpApplicationDataBuf;
1651 r = MSI_EnumComponentQualifiers( comp, iIndex,
1652 &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
1653 msi_free( comp );
1654 return r;
1657 /*************************************************************************
1658 * MsiEnumComponentQualifiersW [MSI.@]
1660 UINT WINAPI MsiEnumComponentQualifiersW( LPCWSTR szComponent, DWORD iIndex,
1661 LPWSTR lpQualifierBuf, LPDWORD pcchQualifierBuf,
1662 LPWSTR lpApplicationDataBuf, LPDWORD pcchApplicationDataBuf )
1664 awstring qual, appdata;
1666 TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
1667 lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
1668 pcchApplicationDataBuf);
1670 qual.unicode = TRUE;
1671 qual.str.w = lpQualifierBuf;
1673 appdata.unicode = TRUE;
1674 appdata.str.w = lpApplicationDataBuf;
1676 return MSI_EnumComponentQualifiers( szComponent, iIndex,
1677 &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
1680 /*************************************************************************
1681 * MsiEnumRelatedProductsW [MSI.@]
1684 UINT WINAPI MsiEnumRelatedProductsW(LPCWSTR szUpgradeCode, DWORD dwReserved,
1685 DWORD iProductIndex, LPWSTR lpProductBuf)
1687 UINT r;
1688 HKEY hkey;
1689 DWORD dwSize = SQUISH_GUID_SIZE;
1690 WCHAR szKeyName[SQUISH_GUID_SIZE];
1692 TRACE("%s %u %u %p\n", debugstr_w(szUpgradeCode), dwReserved,
1693 iProductIndex, lpProductBuf);
1695 if (NULL == szUpgradeCode)
1696 return ERROR_INVALID_PARAMETER;
1697 if (NULL == lpProductBuf)
1698 return ERROR_INVALID_PARAMETER;
1700 r = MSIREG_OpenUpgradeCodesKey(szUpgradeCode, &hkey, FALSE);
1701 if (r != ERROR_SUCCESS)
1702 return ERROR_NO_MORE_ITEMS;
1704 r = RegEnumValueW(hkey, iProductIndex, szKeyName, &dwSize, NULL, NULL, NULL, NULL);
1705 if( r == ERROR_SUCCESS )
1706 unsquash_guid(szKeyName, lpProductBuf);
1707 RegCloseKey(hkey);
1709 return r;
1712 /*************************************************************************
1713 * MsiEnumRelatedProductsA [MSI.@]
1716 UINT WINAPI MsiEnumRelatedProductsA(LPCSTR szUpgradeCode, DWORD dwReserved,
1717 DWORD iProductIndex, LPSTR lpProductBuf)
1719 LPWSTR szwUpgradeCode = NULL;
1720 WCHAR productW[GUID_SIZE];
1721 UINT r;
1723 TRACE("%s %u %u %p\n", debugstr_a(szUpgradeCode), dwReserved,
1724 iProductIndex, lpProductBuf);
1726 if (szUpgradeCode)
1728 szwUpgradeCode = strdupAtoW( szUpgradeCode );
1729 if( !szwUpgradeCode )
1730 return ERROR_OUTOFMEMORY;
1733 r = MsiEnumRelatedProductsW( szwUpgradeCode, dwReserved,
1734 iProductIndex, productW );
1735 if (r == ERROR_SUCCESS)
1737 WideCharToMultiByte( CP_ACP, 0, productW, GUID_SIZE,
1738 lpProductBuf, GUID_SIZE, NULL, NULL );
1740 msi_free( szwUpgradeCode);
1741 return r;
1744 /***********************************************************************
1745 * MsiEnumPatchesExA [MSI.@]
1747 UINT WINAPI MsiEnumPatchesExA(LPCSTR szProductCode, LPCSTR szUserSid,
1748 DWORD dwContext, DWORD dwFilter, DWORD dwIndex, LPSTR szPatchCode,
1749 LPSTR szTargetProductCode, MSIINSTALLCONTEXT *pdwTargetProductContext,
1750 LPSTR szTargetUserSid, LPDWORD pcchTargetUserSid)
1752 LPWSTR prodcode = NULL;
1753 LPWSTR usersid = NULL;
1754 LPWSTR targsid = NULL;
1755 WCHAR patch[GUID_SIZE];
1756 WCHAR targprod[GUID_SIZE];
1757 DWORD len;
1758 UINT r;
1760 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n",
1761 debugstr_a(szProductCode), debugstr_a(szUserSid), dwContext, dwFilter,
1762 dwIndex, szPatchCode, szTargetProductCode, pdwTargetProductContext,
1763 szTargetUserSid, pcchTargetUserSid);
1765 if (szTargetUserSid && !pcchTargetUserSid)
1766 return ERROR_INVALID_PARAMETER;
1768 if (szProductCode) prodcode = strdupAtoW(szProductCode);
1769 if (szUserSid) usersid = strdupAtoW(szUserSid);
1771 r = MsiEnumPatchesExW(prodcode, usersid, dwContext, dwFilter, dwIndex,
1772 patch, targprod, pdwTargetProductContext,
1773 NULL, &len);
1774 if (r != ERROR_SUCCESS)
1775 goto done;
1777 WideCharToMultiByte(CP_ACP, 0, patch, -1, szPatchCode,
1778 GUID_SIZE, NULL, NULL);
1779 WideCharToMultiByte(CP_ACP, 0, targprod, -1, szTargetProductCode,
1780 GUID_SIZE, NULL, NULL);
1782 if (!szTargetUserSid)
1784 if (pcchTargetUserSid)
1785 *pcchTargetUserSid = len;
1787 goto done;
1790 targsid = msi_alloc(++len * sizeof(WCHAR));
1791 if (!targsid)
1793 r = ERROR_OUTOFMEMORY;
1794 goto done;
1797 r = MsiEnumPatchesExW(prodcode, usersid, dwContext, dwFilter, dwIndex,
1798 patch, targprod, pdwTargetProductContext,
1799 targsid, &len);
1800 if (r != ERROR_SUCCESS || !szTargetUserSid)
1801 goto done;
1803 WideCharToMultiByte(CP_ACP, 0, targsid, -1, szTargetUserSid,
1804 *pcchTargetUserSid, NULL, NULL);
1806 len = lstrlenW(targsid);
1807 if (*pcchTargetUserSid < len + 1)
1809 r = ERROR_MORE_DATA;
1810 *pcchTargetUserSid = len * sizeof(WCHAR);
1812 else
1813 *pcchTargetUserSid = len;
1815 done:
1816 msi_free(prodcode);
1817 msi_free(usersid);
1818 msi_free(targsid);
1820 return r;
1823 static UINT msi_get_patch_state(LPCWSTR prodcode, LPCWSTR usersid,
1824 MSIINSTALLCONTEXT context,
1825 LPWSTR patch, MSIPATCHSTATE *state)
1827 DWORD type, val, size;
1828 HKEY prod, hkey = 0;
1829 HKEY udpatch = 0;
1830 LONG res;
1831 UINT r = ERROR_NO_MORE_ITEMS;
1833 *state = MSIPATCHSTATE_INVALID;
1835 r = MSIREG_OpenUserDataProductKey(prodcode, context,
1836 usersid, &prod, FALSE);
1837 if (r != ERROR_SUCCESS)
1838 return ERROR_NO_MORE_ITEMS;
1840 res = RegOpenKeyExW(prod, szPatches, 0, KEY_READ, &hkey);
1841 if (res != ERROR_SUCCESS)
1842 goto done;
1844 res = RegOpenKeyExW(hkey, patch, 0, KEY_READ, &udpatch);
1845 if (res != ERROR_SUCCESS)
1846 goto done;
1848 size = sizeof(DWORD);
1849 res = RegGetValueW(udpatch, NULL, szState, RRF_RT_DWORD, &type, &val, &size);
1850 if (res != ERROR_SUCCESS ||
1851 val < MSIPATCHSTATE_APPLIED || val > MSIPATCHSTATE_REGISTERED)
1853 r = ERROR_BAD_CONFIGURATION;
1854 goto done;
1857 *state = val;
1858 r = ERROR_SUCCESS;
1860 done:
1861 RegCloseKey(udpatch);
1862 RegCloseKey(hkey);
1863 RegCloseKey(prod);
1865 return r;
1868 static UINT msi_check_product_patches(LPCWSTR prodcode, LPCWSTR usersid,
1869 MSIINSTALLCONTEXT context, DWORD filter, DWORD index, DWORD *idx,
1870 LPWSTR patch, LPWSTR targetprod, MSIINSTALLCONTEXT *targetctx,
1871 LPWSTR targetsid, DWORD *sidsize, LPWSTR *transforms)
1873 MSIPATCHSTATE state = MSIPATCHSTATE_INVALID;
1874 LPWSTR ptr, patches = NULL;
1875 HKEY prod, patchkey = 0;
1876 HKEY localprod = 0, localpatch = 0;
1877 DWORD type, size;
1878 LONG res;
1879 UINT temp, r = ERROR_NO_MORE_ITEMS;
1881 if (MSIREG_OpenProductKey(prodcode, usersid, context,
1882 &prod, FALSE) != ERROR_SUCCESS)
1883 return ERROR_NO_MORE_ITEMS;
1885 size = 0;
1886 res = RegGetValueW(prod, szPatches, szPatches, RRF_RT_ANY, &type, NULL,
1887 &size);
1888 if (res != ERROR_SUCCESS)
1889 goto done;
1891 if (type != REG_MULTI_SZ)
1893 r = ERROR_BAD_CONFIGURATION;
1894 goto done;
1897 patches = msi_alloc(size);
1898 if (!patches)
1900 r = ERROR_OUTOFMEMORY;
1901 goto done;
1904 res = RegGetValueW(prod, szPatches, szPatches, RRF_RT_ANY, &type,
1905 patches, &size);
1906 if (res != ERROR_SUCCESS)
1907 goto done;
1909 for (ptr = patches; *ptr && r == ERROR_NO_MORE_ITEMS; ptr += lstrlenW(ptr) + 1)
1911 if (!unsquash_guid(ptr, patch))
1913 r = ERROR_BAD_CONFIGURATION;
1914 goto done;
1917 size = 0;
1918 res = RegGetValueW(prod, szPatches, ptr, RRF_RT_REG_SZ,
1919 &type, NULL, &size);
1920 if (res != ERROR_SUCCESS)
1921 continue;
1923 if (transforms)
1925 *transforms = msi_alloc(size);
1926 if (!*transforms)
1928 r = ERROR_OUTOFMEMORY;
1929 goto done;
1932 res = RegGetValueW(prod, szPatches, ptr, RRF_RT_REG_SZ,
1933 &type, *transforms, &size);
1934 if (res != ERROR_SUCCESS)
1935 continue;
1938 if (context == MSIINSTALLCONTEXT_USERMANAGED)
1940 if (!(filter & MSIPATCHSTATE_APPLIED))
1942 temp = msi_get_patch_state(prodcode, usersid, context,
1943 ptr, &state);
1944 if (temp == ERROR_BAD_CONFIGURATION)
1946 r = ERROR_BAD_CONFIGURATION;
1947 goto done;
1950 if (temp != ERROR_SUCCESS || !(filter & state))
1951 continue;
1954 else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
1956 if (!(filter & MSIPATCHSTATE_APPLIED))
1958 temp = msi_get_patch_state(prodcode, usersid, context,
1959 ptr, &state);
1960 if (temp == ERROR_BAD_CONFIGURATION)
1962 r = ERROR_BAD_CONFIGURATION;
1963 goto done;
1966 if (temp != ERROR_SUCCESS || !(filter & state))
1967 continue;
1969 else
1971 temp = MSIREG_OpenUserDataPatchKey(patch, context,
1972 &patchkey, FALSE);
1973 RegCloseKey(patchkey);
1974 if (temp != ERROR_SUCCESS)
1975 continue;
1978 else if (context == MSIINSTALLCONTEXT_MACHINE)
1980 usersid = szEmpty;
1982 if (MSIREG_OpenUserDataProductKey(prodcode, context, NULL, &localprod, FALSE) == ERROR_SUCCESS &&
1983 RegOpenKeyExW(localprod, szPatches, 0, KEY_READ, &localpatch) == ERROR_SUCCESS &&
1984 RegOpenKeyExW(localpatch, ptr, 0, KEY_READ, &patchkey) == ERROR_SUCCESS)
1986 res = RegGetValueW(patchkey, NULL, szState, RRF_RT_REG_DWORD,
1987 &type, &state, &size);
1989 if (!(filter & state))
1990 res = ERROR_NO_MORE_ITEMS;
1992 RegCloseKey(patchkey);
1995 RegCloseKey(localpatch);
1996 RegCloseKey(localprod);
1998 if (res != ERROR_SUCCESS)
1999 continue;
2002 if (*idx < index)
2004 (*idx)++;
2005 continue;
2008 r = ERROR_SUCCESS;
2009 if (targetprod)
2010 lstrcpyW(targetprod, prodcode);
2012 if (targetctx)
2013 *targetctx = context;
2015 if (targetsid)
2017 lstrcpynW(targetsid, usersid, *sidsize);
2018 if (lstrlenW(usersid) >= *sidsize)
2019 r = ERROR_MORE_DATA;
2022 if (sidsize)
2024 *sidsize = lstrlenW(usersid);
2025 if (!targetsid)
2026 *sidsize *= sizeof(WCHAR);
2030 done:
2031 RegCloseKey(prod);
2032 msi_free(patches);
2034 return r;
2037 static UINT msi_enum_patches(LPCWSTR szProductCode, LPCWSTR szUserSid,
2038 DWORD dwContext, DWORD dwFilter, DWORD dwIndex, DWORD *idx,
2039 LPWSTR szPatchCode, LPWSTR szTargetProductCode,
2040 MSIINSTALLCONTEXT *pdwTargetProductContext, LPWSTR szTargetUserSid,
2041 LPDWORD pcchTargetUserSid, LPWSTR *szTransforms)
2043 LPWSTR usersid = NULL;
2044 UINT r = ERROR_INVALID_PARAMETER;
2046 if (!szUserSid)
2048 szUserSid = usersid = get_user_sid();
2049 if (!usersid) return ERROR_FUNCTION_FAILED;
2052 if (dwContext & MSIINSTALLCONTEXT_USERMANAGED)
2054 r = msi_check_product_patches(szProductCode, szUserSid,
2055 MSIINSTALLCONTEXT_USERMANAGED, dwFilter,
2056 dwIndex, idx, szPatchCode,
2057 szTargetProductCode,
2058 pdwTargetProductContext, szTargetUserSid,
2059 pcchTargetUserSid, szTransforms);
2060 if (r != ERROR_NO_MORE_ITEMS)
2061 goto done;
2064 if (dwContext & MSIINSTALLCONTEXT_USERUNMANAGED)
2066 r = msi_check_product_patches(szProductCode, szUserSid,
2067 MSIINSTALLCONTEXT_USERUNMANAGED, dwFilter,
2068 dwIndex, idx, szPatchCode,
2069 szTargetProductCode,
2070 pdwTargetProductContext, szTargetUserSid,
2071 pcchTargetUserSid, szTransforms);
2072 if (r != ERROR_NO_MORE_ITEMS)
2073 goto done;
2076 if (dwContext & MSIINSTALLCONTEXT_MACHINE)
2078 r = msi_check_product_patches(szProductCode, szUserSid,
2079 MSIINSTALLCONTEXT_MACHINE, dwFilter,
2080 dwIndex, idx, szPatchCode,
2081 szTargetProductCode,
2082 pdwTargetProductContext, szTargetUserSid,
2083 pcchTargetUserSid, szTransforms);
2084 if (r != ERROR_NO_MORE_ITEMS)
2085 goto done;
2088 done:
2089 LocalFree(usersid);
2090 return r;
2093 /***********************************************************************
2094 * MsiEnumPatchesExW [MSI.@]
2096 UINT WINAPI MsiEnumPatchesExW(LPCWSTR szProductCode, LPCWSTR szUserSid,
2097 DWORD dwContext, DWORD dwFilter, DWORD dwIndex, LPWSTR szPatchCode,
2098 LPWSTR szTargetProductCode, MSIINSTALLCONTEXT *pdwTargetProductContext,
2099 LPWSTR szTargetUserSid, LPDWORD pcchTargetUserSid)
2101 WCHAR squished_pc[GUID_SIZE];
2102 DWORD idx = 0;
2103 UINT r;
2105 static DWORD last_index;
2107 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n",
2108 debugstr_w(szProductCode), debugstr_w(szUserSid), dwContext, dwFilter,
2109 dwIndex, szPatchCode, szTargetProductCode, pdwTargetProductContext,
2110 szTargetUserSid, pcchTargetUserSid);
2112 if (!szProductCode || !squash_guid(szProductCode, squished_pc))
2113 return ERROR_INVALID_PARAMETER;
2115 if (szUserSid && !strcmpW( szUserSid, szLocalSid ))
2116 return ERROR_INVALID_PARAMETER;
2118 if (dwContext & MSIINSTALLCONTEXT_MACHINE && szUserSid)
2119 return ERROR_INVALID_PARAMETER;
2121 if (dwContext <= MSIINSTALLCONTEXT_NONE ||
2122 dwContext > MSIINSTALLCONTEXT_ALL)
2123 return ERROR_INVALID_PARAMETER;
2125 if (dwFilter <= MSIPATCHSTATE_INVALID || dwFilter > MSIPATCHSTATE_ALL)
2126 return ERROR_INVALID_PARAMETER;
2128 if (dwIndex && dwIndex - last_index != 1)
2129 return ERROR_INVALID_PARAMETER;
2131 if (dwIndex == 0)
2132 last_index = 0;
2134 r = msi_enum_patches(szProductCode, szUserSid, dwContext, dwFilter,
2135 dwIndex, &idx, szPatchCode, szTargetProductCode,
2136 pdwTargetProductContext, szTargetUserSid,
2137 pcchTargetUserSid, NULL);
2139 if (r == ERROR_SUCCESS)
2140 last_index = dwIndex;
2141 else
2142 last_index = 0;
2144 return r;
2147 /***********************************************************************
2148 * MsiEnumPatchesA [MSI.@]
2150 UINT WINAPI MsiEnumPatchesA(LPCSTR szProduct, DWORD iPatchIndex,
2151 LPSTR lpPatchBuf, LPSTR lpTransformsBuf, LPDWORD pcchTransformsBuf)
2153 LPWSTR product, transforms;
2154 WCHAR patch[GUID_SIZE];
2155 DWORD len;
2156 UINT r;
2158 TRACE("(%s %d %p %p %p)\n", debugstr_a(szProduct), iPatchIndex,
2159 lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);
2161 if (!szProduct || !lpPatchBuf || !lpTransformsBuf || !pcchTransformsBuf)
2162 return ERROR_INVALID_PARAMETER;
2164 product = strdupAtoW(szProduct);
2165 if (!product)
2166 return ERROR_OUTOFMEMORY;
2168 len = *pcchTransformsBuf;
2169 transforms = msi_alloc( len * sizeof(WCHAR) );
2170 if (!transforms)
2172 r = ERROR_OUTOFMEMORY;
2173 goto done;
2176 r = MsiEnumPatchesW(product, iPatchIndex, patch, transforms, &len);
2177 if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
2178 goto done;
2180 WideCharToMultiByte(CP_ACP, 0, patch, -1, lpPatchBuf,
2181 GUID_SIZE, NULL, NULL);
2183 if (!WideCharToMultiByte(CP_ACP, 0, transforms, -1, lpTransformsBuf,
2184 *pcchTransformsBuf, NULL, NULL))
2185 r = ERROR_MORE_DATA;
2187 if (r == ERROR_MORE_DATA)
2189 lpTransformsBuf[*pcchTransformsBuf - 1] = '\0';
2190 *pcchTransformsBuf = len * 2;
2192 else
2193 *pcchTransformsBuf = strlen( lpTransformsBuf );
2195 done:
2196 msi_free(transforms);
2197 msi_free(product);
2199 return r;
2202 /***********************************************************************
2203 * MsiEnumPatchesW [MSI.@]
2205 UINT WINAPI MsiEnumPatchesW(LPCWSTR szProduct, DWORD iPatchIndex,
2206 LPWSTR lpPatchBuf, LPWSTR lpTransformsBuf, LPDWORD pcchTransformsBuf)
2208 WCHAR squished_pc[GUID_SIZE];
2209 LPWSTR transforms = NULL;
2210 HKEY prod;
2211 DWORD idx = 0;
2212 UINT r;
2214 TRACE("(%s %d %p %p %p)\n", debugstr_w(szProduct), iPatchIndex,
2215 lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);
2217 if (!szProduct || !squash_guid(szProduct, squished_pc))
2218 return ERROR_INVALID_PARAMETER;
2220 if (!lpPatchBuf || !lpTransformsBuf || !pcchTransformsBuf)
2221 return ERROR_INVALID_PARAMETER;
2223 if (MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERMANAGED,
2224 &prod, FALSE) != ERROR_SUCCESS &&
2225 MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERUNMANAGED,
2226 &prod, FALSE) != ERROR_SUCCESS &&
2227 MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_MACHINE,
2228 &prod, FALSE) != ERROR_SUCCESS)
2229 return ERROR_UNKNOWN_PRODUCT;
2231 RegCloseKey(prod);
2233 r = msi_enum_patches(szProduct, NULL, MSIINSTALLCONTEXT_ALL,
2234 MSIPATCHSTATE_ALL, iPatchIndex, &idx, lpPatchBuf,
2235 NULL, NULL, NULL, NULL, &transforms);
2236 if (r != ERROR_SUCCESS)
2237 goto done;
2239 lstrcpynW(lpTransformsBuf, transforms, *pcchTransformsBuf);
2240 if (*pcchTransformsBuf <= lstrlenW(transforms))
2242 r = ERROR_MORE_DATA;
2243 *pcchTransformsBuf = lstrlenW(transforms);
2245 else
2246 *pcchTransformsBuf = lstrlenW(transforms);
2248 done:
2249 msi_free(transforms);
2250 return r;
2253 UINT WINAPI MsiEnumProductsExA( LPCSTR product, LPCSTR usersid, DWORD ctx, DWORD index,
2254 CHAR installed_product[GUID_SIZE],
2255 MSIINSTALLCONTEXT *installed_ctx, LPSTR sid, LPDWORD sid_len )
2257 UINT r;
2258 WCHAR installed_productW[GUID_SIZE], *productW = NULL, *usersidW = NULL, *sidW = NULL;
2260 TRACE("%s, %s, %u, %u, %p, %p, %p, %p\n", debugstr_a(product), debugstr_a(usersid),
2261 ctx, index, installed_product, installed_ctx, sid, sid_len);
2263 if (sid && !sid_len) return ERROR_INVALID_PARAMETER;
2264 if (product && !(productW = strdupAtoW( product ))) return ERROR_OUTOFMEMORY;
2265 if (usersid && !(usersidW = strdupAtoW( usersid )))
2267 msi_free( productW );
2268 return ERROR_OUTOFMEMORY;
2270 if (sid && !(sidW = msi_alloc( *sid_len * sizeof(WCHAR) )))
2272 msi_free( usersidW );
2273 msi_free( productW );
2274 return ERROR_OUTOFMEMORY;
2276 r = MsiEnumProductsExW( productW, usersidW, ctx, index, installed_productW,
2277 installed_ctx, sidW, sid_len );
2278 if (r == ERROR_SUCCESS)
2280 if (installed_product) WideCharToMultiByte( CP_ACP, 0, installed_productW, GUID_SIZE,
2281 installed_product, GUID_SIZE, NULL, NULL );
2282 if (sid) WideCharToMultiByte( CP_ACP, 0, sidW, *sid_len + 1, sid, *sid_len + 1, NULL, NULL );
2284 msi_free( productW );
2285 msi_free( usersidW );
2286 msi_free( sidW );
2287 return r;
2290 static UINT fetch_machine_product( const WCHAR *match, DWORD index, DWORD *idx,
2291 WCHAR installed_product[GUID_SIZE],
2292 MSIINSTALLCONTEXT *installed_ctx, WCHAR *sid, DWORD *sid_len )
2294 static const WCHAR productsW[] =
2295 {'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
2296 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
2297 UINT r;
2298 WCHAR product[GUID_SIZE];
2299 DWORD i = 0, len;
2300 REGSAM access = KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY;
2301 HKEY key;
2303 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, productsW, 0, access, &key ))
2304 return ERROR_NO_MORE_ITEMS;
2306 len = sizeof(product)/sizeof(product[0]);
2307 while (!RegEnumKeyExW( key, i, product, &len, NULL, NULL, NULL, NULL ))
2309 if (match && strcmpW( match, product ))
2311 i++;
2312 len = sizeof(product)/sizeof(product[0]);
2313 continue;
2315 if (*idx == index) goto found;
2316 (*idx)++;
2317 len = sizeof(product)/sizeof(product[0]);
2318 i++;
2320 RegCloseKey( key );
2321 return ERROR_NO_MORE_ITEMS;
2323 found:
2324 if (sid_len && *sid_len < 1)
2326 *sid_len = 1;
2327 r = ERROR_MORE_DATA;
2329 else
2331 if (installed_product) unsquash_guid( product, installed_product );
2332 if (installed_ctx) *installed_ctx = MSIINSTALLCONTEXT_MACHINE;
2333 if (sid)
2335 sid[0] = 0;
2336 *sid_len = 0;
2338 r = ERROR_SUCCESS;
2340 RegCloseKey( key );
2341 return r;
2344 static UINT fetch_user_product( const WCHAR *match, const WCHAR *usersid, DWORD ctx, DWORD index,
2345 DWORD *idx, WCHAR installed_product[GUID_SIZE],
2346 MSIINSTALLCONTEXT *installed_ctx, WCHAR *sid, DWORD *sid_len )
2348 static const WCHAR managedW[] =
2349 {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
2350 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s',
2351 'i','o','n','\\','I','n','s','t','a','l','l','e','r','\\','M','a','n','a','g','e','d',0};
2352 static const WCHAR managed_productsW[] =
2353 {'\\','I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
2354 static const WCHAR unmanaged_productsW[] =
2355 {'\\','S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
2356 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
2357 UINT r;
2358 const WCHAR *subkey;
2359 WCHAR path[MAX_PATH], product[GUID_SIZE], user[128];
2360 DWORD i = 0, j = 0, len_product, len_user;
2361 REGSAM access = KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY;
2362 HKEY key_users, key_products;
2364 if (ctx == MSIINSTALLCONTEXT_USERMANAGED)
2366 subkey = managed_productsW;
2367 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, managedW, 0, access, &key_users ))
2368 return ERROR_NO_MORE_ITEMS;
2370 else if (ctx == MSIINSTALLCONTEXT_USERUNMANAGED)
2372 subkey = unmanaged_productsW;
2373 if (RegOpenKeyExW( HKEY_USERS, NULL, 0, access, &key_users ))
2374 return ERROR_NO_MORE_ITEMS;
2376 else return ERROR_INVALID_PARAMETER;
2378 len_user = sizeof(user)/sizeof(user[0]);
2379 while (!RegEnumKeyExW( key_users, i, user, &len_user, NULL, NULL, NULL, NULL ))
2381 if (strcmpW( usersid, user ) && strcmpW( usersid, szAllSid ))
2383 i++;
2384 len_user = sizeof(user)/sizeof(user[0]);
2385 continue;
2387 strcpyW( path, user );
2388 strcatW( path, subkey );
2389 if (RegOpenKeyExW( key_users, path, 0, access, &key_products ))
2391 i++;
2392 len_user = sizeof(user)/sizeof(user[0]);
2393 continue;
2395 len_product = sizeof(product)/sizeof(product[0]);
2396 while (!RegEnumKeyExW( key_products, j, product, &len_product, NULL, NULL, NULL, NULL ))
2398 if (match && strcmpW( match, product ))
2400 j++;
2401 len_product = sizeof(product)/sizeof(product[0]);
2402 continue;
2404 if (*idx == index) goto found;
2405 (*idx)++;
2406 len_product = sizeof(product)/sizeof(product[0]);
2407 j++;
2409 RegCloseKey( key_products );
2410 len_user = sizeof(user)/sizeof(user[0]);
2411 i++;
2413 RegCloseKey( key_users );
2414 return ERROR_NO_MORE_ITEMS;
2416 found:
2417 if (sid_len && *sid_len <= len_user)
2419 *sid_len = len_user;
2420 r = ERROR_MORE_DATA;
2422 else
2424 if (installed_product) unsquash_guid( product, installed_product );
2425 if (installed_ctx) *installed_ctx = ctx;
2426 if (sid)
2428 strcpyW( sid, user );
2429 *sid_len = len_user;
2431 r = ERROR_SUCCESS;
2433 RegCloseKey( key_products );
2434 RegCloseKey( key_users );
2435 return r;
2438 static UINT enum_products( const WCHAR *product, const WCHAR *usersid, DWORD ctx, DWORD index,
2439 DWORD *idx, WCHAR installed_product[GUID_SIZE],
2440 MSIINSTALLCONTEXT *installed_ctx, WCHAR *sid, DWORD *sid_len )
2442 UINT r = ERROR_NO_MORE_ITEMS;
2443 WCHAR *user = NULL;
2445 if (!usersid)
2447 usersid = user = get_user_sid();
2448 if (!user) return ERROR_FUNCTION_FAILED;
2450 if (ctx & MSIINSTALLCONTEXT_MACHINE)
2452 r = fetch_machine_product( product, index, idx, installed_product, installed_ctx,
2453 sid, sid_len );
2454 if (r != ERROR_NO_MORE_ITEMS) goto done;
2456 if (ctx & MSIINSTALLCONTEXT_USERUNMANAGED)
2458 r = fetch_user_product( product, usersid, MSIINSTALLCONTEXT_USERUNMANAGED, index,
2459 idx, installed_product, installed_ctx, sid, sid_len );
2460 if (r != ERROR_NO_MORE_ITEMS) goto done;
2462 if (ctx & MSIINSTALLCONTEXT_USERMANAGED)
2464 r = fetch_user_product( product, usersid, MSIINSTALLCONTEXT_USERMANAGED, index,
2465 idx, installed_product, installed_ctx, sid, sid_len );
2466 if (r != ERROR_NO_MORE_ITEMS) goto done;
2469 done:
2470 LocalFree( user );
2471 return r;
2474 UINT WINAPI MsiEnumProductsExW( LPCWSTR product, LPCWSTR usersid, DWORD ctx, DWORD index,
2475 WCHAR installed_product[GUID_SIZE],
2476 MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid, LPDWORD sid_len )
2478 UINT r;
2479 DWORD idx = 0;
2480 static DWORD last_index;
2482 TRACE("%s, %s, %u, %u, %p, %p, %p, %p\n", debugstr_w(product), debugstr_w(usersid),
2483 ctx, index, installed_product, installed_ctx, sid, sid_len);
2485 if ((sid && !sid_len) || !ctx || (usersid && ctx == MSIINSTALLCONTEXT_MACHINE))
2486 return ERROR_INVALID_PARAMETER;
2488 if (index && index - last_index != 1)
2489 return ERROR_INVALID_PARAMETER;
2491 if (!index) last_index = 0;
2493 r = enum_products( product, usersid, ctx, index, &idx, installed_product, installed_ctx,
2494 sid, sid_len );
2495 if (r == ERROR_SUCCESS)
2496 last_index = index;
2497 else
2498 last_index = 0;
2500 return r;