d3d9/tests: Move a Present out of the loops in fog_with_shader_test().
[wine/multimedia.git] / dlls / msi / registry.c
blob3dc989fe3e3722c58eff4b25045cc238c134c2a8
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 UINT WINAPI MsiEnumClientsExA(LPCSTR component, LPCSTR usersid, DWORD ctx, DWORD index,
1507 CHAR installed_product[GUID_SIZE],
1508 MSIINSTALLCONTEXT *installed_ctx, LPSTR sid, LPDWORD sid_len)
1510 FIXME("%s, %s, %u, %u, %p, %p, %p, %p\n", debugstr_a(component), debugstr_a(usersid),
1511 ctx, index, installed_product, installed_ctx, sid, sid_len);
1512 return ERROR_ACCESS_DENIED;
1515 UINT WINAPI MsiEnumClientsExW(LPCWSTR component, LPCWSTR usersid, DWORD ctx, DWORD index,
1516 WCHAR installed_product[GUID_SIZE],
1517 MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid, LPDWORD sid_len)
1519 FIXME("%s, %s, %u, %u, %p, %p, %p, %p\n", debugstr_w(component), debugstr_w(usersid),
1520 ctx, index, installed_product, installed_ctx, sid, sid_len);
1521 return ERROR_ACCESS_DENIED;
1524 static UINT MSI_EnumComponentQualifiers( LPCWSTR szComponent, DWORD iIndex,
1525 awstring *lpQualBuf, LPDWORD pcchQual,
1526 awstring *lpAppBuf, LPDWORD pcchAppBuf )
1528 DWORD name_sz, val_sz, name_max, val_max, type, ofs;
1529 LPWSTR name = NULL, val = NULL;
1530 UINT r, r2;
1531 HKEY key;
1533 TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
1534 lpQualBuf, pcchQual, lpAppBuf, pcchAppBuf);
1536 if (!szComponent)
1537 return ERROR_INVALID_PARAMETER;
1539 r = MSIREG_OpenUserComponentsKey( szComponent, &key, FALSE );
1540 if (r != ERROR_SUCCESS)
1541 return ERROR_UNKNOWN_COMPONENT;
1543 /* figure out how big the name is we want to return */
1544 name_max = 0x10;
1545 r = ERROR_OUTOFMEMORY;
1546 name = msi_alloc( name_max * sizeof(WCHAR) );
1547 if (!name)
1548 goto end;
1550 val_max = 0x10;
1551 r = ERROR_OUTOFMEMORY;
1552 val = msi_alloc( val_max );
1553 if (!val)
1554 goto end;
1556 /* loop until we allocate enough memory */
1557 while (1)
1559 name_sz = name_max;
1560 val_sz = val_max;
1561 r = RegEnumValueW( key, iIndex, name, &name_sz,
1562 NULL, &type, (LPBYTE)val, &val_sz );
1563 if (r == ERROR_SUCCESS)
1564 break;
1565 if (r != ERROR_MORE_DATA)
1566 goto end;
1568 if (type != REG_MULTI_SZ)
1570 ERR("component data has wrong type (%d)\n", type);
1571 goto end;
1574 r = ERROR_OUTOFMEMORY;
1575 if (name_sz + 1 >= name_max)
1577 name_max *= 2;
1578 msi_free( name );
1579 name = msi_alloc( name_max * sizeof (WCHAR) );
1580 if (!name)
1581 goto end;
1582 continue;
1584 if (val_sz > val_max)
1586 val_max = val_sz + sizeof (WCHAR);
1587 msi_free( val );
1588 val = msi_alloc( val_max * sizeof (WCHAR) );
1589 if (!val)
1590 goto end;
1591 continue;
1593 ERR("should be enough data, but isn't %d %d\n", name_sz, val_sz );
1594 goto end;
1597 ofs = 0;
1598 r = MsiDecomposeDescriptorW( val, NULL, NULL, NULL, &ofs );
1599 if (r != ERROR_SUCCESS)
1600 goto end;
1602 TRACE("Providing %s and %s\n", debugstr_w(name), debugstr_w(val+ofs));
1604 r = msi_strcpy_to_awstring( name, -1, lpQualBuf, pcchQual );
1605 r2 = msi_strcpy_to_awstring( val+ofs, -1, lpAppBuf, pcchAppBuf );
1607 if (r2 != ERROR_SUCCESS)
1608 r = r2;
1610 end:
1611 msi_free(val);
1612 msi_free(name);
1613 RegCloseKey(key);
1614 return r;
1617 /*************************************************************************
1618 * MsiEnumComponentQualifiersA [MSI.@]
1620 UINT WINAPI MsiEnumComponentQualifiersA( LPCSTR szComponent, DWORD iIndex,
1621 LPSTR lpQualifierBuf, LPDWORD pcchQualifierBuf,
1622 LPSTR lpApplicationDataBuf, LPDWORD pcchApplicationDataBuf )
1624 awstring qual, appdata;
1625 LPWSTR comp;
1626 UINT r;
1628 TRACE("%s %08x %p %p %p %p\n", debugstr_a(szComponent), iIndex,
1629 lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
1630 pcchApplicationDataBuf);
1632 comp = strdupAtoW( szComponent );
1633 if (szComponent && !comp)
1634 return ERROR_OUTOFMEMORY;
1636 qual.unicode = FALSE;
1637 qual.str.a = lpQualifierBuf;
1639 appdata.unicode = FALSE;
1640 appdata.str.a = lpApplicationDataBuf;
1642 r = MSI_EnumComponentQualifiers( comp, iIndex,
1643 &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
1644 msi_free( comp );
1645 return r;
1648 /*************************************************************************
1649 * MsiEnumComponentQualifiersW [MSI.@]
1651 UINT WINAPI MsiEnumComponentQualifiersW( LPCWSTR szComponent, DWORD iIndex,
1652 LPWSTR lpQualifierBuf, LPDWORD pcchQualifierBuf,
1653 LPWSTR lpApplicationDataBuf, LPDWORD pcchApplicationDataBuf )
1655 awstring qual, appdata;
1657 TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
1658 lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
1659 pcchApplicationDataBuf);
1661 qual.unicode = TRUE;
1662 qual.str.w = lpQualifierBuf;
1664 appdata.unicode = TRUE;
1665 appdata.str.w = lpApplicationDataBuf;
1667 return MSI_EnumComponentQualifiers( szComponent, iIndex,
1668 &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
1671 /*************************************************************************
1672 * MsiEnumRelatedProductsW [MSI.@]
1675 UINT WINAPI MsiEnumRelatedProductsW(LPCWSTR szUpgradeCode, DWORD dwReserved,
1676 DWORD iProductIndex, LPWSTR lpProductBuf)
1678 UINT r;
1679 HKEY hkey;
1680 DWORD dwSize = SQUISH_GUID_SIZE;
1681 WCHAR szKeyName[SQUISH_GUID_SIZE];
1683 TRACE("%s %u %u %p\n", debugstr_w(szUpgradeCode), dwReserved,
1684 iProductIndex, lpProductBuf);
1686 if (NULL == szUpgradeCode)
1687 return ERROR_INVALID_PARAMETER;
1688 if (NULL == lpProductBuf)
1689 return ERROR_INVALID_PARAMETER;
1691 r = MSIREG_OpenUpgradeCodesKey(szUpgradeCode, &hkey, FALSE);
1692 if (r != ERROR_SUCCESS)
1693 return ERROR_NO_MORE_ITEMS;
1695 r = RegEnumValueW(hkey, iProductIndex, szKeyName, &dwSize, NULL, NULL, NULL, NULL);
1696 if( r == ERROR_SUCCESS )
1697 unsquash_guid(szKeyName, lpProductBuf);
1698 RegCloseKey(hkey);
1700 return r;
1703 /*************************************************************************
1704 * MsiEnumRelatedProductsA [MSI.@]
1707 UINT WINAPI MsiEnumRelatedProductsA(LPCSTR szUpgradeCode, DWORD dwReserved,
1708 DWORD iProductIndex, LPSTR lpProductBuf)
1710 LPWSTR szwUpgradeCode = NULL;
1711 WCHAR productW[GUID_SIZE];
1712 UINT r;
1714 TRACE("%s %u %u %p\n", debugstr_a(szUpgradeCode), dwReserved,
1715 iProductIndex, lpProductBuf);
1717 if (szUpgradeCode)
1719 szwUpgradeCode = strdupAtoW( szUpgradeCode );
1720 if( !szwUpgradeCode )
1721 return ERROR_OUTOFMEMORY;
1724 r = MsiEnumRelatedProductsW( szwUpgradeCode, dwReserved,
1725 iProductIndex, productW );
1726 if (r == ERROR_SUCCESS)
1728 WideCharToMultiByte( CP_ACP, 0, productW, GUID_SIZE,
1729 lpProductBuf, GUID_SIZE, NULL, NULL );
1731 msi_free( szwUpgradeCode);
1732 return r;
1735 /***********************************************************************
1736 * MsiEnumPatchesExA [MSI.@]
1738 UINT WINAPI MsiEnumPatchesExA(LPCSTR szProductCode, LPCSTR szUserSid,
1739 DWORD dwContext, DWORD dwFilter, DWORD dwIndex, LPSTR szPatchCode,
1740 LPSTR szTargetProductCode, MSIINSTALLCONTEXT *pdwTargetProductContext,
1741 LPSTR szTargetUserSid, LPDWORD pcchTargetUserSid)
1743 LPWSTR prodcode = NULL;
1744 LPWSTR usersid = NULL;
1745 LPWSTR targsid = NULL;
1746 WCHAR patch[GUID_SIZE];
1747 WCHAR targprod[GUID_SIZE];
1748 DWORD len;
1749 UINT r;
1751 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n",
1752 debugstr_a(szProductCode), debugstr_a(szUserSid), dwContext, dwFilter,
1753 dwIndex, szPatchCode, szTargetProductCode, pdwTargetProductContext,
1754 szTargetUserSid, pcchTargetUserSid);
1756 if (szTargetUserSid && !pcchTargetUserSid)
1757 return ERROR_INVALID_PARAMETER;
1759 if (szProductCode) prodcode = strdupAtoW(szProductCode);
1760 if (szUserSid) usersid = strdupAtoW(szUserSid);
1762 r = MsiEnumPatchesExW(prodcode, usersid, dwContext, dwFilter, dwIndex,
1763 patch, targprod, pdwTargetProductContext,
1764 NULL, &len);
1765 if (r != ERROR_SUCCESS)
1766 goto done;
1768 WideCharToMultiByte(CP_ACP, 0, patch, -1, szPatchCode,
1769 GUID_SIZE, NULL, NULL);
1770 WideCharToMultiByte(CP_ACP, 0, targprod, -1, szTargetProductCode,
1771 GUID_SIZE, NULL, NULL);
1773 if (!szTargetUserSid)
1775 if (pcchTargetUserSid)
1776 *pcchTargetUserSid = len;
1778 goto done;
1781 targsid = msi_alloc(++len * sizeof(WCHAR));
1782 if (!targsid)
1784 r = ERROR_OUTOFMEMORY;
1785 goto done;
1788 r = MsiEnumPatchesExW(prodcode, usersid, dwContext, dwFilter, dwIndex,
1789 patch, targprod, pdwTargetProductContext,
1790 targsid, &len);
1791 if (r != ERROR_SUCCESS || !szTargetUserSid)
1792 goto done;
1794 WideCharToMultiByte(CP_ACP, 0, targsid, -1, szTargetUserSid,
1795 *pcchTargetUserSid, NULL, NULL);
1797 len = lstrlenW(targsid);
1798 if (*pcchTargetUserSid < len + 1)
1800 r = ERROR_MORE_DATA;
1801 *pcchTargetUserSid = len * sizeof(WCHAR);
1803 else
1804 *pcchTargetUserSid = len;
1806 done:
1807 msi_free(prodcode);
1808 msi_free(usersid);
1809 msi_free(targsid);
1811 return r;
1814 static UINT msi_get_patch_state(LPCWSTR prodcode, LPCWSTR usersid,
1815 MSIINSTALLCONTEXT context,
1816 LPWSTR patch, MSIPATCHSTATE *state)
1818 DWORD type, val, size;
1819 HKEY prod, hkey = 0;
1820 HKEY udpatch = 0;
1821 LONG res;
1822 UINT r = ERROR_NO_MORE_ITEMS;
1824 *state = MSIPATCHSTATE_INVALID;
1826 r = MSIREG_OpenUserDataProductKey(prodcode, context,
1827 usersid, &prod, FALSE);
1828 if (r != ERROR_SUCCESS)
1829 return ERROR_NO_MORE_ITEMS;
1831 res = RegOpenKeyExW(prod, szPatches, 0, KEY_READ, &hkey);
1832 if (res != ERROR_SUCCESS)
1833 goto done;
1835 res = RegOpenKeyExW(hkey, patch, 0, KEY_READ, &udpatch);
1836 if (res != ERROR_SUCCESS)
1837 goto done;
1839 size = sizeof(DWORD);
1840 res = RegGetValueW(udpatch, NULL, szState, RRF_RT_DWORD, &type, &val, &size);
1841 if (res != ERROR_SUCCESS ||
1842 val < MSIPATCHSTATE_APPLIED || val > MSIPATCHSTATE_REGISTERED)
1844 r = ERROR_BAD_CONFIGURATION;
1845 goto done;
1848 *state = val;
1849 r = ERROR_SUCCESS;
1851 done:
1852 RegCloseKey(udpatch);
1853 RegCloseKey(hkey);
1854 RegCloseKey(prod);
1856 return r;
1859 static UINT msi_check_product_patches(LPCWSTR prodcode, LPCWSTR usersid,
1860 MSIINSTALLCONTEXT context, DWORD filter, DWORD index, DWORD *idx,
1861 LPWSTR patch, LPWSTR targetprod, MSIINSTALLCONTEXT *targetctx,
1862 LPWSTR targetsid, DWORD *sidsize, LPWSTR *transforms)
1864 MSIPATCHSTATE state = MSIPATCHSTATE_INVALID;
1865 LPWSTR ptr, patches = NULL;
1866 HKEY prod, patchkey = 0;
1867 HKEY localprod = 0, localpatch = 0;
1868 DWORD type, size;
1869 LONG res;
1870 UINT temp, r = ERROR_NO_MORE_ITEMS;
1872 if (MSIREG_OpenProductKey(prodcode, usersid, context,
1873 &prod, FALSE) != ERROR_SUCCESS)
1874 return ERROR_NO_MORE_ITEMS;
1876 size = 0;
1877 res = RegGetValueW(prod, szPatches, szPatches, RRF_RT_ANY, &type, NULL,
1878 &size);
1879 if (res != ERROR_SUCCESS)
1880 goto done;
1882 if (type != REG_MULTI_SZ)
1884 r = ERROR_BAD_CONFIGURATION;
1885 goto done;
1888 patches = msi_alloc(size);
1889 if (!patches)
1891 r = ERROR_OUTOFMEMORY;
1892 goto done;
1895 res = RegGetValueW(prod, szPatches, szPatches, RRF_RT_ANY, &type,
1896 patches, &size);
1897 if (res != ERROR_SUCCESS)
1898 goto done;
1900 for (ptr = patches; *ptr && r == ERROR_NO_MORE_ITEMS; ptr += lstrlenW(ptr) + 1)
1902 if (!unsquash_guid(ptr, patch))
1904 r = ERROR_BAD_CONFIGURATION;
1905 goto done;
1908 size = 0;
1909 res = RegGetValueW(prod, szPatches, ptr, RRF_RT_REG_SZ,
1910 &type, NULL, &size);
1911 if (res != ERROR_SUCCESS)
1912 continue;
1914 if (transforms)
1916 *transforms = msi_alloc(size);
1917 if (!*transforms)
1919 r = ERROR_OUTOFMEMORY;
1920 goto done;
1923 res = RegGetValueW(prod, szPatches, ptr, RRF_RT_REG_SZ,
1924 &type, *transforms, &size);
1925 if (res != ERROR_SUCCESS)
1926 continue;
1929 if (context == MSIINSTALLCONTEXT_USERMANAGED)
1931 if (!(filter & MSIPATCHSTATE_APPLIED))
1933 temp = msi_get_patch_state(prodcode, usersid, context,
1934 ptr, &state);
1935 if (temp == ERROR_BAD_CONFIGURATION)
1937 r = ERROR_BAD_CONFIGURATION;
1938 goto done;
1941 if (temp != ERROR_SUCCESS || !(filter & state))
1942 continue;
1945 else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
1947 if (!(filter & MSIPATCHSTATE_APPLIED))
1949 temp = msi_get_patch_state(prodcode, usersid, context,
1950 ptr, &state);
1951 if (temp == ERROR_BAD_CONFIGURATION)
1953 r = ERROR_BAD_CONFIGURATION;
1954 goto done;
1957 if (temp != ERROR_SUCCESS || !(filter & state))
1958 continue;
1960 else
1962 temp = MSIREG_OpenUserDataPatchKey(patch, context,
1963 &patchkey, FALSE);
1964 RegCloseKey(patchkey);
1965 if (temp != ERROR_SUCCESS)
1966 continue;
1969 else if (context == MSIINSTALLCONTEXT_MACHINE)
1971 usersid = szEmpty;
1973 if (MSIREG_OpenUserDataProductKey(prodcode, context, NULL, &localprod, FALSE) == ERROR_SUCCESS &&
1974 RegOpenKeyExW(localprod, szPatches, 0, KEY_READ, &localpatch) == ERROR_SUCCESS &&
1975 RegOpenKeyExW(localpatch, ptr, 0, KEY_READ, &patchkey) == ERROR_SUCCESS)
1977 res = RegGetValueW(patchkey, NULL, szState, RRF_RT_REG_DWORD,
1978 &type, &state, &size);
1980 if (!(filter & state))
1981 res = ERROR_NO_MORE_ITEMS;
1983 RegCloseKey(patchkey);
1986 RegCloseKey(localpatch);
1987 RegCloseKey(localprod);
1989 if (res != ERROR_SUCCESS)
1990 continue;
1993 if (*idx < index)
1995 (*idx)++;
1996 continue;
1999 r = ERROR_SUCCESS;
2000 if (targetprod)
2001 lstrcpyW(targetprod, prodcode);
2003 if (targetctx)
2004 *targetctx = context;
2006 if (targetsid)
2008 lstrcpynW(targetsid, usersid, *sidsize);
2009 if (lstrlenW(usersid) >= *sidsize)
2010 r = ERROR_MORE_DATA;
2013 if (sidsize)
2015 *sidsize = lstrlenW(usersid);
2016 if (!targetsid)
2017 *sidsize *= sizeof(WCHAR);
2021 done:
2022 RegCloseKey(prod);
2023 msi_free(patches);
2025 return r;
2028 static UINT msi_enum_patches(LPCWSTR szProductCode, LPCWSTR szUserSid,
2029 DWORD dwContext, DWORD dwFilter, DWORD dwIndex, DWORD *idx,
2030 LPWSTR szPatchCode, LPWSTR szTargetProductCode,
2031 MSIINSTALLCONTEXT *pdwTargetProductContext, LPWSTR szTargetUserSid,
2032 LPDWORD pcchTargetUserSid, LPWSTR *szTransforms)
2034 LPWSTR usersid = NULL;
2035 UINT r = ERROR_INVALID_PARAMETER;
2037 if (!szUserSid)
2039 szUserSid = usersid = get_user_sid();
2040 if (!usersid) return ERROR_FUNCTION_FAILED;
2043 if (dwContext & MSIINSTALLCONTEXT_USERMANAGED)
2045 r = msi_check_product_patches(szProductCode, szUserSid,
2046 MSIINSTALLCONTEXT_USERMANAGED, dwFilter,
2047 dwIndex, idx, szPatchCode,
2048 szTargetProductCode,
2049 pdwTargetProductContext, szTargetUserSid,
2050 pcchTargetUserSid, szTransforms);
2051 if (r != ERROR_NO_MORE_ITEMS)
2052 goto done;
2055 if (dwContext & MSIINSTALLCONTEXT_USERUNMANAGED)
2057 r = msi_check_product_patches(szProductCode, szUserSid,
2058 MSIINSTALLCONTEXT_USERUNMANAGED, dwFilter,
2059 dwIndex, idx, szPatchCode,
2060 szTargetProductCode,
2061 pdwTargetProductContext, szTargetUserSid,
2062 pcchTargetUserSid, szTransforms);
2063 if (r != ERROR_NO_MORE_ITEMS)
2064 goto done;
2067 if (dwContext & MSIINSTALLCONTEXT_MACHINE)
2069 r = msi_check_product_patches(szProductCode, szUserSid,
2070 MSIINSTALLCONTEXT_MACHINE, dwFilter,
2071 dwIndex, idx, szPatchCode,
2072 szTargetProductCode,
2073 pdwTargetProductContext, szTargetUserSid,
2074 pcchTargetUserSid, szTransforms);
2075 if (r != ERROR_NO_MORE_ITEMS)
2076 goto done;
2079 done:
2080 LocalFree(usersid);
2081 return r;
2084 /***********************************************************************
2085 * MsiEnumPatchesExW [MSI.@]
2087 UINT WINAPI MsiEnumPatchesExW(LPCWSTR szProductCode, LPCWSTR szUserSid,
2088 DWORD dwContext, DWORD dwFilter, DWORD dwIndex, LPWSTR szPatchCode,
2089 LPWSTR szTargetProductCode, MSIINSTALLCONTEXT *pdwTargetProductContext,
2090 LPWSTR szTargetUserSid, LPDWORD pcchTargetUserSid)
2092 WCHAR squished_pc[GUID_SIZE];
2093 DWORD idx = 0;
2094 UINT r;
2096 static DWORD last_index;
2098 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n",
2099 debugstr_w(szProductCode), debugstr_w(szUserSid), dwContext, dwFilter,
2100 dwIndex, szPatchCode, szTargetProductCode, pdwTargetProductContext,
2101 szTargetUserSid, pcchTargetUserSid);
2103 if (!szProductCode || !squash_guid(szProductCode, squished_pc))
2104 return ERROR_INVALID_PARAMETER;
2106 if (szUserSid && !strcmpW( szUserSid, szLocalSid ))
2107 return ERROR_INVALID_PARAMETER;
2109 if (dwContext & MSIINSTALLCONTEXT_MACHINE && szUserSid)
2110 return ERROR_INVALID_PARAMETER;
2112 if (dwContext <= MSIINSTALLCONTEXT_NONE ||
2113 dwContext > MSIINSTALLCONTEXT_ALL)
2114 return ERROR_INVALID_PARAMETER;
2116 if (dwFilter <= MSIPATCHSTATE_INVALID || dwFilter > MSIPATCHSTATE_ALL)
2117 return ERROR_INVALID_PARAMETER;
2119 if (dwIndex && dwIndex - last_index != 1)
2120 return ERROR_INVALID_PARAMETER;
2122 if (dwIndex == 0)
2123 last_index = 0;
2125 r = msi_enum_patches(szProductCode, szUserSid, dwContext, dwFilter,
2126 dwIndex, &idx, szPatchCode, szTargetProductCode,
2127 pdwTargetProductContext, szTargetUserSid,
2128 pcchTargetUserSid, NULL);
2130 if (r == ERROR_SUCCESS)
2131 last_index = dwIndex;
2132 else
2133 last_index = 0;
2135 return r;
2138 /***********************************************************************
2139 * MsiEnumPatchesA [MSI.@]
2141 UINT WINAPI MsiEnumPatchesA(LPCSTR szProduct, DWORD iPatchIndex,
2142 LPSTR lpPatchBuf, LPSTR lpTransformsBuf, LPDWORD pcchTransformsBuf)
2144 LPWSTR product, transforms;
2145 WCHAR patch[GUID_SIZE];
2146 DWORD len;
2147 UINT r;
2149 TRACE("(%s %d %p %p %p)\n", debugstr_a(szProduct), iPatchIndex,
2150 lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);
2152 if (!szProduct || !lpPatchBuf || !lpTransformsBuf || !pcchTransformsBuf)
2153 return ERROR_INVALID_PARAMETER;
2155 product = strdupAtoW(szProduct);
2156 if (!product)
2157 return ERROR_OUTOFMEMORY;
2159 len = *pcchTransformsBuf;
2160 transforms = msi_alloc( len * sizeof(WCHAR) );
2161 if (!transforms)
2163 r = ERROR_OUTOFMEMORY;
2164 goto done;
2167 r = MsiEnumPatchesW(product, iPatchIndex, patch, transforms, &len);
2168 if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
2169 goto done;
2171 WideCharToMultiByte(CP_ACP, 0, patch, -1, lpPatchBuf,
2172 GUID_SIZE, NULL, NULL);
2174 if (!WideCharToMultiByte(CP_ACP, 0, transforms, -1, lpTransformsBuf,
2175 *pcchTransformsBuf, NULL, NULL))
2176 r = ERROR_MORE_DATA;
2178 if (r == ERROR_MORE_DATA)
2180 lpTransformsBuf[*pcchTransformsBuf - 1] = '\0';
2181 *pcchTransformsBuf = len * 2;
2183 else
2184 *pcchTransformsBuf = strlen( lpTransformsBuf );
2186 done:
2187 msi_free(transforms);
2188 msi_free(product);
2190 return r;
2193 /***********************************************************************
2194 * MsiEnumPatchesW [MSI.@]
2196 UINT WINAPI MsiEnumPatchesW(LPCWSTR szProduct, DWORD iPatchIndex,
2197 LPWSTR lpPatchBuf, LPWSTR lpTransformsBuf, LPDWORD pcchTransformsBuf)
2199 WCHAR squished_pc[GUID_SIZE];
2200 LPWSTR transforms = NULL;
2201 HKEY prod;
2202 DWORD idx = 0;
2203 UINT r;
2205 TRACE("(%s %d %p %p %p)\n", debugstr_w(szProduct), iPatchIndex,
2206 lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);
2208 if (!szProduct || !squash_guid(szProduct, squished_pc))
2209 return ERROR_INVALID_PARAMETER;
2211 if (!lpPatchBuf || !lpTransformsBuf || !pcchTransformsBuf)
2212 return ERROR_INVALID_PARAMETER;
2214 if (MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERMANAGED,
2215 &prod, FALSE) != ERROR_SUCCESS &&
2216 MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERUNMANAGED,
2217 &prod, FALSE) != ERROR_SUCCESS &&
2218 MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_MACHINE,
2219 &prod, FALSE) != ERROR_SUCCESS)
2220 return ERROR_UNKNOWN_PRODUCT;
2222 RegCloseKey(prod);
2224 r = msi_enum_patches(szProduct, NULL, MSIINSTALLCONTEXT_ALL,
2225 MSIPATCHSTATE_ALL, iPatchIndex, &idx, lpPatchBuf,
2226 NULL, NULL, NULL, NULL, &transforms);
2227 if (r != ERROR_SUCCESS)
2228 goto done;
2230 lstrcpynW(lpTransformsBuf, transforms, *pcchTransformsBuf);
2231 if (*pcchTransformsBuf <= lstrlenW(transforms))
2233 r = ERROR_MORE_DATA;
2234 *pcchTransformsBuf = lstrlenW(transforms);
2236 else
2237 *pcchTransformsBuf = lstrlenW(transforms);
2239 done:
2240 msi_free(transforms);
2241 return r;
2244 UINT WINAPI MsiEnumProductsExA( LPCSTR product, LPCSTR usersid, DWORD ctx, DWORD index,
2245 CHAR installed_product[GUID_SIZE],
2246 MSIINSTALLCONTEXT *installed_ctx, LPSTR sid, LPDWORD sid_len )
2248 UINT r;
2249 WCHAR installed_productW[GUID_SIZE], *productW = NULL, *usersidW = NULL, *sidW = NULL;
2251 TRACE("%s, %s, %u, %u, %p, %p, %p, %p\n", debugstr_a(product), debugstr_a(usersid),
2252 ctx, index, installed_product, installed_ctx, sid, sid_len);
2254 if (sid && !sid_len) return ERROR_INVALID_PARAMETER;
2255 if (product && !(productW = strdupAtoW( product ))) return ERROR_OUTOFMEMORY;
2256 if (usersid && !(usersidW = strdupAtoW( usersid )))
2258 msi_free( productW );
2259 return ERROR_OUTOFMEMORY;
2261 if (sid && !(sidW = msi_alloc( *sid_len * sizeof(WCHAR) )))
2263 msi_free( usersidW );
2264 msi_free( productW );
2265 return ERROR_OUTOFMEMORY;
2267 r = MsiEnumProductsExW( productW, usersidW, ctx, index, installed_productW,
2268 installed_ctx, sidW, sid_len );
2269 if (r == ERROR_SUCCESS)
2271 if (installed_product) WideCharToMultiByte( CP_ACP, 0, installed_productW, GUID_SIZE,
2272 installed_product, GUID_SIZE, NULL, NULL );
2273 if (sid) WideCharToMultiByte( CP_ACP, 0, sidW, *sid_len + 1, sid, *sid_len + 1, NULL, NULL );
2275 msi_free( productW );
2276 msi_free( usersidW );
2277 msi_free( sidW );
2278 return r;
2281 static UINT fetch_machine_product( const WCHAR *match, DWORD index, DWORD *idx,
2282 WCHAR installed_product[GUID_SIZE],
2283 MSIINSTALLCONTEXT *installed_ctx, WCHAR *sid, DWORD *sid_len )
2285 static const WCHAR productsW[] =
2286 {'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
2287 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
2288 UINT r;
2289 WCHAR product[GUID_SIZE];
2290 DWORD i = 0, len;
2291 REGSAM access = KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY;
2292 HKEY key;
2294 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, productsW, 0, access, &key ))
2295 return ERROR_NO_MORE_ITEMS;
2297 len = sizeof(product)/sizeof(product[0]);
2298 while (!RegEnumKeyExW( key, i, product, &len, NULL, NULL, NULL, NULL ))
2300 if (match && strcmpW( match, product ))
2302 i++;
2303 len = sizeof(product)/sizeof(product[0]);
2304 continue;
2306 if (*idx == index) goto found;
2307 (*idx)++;
2308 len = sizeof(product)/sizeof(product[0]);
2309 i++;
2311 RegCloseKey( key );
2312 return ERROR_NO_MORE_ITEMS;
2314 found:
2315 if (sid_len && *sid_len < 1)
2317 *sid_len = 1;
2318 r = ERROR_MORE_DATA;
2320 else
2322 if (installed_product) unsquash_guid( product, installed_product );
2323 if (installed_ctx) *installed_ctx = MSIINSTALLCONTEXT_MACHINE;
2324 if (sid)
2326 sid[0] = 0;
2327 *sid_len = 0;
2329 r = ERROR_SUCCESS;
2331 RegCloseKey( key );
2332 return r;
2335 static UINT fetch_user_product( const WCHAR *match, const WCHAR *usersid, DWORD ctx, DWORD index,
2336 DWORD *idx, WCHAR installed_product[GUID_SIZE],
2337 MSIINSTALLCONTEXT *installed_ctx, WCHAR *sid, DWORD *sid_len )
2339 static const WCHAR managedW[] =
2340 {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
2341 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s',
2342 'i','o','n','\\','I','n','s','t','a','l','l','e','r','\\','M','a','n','a','g','e','d',0};
2343 static const WCHAR managed_productsW[] =
2344 {'\\','I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
2345 static const WCHAR unmanaged_productsW[] =
2346 {'\\','S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
2347 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
2348 UINT r;
2349 const WCHAR *subkey;
2350 WCHAR path[MAX_PATH], product[GUID_SIZE], user[128];
2351 DWORD i = 0, j = 0, len_product, len_user;
2352 REGSAM access = KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY;
2353 HKEY key_users, key_products;
2355 if (ctx == MSIINSTALLCONTEXT_USERMANAGED)
2357 subkey = managed_productsW;
2358 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, managedW, 0, access, &key_users ))
2359 return ERROR_NO_MORE_ITEMS;
2361 else if (ctx == MSIINSTALLCONTEXT_USERUNMANAGED)
2363 subkey = unmanaged_productsW;
2364 if (RegOpenKeyExW( HKEY_USERS, NULL, 0, access, &key_users ))
2365 return ERROR_NO_MORE_ITEMS;
2367 else return ERROR_INVALID_PARAMETER;
2369 len_user = sizeof(user)/sizeof(user[0]);
2370 while (!RegEnumKeyExW( key_users, i, user, &len_user, NULL, NULL, NULL, NULL ))
2372 if (strcmpW( usersid, user ) && strcmpW( usersid, szAllSid ))
2374 i++;
2375 len_user = sizeof(user)/sizeof(user[0]);
2376 continue;
2378 strcpyW( path, user );
2379 strcatW( path, subkey );
2380 if (RegOpenKeyExW( key_users, path, 0, access, &key_products ))
2382 i++;
2383 len_user = sizeof(user)/sizeof(user[0]);
2384 continue;
2386 len_product = sizeof(product)/sizeof(product[0]);
2387 while (!RegEnumKeyExW( key_products, j, product, &len_product, NULL, NULL, NULL, NULL ))
2389 if (match && strcmpW( match, product ))
2391 j++;
2392 len_product = sizeof(product)/sizeof(product[0]);
2393 continue;
2395 if (*idx == index) goto found;
2396 (*idx)++;
2397 len_product = sizeof(product)/sizeof(product[0]);
2398 j++;
2400 RegCloseKey( key_products );
2401 len_user = sizeof(user)/sizeof(user[0]);
2402 i++;
2404 RegCloseKey( key_users );
2405 return ERROR_NO_MORE_ITEMS;
2407 found:
2408 if (sid_len && *sid_len <= len_user)
2410 *sid_len = len_user;
2411 r = ERROR_MORE_DATA;
2413 else
2415 if (installed_product) unsquash_guid( product, installed_product );
2416 if (installed_ctx) *installed_ctx = ctx;
2417 if (sid)
2419 strcpyW( sid, user );
2420 *sid_len = len_user;
2422 r = ERROR_SUCCESS;
2424 RegCloseKey( key_products );
2425 RegCloseKey( key_users );
2426 return r;
2429 static UINT enum_products( const WCHAR *product, const WCHAR *usersid, DWORD ctx, DWORD index,
2430 DWORD *idx, WCHAR installed_product[GUID_SIZE],
2431 MSIINSTALLCONTEXT *installed_ctx, WCHAR *sid, DWORD *sid_len )
2433 UINT r = ERROR_NO_MORE_ITEMS;
2434 WCHAR *user = NULL;
2436 if (!usersid)
2438 usersid = user = get_user_sid();
2439 if (!user) return ERROR_FUNCTION_FAILED;
2441 if (ctx & MSIINSTALLCONTEXT_MACHINE)
2443 r = fetch_machine_product( product, index, idx, installed_product, installed_ctx,
2444 sid, sid_len );
2445 if (r != ERROR_NO_MORE_ITEMS) goto done;
2447 if (ctx & MSIINSTALLCONTEXT_USERUNMANAGED)
2449 r = fetch_user_product( product, usersid, MSIINSTALLCONTEXT_USERUNMANAGED, index,
2450 idx, installed_product, installed_ctx, sid, sid_len );
2451 if (r != ERROR_NO_MORE_ITEMS) goto done;
2453 if (ctx & MSIINSTALLCONTEXT_USERMANAGED)
2455 r = fetch_user_product( product, usersid, MSIINSTALLCONTEXT_USERMANAGED, index,
2456 idx, installed_product, installed_ctx, sid, sid_len );
2457 if (r != ERROR_NO_MORE_ITEMS) goto done;
2460 done:
2461 LocalFree( user );
2462 return r;
2465 UINT WINAPI MsiEnumProductsExW( LPCWSTR product, LPCWSTR usersid, DWORD ctx, DWORD index,
2466 WCHAR installed_product[GUID_SIZE],
2467 MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid, LPDWORD sid_len )
2469 UINT r;
2470 DWORD idx = 0;
2471 static DWORD last_index;
2473 TRACE("%s, %s, %u, %u, %p, %p, %p, %p\n", debugstr_w(product), debugstr_w(usersid),
2474 ctx, index, installed_product, installed_ctx, sid, sid_len);
2476 if ((sid && !sid_len) || !ctx || (usersid && ctx == MSIINSTALLCONTEXT_MACHINE))
2477 return ERROR_INVALID_PARAMETER;
2479 if (index && index - last_index != 1)
2480 return ERROR_INVALID_PARAMETER;
2482 if (!index) last_index = 0;
2484 r = enum_products( product, usersid, ctx, index, &idx, installed_product, installed_ctx,
2485 sid, sid_len );
2486 if (r == ERROR_SUCCESS)
2487 last_index = index;
2488 else
2489 last_index = 0;
2491 return r;