winemac: Implement wglCreateContextAttribsARB.
[wine.git] / dlls / msi / registry.c
blob4dde285bddb9fb7499f03862edaa0e37f5060437
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
25 #define NONAMELESSUNION
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"
41 WINE_DEFAULT_DEBUG_CHANNEL(msi);
43 /*
44 * This module will be all the helper functions for registry access by the
45 * installer bits.
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};
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};
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};
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};
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};
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};
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};
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};
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};
103 static const WCHAR szInstaller_LocalManagedProd_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','\\','%','s',0};
109 static const WCHAR szInstaller_LocalManagedFeat_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','\\','F','e','a','t','u','r','e','s','\\','%','s',0};
115 static const WCHAR szInstaller_Products[] = {
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','\\','P','r','o','d','u','c','t','s',0};
120 static const WCHAR szInstaller_Patches[] = {
121 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
122 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
123 'I','n','s','t','a','l','l','e','r','\\','P','a','t','c','h','e','s',0};
125 static const WCHAR szInstaller_LocalClassesProducts[] = {
126 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
127 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
129 static const WCHAR szInstaller_LocalClassesFeatures[] = {
130 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
131 'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s',0};
133 static const WCHAR szInstaller_LocalClassesProd[] = {
134 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
135 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s','\\',0};
137 static const WCHAR szInstaller_LocalClassesFeat[] = {
138 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
139 'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s','\\',0};
141 static const WCHAR szInstaller_ClassesUpgradeCode[] = {
142 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
143 'I','n','s','t','a','l','l','e','r','\\','U','p','g','r','a','d','e','C','o','d','e','s','\\',0};
145 static const WCHAR szInstaller_ClassesUpgradeCodes[] = {
146 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
147 'I','n','s','t','a','l','l','e','r','\\','U','p','g','r','a','d','e','C','o','d','e','s',0};
149 static const WCHAR szInstaller_Features[] = {
150 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
151 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
152 'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s','\\',0};
154 static const WCHAR szInstaller_UpgradeCodes[] = {
155 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
156 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
157 'I','n','s','t','a','l','l','e','r','\\','U','p','g','r','a','d','e','C','o','d','e','s','\\',0};
159 static const WCHAR szInstaller_UserUpgradeCodes[] = {
160 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
161 'I','n','s','t','a','l','l','e','r','\\','U','p','g','r','a','d','e','C','o','d','e','s','\\',0};
163 static const WCHAR szUninstall[] = {
164 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
165 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
166 'U','n','i','n','s','t','a','l','l','\\',0};
168 static const WCHAR szUninstall_32node[] = {
169 'S','o','f','t','w','a','r','e','\\','W','o','w','6','4','3','2','N','o','d','e','\\',
170 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
171 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','U','n','i','n','s','t','a','l','l','\\',0};
173 static const WCHAR szUserComponents[] = {
174 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
175 'I','n','s','t','a','l','l','e','r','\\','C','o','m','p','o','n','e','n','t','s','\\',0};
177 static const WCHAR szUserFeatures[] = {
178 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
179 'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s','\\',0};
181 static const WCHAR szUserProducts[] = {
182 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
183 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s','\\',0};
185 static const WCHAR szUserPatches[] = {
186 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
187 'I','n','s','t','a','l','l','e','r','\\','P','a','t','c','h','e','s','\\',0};
189 BOOL unsquash_guid(LPCWSTR in, LPWSTR out)
191 DWORD i,n=0;
193 if (lstrlenW(in) != 32)
194 return FALSE;
196 out[n++]='{';
197 for(i=0; i<8; i++)
198 out[n++] = in[7-i];
199 out[n++]='-';
200 for(i=0; i<4; i++)
201 out[n++] = in[11-i];
202 out[n++]='-';
203 for(i=0; i<4; i++)
204 out[n++] = in[15-i];
205 out[n++]='-';
206 for(i=0; i<2; i++)
208 out[n++] = in[17+i*2];
209 out[n++] = in[16+i*2];
211 out[n++]='-';
212 for( ; i<8; i++)
214 out[n++] = in[17+i*2];
215 out[n++] = in[16+i*2];
217 out[n++]='}';
218 out[n]=0;
219 return TRUE;
222 BOOL squash_guid(LPCWSTR in, LPWSTR out)
224 DWORD i,n=1;
225 GUID guid;
227 out[0] = 0;
229 if (FAILED(CLSIDFromString((LPCOLESTR)in, &guid)))
230 return FALSE;
232 for(i=0; i<8; i++)
233 out[7-i] = in[n++];
234 n++;
235 for(i=0; i<4; i++)
236 out[11-i] = in[n++];
237 n++;
238 for(i=0; i<4; i++)
239 out[15-i] = in[n++];
240 n++;
241 for(i=0; i<2; i++)
243 out[17+i*2] = in[n++];
244 out[16+i*2] = in[n++];
246 n++;
247 for( ; i<8; i++)
249 out[17+i*2] = in[n++];
250 out[16+i*2] = in[n++];
252 out[32]=0;
253 return TRUE;
257 /* tables for encoding and decoding base85 */
258 static const unsigned char table_dec85[0x80] = {
259 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
260 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
261 0xff,0x00,0xff,0xff,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0xff,
262 0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0xff,0xff,0xff,0x16,0xff,0x17,
263 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
264 0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0xff,0x34,0x35,0x36,
265 0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x42,0x43,0x44,0x45,0x46,
266 0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0xff,0x53,0x54,0xff,
269 static const char table_enc85[] =
270 "!$%&'()*+,-.0123456789=?@ABCDEFGHIJKLMNO"
271 "PQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwx"
272 "yz{}~";
275 * Converts a base85 encoded guid into a GUID pointer
276 * Base85 encoded GUIDs should be 20 characters long.
278 * returns TRUE if successful, FALSE if not
280 BOOL decode_base85_guid( LPCWSTR str, GUID *guid )
282 DWORD i, val = 0, base = 1, *p;
284 if (!str)
285 return FALSE;
287 p = (DWORD*) guid;
288 for( i=0; i<20; i++ )
290 if( (i%5) == 0 )
292 val = 0;
293 base = 1;
295 val += table_dec85[str[i]] * base;
296 if( str[i] >= 0x80 )
297 return FALSE;
298 if( table_dec85[str[i]] == 0xff )
299 return FALSE;
300 if( (i%5) == 4 )
301 p[i/5] = val;
302 base *= 85;
304 return TRUE;
308 * Encodes a base85 guid given a GUID pointer
309 * Caller should provide a 21 character buffer for the encoded string.
311 * returns TRUE if successful, FALSE if not
313 BOOL encode_base85_guid( GUID *guid, LPWSTR str )
315 unsigned int x, *p, i;
317 p = (unsigned int*) guid;
318 for( i=0; i<4; i++ )
320 x = p[i];
321 *str++ = table_enc85[x%85];
322 x = x/85;
323 *str++ = table_enc85[x%85];
324 x = x/85;
325 *str++ = table_enc85[x%85];
326 x = x/85;
327 *str++ = table_enc85[x%85];
328 x = x/85;
329 *str++ = table_enc85[x%85];
331 *str = 0;
333 return TRUE;
336 DWORD msi_version_str_to_dword(LPCWSTR p)
338 DWORD major, minor = 0, build = 0, version = 0;
340 if (!p)
341 return version;
343 major = atoiW(p);
345 p = strchrW(p, '.');
346 if (p)
348 minor = atoiW(p+1);
349 p = strchrW(p+1, '.');
350 if (p)
351 build = atoiW(p+1);
354 return MAKELONG(build, MAKEWORD(minor, major));
357 LONG msi_reg_set_val_str( HKEY hkey, LPCWSTR name, LPCWSTR value )
359 DWORD len;
360 if (!value) value = szEmpty;
361 len = (lstrlenW(value) + 1) * sizeof (WCHAR);
362 return RegSetValueExW( hkey, name, 0, REG_SZ, (const BYTE *)value, len );
365 LONG msi_reg_set_val_multi_str( HKEY hkey, LPCWSTR name, LPCWSTR value )
367 LPCWSTR p = value;
368 while (*p) p += lstrlenW(p) + 1;
369 return RegSetValueExW( hkey, name, 0, REG_MULTI_SZ,
370 (const BYTE *)value, (p + 1 - value) * sizeof(WCHAR) );
373 LONG msi_reg_set_val_dword( HKEY hkey, LPCWSTR name, DWORD val )
375 return RegSetValueExW( hkey, name, 0, REG_DWORD, (LPBYTE)&val, sizeof (DWORD) );
378 LONG msi_reg_set_subkey_val( HKEY hkey, LPCWSTR path, LPCWSTR name, LPCWSTR val )
380 HKEY hsubkey = 0;
381 LONG r;
383 r = RegCreateKeyW( hkey, path, &hsubkey );
384 if (r != ERROR_SUCCESS)
385 return r;
386 r = msi_reg_set_val_str( hsubkey, name, val );
387 RegCloseKey( hsubkey );
388 return r;
391 LPWSTR msi_reg_get_val_str( HKEY hkey, LPCWSTR name )
393 DWORD len = 0;
394 LPWSTR val;
395 LONG r;
397 r = RegQueryValueExW(hkey, name, NULL, NULL, NULL, &len);
398 if (r != ERROR_SUCCESS)
399 return NULL;
401 len += sizeof (WCHAR);
402 val = msi_alloc( len );
403 if (!val)
404 return NULL;
405 val[0] = 0;
406 RegQueryValueExW(hkey, name, NULL, NULL, (LPBYTE) val, &len);
407 return val;
410 BOOL msi_reg_get_val_dword( HKEY hkey, LPCWSTR name, DWORD *val)
412 DWORD type, len = sizeof (DWORD);
413 LONG r = RegQueryValueExW(hkey, name, NULL, &type, (LPBYTE) val, &len);
414 return r == ERROR_SUCCESS && type == REG_DWORD;
417 static WCHAR *get_user_sid(void)
419 HANDLE token;
420 DWORD size = 256;
421 TOKEN_USER *user;
422 WCHAR *ret;
424 if (!OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &token )) return NULL;
425 if (!(user = msi_alloc( size )))
427 CloseHandle( token );
428 return NULL;
430 if (!GetTokenInformation( token, TokenUser, user, size, &size ))
432 msi_free( user );
433 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER || !(user = msi_alloc( size )))
435 CloseHandle( token );
436 return NULL;
438 GetTokenInformation( token, TokenUser, user, size, &size );
440 CloseHandle( token );
441 if (!ConvertSidToStringSidW( user->User.Sid, &ret ))
443 msi_free( user );
444 return NULL;
446 msi_free( user );
447 return ret;
450 UINT MSIREG_OpenUninstallKey(const WCHAR *product, enum platform platform, HKEY *key, BOOL create)
452 WCHAR keypath[0x200];
454 TRACE("%s\n", debugstr_w(product));
456 if (is_64bit && platform == PLATFORM_INTEL)
458 strcpyW(keypath, szUninstall_32node);
459 strcatW(keypath, product);
461 else
463 strcpyW(keypath, szUninstall);
464 strcatW(keypath, product);
466 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, KEY_ALL_ACCESS, NULL, key, NULL);
467 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, KEY_ALL_ACCESS, key);
470 UINT MSIREG_DeleteUninstallKey(const WCHAR *product, enum platform platform)
472 WCHAR keypath[0x200];
474 TRACE("%s\n", debugstr_w(product));
476 if (is_64bit && platform == PLATFORM_INTEL)
478 strcpyW(keypath, szUninstall_32node);
479 strcatW(keypath, product);
481 else
483 strcpyW(keypath, szUninstall);
484 strcatW(keypath, product);
486 return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
489 UINT MSIREG_OpenProductKey(LPCWSTR szProduct, LPCWSTR szUserSid, MSIINSTALLCONTEXT context, HKEY *key, BOOL create)
491 LPWSTR usersid = NULL;
492 HKEY root = HKEY_LOCAL_MACHINE;
493 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
494 WCHAR squished_pc[GUID_SIZE], keypath[MAX_PATH];
496 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
497 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
499 if (context == MSIINSTALLCONTEXT_MACHINE)
501 strcpyW(keypath, szInstaller_LocalClassesProd);
502 strcatW(keypath, squished_pc);
504 else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
506 root = HKEY_CURRENT_USER;
507 strcpyW(keypath, szUserProducts);
508 strcatW(keypath, squished_pc);
510 else
512 if (!szUserSid)
514 if (!(usersid = get_user_sid()))
516 ERR("Failed to retrieve user SID\n");
517 return ERROR_FUNCTION_FAILED;
519 szUserSid = usersid;
521 sprintfW(keypath, szInstaller_LocalManagedProd_fmt, szUserSid, squished_pc);
522 LocalFree(usersid);
524 if (create) return RegCreateKeyExW(root, keypath, 0, NULL, 0, access, NULL, key, NULL);
525 return RegOpenKeyExW(root, keypath, 0, access, key);
528 UINT MSIREG_DeleteUserProductKey(LPCWSTR szProduct)
530 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
532 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
533 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
535 strcpyW(keypath, szUserProducts);
536 strcatW(keypath, squished_pc);
537 return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
540 UINT MSIREG_OpenUserPatchesKey(LPCWSTR szPatch, HKEY *key, BOOL create)
542 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
544 if (!squash_guid(szPatch, squished_pc)) return ERROR_FUNCTION_FAILED;
545 TRACE("%s squished %s\n", debugstr_w(szPatch), debugstr_w(squished_pc));
547 strcpyW(keypath, szUserPatches);
548 strcatW(keypath, squished_pc);
550 if (create) return RegCreateKeyW(HKEY_CURRENT_USER, keypath, key);
551 return RegOpenKeyW(HKEY_CURRENT_USER, keypath, key);
554 UINT MSIREG_OpenFeaturesKey(LPCWSTR szProduct, LPCWSTR szUserSid, MSIINSTALLCONTEXT context,
555 HKEY *key, BOOL create)
557 HKEY root = HKEY_LOCAL_MACHINE;
558 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
559 WCHAR squished_pc[GUID_SIZE], keypath[MAX_PATH], *usersid = NULL;
561 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
562 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
564 if (context == MSIINSTALLCONTEXT_MACHINE)
566 strcpyW(keypath, szInstaller_LocalClassesFeat);
567 strcatW(keypath, squished_pc);
569 else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
571 root = HKEY_CURRENT_USER;
572 strcpyW(keypath, szUserFeatures);
573 strcatW(keypath, squished_pc);
575 else
577 if (!szUserSid)
579 if (!(usersid = get_user_sid()))
581 ERR("Failed to retrieve user SID\n");
582 return ERROR_FUNCTION_FAILED;
584 szUserSid = usersid;
586 sprintfW(keypath, szInstaller_LocalManagedFeat_fmt, szUserSid, squished_pc);
587 LocalFree(usersid);
589 if (create) return RegCreateKeyExW(root, keypath, 0, NULL, 0, access, NULL, key, NULL);
590 return RegOpenKeyExW(root, keypath, 0, access, key);
593 UINT MSIREG_DeleteUserFeaturesKey(LPCWSTR szProduct)
595 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
597 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
598 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
600 strcpyW(keypath, szUserFeatures);
601 strcatW(keypath, squished_pc);
602 return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
605 static UINT MSIREG_OpenInstallerFeaturesKey(LPCWSTR szProduct, HKEY *key, BOOL create)
607 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
608 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
610 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
611 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
613 strcpyW(keypath, szInstaller_Features);
614 strcatW(keypath, squished_pc);
616 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
617 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
620 UINT MSIREG_OpenUserDataFeaturesKey(LPCWSTR szProduct, LPCWSTR szUserSid, MSIINSTALLCONTEXT context,
621 HKEY *key, BOOL create)
623 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
624 WCHAR squished_pc[GUID_SIZE], keypath[0x200], *usersid = NULL;
626 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
627 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
629 if (context == MSIINSTALLCONTEXT_MACHINE)
631 sprintfW(keypath, szUserDataFeatures_fmt, szLocalSid, squished_pc);
633 else
635 if (!szUserSid)
637 if (!(usersid = get_user_sid()))
639 ERR("Failed to retrieve user SID\n");
640 return ERROR_FUNCTION_FAILED;
642 szUserSid = usersid;
644 sprintfW(keypath, szUserDataFeatures_fmt, szUserSid, squished_pc);
645 LocalFree(usersid);
647 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
648 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
651 UINT MSIREG_OpenUserComponentsKey(LPCWSTR szComponent, HKEY *key, BOOL create)
653 WCHAR squished_cc[GUID_SIZE], keypath[0x200];
655 if (!squash_guid(szComponent, squished_cc)) return ERROR_FUNCTION_FAILED;
656 TRACE("%s squished %s\n", debugstr_w(szComponent), debugstr_w(squished_cc));
658 strcpyW(keypath, szUserComponents);
659 strcatW(keypath, squished_cc);
661 if (create) return RegCreateKeyW(HKEY_CURRENT_USER, keypath, key);
662 return RegOpenKeyW(HKEY_CURRENT_USER, keypath, key);
665 UINT MSIREG_OpenUserDataComponentKey(LPCWSTR szComponent, LPCWSTR szUserSid, HKEY *key, BOOL create)
667 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
668 WCHAR comp[GUID_SIZE], keypath[0x200];
669 LPWSTR usersid;
671 if (!squash_guid(szComponent, comp)) return ERROR_FUNCTION_FAILED;
672 TRACE("%s squished %s\n", debugstr_w(szComponent), debugstr_w(comp));
674 if (!szUserSid)
676 if (!(usersid = get_user_sid()))
678 ERR("Failed to retrieve user SID\n");
679 return ERROR_FUNCTION_FAILED;
681 sprintfW(keypath, szUserDataComp_fmt, usersid, comp);
682 LocalFree(usersid);
684 else
685 sprintfW(keypath, szUserDataComp_fmt, szUserSid, comp);
687 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
688 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
691 UINT MSIREG_DeleteUserDataComponentKey(LPCWSTR szComponent, LPCWSTR szUserSid)
693 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
694 WCHAR comp[GUID_SIZE], keypath[0x200];
695 LPWSTR usersid;
696 HKEY hkey;
697 LONG r;
699 if (!squash_guid(szComponent, comp)) return ERROR_FUNCTION_FAILED;
700 TRACE("%s squished %s\n", debugstr_w(szComponent), debugstr_w(comp));
702 if (!szUserSid)
704 if (!(usersid = get_user_sid()))
706 ERR("Failed to retrieve user SID\n");
707 return ERROR_FUNCTION_FAILED;
709 sprintfW(keypath, szUserDataComponents_fmt, usersid);
710 LocalFree(usersid);
712 else
713 sprintfW(keypath, szUserDataComponents_fmt, szUserSid);
715 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, &hkey)) return ERROR_SUCCESS;
716 r = RegDeleteTreeW(hkey, comp);
717 RegCloseKey(hkey);
718 return r;
721 UINT MSIREG_OpenUserDataProductKey(LPCWSTR szProduct, MSIINSTALLCONTEXT dwContext, LPCWSTR szUserSid, HKEY *key, BOOL create)
723 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
724 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
725 LPWSTR usersid;
727 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
728 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
730 if (dwContext == MSIINSTALLCONTEXT_MACHINE)
731 sprintfW(keypath, szUserDataProd_fmt, szLocalSid, squished_pc);
732 else if (szUserSid)
733 sprintfW(keypath, szUserDataProd_fmt, szUserSid, squished_pc);
734 else
736 if (!(usersid = get_user_sid()))
738 ERR("Failed to retrieve user SID\n");
739 return ERROR_FUNCTION_FAILED;
741 sprintfW(keypath, szUserDataProd_fmt, usersid, squished_pc);
742 LocalFree(usersid);
744 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
745 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
748 UINT MSIREG_OpenUserDataPatchKey(LPCWSTR szPatch, MSIINSTALLCONTEXT dwContext, HKEY *key, BOOL create)
750 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
751 WCHAR squished_patch[GUID_SIZE], keypath[0x200];
752 LPWSTR usersid;
754 if (!squash_guid(szPatch, squished_patch)) return ERROR_FUNCTION_FAILED;
755 TRACE("%s squished %s\n", debugstr_w(szPatch), debugstr_w(squished_patch));
757 if (dwContext == MSIINSTALLCONTEXT_MACHINE)
758 sprintfW(keypath, szUserDataPatch_fmt, szLocalSid, squished_patch);
759 else
761 if (!(usersid = get_user_sid()))
763 ERR("Failed to retrieve user SID\n");
764 return ERROR_FUNCTION_FAILED;
766 sprintfW(keypath, szUserDataPatch_fmt, usersid, squished_patch);
767 LocalFree(usersid);
769 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
770 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
773 UINT MSIREG_DeleteUserDataPatchKey(LPCWSTR patch, MSIINSTALLCONTEXT context)
775 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
776 WCHAR squished_patch[GUID_SIZE], keypath[0x200];
777 LPWSTR usersid;
778 HKEY hkey;
779 LONG r;
781 if (!squash_guid(patch, squished_patch)) return ERROR_FUNCTION_FAILED;
782 TRACE("%s squished %s\n", debugstr_w(patch), debugstr_w(squished_patch));
784 if (context == MSIINSTALLCONTEXT_MACHINE)
785 sprintfW(keypath, szUserDataPatches_fmt, szLocalSid);
786 else
788 if (!(usersid = get_user_sid()))
790 ERR("Failed to retrieve user SID\n");
791 return ERROR_FUNCTION_FAILED;
793 sprintfW(keypath, szUserDataPatches_fmt, usersid);
794 LocalFree(usersid);
796 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, &hkey)) return ERROR_SUCCESS;
797 r = RegDeleteTreeW(hkey, squished_patch);
798 RegCloseKey(hkey);
799 return r;
802 UINT MSIREG_OpenUserDataProductPatchesKey(LPCWSTR product, MSIINSTALLCONTEXT context, HKEY *key, BOOL create)
804 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
805 WCHAR squished_product[GUID_SIZE], keypath[0x200];
806 LPWSTR usersid;
808 if (!squash_guid(product, squished_product)) return ERROR_FUNCTION_FAILED;
809 TRACE("%s squished %s\n", debugstr_w(product), debugstr_w(squished_product));
811 if (context == MSIINSTALLCONTEXT_MACHINE)
812 sprintfW(keypath, szUserDataProductPatches_fmt, szLocalSid, squished_product);
813 else
815 if (!(usersid = get_user_sid()))
817 ERR("Failed to retrieve user SID\n");
818 return ERROR_FUNCTION_FAILED;
820 sprintfW(keypath, szUserDataProductPatches_fmt, usersid, squished_product);
821 LocalFree(usersid);
823 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
824 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
827 UINT MSIREG_OpenInstallProps(LPCWSTR szProduct, MSIINSTALLCONTEXT dwContext, LPCWSTR szUserSid, HKEY *key, BOOL create)
829 LPWSTR usersid;
830 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
831 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
833 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
834 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
836 if (dwContext == MSIINSTALLCONTEXT_MACHINE)
837 sprintfW(keypath, szInstallProperties_fmt, szLocalSid, squished_pc);
838 else if (szUserSid)
839 sprintfW(keypath, szInstallProperties_fmt, szUserSid, squished_pc);
840 else
842 if (!(usersid = get_user_sid()))
844 ERR("Failed to retrieve user SID\n");
845 return ERROR_FUNCTION_FAILED;
847 sprintfW(keypath, szInstallProperties_fmt, usersid, squished_pc);
848 LocalFree(usersid);
850 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
851 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
854 UINT MSIREG_DeleteUserDataProductKey(LPCWSTR szProduct)
856 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
857 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
858 LPWSTR usersid;
859 HKEY hkey;
860 LONG r;
862 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
863 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
865 if (!(usersid = get_user_sid()))
867 ERR("Failed to retrieve user SID\n");
868 return ERROR_FUNCTION_FAILED;
870 sprintfW(keypath, szUserDataProducts_fmt, usersid);
871 LocalFree(usersid);
873 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, &hkey)) return ERROR_SUCCESS;
874 r = RegDeleteTreeW(hkey, squished_pc);
875 RegCloseKey(hkey);
876 return r;
879 UINT MSIREG_DeleteProductKey(LPCWSTR szProduct)
881 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
882 WCHAR squished_pc[GUID_SIZE];
883 HKEY hkey;
884 LONG r;
886 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
887 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
889 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_Products, 0, access, &hkey)) return ERROR_SUCCESS;
890 r = RegDeleteTreeW(hkey, squished_pc);
891 RegCloseKey(hkey);
892 return r;
895 UINT MSIREG_OpenPatchesKey(LPCWSTR szPatch, HKEY *key, BOOL create)
897 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
898 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
900 if (!squash_guid(szPatch, squished_pc)) return ERROR_FUNCTION_FAILED;
901 TRACE("%s squished %s\n", debugstr_w(szPatch), debugstr_w(squished_pc));
903 sprintfW(keypath, szInstaller_Patches, squished_pc);
905 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
906 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
909 UINT MSIREG_OpenUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY *key, BOOL create)
911 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
912 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
914 if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
915 TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
917 strcpyW(keypath, szInstaller_UpgradeCodes);
918 strcatW(keypath, squished_pc);
920 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
921 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
924 UINT MSIREG_OpenUserUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY* key, BOOL create)
926 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
928 if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
929 TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
931 strcpyW(keypath, szInstaller_UserUpgradeCodes);
932 strcatW(keypath, squished_pc);
934 if (create) return RegCreateKeyW(HKEY_CURRENT_USER, keypath, key);
935 return RegOpenKeyW(HKEY_CURRENT_USER, keypath, key);
938 UINT MSIREG_DeleteUserUpgradeCodesKey(LPCWSTR szUpgradeCode)
940 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
942 if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
943 TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
945 strcpyW(keypath, szInstaller_UserUpgradeCodes);
946 strcatW(keypath, squished_pc);
947 return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
950 UINT MSIREG_DeleteLocalClassesProductKey(LPCWSTR szProductCode)
952 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
953 WCHAR squished_pc[GUID_SIZE];
954 HKEY hkey;
955 LONG r;
957 if (!squash_guid(szProductCode, squished_pc)) return ERROR_FUNCTION_FAILED;
958 TRACE("%s squished %s\n", debugstr_w(szProductCode), debugstr_w(squished_pc));
960 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_LocalClassesProducts, 0, access, &hkey)) return ERROR_SUCCESS;
961 r = RegDeleteTreeW(hkey, squished_pc);
962 RegCloseKey(hkey);
963 return r;
966 UINT MSIREG_DeleteLocalClassesFeaturesKey(LPCWSTR szProductCode)
968 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
969 WCHAR squished_pc[GUID_SIZE];
970 HKEY hkey;
971 LONG r;
973 if (!squash_guid(szProductCode, squished_pc)) return ERROR_FUNCTION_FAILED;
974 TRACE("%s squished %s\n", debugstr_w(szProductCode), debugstr_w(squished_pc));
976 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_LocalClassesFeatures, 0, access, &hkey)) return ERROR_SUCCESS;
977 r = RegDeleteTreeW(hkey, squished_pc);
978 RegCloseKey(hkey);
979 return r;
982 UINT MSIREG_OpenClassesUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY *key, BOOL create)
984 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
985 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
987 if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
988 TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
990 strcpyW(keypath, szInstaller_ClassesUpgradeCode);
991 strcatW(keypath, squished_pc);
993 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
994 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
997 UINT MSIREG_DeleteClassesUpgradeCodesKey(LPCWSTR szUpgradeCode)
999 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
1000 WCHAR squished_pc[GUID_SIZE];
1001 HKEY hkey;
1002 LONG r;
1004 if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
1005 TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
1007 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_ClassesUpgradeCodes, 0, access, &hkey)) return ERROR_SUCCESS;
1008 r = RegDeleteTreeW(hkey, squished_pc);
1009 RegCloseKey(hkey);
1010 return r;
1013 /*************************************************************************
1014 * MsiDecomposeDescriptorW [MSI.@]
1016 * Decomposes an MSI descriptor into product, feature and component parts.
1017 * An MSI descriptor is a string of the form:
1018 * [base 85 guid] [feature code] '>' [base 85 guid]
1020 * PARAMS
1021 * szDescriptor [I] the descriptor to decompose
1022 * szProduct [O] buffer of MAX_FEATURE_CHARS+1 for the product guid
1023 * szFeature [O] buffer of MAX_FEATURE_CHARS+1 for the feature code
1024 * szComponent [O] buffer of MAX_FEATURE_CHARS+1 for the component guid
1025 * pUsed [O] the length of the descriptor
1027 * RETURNS
1028 * ERROR_SUCCESS if everything worked correctly
1029 * ERROR_INVALID_PARAMETER if the descriptor was invalid
1032 UINT WINAPI MsiDecomposeDescriptorW( LPCWSTR szDescriptor, LPWSTR szProduct,
1033 LPWSTR szFeature, LPWSTR szComponent, LPDWORD pUsed )
1035 UINT r, len;
1036 LPWSTR p;
1037 GUID product, component;
1039 TRACE("%s %p %p %p %p\n", debugstr_w(szDescriptor), szProduct,
1040 szFeature, szComponent, pUsed);
1042 r = decode_base85_guid( szDescriptor, &product );
1043 if( !r )
1044 return ERROR_INVALID_PARAMETER;
1046 TRACE("product %s\n", debugstr_guid( &product ));
1048 p = strchrW(&szDescriptor[20],'>');
1049 if( !p )
1050 return ERROR_INVALID_PARAMETER;
1052 len = (p - &szDescriptor[20]);
1053 if( len > MAX_FEATURE_CHARS )
1054 return ERROR_INVALID_PARAMETER;
1056 TRACE("feature %s\n", debugstr_wn( &szDescriptor[20], len ));
1058 r = decode_base85_guid( p+1, &component );
1059 if( !r )
1060 return ERROR_INVALID_PARAMETER;
1062 TRACE("component %s\n", debugstr_guid( &component ));
1064 if (szProduct)
1065 StringFromGUID2( &product, szProduct, MAX_FEATURE_CHARS+1 );
1066 if (szComponent)
1067 StringFromGUID2( &component, szComponent, MAX_FEATURE_CHARS+1 );
1068 if (szFeature)
1070 memcpy( szFeature, &szDescriptor[20], len*sizeof(WCHAR) );
1071 szFeature[len] = 0;
1073 len = ( &p[21] - szDescriptor );
1075 TRACE("length = %d\n", len);
1076 if (pUsed) *pUsed = len;
1078 return ERROR_SUCCESS;
1081 UINT WINAPI MsiDecomposeDescriptorA( LPCSTR szDescriptor, LPSTR szProduct,
1082 LPSTR szFeature, LPSTR szComponent, LPDWORD pUsed )
1084 WCHAR product[MAX_FEATURE_CHARS+1];
1085 WCHAR feature[MAX_FEATURE_CHARS+1];
1086 WCHAR component[MAX_FEATURE_CHARS+1];
1087 LPWSTR str = NULL, p = NULL, f = NULL, c = NULL;
1088 UINT r;
1090 TRACE("%s %p %p %p %p\n", debugstr_a(szDescriptor), szProduct,
1091 szFeature, szComponent, pUsed);
1093 str = strdupAtoW( szDescriptor );
1094 if( szDescriptor && !str )
1095 return ERROR_OUTOFMEMORY;
1097 if (szProduct)
1098 p = product;
1099 if (szFeature)
1100 f = feature;
1101 if (szComponent)
1102 c = component;
1104 r = MsiDecomposeDescriptorW( str, p, f, c, pUsed );
1106 if (r == ERROR_SUCCESS)
1108 WideCharToMultiByte( CP_ACP, 0, p, -1,
1109 szProduct, MAX_FEATURE_CHARS+1, NULL, NULL );
1110 WideCharToMultiByte( CP_ACP, 0, f, -1,
1111 szFeature, MAX_FEATURE_CHARS+1, NULL, NULL );
1112 WideCharToMultiByte( CP_ACP, 0, c, -1,
1113 szComponent, MAX_FEATURE_CHARS+1, NULL, NULL );
1116 msi_free( str );
1118 return r;
1121 UINT WINAPI MsiEnumProductsA(DWORD index, LPSTR lpguid)
1123 DWORD r;
1124 WCHAR szwGuid[GUID_SIZE];
1126 TRACE("%d %p\n", index, lpguid);
1128 if (NULL == lpguid)
1129 return ERROR_INVALID_PARAMETER;
1130 r = MsiEnumProductsW(index, szwGuid);
1131 if( r == ERROR_SUCCESS )
1132 WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
1134 return r;
1137 UINT WINAPI MsiEnumProductsW(DWORD index, LPWSTR lpguid)
1139 TRACE("%d %p\n", index, lpguid);
1141 if (NULL == lpguid)
1142 return ERROR_INVALID_PARAMETER;
1144 return MsiEnumProductsExW( NULL, szAllSid, MSIINSTALLCONTEXT_ALL, index, lpguid,
1145 NULL, NULL, NULL );
1148 UINT WINAPI MsiEnumFeaturesA(LPCSTR szProduct, DWORD index,
1149 LPSTR szFeature, LPSTR szParent)
1151 DWORD r;
1152 WCHAR szwFeature[GUID_SIZE], szwParent[GUID_SIZE];
1153 LPWSTR szwProduct = NULL;
1155 TRACE("%s %d %p %p\n", debugstr_a(szProduct), index, szFeature, szParent);
1157 if( szProduct )
1159 szwProduct = strdupAtoW( szProduct );
1160 if( !szwProduct )
1161 return ERROR_OUTOFMEMORY;
1164 r = MsiEnumFeaturesW(szwProduct, index, szwFeature, szwParent);
1165 if( r == ERROR_SUCCESS )
1167 WideCharToMultiByte(CP_ACP, 0, szwFeature, -1,
1168 szFeature, GUID_SIZE, NULL, NULL);
1169 WideCharToMultiByte(CP_ACP, 0, szwParent, -1,
1170 szParent, GUID_SIZE, NULL, NULL);
1173 msi_free( szwProduct);
1175 return r;
1178 UINT WINAPI MsiEnumFeaturesW(LPCWSTR szProduct, DWORD index,
1179 LPWSTR szFeature, LPWSTR szParent)
1181 HKEY hkeyProduct = 0;
1182 DWORD r, sz;
1184 TRACE("%s %d %p %p\n", debugstr_w(szProduct), index, szFeature, szParent);
1186 if( !szProduct )
1187 return ERROR_INVALID_PARAMETER;
1189 r = MSIREG_OpenInstallerFeaturesKey(szProduct,&hkeyProduct,FALSE);
1190 if( r != ERROR_SUCCESS )
1191 return ERROR_NO_MORE_ITEMS;
1193 sz = GUID_SIZE;
1194 r = RegEnumValueW(hkeyProduct, index, szFeature, &sz, NULL, NULL, NULL, NULL);
1195 RegCloseKey(hkeyProduct);
1197 return r;
1200 UINT WINAPI MsiEnumComponentsA(DWORD index, LPSTR lpguid)
1202 DWORD r;
1203 WCHAR szwGuid[GUID_SIZE];
1205 TRACE("%u, %p\n", index, lpguid);
1207 if (!lpguid) return ERROR_INVALID_PARAMETER;
1209 r = MsiEnumComponentsW(index, szwGuid);
1210 if( r == ERROR_SUCCESS )
1211 WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
1213 return r;
1216 UINT WINAPI MsiEnumComponentsW(DWORD index, LPWSTR lpguid)
1218 TRACE("%u, %p\n", index, lpguid);
1220 if (!lpguid) return ERROR_INVALID_PARAMETER;
1222 return MsiEnumComponentsExW( szAllSid, MSIINSTALLCONTEXT_ALL, index, lpguid, NULL, NULL, NULL );
1225 UINT WINAPI MsiEnumComponentsExA( LPCSTR user_sid, DWORD ctx, DWORD index, CHAR guid[39],
1226 MSIINSTALLCONTEXT *installed_ctx, LPSTR sid, LPDWORD sid_len )
1228 UINT r;
1229 WCHAR *user_sidW = NULL, *sidW = NULL, guidW[GUID_SIZE];
1231 TRACE("%s, %u, %u, %p, %p, %p, %p\n", debugstr_a(user_sid), ctx, index, guid, installed_ctx,
1232 sid, sid_len);
1234 if (sid && !sid_len) return ERROR_INVALID_PARAMETER;
1235 if (user_sid && !(user_sidW = strdupAtoW( user_sid ))) return ERROR_OUTOFMEMORY;
1236 if (sid && !(sidW = msi_alloc( *sid_len * sizeof(WCHAR) )))
1238 msi_free( user_sidW );
1239 return ERROR_OUTOFMEMORY;
1241 r = MsiEnumComponentsExW( user_sidW, ctx, index, guidW, installed_ctx, sidW, sid_len );
1242 if (r == ERROR_SUCCESS)
1244 if (guid) WideCharToMultiByte( CP_ACP, 0, guidW, GUID_SIZE, guid, GUID_SIZE, NULL, NULL );
1245 if (sid) WideCharToMultiByte( CP_ACP, 0, sidW, *sid_len + 1, sid, *sid_len + 1, NULL, NULL );
1247 msi_free( user_sidW );
1248 msi_free( sidW );
1249 return r;
1252 static UINT fetch_machine_component( DWORD ctx, DWORD index, DWORD *idx, WCHAR guid[39],
1253 MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid, LPDWORD sid_len )
1255 static const WCHAR componentsW[] =
1256 {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1257 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1258 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
1259 'S','-','1','-','5','-','1','8','\\','C','o','m','p','o','n','e','n','t','s',0};
1260 UINT r = ERROR_SUCCESS;
1261 WCHAR component[GUID_SIZE];
1262 DWORD i = 0, len_component;
1263 REGSAM access = KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY;
1264 HKEY key_components;
1266 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, componentsW, 0, access, &key_components ))
1267 return ERROR_NO_MORE_ITEMS;
1269 len_component = sizeof(component)/sizeof(component[0]);
1270 while (!RegEnumKeyExW( key_components, i, component, &len_component, NULL, NULL, NULL, NULL ))
1272 if (*idx == index) goto found;
1273 (*idx)++;
1274 len_component = sizeof(component)/sizeof(component[0]);
1275 i++;
1277 RegCloseKey( key_components );
1278 return ERROR_NO_MORE_ITEMS;
1280 found:
1281 if (sid_len)
1283 if (*sid_len < 1)
1285 *sid_len = 1;
1286 r = ERROR_MORE_DATA;
1288 else if (sid)
1290 *sid_len = 0;
1291 sid[0] = 0;
1294 if (guid) unsquash_guid( component, guid );
1295 if (installed_ctx) *installed_ctx = MSIINSTALLCONTEXT_MACHINE;
1296 RegCloseKey( key_components );
1297 return r;
1300 static UINT fetch_user_component( const WCHAR *usersid, DWORD ctx, DWORD index, DWORD *idx,
1301 WCHAR guid[39], MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid,
1302 LPDWORD sid_len )
1304 static const WCHAR userdataW[] =
1305 {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1306 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1307 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a',0};
1308 static const WCHAR componentsW[] = {'\\','C','o','m','p','o','n','e','n','t','s',0};
1309 UINT r = ERROR_SUCCESS;
1310 WCHAR path[MAX_PATH], component[GUID_SIZE], user[128];
1311 DWORD i = 0, j = 0, len_component, len_user;
1312 REGSAM access = KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY;
1313 HKEY key_users, key_components;
1315 if (ctx == MSIINSTALLCONTEXT_USERMANAGED) /* FIXME: were to find these? */
1316 return ERROR_NO_MORE_ITEMS;
1318 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, userdataW, 0, access, &key_users ))
1319 return ERROR_NO_MORE_ITEMS;
1321 len_user = sizeof(user)/sizeof(user[0]);
1322 while (!RegEnumKeyExW( key_users, i, user, &len_user, NULL, NULL, NULL, NULL ))
1324 if ((strcmpW( usersid, szAllSid ) && strcmpW( usersid, user )) ||
1325 !strcmpW( szLocalSid, user ))
1327 i++;
1328 len_user = sizeof(user)/sizeof(user[0]);
1329 continue;
1331 strcpyW( path, user );
1332 strcatW( path, componentsW );
1333 if (RegOpenKeyExW( key_users, path, 0, access, &key_components ))
1335 i++;
1336 len_user = sizeof(user)/sizeof(user[0]);
1337 continue;
1339 len_component = sizeof(component)/sizeof(component[0]);
1340 while (!RegEnumKeyExW( key_components, j, component, &len_component, NULL, NULL, NULL, NULL ))
1342 if (*idx == index) goto found;
1343 (*idx)++;
1344 len_component = sizeof(component)/sizeof(component[0]);
1345 j++;
1347 RegCloseKey( key_components );
1348 len_user = sizeof(user)/sizeof(user[0]);
1349 i++;
1351 RegCloseKey( key_users );
1352 return ERROR_NO_MORE_ITEMS;
1354 found:
1355 if (sid_len)
1357 if (*sid_len < len_user + 1)
1359 *sid_len = len_user + 1;
1360 r = ERROR_MORE_DATA;
1362 else if (sid)
1364 *sid_len = len_user;
1365 strcpyW( sid, user );
1368 if (guid) unsquash_guid( component, guid );
1369 if (installed_ctx) *installed_ctx = ctx;
1370 RegCloseKey( key_components );
1371 RegCloseKey( key_users );
1372 return r;
1375 static UINT enum_components( const WCHAR *usersid, DWORD ctx, DWORD index, DWORD *idx, WCHAR guid[39],
1376 MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid, LPDWORD sid_len )
1378 UINT r = ERROR_NO_MORE_ITEMS;
1379 WCHAR *user = NULL;
1381 if (!usersid)
1383 usersid = user = get_user_sid();
1384 if (!user) return ERROR_FUNCTION_FAILED;
1386 if (ctx & MSIINSTALLCONTEXT_USERMANAGED)
1388 r = fetch_user_component( usersid, MSIINSTALLCONTEXT_USERMANAGED, index, idx, guid,
1389 installed_ctx, sid, sid_len );
1390 if (r != ERROR_NO_MORE_ITEMS) goto done;
1392 if (ctx & MSIINSTALLCONTEXT_USERUNMANAGED)
1394 r = fetch_user_component( usersid, MSIINSTALLCONTEXT_USERUNMANAGED, index, idx, guid,
1395 installed_ctx, sid, sid_len );
1396 if (r != ERROR_NO_MORE_ITEMS) goto done;
1398 if (ctx & MSIINSTALLCONTEXT_MACHINE)
1400 r = fetch_machine_component( MSIINSTALLCONTEXT_MACHINE, index, idx, guid, installed_ctx,
1401 sid, sid_len );
1402 if (r != ERROR_NO_MORE_ITEMS) goto done;
1405 done:
1406 LocalFree( user );
1407 return r;
1410 UINT WINAPI MsiEnumComponentsExW( LPCWSTR user_sid, DWORD ctx, DWORD index, WCHAR guid[39],
1411 MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid, LPDWORD sid_len )
1413 UINT r;
1414 DWORD idx = 0;
1415 static DWORD last_index;
1417 TRACE("%s, %u, %u, %p, %p, %p, %p\n", debugstr_w(user_sid), ctx, index, guid, installed_ctx,
1418 sid, sid_len);
1420 if ((sid && !sid_len) || !ctx || (user_sid && ctx == MSIINSTALLCONTEXT_MACHINE))
1421 return ERROR_INVALID_PARAMETER;
1423 if (index && index - last_index != 1)
1424 return ERROR_INVALID_PARAMETER;
1426 if (!index) last_index = 0;
1428 r = enum_components( user_sid, ctx, index, &idx, guid, installed_ctx, sid, sid_len );
1429 if (r == ERROR_SUCCESS)
1430 last_index = index;
1431 else
1432 last_index = 0;
1434 return r;
1437 UINT WINAPI MsiEnumClientsA(LPCSTR szComponent, DWORD index, LPSTR szProduct)
1439 DWORD r;
1440 WCHAR szwProduct[GUID_SIZE];
1441 LPWSTR szwComponent = NULL;
1443 TRACE("%s %d %p\n", debugstr_a(szComponent), index, szProduct);
1445 if ( !szProduct )
1446 return ERROR_INVALID_PARAMETER;
1448 if( szComponent )
1450 szwComponent = strdupAtoW( szComponent );
1451 if( !szwComponent )
1452 return ERROR_OUTOFMEMORY;
1455 r = MsiEnumClientsW(szComponent?szwComponent:NULL, index, szwProduct);
1456 if( r == ERROR_SUCCESS )
1458 WideCharToMultiByte(CP_ACP, 0, szwProduct, -1,
1459 szProduct, GUID_SIZE, NULL, NULL);
1462 msi_free( szwComponent);
1464 return r;
1467 UINT WINAPI MsiEnumClientsW(LPCWSTR szComponent, DWORD index, LPWSTR szProduct)
1469 HKEY hkeyComp = 0;
1470 DWORD r, sz;
1471 WCHAR szValName[SQUISH_GUID_SIZE];
1473 TRACE("%s %d %p\n", debugstr_w(szComponent), index, szProduct);
1475 if (!szComponent || !*szComponent || !szProduct)
1476 return ERROR_INVALID_PARAMETER;
1478 if (MSIREG_OpenUserDataComponentKey(szComponent, NULL, &hkeyComp, FALSE) != ERROR_SUCCESS &&
1479 MSIREG_OpenUserDataComponentKey(szComponent, szLocalSid, &hkeyComp, FALSE) != ERROR_SUCCESS)
1480 return ERROR_UNKNOWN_COMPONENT;
1482 /* see if there are any products at all */
1483 sz = SQUISH_GUID_SIZE;
1484 r = RegEnumValueW(hkeyComp, 0, szValName, &sz, NULL, NULL, NULL, NULL);
1485 if (r != ERROR_SUCCESS)
1487 RegCloseKey(hkeyComp);
1489 if (index != 0)
1490 return ERROR_INVALID_PARAMETER;
1492 return ERROR_UNKNOWN_COMPONENT;
1495 sz = SQUISH_GUID_SIZE;
1496 r = RegEnumValueW(hkeyComp, index, szValName, &sz, NULL, NULL, NULL, NULL);
1497 if( r == ERROR_SUCCESS )
1499 unsquash_guid(szValName, szProduct);
1500 TRACE("-> %s\n", debugstr_w(szProduct));
1502 RegCloseKey(hkeyComp);
1503 return r;
1506 static UINT MSI_EnumComponentQualifiers( LPCWSTR szComponent, DWORD iIndex,
1507 awstring *lpQualBuf, LPDWORD pcchQual,
1508 awstring *lpAppBuf, LPDWORD pcchAppBuf )
1510 DWORD name_sz, val_sz, name_max, val_max, type, ofs;
1511 LPWSTR name = NULL, val = NULL;
1512 UINT r, r2;
1513 HKEY key;
1515 TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
1516 lpQualBuf, pcchQual, lpAppBuf, pcchAppBuf);
1518 if (!szComponent)
1519 return ERROR_INVALID_PARAMETER;
1521 r = MSIREG_OpenUserComponentsKey( szComponent, &key, FALSE );
1522 if (r != ERROR_SUCCESS)
1523 return ERROR_UNKNOWN_COMPONENT;
1525 /* figure out how big the name is we want to return */
1526 name_max = 0x10;
1527 r = ERROR_OUTOFMEMORY;
1528 name = msi_alloc( name_max * sizeof(WCHAR) );
1529 if (!name)
1530 goto end;
1532 val_max = 0x10;
1533 r = ERROR_OUTOFMEMORY;
1534 val = msi_alloc( val_max );
1535 if (!val)
1536 goto end;
1538 /* loop until we allocate enough memory */
1539 while (1)
1541 name_sz = name_max;
1542 val_sz = val_max;
1543 r = RegEnumValueW( key, iIndex, name, &name_sz,
1544 NULL, &type, (LPBYTE)val, &val_sz );
1545 if (r == ERROR_SUCCESS)
1546 break;
1547 if (r != ERROR_MORE_DATA)
1548 goto end;
1550 if (type != REG_MULTI_SZ)
1552 ERR("component data has wrong type (%d)\n", type);
1553 goto end;
1556 r = ERROR_OUTOFMEMORY;
1557 if (name_sz + 1 >= name_max)
1559 name_max *= 2;
1560 msi_free( name );
1561 name = msi_alloc( name_max * sizeof (WCHAR) );
1562 if (!name)
1563 goto end;
1564 continue;
1566 if (val_sz > val_max)
1568 val_max = val_sz + sizeof (WCHAR);
1569 msi_free( val );
1570 val = msi_alloc( val_max * sizeof (WCHAR) );
1571 if (!val)
1572 goto end;
1573 continue;
1575 ERR("should be enough data, but isn't %d %d\n", name_sz, val_sz );
1576 goto end;
1579 ofs = 0;
1580 r = MsiDecomposeDescriptorW( val, NULL, NULL, NULL, &ofs );
1581 if (r != ERROR_SUCCESS)
1582 goto end;
1584 TRACE("Providing %s and %s\n", debugstr_w(name), debugstr_w(val+ofs));
1586 r = msi_strcpy_to_awstring( name, -1, lpQualBuf, pcchQual );
1587 r2 = msi_strcpy_to_awstring( val+ofs, -1, lpAppBuf, pcchAppBuf );
1589 if (r2 != ERROR_SUCCESS)
1590 r = r2;
1592 end:
1593 msi_free(val);
1594 msi_free(name);
1595 RegCloseKey(key);
1596 return r;
1599 /*************************************************************************
1600 * MsiEnumComponentQualifiersA [MSI.@]
1602 UINT WINAPI MsiEnumComponentQualifiersA( LPCSTR szComponent, DWORD iIndex,
1603 LPSTR lpQualifierBuf, LPDWORD pcchQualifierBuf,
1604 LPSTR lpApplicationDataBuf, LPDWORD pcchApplicationDataBuf )
1606 awstring qual, appdata;
1607 LPWSTR comp;
1608 UINT r;
1610 TRACE("%s %08x %p %p %p %p\n", debugstr_a(szComponent), iIndex,
1611 lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
1612 pcchApplicationDataBuf);
1614 comp = strdupAtoW( szComponent );
1615 if (szComponent && !comp)
1616 return ERROR_OUTOFMEMORY;
1618 qual.unicode = FALSE;
1619 qual.str.a = lpQualifierBuf;
1621 appdata.unicode = FALSE;
1622 appdata.str.a = lpApplicationDataBuf;
1624 r = MSI_EnumComponentQualifiers( comp, iIndex,
1625 &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
1626 msi_free( comp );
1627 return r;
1630 /*************************************************************************
1631 * MsiEnumComponentQualifiersW [MSI.@]
1633 UINT WINAPI MsiEnumComponentQualifiersW( LPCWSTR szComponent, DWORD iIndex,
1634 LPWSTR lpQualifierBuf, LPDWORD pcchQualifierBuf,
1635 LPWSTR lpApplicationDataBuf, LPDWORD pcchApplicationDataBuf )
1637 awstring qual, appdata;
1639 TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
1640 lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
1641 pcchApplicationDataBuf);
1643 qual.unicode = TRUE;
1644 qual.str.w = lpQualifierBuf;
1646 appdata.unicode = TRUE;
1647 appdata.str.w = lpApplicationDataBuf;
1649 return MSI_EnumComponentQualifiers( szComponent, iIndex,
1650 &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
1653 /*************************************************************************
1654 * MsiEnumRelatedProductsW [MSI.@]
1657 UINT WINAPI MsiEnumRelatedProductsW(LPCWSTR szUpgradeCode, DWORD dwReserved,
1658 DWORD iProductIndex, LPWSTR lpProductBuf)
1660 UINT r;
1661 HKEY hkey;
1662 DWORD dwSize = SQUISH_GUID_SIZE;
1663 WCHAR szKeyName[SQUISH_GUID_SIZE];
1665 TRACE("%s %u %u %p\n", debugstr_w(szUpgradeCode), dwReserved,
1666 iProductIndex, lpProductBuf);
1668 if (NULL == szUpgradeCode)
1669 return ERROR_INVALID_PARAMETER;
1670 if (NULL == lpProductBuf)
1671 return ERROR_INVALID_PARAMETER;
1673 r = MSIREG_OpenUpgradeCodesKey(szUpgradeCode, &hkey, FALSE);
1674 if (r != ERROR_SUCCESS)
1675 return ERROR_NO_MORE_ITEMS;
1677 r = RegEnumValueW(hkey, iProductIndex, szKeyName, &dwSize, NULL, NULL, NULL, NULL);
1678 if( r == ERROR_SUCCESS )
1679 unsquash_guid(szKeyName, lpProductBuf);
1680 RegCloseKey(hkey);
1682 return r;
1685 /*************************************************************************
1686 * MsiEnumRelatedProductsA [MSI.@]
1689 UINT WINAPI MsiEnumRelatedProductsA(LPCSTR szUpgradeCode, DWORD dwReserved,
1690 DWORD iProductIndex, LPSTR lpProductBuf)
1692 LPWSTR szwUpgradeCode = NULL;
1693 WCHAR productW[GUID_SIZE];
1694 UINT r;
1696 TRACE("%s %u %u %p\n", debugstr_a(szUpgradeCode), dwReserved,
1697 iProductIndex, lpProductBuf);
1699 if (szUpgradeCode)
1701 szwUpgradeCode = strdupAtoW( szUpgradeCode );
1702 if( !szwUpgradeCode )
1703 return ERROR_OUTOFMEMORY;
1706 r = MsiEnumRelatedProductsW( szwUpgradeCode, dwReserved,
1707 iProductIndex, productW );
1708 if (r == ERROR_SUCCESS)
1710 WideCharToMultiByte( CP_ACP, 0, productW, GUID_SIZE,
1711 lpProductBuf, GUID_SIZE, NULL, NULL );
1713 msi_free( szwUpgradeCode);
1714 return r;
1717 /***********************************************************************
1718 * MsiEnumPatchesExA [MSI.@]
1720 UINT WINAPI MsiEnumPatchesExA(LPCSTR szProductCode, LPCSTR szUserSid,
1721 DWORD dwContext, DWORD dwFilter, DWORD dwIndex, LPSTR szPatchCode,
1722 LPSTR szTargetProductCode, MSIINSTALLCONTEXT *pdwTargetProductContext,
1723 LPSTR szTargetUserSid, LPDWORD pcchTargetUserSid)
1725 LPWSTR prodcode = NULL;
1726 LPWSTR usersid = NULL;
1727 LPWSTR targsid = NULL;
1728 WCHAR patch[GUID_SIZE];
1729 WCHAR targprod[GUID_SIZE];
1730 DWORD len;
1731 UINT r;
1733 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n",
1734 debugstr_a(szProductCode), debugstr_a(szUserSid), dwContext, dwFilter,
1735 dwIndex, szPatchCode, szTargetProductCode, pdwTargetProductContext,
1736 szTargetUserSid, pcchTargetUserSid);
1738 if (szTargetUserSid && !pcchTargetUserSid)
1739 return ERROR_INVALID_PARAMETER;
1741 if (szProductCode) prodcode = strdupAtoW(szProductCode);
1742 if (szUserSid) usersid = strdupAtoW(szUserSid);
1744 r = MsiEnumPatchesExW(prodcode, usersid, dwContext, dwFilter, dwIndex,
1745 patch, targprod, pdwTargetProductContext,
1746 NULL, &len);
1747 if (r != ERROR_SUCCESS)
1748 goto done;
1750 WideCharToMultiByte(CP_ACP, 0, patch, -1, szPatchCode,
1751 GUID_SIZE, NULL, NULL);
1752 WideCharToMultiByte(CP_ACP, 0, targprod, -1, szTargetProductCode,
1753 GUID_SIZE, NULL, NULL);
1755 if (!szTargetUserSid)
1757 if (pcchTargetUserSid)
1758 *pcchTargetUserSid = len;
1760 goto done;
1763 targsid = msi_alloc(++len * sizeof(WCHAR));
1764 if (!targsid)
1766 r = ERROR_OUTOFMEMORY;
1767 goto done;
1770 r = MsiEnumPatchesExW(prodcode, usersid, dwContext, dwFilter, dwIndex,
1771 patch, targprod, pdwTargetProductContext,
1772 targsid, &len);
1773 if (r != ERROR_SUCCESS || !szTargetUserSid)
1774 goto done;
1776 WideCharToMultiByte(CP_ACP, 0, targsid, -1, szTargetUserSid,
1777 *pcchTargetUserSid, NULL, NULL);
1779 len = lstrlenW(targsid);
1780 if (*pcchTargetUserSid < len + 1)
1782 r = ERROR_MORE_DATA;
1783 *pcchTargetUserSid = len * sizeof(WCHAR);
1785 else
1786 *pcchTargetUserSid = len;
1788 done:
1789 msi_free(prodcode);
1790 msi_free(usersid);
1791 msi_free(targsid);
1793 return r;
1796 static UINT msi_get_patch_state(LPCWSTR prodcode, LPCWSTR usersid,
1797 MSIINSTALLCONTEXT context,
1798 LPWSTR patch, MSIPATCHSTATE *state)
1800 DWORD type, val, size;
1801 HKEY prod, hkey = 0;
1802 HKEY udpatch = 0;
1803 LONG res;
1804 UINT r = ERROR_NO_MORE_ITEMS;
1806 *state = MSIPATCHSTATE_INVALID;
1808 r = MSIREG_OpenUserDataProductKey(prodcode, context,
1809 usersid, &prod, FALSE);
1810 if (r != ERROR_SUCCESS)
1811 return ERROR_NO_MORE_ITEMS;
1813 res = RegOpenKeyExW(prod, szPatches, 0, KEY_READ, &hkey);
1814 if (res != ERROR_SUCCESS)
1815 goto done;
1817 res = RegOpenKeyExW(hkey, patch, 0, KEY_READ, &udpatch);
1818 if (res != ERROR_SUCCESS)
1819 goto done;
1821 size = sizeof(DWORD);
1822 res = RegGetValueW(udpatch, NULL, szState, RRF_RT_DWORD, &type, &val, &size);
1823 if (res != ERROR_SUCCESS ||
1824 val < MSIPATCHSTATE_APPLIED || val > MSIPATCHSTATE_REGISTERED)
1826 r = ERROR_BAD_CONFIGURATION;
1827 goto done;
1830 *state = val;
1831 r = ERROR_SUCCESS;
1833 done:
1834 RegCloseKey(udpatch);
1835 RegCloseKey(hkey);
1836 RegCloseKey(prod);
1838 return r;
1841 static UINT msi_check_product_patches(LPCWSTR prodcode, LPCWSTR usersid,
1842 MSIINSTALLCONTEXT context, DWORD filter, DWORD index, DWORD *idx,
1843 LPWSTR patch, LPWSTR targetprod, MSIINSTALLCONTEXT *targetctx,
1844 LPWSTR targetsid, DWORD *sidsize, LPWSTR *transforms)
1846 MSIPATCHSTATE state = MSIPATCHSTATE_INVALID;
1847 LPWSTR ptr, patches = NULL;
1848 HKEY prod, patchkey = 0;
1849 HKEY localprod = 0, localpatch = 0;
1850 DWORD type, size;
1851 LONG res;
1852 UINT temp, r = ERROR_NO_MORE_ITEMS;
1854 if (MSIREG_OpenProductKey(prodcode, usersid, context,
1855 &prod, FALSE) != ERROR_SUCCESS)
1856 return ERROR_NO_MORE_ITEMS;
1858 size = 0;
1859 res = RegGetValueW(prod, szPatches, szPatches, RRF_RT_ANY, &type, NULL,
1860 &size);
1861 if (res != ERROR_SUCCESS)
1862 goto done;
1864 if (type != REG_MULTI_SZ)
1866 r = ERROR_BAD_CONFIGURATION;
1867 goto done;
1870 patches = msi_alloc(size);
1871 if (!patches)
1873 r = ERROR_OUTOFMEMORY;
1874 goto done;
1877 res = RegGetValueW(prod, szPatches, szPatches, RRF_RT_ANY, &type,
1878 patches, &size);
1879 if (res != ERROR_SUCCESS)
1880 goto done;
1882 for (ptr = patches; *ptr && r == ERROR_NO_MORE_ITEMS; ptr += lstrlenW(ptr) + 1)
1884 if (!unsquash_guid(ptr, patch))
1886 r = ERROR_BAD_CONFIGURATION;
1887 goto done;
1890 size = 0;
1891 res = RegGetValueW(prod, szPatches, ptr, RRF_RT_REG_SZ,
1892 &type, NULL, &size);
1893 if (res != ERROR_SUCCESS)
1894 continue;
1896 if (transforms)
1898 *transforms = msi_alloc(size);
1899 if (!*transforms)
1901 r = ERROR_OUTOFMEMORY;
1902 goto done;
1905 res = RegGetValueW(prod, szPatches, ptr, RRF_RT_REG_SZ,
1906 &type, *transforms, &size);
1907 if (res != ERROR_SUCCESS)
1908 continue;
1911 if (context == MSIINSTALLCONTEXT_USERMANAGED)
1913 if (!(filter & MSIPATCHSTATE_APPLIED))
1915 temp = msi_get_patch_state(prodcode, usersid, context,
1916 ptr, &state);
1917 if (temp == ERROR_BAD_CONFIGURATION)
1919 r = ERROR_BAD_CONFIGURATION;
1920 goto done;
1923 if (temp != ERROR_SUCCESS || !(filter & state))
1924 continue;
1927 else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
1929 if (!(filter & MSIPATCHSTATE_APPLIED))
1931 temp = msi_get_patch_state(prodcode, usersid, context,
1932 ptr, &state);
1933 if (temp == ERROR_BAD_CONFIGURATION)
1935 r = ERROR_BAD_CONFIGURATION;
1936 goto done;
1939 if (temp != ERROR_SUCCESS || !(filter & state))
1940 continue;
1942 else
1944 temp = MSIREG_OpenUserDataPatchKey(patch, context,
1945 &patchkey, FALSE);
1946 RegCloseKey(patchkey);
1947 if (temp != ERROR_SUCCESS)
1948 continue;
1951 else if (context == MSIINSTALLCONTEXT_MACHINE)
1953 usersid = szEmpty;
1955 if (MSIREG_OpenUserDataProductKey(prodcode, context, NULL, &localprod, FALSE) == ERROR_SUCCESS &&
1956 RegOpenKeyExW(localprod, szPatches, 0, KEY_READ, &localpatch) == ERROR_SUCCESS &&
1957 RegOpenKeyExW(localpatch, ptr, 0, KEY_READ, &patchkey) == ERROR_SUCCESS)
1959 res = RegGetValueW(patchkey, NULL, szState, RRF_RT_REG_DWORD,
1960 &type, &state, &size);
1962 if (!(filter & state))
1963 res = ERROR_NO_MORE_ITEMS;
1965 RegCloseKey(patchkey);
1968 RegCloseKey(localpatch);
1969 RegCloseKey(localprod);
1971 if (res != ERROR_SUCCESS)
1972 continue;
1975 if (*idx < index)
1977 (*idx)++;
1978 continue;
1981 r = ERROR_SUCCESS;
1982 if (targetprod)
1983 lstrcpyW(targetprod, prodcode);
1985 if (targetctx)
1986 *targetctx = context;
1988 if (targetsid)
1990 lstrcpynW(targetsid, usersid, *sidsize);
1991 if (lstrlenW(usersid) >= *sidsize)
1992 r = ERROR_MORE_DATA;
1995 if (sidsize)
1997 *sidsize = lstrlenW(usersid);
1998 if (!targetsid)
1999 *sidsize *= sizeof(WCHAR);
2003 done:
2004 RegCloseKey(prod);
2005 msi_free(patches);
2007 return r;
2010 static UINT msi_enum_patches(LPCWSTR szProductCode, LPCWSTR szUserSid,
2011 DWORD dwContext, DWORD dwFilter, DWORD dwIndex, DWORD *idx,
2012 LPWSTR szPatchCode, LPWSTR szTargetProductCode,
2013 MSIINSTALLCONTEXT *pdwTargetProductContext, LPWSTR szTargetUserSid,
2014 LPDWORD pcchTargetUserSid, LPWSTR *szTransforms)
2016 LPWSTR usersid = NULL;
2017 UINT r = ERROR_INVALID_PARAMETER;
2019 if (!szUserSid)
2021 szUserSid = usersid = get_user_sid();
2022 if (!usersid) return ERROR_FUNCTION_FAILED;
2025 if (dwContext & MSIINSTALLCONTEXT_USERMANAGED)
2027 r = msi_check_product_patches(szProductCode, szUserSid,
2028 MSIINSTALLCONTEXT_USERMANAGED, dwFilter,
2029 dwIndex, idx, szPatchCode,
2030 szTargetProductCode,
2031 pdwTargetProductContext, szTargetUserSid,
2032 pcchTargetUserSid, szTransforms);
2033 if (r != ERROR_NO_MORE_ITEMS)
2034 goto done;
2037 if (dwContext & MSIINSTALLCONTEXT_USERUNMANAGED)
2039 r = msi_check_product_patches(szProductCode, szUserSid,
2040 MSIINSTALLCONTEXT_USERUNMANAGED, dwFilter,
2041 dwIndex, idx, szPatchCode,
2042 szTargetProductCode,
2043 pdwTargetProductContext, szTargetUserSid,
2044 pcchTargetUserSid, szTransforms);
2045 if (r != ERROR_NO_MORE_ITEMS)
2046 goto done;
2049 if (dwContext & MSIINSTALLCONTEXT_MACHINE)
2051 r = msi_check_product_patches(szProductCode, szUserSid,
2052 MSIINSTALLCONTEXT_MACHINE, dwFilter,
2053 dwIndex, idx, szPatchCode,
2054 szTargetProductCode,
2055 pdwTargetProductContext, szTargetUserSid,
2056 pcchTargetUserSid, szTransforms);
2057 if (r != ERROR_NO_MORE_ITEMS)
2058 goto done;
2061 done:
2062 LocalFree(usersid);
2063 return r;
2066 /***********************************************************************
2067 * MsiEnumPatchesExW [MSI.@]
2069 UINT WINAPI MsiEnumPatchesExW(LPCWSTR szProductCode, LPCWSTR szUserSid,
2070 DWORD dwContext, DWORD dwFilter, DWORD dwIndex, LPWSTR szPatchCode,
2071 LPWSTR szTargetProductCode, MSIINSTALLCONTEXT *pdwTargetProductContext,
2072 LPWSTR szTargetUserSid, LPDWORD pcchTargetUserSid)
2074 WCHAR squished_pc[GUID_SIZE];
2075 DWORD idx = 0;
2076 UINT r;
2078 static DWORD last_index;
2080 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n",
2081 debugstr_w(szProductCode), debugstr_w(szUserSid), dwContext, dwFilter,
2082 dwIndex, szPatchCode, szTargetProductCode, pdwTargetProductContext,
2083 szTargetUserSid, pcchTargetUserSid);
2085 if (!szProductCode || !squash_guid(szProductCode, squished_pc))
2086 return ERROR_INVALID_PARAMETER;
2088 if (szUserSid && !strcmpW( szUserSid, szLocalSid ))
2089 return ERROR_INVALID_PARAMETER;
2091 if (dwContext & MSIINSTALLCONTEXT_MACHINE && szUserSid)
2092 return ERROR_INVALID_PARAMETER;
2094 if (dwContext <= MSIINSTALLCONTEXT_NONE ||
2095 dwContext > MSIINSTALLCONTEXT_ALL)
2096 return ERROR_INVALID_PARAMETER;
2098 if (dwFilter <= MSIPATCHSTATE_INVALID || dwFilter > MSIPATCHSTATE_ALL)
2099 return ERROR_INVALID_PARAMETER;
2101 if (dwIndex && dwIndex - last_index != 1)
2102 return ERROR_INVALID_PARAMETER;
2104 if (dwIndex == 0)
2105 last_index = 0;
2107 r = msi_enum_patches(szProductCode, szUserSid, dwContext, dwFilter,
2108 dwIndex, &idx, szPatchCode, szTargetProductCode,
2109 pdwTargetProductContext, szTargetUserSid,
2110 pcchTargetUserSid, NULL);
2112 if (r == ERROR_SUCCESS)
2113 last_index = dwIndex;
2114 else
2115 last_index = 0;
2117 return r;
2120 /***********************************************************************
2121 * MsiEnumPatchesA [MSI.@]
2123 UINT WINAPI MsiEnumPatchesA(LPCSTR szProduct, DWORD iPatchIndex,
2124 LPSTR lpPatchBuf, LPSTR lpTransformsBuf, LPDWORD pcchTransformsBuf)
2126 LPWSTR product, transforms;
2127 WCHAR patch[GUID_SIZE];
2128 DWORD len;
2129 UINT r;
2131 TRACE("(%s %d %p %p %p)\n", debugstr_a(szProduct), iPatchIndex,
2132 lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);
2134 if (!szProduct || !lpPatchBuf || !lpTransformsBuf || !pcchTransformsBuf)
2135 return ERROR_INVALID_PARAMETER;
2137 product = strdupAtoW(szProduct);
2138 if (!product)
2139 return ERROR_OUTOFMEMORY;
2141 len = *pcchTransformsBuf;
2142 transforms = msi_alloc( len * sizeof(WCHAR) );
2143 if (!transforms)
2145 r = ERROR_OUTOFMEMORY;
2146 goto done;
2149 r = MsiEnumPatchesW(product, iPatchIndex, patch, transforms, &len);
2150 if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
2151 goto done;
2153 WideCharToMultiByte(CP_ACP, 0, patch, -1, lpPatchBuf,
2154 GUID_SIZE, NULL, NULL);
2156 if (!WideCharToMultiByte(CP_ACP, 0, transforms, -1, lpTransformsBuf,
2157 *pcchTransformsBuf, NULL, NULL))
2158 r = ERROR_MORE_DATA;
2160 if (r == ERROR_MORE_DATA)
2162 lpTransformsBuf[*pcchTransformsBuf - 1] = '\0';
2163 *pcchTransformsBuf = len * 2;
2165 else
2166 *pcchTransformsBuf = strlen( lpTransformsBuf );
2168 done:
2169 msi_free(transforms);
2170 msi_free(product);
2172 return r;
2175 /***********************************************************************
2176 * MsiEnumPatchesW [MSI.@]
2178 UINT WINAPI MsiEnumPatchesW(LPCWSTR szProduct, DWORD iPatchIndex,
2179 LPWSTR lpPatchBuf, LPWSTR lpTransformsBuf, LPDWORD pcchTransformsBuf)
2181 WCHAR squished_pc[GUID_SIZE];
2182 LPWSTR transforms = NULL;
2183 HKEY prod;
2184 DWORD idx = 0;
2185 UINT r;
2187 TRACE("(%s %d %p %p %p)\n", debugstr_w(szProduct), iPatchIndex,
2188 lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);
2190 if (!szProduct || !squash_guid(szProduct, squished_pc))
2191 return ERROR_INVALID_PARAMETER;
2193 if (!lpPatchBuf || !lpTransformsBuf || !pcchTransformsBuf)
2194 return ERROR_INVALID_PARAMETER;
2196 if (MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERMANAGED,
2197 &prod, FALSE) != ERROR_SUCCESS &&
2198 MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERUNMANAGED,
2199 &prod, FALSE) != ERROR_SUCCESS &&
2200 MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_MACHINE,
2201 &prod, FALSE) != ERROR_SUCCESS)
2202 return ERROR_UNKNOWN_PRODUCT;
2204 RegCloseKey(prod);
2206 r = msi_enum_patches(szProduct, NULL, MSIINSTALLCONTEXT_ALL,
2207 MSIPATCHSTATE_ALL, iPatchIndex, &idx, lpPatchBuf,
2208 NULL, NULL, NULL, NULL, &transforms);
2209 if (r != ERROR_SUCCESS)
2210 goto done;
2212 lstrcpynW(lpTransformsBuf, transforms, *pcchTransformsBuf);
2213 if (*pcchTransformsBuf <= lstrlenW(transforms))
2215 r = ERROR_MORE_DATA;
2216 *pcchTransformsBuf = lstrlenW(transforms);
2218 else
2219 *pcchTransformsBuf = lstrlenW(transforms);
2221 done:
2222 msi_free(transforms);
2223 return r;
2226 UINT WINAPI MsiEnumProductsExA( LPCSTR product, LPCSTR usersid, DWORD ctx, DWORD index,
2227 CHAR installed_product[GUID_SIZE],
2228 MSIINSTALLCONTEXT *installed_ctx, LPSTR sid, LPDWORD sid_len )
2230 UINT r;
2231 WCHAR installed_productW[GUID_SIZE], *productW = NULL, *usersidW = NULL, *sidW = NULL;
2233 TRACE("%s, %s, %u, %u, %p, %p, %p, %p\n", debugstr_a(product), debugstr_a(usersid),
2234 ctx, index, installed_product, installed_ctx, sid, sid_len);
2236 if (sid && !sid_len) return ERROR_INVALID_PARAMETER;
2237 if (product && !(productW = strdupAtoW( product ))) return ERROR_OUTOFMEMORY;
2238 if (usersid && !(usersidW = strdupAtoW( usersid )))
2240 msi_free( productW );
2241 return ERROR_OUTOFMEMORY;
2243 if (sid && !(sidW = msi_alloc( *sid_len * sizeof(WCHAR) )))
2245 msi_free( usersidW );
2246 msi_free( productW );
2247 return ERROR_OUTOFMEMORY;
2249 r = MsiEnumProductsExW( productW, usersidW, ctx, index, installed_productW,
2250 installed_ctx, sidW, sid_len );
2251 if (r == ERROR_SUCCESS)
2253 if (installed_product) WideCharToMultiByte( CP_ACP, 0, installed_productW, GUID_SIZE,
2254 installed_product, GUID_SIZE, NULL, NULL );
2255 if (sid) WideCharToMultiByte( CP_ACP, 0, sidW, *sid_len + 1, sid, *sid_len + 1, NULL, NULL );
2257 msi_free( productW );
2258 msi_free( usersidW );
2259 msi_free( sidW );
2260 return r;
2263 static UINT fetch_machine_product( const WCHAR *match, DWORD index, DWORD *idx,
2264 WCHAR installed_product[GUID_SIZE],
2265 MSIINSTALLCONTEXT *installed_ctx, WCHAR *sid, DWORD *sid_len )
2267 static const WCHAR productsW[] =
2268 {'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
2269 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
2270 UINT r;
2271 WCHAR product[GUID_SIZE];
2272 DWORD i = 0, len;
2273 REGSAM access = KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY;
2274 HKEY key;
2276 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, productsW, 0, access, &key ))
2277 return ERROR_NO_MORE_ITEMS;
2279 len = sizeof(product)/sizeof(product[0]);
2280 while (!RegEnumKeyExW( key, i, product, &len, NULL, NULL, NULL, NULL ))
2282 if (match && strcmpW( match, product ))
2284 i++;
2285 len = sizeof(product)/sizeof(product[0]);
2286 continue;
2288 if (*idx == index) goto found;
2289 (*idx)++;
2290 len = sizeof(product)/sizeof(product[0]);
2291 i++;
2293 RegCloseKey( key );
2294 return ERROR_NO_MORE_ITEMS;
2296 found:
2297 if (sid_len && *sid_len < 1)
2299 *sid_len = 1;
2300 r = ERROR_MORE_DATA;
2302 else
2304 if (installed_product) unsquash_guid( product, installed_product );
2305 if (installed_ctx) *installed_ctx = MSIINSTALLCONTEXT_MACHINE;
2306 if (sid)
2308 sid[0] = 0;
2309 *sid_len = 0;
2311 r = ERROR_SUCCESS;
2313 RegCloseKey( key );
2314 return r;
2317 static UINT fetch_user_product( const WCHAR *match, const WCHAR *usersid, DWORD ctx, DWORD index,
2318 DWORD *idx, WCHAR installed_product[GUID_SIZE],
2319 MSIINSTALLCONTEXT *installed_ctx, WCHAR *sid, DWORD *sid_len )
2321 static const WCHAR managedW[] =
2322 {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
2323 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s',
2324 'i','o','n','\\','I','n','s','t','a','l','l','e','r','\\','M','a','n','a','g','e','d',0};
2325 static const WCHAR managed_productsW[] =
2326 {'\\','I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
2327 static const WCHAR unmanaged_productsW[] =
2328 {'\\','S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
2329 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
2330 UINT r;
2331 const WCHAR *subkey;
2332 WCHAR path[MAX_PATH], product[GUID_SIZE], user[128];
2333 DWORD i = 0, j = 0, len_product, len_user;
2334 REGSAM access = KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY;
2335 HKEY key_users, key_products;
2337 if (ctx == MSIINSTALLCONTEXT_USERMANAGED)
2339 subkey = managed_productsW;
2340 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, managedW, 0, access, &key_users ))
2341 return ERROR_NO_MORE_ITEMS;
2343 else if (ctx == MSIINSTALLCONTEXT_USERUNMANAGED)
2345 subkey = unmanaged_productsW;
2346 if (RegOpenKeyExW( HKEY_USERS, NULL, 0, access, &key_users ))
2347 return ERROR_NO_MORE_ITEMS;
2349 else return ERROR_INVALID_PARAMETER;
2351 len_user = sizeof(user)/sizeof(user[0]);
2352 while (!RegEnumKeyExW( key_users, i, user, &len_user, NULL, NULL, NULL, NULL ))
2354 if (strcmpW( usersid, user ) && strcmpW( usersid, szAllSid ))
2356 i++;
2357 len_user = sizeof(user)/sizeof(user[0]);
2358 continue;
2360 strcpyW( path, user );
2361 strcatW( path, subkey );
2362 if (RegOpenKeyExW( key_users, path, 0, access, &key_products ))
2364 i++;
2365 len_user = sizeof(user)/sizeof(user[0]);
2366 continue;
2368 len_product = sizeof(product)/sizeof(product[0]);
2369 while (!RegEnumKeyExW( key_products, j, product, &len_product, NULL, NULL, NULL, NULL ))
2371 if (match && strcmpW( match, product ))
2373 j++;
2374 len_product = sizeof(product)/sizeof(product[0]);
2375 continue;
2377 if (*idx == index) goto found;
2378 (*idx)++;
2379 len_product = sizeof(product)/sizeof(product[0]);
2380 j++;
2382 RegCloseKey( key_products );
2383 len_user = sizeof(user)/sizeof(user[0]);
2384 i++;
2386 RegCloseKey( key_users );
2387 return ERROR_NO_MORE_ITEMS;
2389 found:
2390 if (sid_len && *sid_len <= len_user)
2392 *sid_len = len_user;
2393 r = ERROR_MORE_DATA;
2395 else
2397 if (installed_product) unsquash_guid( product, installed_product );
2398 if (installed_ctx) *installed_ctx = ctx;
2399 if (sid)
2401 strcpyW( sid, user );
2402 *sid_len = len_user;
2404 r = ERROR_SUCCESS;
2406 RegCloseKey( key_products );
2407 RegCloseKey( key_users );
2408 return r;
2411 static UINT enum_products( const WCHAR *product, const WCHAR *usersid, DWORD ctx, DWORD index,
2412 DWORD *idx, WCHAR installed_product[GUID_SIZE],
2413 MSIINSTALLCONTEXT *installed_ctx, WCHAR *sid, DWORD *sid_len )
2415 UINT r = ERROR_NO_MORE_ITEMS;
2416 WCHAR *user = NULL;
2418 if (!usersid)
2420 usersid = user = get_user_sid();
2421 if (!user) return ERROR_FUNCTION_FAILED;
2423 if (ctx & MSIINSTALLCONTEXT_MACHINE)
2425 r = fetch_machine_product( product, index, idx, installed_product, installed_ctx,
2426 sid, sid_len );
2427 if (r != ERROR_NO_MORE_ITEMS) goto done;
2429 if (ctx & MSIINSTALLCONTEXT_USERUNMANAGED)
2431 r = fetch_user_product( product, usersid, MSIINSTALLCONTEXT_USERUNMANAGED, index,
2432 idx, installed_product, installed_ctx, sid, sid_len );
2433 if (r != ERROR_NO_MORE_ITEMS) goto done;
2435 if (ctx & MSIINSTALLCONTEXT_USERMANAGED)
2437 r = fetch_user_product( product, usersid, MSIINSTALLCONTEXT_USERMANAGED, index,
2438 idx, installed_product, installed_ctx, sid, sid_len );
2439 if (r != ERROR_NO_MORE_ITEMS) goto done;
2442 done:
2443 LocalFree( user );
2444 return r;
2447 UINT WINAPI MsiEnumProductsExW( LPCWSTR product, LPCWSTR usersid, DWORD ctx, DWORD index,
2448 WCHAR installed_product[GUID_SIZE],
2449 MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid, LPDWORD sid_len )
2451 UINT r;
2452 DWORD idx = 0;
2453 static DWORD last_index;
2455 TRACE("%s, %s, %u, %u, %p, %p, %p, %p\n", debugstr_w(product), debugstr_w(usersid),
2456 ctx, index, installed_product, installed_ctx, sid, sid_len);
2458 if ((sid && !sid_len) || !ctx || (usersid && ctx == MSIINSTALLCONTEXT_MACHINE))
2459 return ERROR_INVALID_PARAMETER;
2461 if (index && index - last_index != 1)
2462 return ERROR_INVALID_PARAMETER;
2464 if (!index) last_index = 0;
2466 r = enum_products( product, usersid, ctx, index, &idx, installed_product, installed_ctx,
2467 sid, sid_len );
2468 if (r == ERROR_SUCCESS)
2469 last_index = index;
2470 else
2471 last_index = 0;
2473 return r;