wined3d: Use a shader for unconverted blits.
[wine.git] / dlls / msi / registry.c
blob2106ab2fcbc6c8d9a274b92761d2cc28b81cd3d4
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2005 Mike McCormack for CodeWeavers
5 * Copyright 2005 Aric Stewart for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <stdarg.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winnls.h"
30 #include "shlwapi.h"
31 #include "wine/debug.h"
32 #include "msi.h"
33 #include "msipriv.h"
34 #include "wincrypt.h"
35 #include "wine/unicode.h"
36 #include "winver.h"
37 #include "winuser.h"
38 #include "sddl.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msi);
42 /*
43 * This module will be all the helper functions for registry access by the
44 * installer bits.
47 static const WCHAR szUserDataFeatures_fmt[] = {
48 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
49 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
50 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
51 '%','s','\\','P','r','o','d','u','c','t','s','\\','%','s','\\','F','e','a','t','u','r','e','s',0};
53 static const WCHAR szUserDataComp_fmt[] = {
54 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
55 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
56 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
57 '%','s','\\','C','o','m','p','o','n','e','n','t','s','\\','%','s',0};
59 static const WCHAR szUserDataComponents_fmt[] = {
60 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
61 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
62 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
63 '%','s','\\','C','o','m','p','o','n','e','n','t','s',0};
65 static const WCHAR szUserDataProd_fmt[] = {
66 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
67 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
68 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
69 '%','s','\\','P','r','o','d','u','c','t','s','\\','%','s',0};
71 static const WCHAR szUserDataProducts_fmt[] = {
72 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
73 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
74 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
75 '%','s','\\','P','r','o','d','u','c','t','s',0};
77 static const WCHAR szUserDataPatch_fmt[] = {
78 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
79 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
80 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
81 '%','s','\\','P','a','t','c','h','e','s','\\','%','s',0};
83 static const WCHAR szUserDataPatches_fmt[] = {
84 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
85 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
86 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
87 '%','s','\\','P','a','t','c','h','e','s',0};
89 static const WCHAR szUserDataProductPatches_fmt[] = {
90 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
91 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
92 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
93 '%','s','\\','P','r','o','d','u','c','t','s','\\','%','s','\\','P','a','t','c','h','e','s',0};
95 static const WCHAR szInstallProperties_fmt[] = {
96 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
97 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
98 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
99 '%','s','\\','P','r','o','d','u','c','t','s','\\','%','s','\\',
100 'I','n','s','t','a','l','l','P','r','o','p','e','r','t','i','e','s',0};
102 static const WCHAR szInstaller_LocalManagedProd_fmt[] = {
103 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
104 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
105 'I','n','s','t','a','l','l','e','r','\\','M','a','n','a','g','e','d','\\','%','s','\\',
106 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s','\\','%','s',0};
108 static const WCHAR szInstaller_LocalManagedFeat_fmt[] = {
109 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
110 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
111 'I','n','s','t','a','l','l','e','r','\\','M','a','n','a','g','e','d','\\','%','s','\\',
112 'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s','\\','%','s',0};
114 static const WCHAR szInstaller_Products[] = {
115 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
116 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
117 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
119 static const WCHAR szInstaller_Patches[] = {
120 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
121 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
122 'I','n','s','t','a','l','l','e','r','\\','P','a','t','c','h','e','s',0};
124 static const WCHAR szInstaller_LocalClassesProducts[] = {
125 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
126 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
128 static const WCHAR szInstaller_LocalClassesFeatures[] = {
129 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
130 'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s',0};
132 static const WCHAR szInstaller_LocalClassesProd[] = {
133 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
134 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s','\\',0};
136 static const WCHAR szInstaller_LocalClassesFeat[] = {
137 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
138 'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s','\\',0};
140 static const WCHAR szInstaller_ClassesUpgradeCode[] = {
141 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
142 'I','n','s','t','a','l','l','e','r','\\','U','p','g','r','a','d','e','C','o','d','e','s','\\',0};
144 static const WCHAR szInstaller_ClassesUpgradeCodes[] = {
145 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
146 'I','n','s','t','a','l','l','e','r','\\','U','p','g','r','a','d','e','C','o','d','e','s',0};
148 static const WCHAR szInstaller_Features[] = {
149 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
150 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
151 'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s','\\',0};
153 static const WCHAR szInstaller_UpgradeCodes[] = {
154 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
155 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
156 'I','n','s','t','a','l','l','e','r','\\','U','p','g','r','a','d','e','C','o','d','e','s','\\',0};
158 static const WCHAR szInstaller_UserUpgradeCodes[] = {
159 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
160 'I','n','s','t','a','l','l','e','r','\\','U','p','g','r','a','d','e','C','o','d','e','s','\\',0};
162 static const WCHAR szUninstall[] = {
163 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
164 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
165 'U','n','i','n','s','t','a','l','l','\\',0};
167 static const WCHAR szUninstall_32node[] = {
168 'S','o','f','t','w','a','r','e','\\','W','o','w','6','4','3','2','N','o','d','e','\\',
169 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
170 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','U','n','i','n','s','t','a','l','l','\\',0};
172 static const WCHAR szUserComponents[] = {
173 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
174 'I','n','s','t','a','l','l','e','r','\\','C','o','m','p','o','n','e','n','t','s','\\',0};
176 static const WCHAR szUserFeatures[] = {
177 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
178 'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s','\\',0};
180 static const WCHAR szUserProducts[] = {
181 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
182 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s','\\',0};
184 static const WCHAR szUserPatches[] = {
185 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
186 'I','n','s','t','a','l','l','e','r','\\','P','a','t','c','h','e','s','\\',0};
188 BOOL unsquash_guid(LPCWSTR in, LPWSTR out)
190 DWORD i,n=0;
192 if (lstrlenW(in) != 32)
193 return FALSE;
195 out[n++]='{';
196 for(i=0; i<8; i++)
197 out[n++] = in[7-i];
198 out[n++]='-';
199 for(i=0; i<4; i++)
200 out[n++] = in[11-i];
201 out[n++]='-';
202 for(i=0; i<4; i++)
203 out[n++] = in[15-i];
204 out[n++]='-';
205 for(i=0; i<2; i++)
207 out[n++] = in[17+i*2];
208 out[n++] = in[16+i*2];
210 out[n++]='-';
211 for( ; i<8; i++)
213 out[n++] = in[17+i*2];
214 out[n++] = in[16+i*2];
216 out[n++]='}';
217 out[n]=0;
218 return TRUE;
221 BOOL squash_guid(LPCWSTR in, LPWSTR out)
223 DWORD i,n=1;
224 GUID guid;
226 out[0] = 0;
228 if (FAILED(CLSIDFromString((LPCOLESTR)in, &guid)))
229 return FALSE;
231 for(i=0; i<8; i++)
232 out[7-i] = in[n++];
233 n++;
234 for(i=0; i<4; i++)
235 out[11-i] = in[n++];
236 n++;
237 for(i=0; i<4; i++)
238 out[15-i] = in[n++];
239 n++;
240 for(i=0; i<2; i++)
242 out[17+i*2] = in[n++];
243 out[16+i*2] = in[n++];
245 n++;
246 for( ; i<8; i++)
248 out[17+i*2] = in[n++];
249 out[16+i*2] = in[n++];
251 out[32]=0;
252 return TRUE;
256 /* tables for encoding and decoding base85 */
257 static const unsigned char table_dec85[0x80] = {
258 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
259 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
260 0xff,0x00,0xff,0xff,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0xff,
261 0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0xff,0xff,0xff,0x16,0xff,0x17,
262 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
263 0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0xff,0x34,0x35,0x36,
264 0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x42,0x43,0x44,0x45,0x46,
265 0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0xff,0x53,0x54,0xff,
268 static const char table_enc85[] =
269 "!$%&'()*+,-.0123456789=?@ABCDEFGHIJKLMNO"
270 "PQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwx"
271 "yz{}~";
274 * Converts a base85 encoded guid into a GUID pointer
275 * Base85 encoded GUIDs should be 20 characters long.
277 * returns TRUE if successful, FALSE if not
279 BOOL decode_base85_guid( LPCWSTR str, GUID *guid )
281 DWORD i, val = 0, base = 1, *p;
283 if (!str)
284 return FALSE;
286 p = (DWORD*) guid;
287 for( i=0; i<20; i++ )
289 if( (i%5) == 0 )
291 val = 0;
292 base = 1;
294 val += table_dec85[str[i]] * base;
295 if( str[i] >= 0x80 )
296 return FALSE;
297 if( table_dec85[str[i]] == 0xff )
298 return FALSE;
299 if( (i%5) == 4 )
300 p[i/5] = val;
301 base *= 85;
303 return TRUE;
307 * Encodes a base85 guid given a GUID pointer
308 * Caller should provide a 21 character buffer for the encoded string.
310 * returns TRUE if successful, FALSE if not
312 BOOL encode_base85_guid( GUID *guid, LPWSTR str )
314 unsigned int x, *p, i;
316 p = (unsigned int*) guid;
317 for( i=0; i<4; i++ )
319 x = p[i];
320 *str++ = table_enc85[x%85];
321 x = x/85;
322 *str++ = table_enc85[x%85];
323 x = x/85;
324 *str++ = table_enc85[x%85];
325 x = x/85;
326 *str++ = table_enc85[x%85];
327 x = x/85;
328 *str++ = table_enc85[x%85];
330 *str = 0;
332 return TRUE;
335 DWORD msi_version_str_to_dword(LPCWSTR p)
337 DWORD major, minor = 0, build = 0, version = 0;
339 if (!p)
340 return version;
342 major = atoiW(p);
344 p = strchrW(p, '.');
345 if (p)
347 minor = atoiW(p+1);
348 p = strchrW(p+1, '.');
349 if (p)
350 build = atoiW(p+1);
353 return MAKELONG(build, MAKEWORD(minor, major));
356 LONG msi_reg_set_val_str( HKEY hkey, LPCWSTR name, LPCWSTR value )
358 DWORD len;
359 if (!value) value = szEmpty;
360 len = (lstrlenW(value) + 1) * sizeof (WCHAR);
361 return RegSetValueExW( hkey, name, 0, REG_SZ, (const BYTE *)value, len );
364 LONG msi_reg_set_val_multi_str( HKEY hkey, LPCWSTR name, LPCWSTR value )
366 LPCWSTR p = value;
367 while (*p) p += lstrlenW(p) + 1;
368 return RegSetValueExW( hkey, name, 0, REG_MULTI_SZ,
369 (const BYTE *)value, (p + 1 - value) * sizeof(WCHAR) );
372 LONG msi_reg_set_val_dword( HKEY hkey, LPCWSTR name, DWORD val )
374 return RegSetValueExW( hkey, name, 0, REG_DWORD, (LPBYTE)&val, sizeof (DWORD) );
377 LONG msi_reg_set_subkey_val( HKEY hkey, LPCWSTR path, LPCWSTR name, LPCWSTR val )
379 HKEY hsubkey = 0;
380 LONG r;
382 r = RegCreateKeyW( hkey, path, &hsubkey );
383 if (r != ERROR_SUCCESS)
384 return r;
385 r = msi_reg_set_val_str( hsubkey, name, val );
386 RegCloseKey( hsubkey );
387 return r;
390 LPWSTR msi_reg_get_val_str( HKEY hkey, LPCWSTR name )
392 DWORD len = 0;
393 LPWSTR val;
394 LONG r;
396 r = RegQueryValueExW(hkey, name, NULL, NULL, NULL, &len);
397 if (r != ERROR_SUCCESS)
398 return NULL;
400 len += sizeof (WCHAR);
401 val = msi_alloc( len );
402 if (!val)
403 return NULL;
404 val[0] = 0;
405 RegQueryValueExW(hkey, name, NULL, NULL, (LPBYTE) val, &len);
406 return val;
409 BOOL msi_reg_get_val_dword( HKEY hkey, LPCWSTR name, DWORD *val)
411 DWORD type, len = sizeof (DWORD);
412 LONG r = RegQueryValueExW(hkey, name, NULL, &type, (LPBYTE) val, &len);
413 return r == ERROR_SUCCESS && type == REG_DWORD;
416 static WCHAR *get_user_sid(void)
418 HANDLE token;
419 DWORD size = 256;
420 TOKEN_USER *user;
421 WCHAR *ret;
423 if (!OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &token )) return NULL;
424 if (!(user = msi_alloc( size )))
426 CloseHandle( token );
427 return NULL;
429 if (!GetTokenInformation( token, TokenUser, user, size, &size ))
431 msi_free( user );
432 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER || !(user = msi_alloc( size )))
434 CloseHandle( token );
435 return NULL;
437 GetTokenInformation( token, TokenUser, user, size, &size );
439 CloseHandle( token );
440 if (!ConvertSidToStringSidW( user->User.Sid, &ret ))
442 msi_free( user );
443 return NULL;
445 msi_free( user );
446 return ret;
449 UINT MSIREG_OpenUninstallKey(const WCHAR *product, enum platform platform, HKEY *key, BOOL create)
451 WCHAR keypath[0x200];
453 TRACE("%s\n", debugstr_w(product));
455 if (is_64bit && platform == PLATFORM_INTEL)
457 strcpyW(keypath, szUninstall_32node);
458 strcatW(keypath, product);
460 else
462 strcpyW(keypath, szUninstall);
463 strcatW(keypath, product);
465 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, KEY_ALL_ACCESS, NULL, key, NULL);
466 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, KEY_ALL_ACCESS, key);
469 UINT MSIREG_DeleteUninstallKey(const WCHAR *product, enum platform platform)
471 WCHAR keypath[0x200];
473 TRACE("%s\n", debugstr_w(product));
475 if (is_64bit && platform == PLATFORM_INTEL)
477 strcpyW(keypath, szUninstall_32node);
478 strcatW(keypath, product);
480 else
482 strcpyW(keypath, szUninstall);
483 strcatW(keypath, product);
485 return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
488 UINT MSIREG_OpenProductKey(LPCWSTR szProduct, LPCWSTR szUserSid, MSIINSTALLCONTEXT context, HKEY *key, BOOL create)
490 LPWSTR usersid = NULL;
491 HKEY root = HKEY_LOCAL_MACHINE;
492 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
493 WCHAR squished_pc[GUID_SIZE], keypath[MAX_PATH];
495 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
496 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
498 if (context == MSIINSTALLCONTEXT_MACHINE)
500 strcpyW(keypath, szInstaller_LocalClassesProd);
501 strcatW(keypath, squished_pc);
503 else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
505 root = HKEY_CURRENT_USER;
506 strcpyW(keypath, szUserProducts);
507 strcatW(keypath, squished_pc);
509 else
511 if (!szUserSid)
513 if (!(usersid = get_user_sid()))
515 ERR("Failed to retrieve user SID\n");
516 return ERROR_FUNCTION_FAILED;
518 szUserSid = usersid;
520 sprintfW(keypath, szInstaller_LocalManagedProd_fmt, szUserSid, squished_pc);
521 LocalFree(usersid);
523 if (create) return RegCreateKeyExW(root, keypath, 0, NULL, 0, access, NULL, key, NULL);
524 return RegOpenKeyExW(root, keypath, 0, access, key);
527 UINT MSIREG_DeleteUserProductKey(LPCWSTR szProduct)
529 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
531 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
532 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
534 strcpyW(keypath, szUserProducts);
535 strcatW(keypath, squished_pc);
536 return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
539 UINT MSIREG_OpenUserPatchesKey(LPCWSTR szPatch, HKEY *key, BOOL create)
541 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
543 if (!squash_guid(szPatch, squished_pc)) return ERROR_FUNCTION_FAILED;
544 TRACE("%s squished %s\n", debugstr_w(szPatch), debugstr_w(squished_pc));
546 strcpyW(keypath, szUserPatches);
547 strcatW(keypath, squished_pc);
549 if (create) return RegCreateKeyW(HKEY_CURRENT_USER, keypath, key);
550 return RegOpenKeyW(HKEY_CURRENT_USER, keypath, key);
553 UINT MSIREG_OpenFeaturesKey(LPCWSTR szProduct, LPCWSTR szUserSid, MSIINSTALLCONTEXT context,
554 HKEY *key, BOOL create)
556 HKEY root = HKEY_LOCAL_MACHINE;
557 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
558 WCHAR squished_pc[GUID_SIZE], keypath[MAX_PATH], *usersid = NULL;
560 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
561 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
563 if (context == MSIINSTALLCONTEXT_MACHINE)
565 strcpyW(keypath, szInstaller_LocalClassesFeat);
566 strcatW(keypath, squished_pc);
568 else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
570 root = HKEY_CURRENT_USER;
571 strcpyW(keypath, szUserFeatures);
572 strcatW(keypath, squished_pc);
574 else
576 if (!szUserSid)
578 if (!(usersid = get_user_sid()))
580 ERR("Failed to retrieve user SID\n");
581 return ERROR_FUNCTION_FAILED;
583 szUserSid = usersid;
585 sprintfW(keypath, szInstaller_LocalManagedFeat_fmt, szUserSid, squished_pc);
586 LocalFree(usersid);
588 if (create) return RegCreateKeyExW(root, keypath, 0, NULL, 0, access, NULL, key, NULL);
589 return RegOpenKeyExW(root, keypath, 0, access, key);
592 UINT MSIREG_DeleteUserFeaturesKey(LPCWSTR szProduct)
594 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
596 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
597 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
599 strcpyW(keypath, szUserFeatures);
600 strcatW(keypath, squished_pc);
601 return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
604 static UINT MSIREG_OpenInstallerFeaturesKey(LPCWSTR szProduct, HKEY *key, BOOL create)
606 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
607 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
609 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
610 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
612 strcpyW(keypath, szInstaller_Features);
613 strcatW(keypath, squished_pc);
615 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
616 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
619 UINT MSIREG_OpenUserDataFeaturesKey(LPCWSTR szProduct, LPCWSTR szUserSid, MSIINSTALLCONTEXT context,
620 HKEY *key, BOOL create)
622 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
623 WCHAR squished_pc[GUID_SIZE], keypath[0x200], *usersid = NULL;
625 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
626 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
628 if (context == MSIINSTALLCONTEXT_MACHINE)
630 sprintfW(keypath, szUserDataFeatures_fmt, szLocalSid, squished_pc);
632 else
634 if (!szUserSid)
636 if (!(usersid = get_user_sid()))
638 ERR("Failed to retrieve user SID\n");
639 return ERROR_FUNCTION_FAILED;
641 szUserSid = usersid;
643 sprintfW(keypath, szUserDataFeatures_fmt, szUserSid, squished_pc);
644 LocalFree(usersid);
646 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
647 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
650 UINT MSIREG_OpenUserComponentsKey(LPCWSTR szComponent, HKEY *key, BOOL create)
652 WCHAR squished_cc[GUID_SIZE], keypath[0x200];
654 if (!squash_guid(szComponent, squished_cc)) return ERROR_FUNCTION_FAILED;
655 TRACE("%s squished %s\n", debugstr_w(szComponent), debugstr_w(squished_cc));
657 strcpyW(keypath, szUserComponents);
658 strcatW(keypath, squished_cc);
660 if (create) return RegCreateKeyW(HKEY_CURRENT_USER, keypath, key);
661 return RegOpenKeyW(HKEY_CURRENT_USER, keypath, key);
664 UINT MSIREG_OpenUserDataComponentKey(LPCWSTR szComponent, LPCWSTR szUserSid, HKEY *key, BOOL create)
666 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
667 WCHAR comp[GUID_SIZE], keypath[0x200];
668 LPWSTR usersid;
670 if (!squash_guid(szComponent, comp)) return ERROR_FUNCTION_FAILED;
671 TRACE("%s squished %s\n", debugstr_w(szComponent), debugstr_w(comp));
673 if (!szUserSid)
675 if (!(usersid = get_user_sid()))
677 ERR("Failed to retrieve user SID\n");
678 return ERROR_FUNCTION_FAILED;
680 sprintfW(keypath, szUserDataComp_fmt, usersid, comp);
681 LocalFree(usersid);
683 else
684 sprintfW(keypath, szUserDataComp_fmt, szUserSid, comp);
686 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
687 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
690 UINT MSIREG_DeleteUserDataComponentKey(LPCWSTR szComponent, LPCWSTR szUserSid)
692 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
693 WCHAR comp[GUID_SIZE], keypath[0x200];
694 LPWSTR usersid;
695 HKEY hkey;
696 LONG r;
698 if (!squash_guid(szComponent, comp)) return ERROR_FUNCTION_FAILED;
699 TRACE("%s squished %s\n", debugstr_w(szComponent), debugstr_w(comp));
701 if (!szUserSid)
703 if (!(usersid = get_user_sid()))
705 ERR("Failed to retrieve user SID\n");
706 return ERROR_FUNCTION_FAILED;
708 sprintfW(keypath, szUserDataComponents_fmt, usersid);
709 LocalFree(usersid);
711 else
712 sprintfW(keypath, szUserDataComponents_fmt, szUserSid);
714 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, &hkey)) return ERROR_SUCCESS;
715 r = RegDeleteTreeW(hkey, comp);
716 RegCloseKey(hkey);
717 return r;
720 UINT MSIREG_OpenUserDataProductKey(LPCWSTR szProduct, MSIINSTALLCONTEXT dwContext, LPCWSTR szUserSid, HKEY *key, BOOL create)
722 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
723 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
724 LPWSTR usersid;
726 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
727 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
729 if (dwContext == MSIINSTALLCONTEXT_MACHINE)
730 sprintfW(keypath, szUserDataProd_fmt, szLocalSid, squished_pc);
731 else if (szUserSid)
732 sprintfW(keypath, szUserDataProd_fmt, szUserSid, squished_pc);
733 else
735 if (!(usersid = get_user_sid()))
737 ERR("Failed to retrieve user SID\n");
738 return ERROR_FUNCTION_FAILED;
740 sprintfW(keypath, szUserDataProd_fmt, usersid, squished_pc);
741 LocalFree(usersid);
743 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
744 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
747 UINT MSIREG_OpenUserDataPatchKey(LPCWSTR szPatch, MSIINSTALLCONTEXT dwContext, HKEY *key, BOOL create)
749 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
750 WCHAR squished_patch[GUID_SIZE], keypath[0x200];
751 LPWSTR usersid;
753 if (!squash_guid(szPatch, squished_patch)) return ERROR_FUNCTION_FAILED;
754 TRACE("%s squished %s\n", debugstr_w(szPatch), debugstr_w(squished_patch));
756 if (dwContext == MSIINSTALLCONTEXT_MACHINE)
757 sprintfW(keypath, szUserDataPatch_fmt, szLocalSid, squished_patch);
758 else
760 if (!(usersid = get_user_sid()))
762 ERR("Failed to retrieve user SID\n");
763 return ERROR_FUNCTION_FAILED;
765 sprintfW(keypath, szUserDataPatch_fmt, usersid, squished_patch);
766 LocalFree(usersid);
768 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
769 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
772 UINT MSIREG_DeleteUserDataPatchKey(LPCWSTR patch, MSIINSTALLCONTEXT context)
774 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
775 WCHAR squished_patch[GUID_SIZE], keypath[0x200];
776 LPWSTR usersid;
777 HKEY hkey;
778 LONG r;
780 if (!squash_guid(patch, squished_patch)) return ERROR_FUNCTION_FAILED;
781 TRACE("%s squished %s\n", debugstr_w(patch), debugstr_w(squished_patch));
783 if (context == MSIINSTALLCONTEXT_MACHINE)
784 sprintfW(keypath, szUserDataPatches_fmt, szLocalSid);
785 else
787 if (!(usersid = get_user_sid()))
789 ERR("Failed to retrieve user SID\n");
790 return ERROR_FUNCTION_FAILED;
792 sprintfW(keypath, szUserDataPatches_fmt, usersid);
793 LocalFree(usersid);
795 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, &hkey)) return ERROR_SUCCESS;
796 r = RegDeleteTreeW(hkey, squished_patch);
797 RegCloseKey(hkey);
798 return r;
801 UINT MSIREG_OpenUserDataProductPatchesKey(LPCWSTR product, MSIINSTALLCONTEXT context, HKEY *key, BOOL create)
803 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
804 WCHAR squished_product[GUID_SIZE], keypath[0x200];
805 LPWSTR usersid;
807 if (!squash_guid(product, squished_product)) return ERROR_FUNCTION_FAILED;
808 TRACE("%s squished %s\n", debugstr_w(product), debugstr_w(squished_product));
810 if (context == MSIINSTALLCONTEXT_MACHINE)
811 sprintfW(keypath, szUserDataProductPatches_fmt, szLocalSid, squished_product);
812 else
814 if (!(usersid = get_user_sid()))
816 ERR("Failed to retrieve user SID\n");
817 return ERROR_FUNCTION_FAILED;
819 sprintfW(keypath, szUserDataProductPatches_fmt, usersid, squished_product);
820 LocalFree(usersid);
822 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
823 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
826 UINT MSIREG_OpenInstallProps(LPCWSTR szProduct, MSIINSTALLCONTEXT dwContext, LPCWSTR szUserSid, HKEY *key, BOOL create)
828 LPWSTR usersid;
829 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
830 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
832 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
833 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
835 if (dwContext == MSIINSTALLCONTEXT_MACHINE)
836 sprintfW(keypath, szInstallProperties_fmt, szLocalSid, squished_pc);
837 else if (szUserSid)
838 sprintfW(keypath, szInstallProperties_fmt, szUserSid, squished_pc);
839 else
841 if (!(usersid = get_user_sid()))
843 ERR("Failed to retrieve user SID\n");
844 return ERROR_FUNCTION_FAILED;
846 sprintfW(keypath, szInstallProperties_fmt, usersid, squished_pc);
847 LocalFree(usersid);
849 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
850 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
853 UINT MSIREG_DeleteUserDataProductKey(LPCWSTR szProduct)
855 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
856 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
857 LPWSTR usersid;
858 HKEY hkey;
859 LONG r;
861 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
862 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
864 if (!(usersid = get_user_sid()))
866 ERR("Failed to retrieve user SID\n");
867 return ERROR_FUNCTION_FAILED;
869 sprintfW(keypath, szUserDataProducts_fmt, usersid);
870 LocalFree(usersid);
872 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, &hkey)) return ERROR_SUCCESS;
873 r = RegDeleteTreeW(hkey, squished_pc);
874 RegCloseKey(hkey);
875 return r;
878 UINT MSIREG_DeleteProductKey(LPCWSTR szProduct)
880 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
881 WCHAR squished_pc[GUID_SIZE];
882 HKEY hkey;
883 LONG r;
885 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
886 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
888 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_Products, 0, access, &hkey)) return ERROR_SUCCESS;
889 r = RegDeleteTreeW(hkey, squished_pc);
890 RegCloseKey(hkey);
891 return r;
894 UINT MSIREG_OpenPatchesKey(LPCWSTR szPatch, HKEY *key, BOOL create)
896 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
897 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
899 if (!squash_guid(szPatch, squished_pc)) return ERROR_FUNCTION_FAILED;
900 TRACE("%s squished %s\n", debugstr_w(szPatch), debugstr_w(squished_pc));
902 sprintfW(keypath, szInstaller_Patches, squished_pc);
904 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
905 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
908 UINT MSIREG_OpenUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY *key, BOOL create)
910 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
911 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
913 if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
914 TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
916 strcpyW(keypath, szInstaller_UpgradeCodes);
917 strcatW(keypath, squished_pc);
919 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
920 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
923 UINT MSIREG_OpenUserUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY* key, BOOL create)
925 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
927 if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
928 TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
930 strcpyW(keypath, szInstaller_UserUpgradeCodes);
931 strcatW(keypath, squished_pc);
933 if (create) return RegCreateKeyW(HKEY_CURRENT_USER, keypath, key);
934 return RegOpenKeyW(HKEY_CURRENT_USER, keypath, key);
937 UINT MSIREG_DeleteUserUpgradeCodesKey(LPCWSTR szUpgradeCode)
939 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
941 if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
942 TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
944 strcpyW(keypath, szInstaller_UserUpgradeCodes);
945 strcatW(keypath, squished_pc);
946 return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
949 UINT MSIREG_DeleteLocalClassesProductKey(LPCWSTR szProductCode)
951 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
952 WCHAR squished_pc[GUID_SIZE];
953 HKEY hkey;
954 LONG r;
956 if (!squash_guid(szProductCode, squished_pc)) return ERROR_FUNCTION_FAILED;
957 TRACE("%s squished %s\n", debugstr_w(szProductCode), debugstr_w(squished_pc));
959 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_LocalClassesProducts, 0, access, &hkey)) return ERROR_SUCCESS;
960 r = RegDeleteTreeW(hkey, squished_pc);
961 RegCloseKey(hkey);
962 return r;
965 UINT MSIREG_DeleteLocalClassesFeaturesKey(LPCWSTR szProductCode)
967 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
968 WCHAR squished_pc[GUID_SIZE];
969 HKEY hkey;
970 LONG r;
972 if (!squash_guid(szProductCode, squished_pc)) return ERROR_FUNCTION_FAILED;
973 TRACE("%s squished %s\n", debugstr_w(szProductCode), debugstr_w(squished_pc));
975 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_LocalClassesFeatures, 0, access, &hkey)) return ERROR_SUCCESS;
976 r = RegDeleteTreeW(hkey, squished_pc);
977 RegCloseKey(hkey);
978 return r;
981 UINT MSIREG_OpenClassesUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY *key, BOOL create)
983 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
984 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
986 if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
987 TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
989 strcpyW(keypath, szInstaller_ClassesUpgradeCode);
990 strcatW(keypath, squished_pc);
992 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
993 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
996 UINT MSIREG_DeleteClassesUpgradeCodesKey(LPCWSTR szUpgradeCode)
998 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
999 WCHAR squished_pc[GUID_SIZE];
1000 HKEY hkey;
1001 LONG r;
1003 if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
1004 TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
1006 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_ClassesUpgradeCodes, 0, access, &hkey)) return ERROR_SUCCESS;
1007 r = RegDeleteTreeW(hkey, squished_pc);
1008 RegCloseKey(hkey);
1009 return r;
1012 /*************************************************************************
1013 * MsiDecomposeDescriptorW [MSI.@]
1015 * Decomposes an MSI descriptor into product, feature and component parts.
1016 * An MSI descriptor is a string of the form:
1017 * [base 85 guid] [feature code] '>' [base 85 guid]
1019 * PARAMS
1020 * szDescriptor [I] the descriptor to decompose
1021 * szProduct [O] buffer of MAX_FEATURE_CHARS+1 for the product guid
1022 * szFeature [O] buffer of MAX_FEATURE_CHARS+1 for the feature code
1023 * szComponent [O] buffer of MAX_FEATURE_CHARS+1 for the component guid
1024 * pUsed [O] the length of the descriptor
1026 * RETURNS
1027 * ERROR_SUCCESS if everything worked correctly
1028 * ERROR_INVALID_PARAMETER if the descriptor was invalid
1031 UINT WINAPI MsiDecomposeDescriptorW( LPCWSTR szDescriptor, LPWSTR szProduct,
1032 LPWSTR szFeature, LPWSTR szComponent, LPDWORD pUsed )
1034 UINT r, len;
1035 LPWSTR p;
1036 GUID product, component;
1038 TRACE("%s %p %p %p %p\n", debugstr_w(szDescriptor), szProduct,
1039 szFeature, szComponent, pUsed);
1041 r = decode_base85_guid( szDescriptor, &product );
1042 if( !r )
1043 return ERROR_INVALID_PARAMETER;
1045 TRACE("product %s\n", debugstr_guid( &product ));
1047 p = strchrW(&szDescriptor[20],'>');
1048 if( !p )
1049 return ERROR_INVALID_PARAMETER;
1051 len = (p - &szDescriptor[20]);
1052 if( len > MAX_FEATURE_CHARS )
1053 return ERROR_INVALID_PARAMETER;
1055 TRACE("feature %s\n", debugstr_wn( &szDescriptor[20], len ));
1057 r = decode_base85_guid( p+1, &component );
1058 if( !r )
1059 return ERROR_INVALID_PARAMETER;
1061 TRACE("component %s\n", debugstr_guid( &component ));
1063 if (szProduct)
1064 StringFromGUID2( &product, szProduct, MAX_FEATURE_CHARS+1 );
1065 if (szComponent)
1066 StringFromGUID2( &component, szComponent, MAX_FEATURE_CHARS+1 );
1067 if (szFeature)
1069 memcpy( szFeature, &szDescriptor[20], len*sizeof(WCHAR) );
1070 szFeature[len] = 0;
1072 len = ( &p[21] - szDescriptor );
1074 TRACE("length = %d\n", len);
1075 if (pUsed) *pUsed = len;
1077 return ERROR_SUCCESS;
1080 UINT WINAPI MsiDecomposeDescriptorA( LPCSTR szDescriptor, LPSTR szProduct,
1081 LPSTR szFeature, LPSTR szComponent, LPDWORD pUsed )
1083 WCHAR product[MAX_FEATURE_CHARS+1];
1084 WCHAR feature[MAX_FEATURE_CHARS+1];
1085 WCHAR component[MAX_FEATURE_CHARS+1];
1086 LPWSTR str = NULL, p = NULL, f = NULL, c = NULL;
1087 UINT r;
1089 TRACE("%s %p %p %p %p\n", debugstr_a(szDescriptor), szProduct,
1090 szFeature, szComponent, pUsed);
1092 str = strdupAtoW( szDescriptor );
1093 if( szDescriptor && !str )
1094 return ERROR_OUTOFMEMORY;
1096 if (szProduct)
1097 p = product;
1098 if (szFeature)
1099 f = feature;
1100 if (szComponent)
1101 c = component;
1103 r = MsiDecomposeDescriptorW( str, p, f, c, pUsed );
1105 if (r == ERROR_SUCCESS)
1107 WideCharToMultiByte( CP_ACP, 0, p, -1,
1108 szProduct, MAX_FEATURE_CHARS+1, NULL, NULL );
1109 WideCharToMultiByte( CP_ACP, 0, f, -1,
1110 szFeature, MAX_FEATURE_CHARS+1, NULL, NULL );
1111 WideCharToMultiByte( CP_ACP, 0, c, -1,
1112 szComponent, MAX_FEATURE_CHARS+1, NULL, NULL );
1115 msi_free( str );
1117 return r;
1120 UINT WINAPI MsiEnumProductsA(DWORD index, LPSTR lpguid)
1122 DWORD r;
1123 WCHAR szwGuid[GUID_SIZE];
1125 TRACE("%d %p\n", index, lpguid);
1127 if (NULL == lpguid)
1128 return ERROR_INVALID_PARAMETER;
1129 r = MsiEnumProductsW(index, szwGuid);
1130 if( r == ERROR_SUCCESS )
1131 WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
1133 return r;
1136 UINT WINAPI MsiEnumProductsW(DWORD index, LPWSTR lpguid)
1138 TRACE("%d %p\n", index, lpguid);
1140 if (NULL == lpguid)
1141 return ERROR_INVALID_PARAMETER;
1143 return MsiEnumProductsExW( NULL, szAllSid, MSIINSTALLCONTEXT_ALL, index, lpguid,
1144 NULL, NULL, NULL );
1147 UINT WINAPI MsiEnumFeaturesA(LPCSTR szProduct, DWORD index,
1148 LPSTR szFeature, LPSTR szParent)
1150 DWORD r;
1151 WCHAR szwFeature[GUID_SIZE], szwParent[GUID_SIZE];
1152 LPWSTR szwProduct = NULL;
1154 TRACE("%s %d %p %p\n", debugstr_a(szProduct), index, szFeature, szParent);
1156 if( szProduct )
1158 szwProduct = strdupAtoW( szProduct );
1159 if( !szwProduct )
1160 return ERROR_OUTOFMEMORY;
1163 r = MsiEnumFeaturesW(szwProduct, index, szwFeature, szwParent);
1164 if( r == ERROR_SUCCESS )
1166 WideCharToMultiByte(CP_ACP, 0, szwFeature, -1,
1167 szFeature, GUID_SIZE, NULL, NULL);
1168 WideCharToMultiByte(CP_ACP, 0, szwParent, -1,
1169 szParent, GUID_SIZE, NULL, NULL);
1172 msi_free( szwProduct);
1174 return r;
1177 UINT WINAPI MsiEnumFeaturesW(LPCWSTR szProduct, DWORD index,
1178 LPWSTR szFeature, LPWSTR szParent)
1180 HKEY hkeyProduct = 0;
1181 DWORD r, sz;
1183 TRACE("%s %d %p %p\n", debugstr_w(szProduct), index, szFeature, szParent);
1185 if( !szProduct )
1186 return ERROR_INVALID_PARAMETER;
1188 r = MSIREG_OpenInstallerFeaturesKey(szProduct,&hkeyProduct,FALSE);
1189 if( r != ERROR_SUCCESS )
1190 return ERROR_NO_MORE_ITEMS;
1192 sz = GUID_SIZE;
1193 r = RegEnumValueW(hkeyProduct, index, szFeature, &sz, NULL, NULL, NULL, NULL);
1194 RegCloseKey(hkeyProduct);
1196 return r;
1199 UINT WINAPI MsiEnumComponentsA(DWORD index, LPSTR lpguid)
1201 DWORD r;
1202 WCHAR szwGuid[GUID_SIZE];
1204 TRACE("%u, %p\n", index, lpguid);
1206 if (!lpguid) return ERROR_INVALID_PARAMETER;
1208 r = MsiEnumComponentsW(index, szwGuid);
1209 if( r == ERROR_SUCCESS )
1210 WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
1212 return r;
1215 UINT WINAPI MsiEnumComponentsW(DWORD index, LPWSTR lpguid)
1217 TRACE("%u, %p\n", index, lpguid);
1219 if (!lpguid) return ERROR_INVALID_PARAMETER;
1221 return MsiEnumComponentsExW( szAllSid, MSIINSTALLCONTEXT_ALL, index, lpguid, NULL, NULL, NULL );
1224 UINT WINAPI MsiEnumComponentsExA( LPCSTR user_sid, DWORD ctx, DWORD index, CHAR guid[39],
1225 MSIINSTALLCONTEXT *installed_ctx, LPSTR sid, LPDWORD sid_len )
1227 UINT r;
1228 WCHAR *user_sidW = NULL, *sidW = NULL, guidW[GUID_SIZE];
1230 TRACE("%s, %u, %u, %p, %p, %p, %p\n", debugstr_a(user_sid), ctx, index, guid, installed_ctx,
1231 sid, sid_len);
1233 if (sid && !sid_len) return ERROR_INVALID_PARAMETER;
1234 if (user_sid && !(user_sidW = strdupAtoW( user_sid ))) return ERROR_OUTOFMEMORY;
1235 if (sid && !(sidW = msi_alloc( *sid_len * sizeof(WCHAR) )))
1237 msi_free( user_sidW );
1238 return ERROR_OUTOFMEMORY;
1240 r = MsiEnumComponentsExW( user_sidW, ctx, index, guidW, installed_ctx, sidW, sid_len );
1241 if (r == ERROR_SUCCESS)
1243 if (guid) WideCharToMultiByte( CP_ACP, 0, guidW, GUID_SIZE, guid, GUID_SIZE, NULL, NULL );
1244 if (sid) WideCharToMultiByte( CP_ACP, 0, sidW, *sid_len + 1, sid, *sid_len + 1, NULL, NULL );
1246 msi_free( user_sidW );
1247 msi_free( sidW );
1248 return r;
1251 static UINT fetch_machine_component( DWORD ctx, DWORD index, DWORD *idx, WCHAR guid[39],
1252 MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid, LPDWORD sid_len )
1254 static const WCHAR componentsW[] =
1255 {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1256 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1257 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a','\\',
1258 'S','-','1','-','5','-','1','8','\\','C','o','m','p','o','n','e','n','t','s',0};
1259 UINT r = ERROR_SUCCESS;
1260 WCHAR component[GUID_SIZE];
1261 DWORD i = 0, len_component;
1262 REGSAM access = KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY;
1263 HKEY key_components;
1265 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, componentsW, 0, access, &key_components ))
1266 return ERROR_NO_MORE_ITEMS;
1268 len_component = sizeof(component)/sizeof(component[0]);
1269 while (!RegEnumKeyExW( key_components, i, component, &len_component, NULL, NULL, NULL, NULL ))
1271 if (*idx == index) goto found;
1272 (*idx)++;
1273 len_component = sizeof(component)/sizeof(component[0]);
1274 i++;
1276 RegCloseKey( key_components );
1277 return ERROR_NO_MORE_ITEMS;
1279 found:
1280 if (sid_len)
1282 if (*sid_len < 1)
1284 *sid_len = 1;
1285 r = ERROR_MORE_DATA;
1287 else if (sid)
1289 *sid_len = 0;
1290 sid[0] = 0;
1293 if (guid) unsquash_guid( component, guid );
1294 if (installed_ctx) *installed_ctx = MSIINSTALLCONTEXT_MACHINE;
1295 RegCloseKey( key_components );
1296 return r;
1299 static UINT fetch_user_component( const WCHAR *usersid, DWORD ctx, DWORD index, DWORD *idx,
1300 WCHAR guid[39], MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid,
1301 LPDWORD sid_len )
1303 static const WCHAR userdataW[] =
1304 {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1305 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1306 'I','n','s','t','a','l','l','e','r','\\','U','s','e','r','D','a','t','a',0};
1307 static const WCHAR componentsW[] = {'\\','C','o','m','p','o','n','e','n','t','s',0};
1308 UINT r = ERROR_SUCCESS;
1309 WCHAR path[MAX_PATH], component[GUID_SIZE], user[128];
1310 DWORD i = 0, j = 0, len_component, len_user;
1311 REGSAM access = KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY;
1312 HKEY key_users, key_components;
1314 if (ctx == MSIINSTALLCONTEXT_USERMANAGED) /* FIXME: were to find these? */
1315 return ERROR_NO_MORE_ITEMS;
1317 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, userdataW, 0, access, &key_users ))
1318 return ERROR_NO_MORE_ITEMS;
1320 len_user = sizeof(user)/sizeof(user[0]);
1321 while (!RegEnumKeyExW( key_users, i, user, &len_user, NULL, NULL, NULL, NULL ))
1323 if ((strcmpW( usersid, szAllSid ) && strcmpW( usersid, user )) ||
1324 !strcmpW( szLocalSid, user ))
1326 i++;
1327 len_user = sizeof(user)/sizeof(user[0]);
1328 continue;
1330 strcpyW( path, user );
1331 strcatW( path, componentsW );
1332 if (RegOpenKeyExW( key_users, path, 0, access, &key_components ))
1334 i++;
1335 len_user = sizeof(user)/sizeof(user[0]);
1336 continue;
1338 len_component = sizeof(component)/sizeof(component[0]);
1339 while (!RegEnumKeyExW( key_components, j, component, &len_component, NULL, NULL, NULL, NULL ))
1341 if (*idx == index) goto found;
1342 (*idx)++;
1343 len_component = sizeof(component)/sizeof(component[0]);
1344 j++;
1346 RegCloseKey( key_components );
1347 len_user = sizeof(user)/sizeof(user[0]);
1348 i++;
1350 RegCloseKey( key_users );
1351 return ERROR_NO_MORE_ITEMS;
1353 found:
1354 if (sid_len)
1356 if (*sid_len < len_user + 1)
1358 *sid_len = len_user + 1;
1359 r = ERROR_MORE_DATA;
1361 else if (sid)
1363 *sid_len = len_user;
1364 strcpyW( sid, user );
1367 if (guid) unsquash_guid( component, guid );
1368 if (installed_ctx) *installed_ctx = ctx;
1369 RegCloseKey( key_components );
1370 RegCloseKey( key_users );
1371 return r;
1374 static UINT enum_components( const WCHAR *usersid, DWORD ctx, DWORD index, DWORD *idx, WCHAR guid[39],
1375 MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid, LPDWORD sid_len )
1377 UINT r = ERROR_NO_MORE_ITEMS;
1378 WCHAR *user = NULL;
1380 if (!usersid)
1382 usersid = user = get_user_sid();
1383 if (!user) return ERROR_FUNCTION_FAILED;
1385 if (ctx & MSIINSTALLCONTEXT_USERMANAGED)
1387 r = fetch_user_component( usersid, MSIINSTALLCONTEXT_USERMANAGED, index, idx, guid,
1388 installed_ctx, sid, sid_len );
1389 if (r != ERROR_NO_MORE_ITEMS) goto done;
1391 if (ctx & MSIINSTALLCONTEXT_USERUNMANAGED)
1393 r = fetch_user_component( usersid, MSIINSTALLCONTEXT_USERUNMANAGED, index, idx, guid,
1394 installed_ctx, sid, sid_len );
1395 if (r != ERROR_NO_MORE_ITEMS) goto done;
1397 if (ctx & MSIINSTALLCONTEXT_MACHINE)
1399 r = fetch_machine_component( MSIINSTALLCONTEXT_MACHINE, index, idx, guid, installed_ctx,
1400 sid, sid_len );
1401 if (r != ERROR_NO_MORE_ITEMS) goto done;
1404 done:
1405 LocalFree( user );
1406 return r;
1409 UINT WINAPI MsiEnumComponentsExW( LPCWSTR user_sid, DWORD ctx, DWORD index, WCHAR guid[39],
1410 MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid, LPDWORD sid_len )
1412 UINT r;
1413 DWORD idx = 0;
1414 static DWORD last_index;
1416 TRACE("%s, %u, %u, %p, %p, %p, %p\n", debugstr_w(user_sid), ctx, index, guid, installed_ctx,
1417 sid, sid_len);
1419 if ((sid && !sid_len) || !ctx || (user_sid && ctx == MSIINSTALLCONTEXT_MACHINE))
1420 return ERROR_INVALID_PARAMETER;
1422 if (index && index - last_index != 1)
1423 return ERROR_INVALID_PARAMETER;
1425 if (!index) last_index = 0;
1427 r = enum_components( user_sid, ctx, index, &idx, guid, installed_ctx, sid, sid_len );
1428 if (r == ERROR_SUCCESS)
1429 last_index = index;
1430 else
1431 last_index = 0;
1433 return r;
1436 UINT WINAPI MsiEnumClientsA(LPCSTR szComponent, DWORD index, LPSTR szProduct)
1438 DWORD r;
1439 WCHAR szwProduct[GUID_SIZE];
1440 LPWSTR szwComponent = NULL;
1442 TRACE("%s %d %p\n", debugstr_a(szComponent), index, szProduct);
1444 if ( !szProduct )
1445 return ERROR_INVALID_PARAMETER;
1447 if( szComponent )
1449 szwComponent = strdupAtoW( szComponent );
1450 if( !szwComponent )
1451 return ERROR_OUTOFMEMORY;
1454 r = MsiEnumClientsW(szComponent?szwComponent:NULL, index, szwProduct);
1455 if( r == ERROR_SUCCESS )
1457 WideCharToMultiByte(CP_ACP, 0, szwProduct, -1,
1458 szProduct, GUID_SIZE, NULL, NULL);
1461 msi_free( szwComponent);
1463 return r;
1466 UINT WINAPI MsiEnumClientsW(LPCWSTR szComponent, DWORD index, LPWSTR szProduct)
1468 HKEY hkeyComp = 0;
1469 DWORD r, sz;
1470 WCHAR szValName[SQUISH_GUID_SIZE];
1472 TRACE("%s %d %p\n", debugstr_w(szComponent), index, szProduct);
1474 if (!szComponent || !*szComponent || !szProduct)
1475 return ERROR_INVALID_PARAMETER;
1477 if (MSIREG_OpenUserDataComponentKey(szComponent, NULL, &hkeyComp, FALSE) != ERROR_SUCCESS &&
1478 MSIREG_OpenUserDataComponentKey(szComponent, szLocalSid, &hkeyComp, FALSE) != ERROR_SUCCESS)
1479 return ERROR_UNKNOWN_COMPONENT;
1481 /* see if there are any products at all */
1482 sz = SQUISH_GUID_SIZE;
1483 r = RegEnumValueW(hkeyComp, 0, szValName, &sz, NULL, NULL, NULL, NULL);
1484 if (r != ERROR_SUCCESS)
1486 RegCloseKey(hkeyComp);
1488 if (index != 0)
1489 return ERROR_INVALID_PARAMETER;
1491 return ERROR_UNKNOWN_COMPONENT;
1494 sz = SQUISH_GUID_SIZE;
1495 r = RegEnumValueW(hkeyComp, index, szValName, &sz, NULL, NULL, NULL, NULL);
1496 if( r == ERROR_SUCCESS )
1498 unsquash_guid(szValName, szProduct);
1499 TRACE("-> %s\n", debugstr_w(szProduct));
1501 RegCloseKey(hkeyComp);
1502 return r;
1505 UINT WINAPI MsiEnumClientsExA(LPCSTR component, LPCSTR usersid, DWORD ctx, DWORD index,
1506 CHAR installed_product[GUID_SIZE],
1507 MSIINSTALLCONTEXT *installed_ctx, LPSTR sid, LPDWORD sid_len)
1509 FIXME("%s, %s, %u, %u, %p, %p, %p, %p\n", debugstr_a(component), debugstr_a(usersid),
1510 ctx, index, installed_product, installed_ctx, sid, sid_len);
1511 return ERROR_ACCESS_DENIED;
1514 UINT WINAPI MsiEnumClientsExW(LPCWSTR component, LPCWSTR usersid, DWORD ctx, DWORD index,
1515 WCHAR installed_product[GUID_SIZE],
1516 MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid, LPDWORD sid_len)
1518 FIXME("%s, %s, %u, %u, %p, %p, %p, %p\n", debugstr_w(component), debugstr_w(usersid),
1519 ctx, index, installed_product, installed_ctx, sid, sid_len);
1520 return ERROR_ACCESS_DENIED;
1523 static UINT MSI_EnumComponentQualifiers( LPCWSTR szComponent, DWORD iIndex,
1524 awstring *lpQualBuf, LPDWORD pcchQual,
1525 awstring *lpAppBuf, LPDWORD pcchAppBuf )
1527 DWORD name_sz, val_sz, name_max, val_max, type, ofs;
1528 LPWSTR name = NULL, val = NULL;
1529 UINT r, r2;
1530 HKEY key;
1532 TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
1533 lpQualBuf, pcchQual, lpAppBuf, pcchAppBuf);
1535 if (!szComponent)
1536 return ERROR_INVALID_PARAMETER;
1538 r = MSIREG_OpenUserComponentsKey( szComponent, &key, FALSE );
1539 if (r != ERROR_SUCCESS)
1540 return ERROR_UNKNOWN_COMPONENT;
1542 /* figure out how big the name is we want to return */
1543 name_max = 0x10;
1544 r = ERROR_OUTOFMEMORY;
1545 name = msi_alloc( name_max * sizeof(WCHAR) );
1546 if (!name)
1547 goto end;
1549 val_max = 0x10;
1550 r = ERROR_OUTOFMEMORY;
1551 val = msi_alloc( val_max );
1552 if (!val)
1553 goto end;
1555 /* loop until we allocate enough memory */
1556 while (1)
1558 name_sz = name_max;
1559 val_sz = val_max;
1560 r = RegEnumValueW( key, iIndex, name, &name_sz,
1561 NULL, &type, (LPBYTE)val, &val_sz );
1562 if (r == ERROR_SUCCESS)
1563 break;
1564 if (r != ERROR_MORE_DATA)
1565 goto end;
1567 if (type != REG_MULTI_SZ)
1569 ERR("component data has wrong type (%d)\n", type);
1570 goto end;
1573 r = ERROR_OUTOFMEMORY;
1574 if (name_sz + 1 >= name_max)
1576 name_max *= 2;
1577 msi_free( name );
1578 name = msi_alloc( name_max * sizeof (WCHAR) );
1579 if (!name)
1580 goto end;
1581 continue;
1583 if (val_sz > val_max)
1585 val_max = val_sz + sizeof (WCHAR);
1586 msi_free( val );
1587 val = msi_alloc( val_max * sizeof (WCHAR) );
1588 if (!val)
1589 goto end;
1590 continue;
1592 ERR("should be enough data, but isn't %d %d\n", name_sz, val_sz );
1593 goto end;
1596 ofs = 0;
1597 r = MsiDecomposeDescriptorW( val, NULL, NULL, NULL, &ofs );
1598 if (r != ERROR_SUCCESS)
1599 goto end;
1601 TRACE("Providing %s and %s\n", debugstr_w(name), debugstr_w(val+ofs));
1603 r = msi_strcpy_to_awstring( name, -1, lpQualBuf, pcchQual );
1604 r2 = msi_strcpy_to_awstring( val+ofs, -1, lpAppBuf, pcchAppBuf );
1606 if (r2 != ERROR_SUCCESS)
1607 r = r2;
1609 end:
1610 msi_free(val);
1611 msi_free(name);
1612 RegCloseKey(key);
1613 return r;
1616 /*************************************************************************
1617 * MsiEnumComponentQualifiersA [MSI.@]
1619 UINT WINAPI MsiEnumComponentQualifiersA( LPCSTR szComponent, DWORD iIndex,
1620 LPSTR lpQualifierBuf, LPDWORD pcchQualifierBuf,
1621 LPSTR lpApplicationDataBuf, LPDWORD pcchApplicationDataBuf )
1623 awstring qual, appdata;
1624 LPWSTR comp;
1625 UINT r;
1627 TRACE("%s %08x %p %p %p %p\n", debugstr_a(szComponent), iIndex,
1628 lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
1629 pcchApplicationDataBuf);
1631 comp = strdupAtoW( szComponent );
1632 if (szComponent && !comp)
1633 return ERROR_OUTOFMEMORY;
1635 qual.unicode = FALSE;
1636 qual.str.a = lpQualifierBuf;
1638 appdata.unicode = FALSE;
1639 appdata.str.a = lpApplicationDataBuf;
1641 r = MSI_EnumComponentQualifiers( comp, iIndex,
1642 &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
1643 msi_free( comp );
1644 return r;
1647 /*************************************************************************
1648 * MsiEnumComponentQualifiersW [MSI.@]
1650 UINT WINAPI MsiEnumComponentQualifiersW( LPCWSTR szComponent, DWORD iIndex,
1651 LPWSTR lpQualifierBuf, LPDWORD pcchQualifierBuf,
1652 LPWSTR lpApplicationDataBuf, LPDWORD pcchApplicationDataBuf )
1654 awstring qual, appdata;
1656 TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
1657 lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
1658 pcchApplicationDataBuf);
1660 qual.unicode = TRUE;
1661 qual.str.w = lpQualifierBuf;
1663 appdata.unicode = TRUE;
1664 appdata.str.w = lpApplicationDataBuf;
1666 return MSI_EnumComponentQualifiers( szComponent, iIndex,
1667 &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
1670 /*************************************************************************
1671 * MsiEnumRelatedProductsW [MSI.@]
1674 UINT WINAPI MsiEnumRelatedProductsW(LPCWSTR szUpgradeCode, DWORD dwReserved,
1675 DWORD iProductIndex, LPWSTR lpProductBuf)
1677 UINT r;
1678 HKEY hkey;
1679 DWORD dwSize = SQUISH_GUID_SIZE;
1680 WCHAR szKeyName[SQUISH_GUID_SIZE];
1682 TRACE("%s %u %u %p\n", debugstr_w(szUpgradeCode), dwReserved,
1683 iProductIndex, lpProductBuf);
1685 if (NULL == szUpgradeCode)
1686 return ERROR_INVALID_PARAMETER;
1687 if (NULL == lpProductBuf)
1688 return ERROR_INVALID_PARAMETER;
1690 r = MSIREG_OpenUpgradeCodesKey(szUpgradeCode, &hkey, FALSE);
1691 if (r != ERROR_SUCCESS)
1692 return ERROR_NO_MORE_ITEMS;
1694 r = RegEnumValueW(hkey, iProductIndex, szKeyName, &dwSize, NULL, NULL, NULL, NULL);
1695 if( r == ERROR_SUCCESS )
1696 unsquash_guid(szKeyName, lpProductBuf);
1697 RegCloseKey(hkey);
1699 return r;
1702 /*************************************************************************
1703 * MsiEnumRelatedProductsA [MSI.@]
1706 UINT WINAPI MsiEnumRelatedProductsA(LPCSTR szUpgradeCode, DWORD dwReserved,
1707 DWORD iProductIndex, LPSTR lpProductBuf)
1709 LPWSTR szwUpgradeCode = NULL;
1710 WCHAR productW[GUID_SIZE];
1711 UINT r;
1713 TRACE("%s %u %u %p\n", debugstr_a(szUpgradeCode), dwReserved,
1714 iProductIndex, lpProductBuf);
1716 if (szUpgradeCode)
1718 szwUpgradeCode = strdupAtoW( szUpgradeCode );
1719 if( !szwUpgradeCode )
1720 return ERROR_OUTOFMEMORY;
1723 r = MsiEnumRelatedProductsW( szwUpgradeCode, dwReserved,
1724 iProductIndex, productW );
1725 if (r == ERROR_SUCCESS)
1727 WideCharToMultiByte( CP_ACP, 0, productW, GUID_SIZE,
1728 lpProductBuf, GUID_SIZE, NULL, NULL );
1730 msi_free( szwUpgradeCode);
1731 return r;
1734 /***********************************************************************
1735 * MsiEnumPatchesExA [MSI.@]
1737 UINT WINAPI MsiEnumPatchesExA(LPCSTR szProductCode, LPCSTR szUserSid,
1738 DWORD dwContext, DWORD dwFilter, DWORD dwIndex, LPSTR szPatchCode,
1739 LPSTR szTargetProductCode, MSIINSTALLCONTEXT *pdwTargetProductContext,
1740 LPSTR szTargetUserSid, LPDWORD pcchTargetUserSid)
1742 LPWSTR prodcode = NULL;
1743 LPWSTR usersid = NULL;
1744 LPWSTR targsid = NULL;
1745 WCHAR patch[GUID_SIZE];
1746 WCHAR targprod[GUID_SIZE];
1747 DWORD len;
1748 UINT r;
1750 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n",
1751 debugstr_a(szProductCode), debugstr_a(szUserSid), dwContext, dwFilter,
1752 dwIndex, szPatchCode, szTargetProductCode, pdwTargetProductContext,
1753 szTargetUserSid, pcchTargetUserSid);
1755 if (szTargetUserSid && !pcchTargetUserSid)
1756 return ERROR_INVALID_PARAMETER;
1758 if (szProductCode) prodcode = strdupAtoW(szProductCode);
1759 if (szUserSid) usersid = strdupAtoW(szUserSid);
1761 r = MsiEnumPatchesExW(prodcode, usersid, dwContext, dwFilter, dwIndex,
1762 patch, targprod, pdwTargetProductContext,
1763 NULL, &len);
1764 if (r != ERROR_SUCCESS)
1765 goto done;
1767 WideCharToMultiByte(CP_ACP, 0, patch, -1, szPatchCode,
1768 GUID_SIZE, NULL, NULL);
1769 WideCharToMultiByte(CP_ACP, 0, targprod, -1, szTargetProductCode,
1770 GUID_SIZE, NULL, NULL);
1772 if (!szTargetUserSid)
1774 if (pcchTargetUserSid)
1775 *pcchTargetUserSid = len;
1777 goto done;
1780 targsid = msi_alloc(++len * sizeof(WCHAR));
1781 if (!targsid)
1783 r = ERROR_OUTOFMEMORY;
1784 goto done;
1787 r = MsiEnumPatchesExW(prodcode, usersid, dwContext, dwFilter, dwIndex,
1788 patch, targprod, pdwTargetProductContext,
1789 targsid, &len);
1790 if (r != ERROR_SUCCESS || !szTargetUserSid)
1791 goto done;
1793 WideCharToMultiByte(CP_ACP, 0, targsid, -1, szTargetUserSid,
1794 *pcchTargetUserSid, NULL, NULL);
1796 len = lstrlenW(targsid);
1797 if (*pcchTargetUserSid < len + 1)
1799 r = ERROR_MORE_DATA;
1800 *pcchTargetUserSid = len * sizeof(WCHAR);
1802 else
1803 *pcchTargetUserSid = len;
1805 done:
1806 msi_free(prodcode);
1807 msi_free(usersid);
1808 msi_free(targsid);
1810 return r;
1813 static UINT msi_get_patch_state(LPCWSTR prodcode, LPCWSTR usersid,
1814 MSIINSTALLCONTEXT context,
1815 LPWSTR patch, MSIPATCHSTATE *state)
1817 DWORD type, val, size;
1818 HKEY prod, hkey = 0;
1819 HKEY udpatch = 0;
1820 LONG res;
1821 UINT r = ERROR_NO_MORE_ITEMS;
1823 *state = MSIPATCHSTATE_INVALID;
1825 r = MSIREG_OpenUserDataProductKey(prodcode, context,
1826 usersid, &prod, FALSE);
1827 if (r != ERROR_SUCCESS)
1828 return ERROR_NO_MORE_ITEMS;
1830 res = RegOpenKeyExW(prod, szPatches, 0, KEY_READ, &hkey);
1831 if (res != ERROR_SUCCESS)
1832 goto done;
1834 res = RegOpenKeyExW(hkey, patch, 0, KEY_READ, &udpatch);
1835 if (res != ERROR_SUCCESS)
1836 goto done;
1838 size = sizeof(DWORD);
1839 res = RegGetValueW(udpatch, NULL, szState, RRF_RT_DWORD, &type, &val, &size);
1840 if (res != ERROR_SUCCESS ||
1841 val < MSIPATCHSTATE_APPLIED || val > MSIPATCHSTATE_REGISTERED)
1843 r = ERROR_BAD_CONFIGURATION;
1844 goto done;
1847 *state = val;
1848 r = ERROR_SUCCESS;
1850 done:
1851 RegCloseKey(udpatch);
1852 RegCloseKey(hkey);
1853 RegCloseKey(prod);
1855 return r;
1858 static UINT msi_check_product_patches(LPCWSTR prodcode, LPCWSTR usersid,
1859 MSIINSTALLCONTEXT context, DWORD filter, DWORD index, DWORD *idx,
1860 LPWSTR patch, LPWSTR targetprod, MSIINSTALLCONTEXT *targetctx,
1861 LPWSTR targetsid, DWORD *sidsize, LPWSTR *transforms)
1863 MSIPATCHSTATE state = MSIPATCHSTATE_INVALID;
1864 LPWSTR ptr, patches = NULL;
1865 HKEY prod, patchkey = 0;
1866 HKEY localprod = 0, localpatch = 0;
1867 DWORD type, size;
1868 LONG res;
1869 UINT temp, r = ERROR_NO_MORE_ITEMS;
1871 if (MSIREG_OpenProductKey(prodcode, usersid, context,
1872 &prod, FALSE) != ERROR_SUCCESS)
1873 return ERROR_NO_MORE_ITEMS;
1875 size = 0;
1876 res = RegGetValueW(prod, szPatches, szPatches, RRF_RT_ANY, &type, NULL,
1877 &size);
1878 if (res != ERROR_SUCCESS)
1879 goto done;
1881 if (type != REG_MULTI_SZ)
1883 r = ERROR_BAD_CONFIGURATION;
1884 goto done;
1887 patches = msi_alloc(size);
1888 if (!patches)
1890 r = ERROR_OUTOFMEMORY;
1891 goto done;
1894 res = RegGetValueW(prod, szPatches, szPatches, RRF_RT_ANY, &type,
1895 patches, &size);
1896 if (res != ERROR_SUCCESS)
1897 goto done;
1899 for (ptr = patches; *ptr && r == ERROR_NO_MORE_ITEMS; ptr += lstrlenW(ptr) + 1)
1901 if (!unsquash_guid(ptr, patch))
1903 r = ERROR_BAD_CONFIGURATION;
1904 goto done;
1907 size = 0;
1908 res = RegGetValueW(prod, szPatches, ptr, RRF_RT_REG_SZ,
1909 &type, NULL, &size);
1910 if (res != ERROR_SUCCESS)
1911 continue;
1913 if (transforms)
1915 *transforms = msi_alloc(size);
1916 if (!*transforms)
1918 r = ERROR_OUTOFMEMORY;
1919 goto done;
1922 res = RegGetValueW(prod, szPatches, ptr, RRF_RT_REG_SZ,
1923 &type, *transforms, &size);
1924 if (res != ERROR_SUCCESS)
1925 continue;
1928 if (context == MSIINSTALLCONTEXT_USERMANAGED)
1930 if (!(filter & MSIPATCHSTATE_APPLIED))
1932 temp = msi_get_patch_state(prodcode, usersid, context,
1933 ptr, &state);
1934 if (temp == ERROR_BAD_CONFIGURATION)
1936 r = ERROR_BAD_CONFIGURATION;
1937 goto done;
1940 if (temp != ERROR_SUCCESS || !(filter & state))
1941 continue;
1944 else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
1946 if (!(filter & MSIPATCHSTATE_APPLIED))
1948 temp = msi_get_patch_state(prodcode, usersid, context,
1949 ptr, &state);
1950 if (temp == ERROR_BAD_CONFIGURATION)
1952 r = ERROR_BAD_CONFIGURATION;
1953 goto done;
1956 if (temp != ERROR_SUCCESS || !(filter & state))
1957 continue;
1959 else
1961 temp = MSIREG_OpenUserDataPatchKey(patch, context,
1962 &patchkey, FALSE);
1963 RegCloseKey(patchkey);
1964 if (temp != ERROR_SUCCESS)
1965 continue;
1968 else if (context == MSIINSTALLCONTEXT_MACHINE)
1970 usersid = szEmpty;
1972 if (MSIREG_OpenUserDataProductKey(prodcode, context, NULL, &localprod, FALSE) == ERROR_SUCCESS &&
1973 RegOpenKeyExW(localprod, szPatches, 0, KEY_READ, &localpatch) == ERROR_SUCCESS &&
1974 RegOpenKeyExW(localpatch, ptr, 0, KEY_READ, &patchkey) == ERROR_SUCCESS)
1976 res = RegGetValueW(patchkey, NULL, szState, RRF_RT_REG_DWORD,
1977 &type, &state, &size);
1979 if (!(filter & state))
1980 res = ERROR_NO_MORE_ITEMS;
1982 RegCloseKey(patchkey);
1985 RegCloseKey(localpatch);
1986 RegCloseKey(localprod);
1988 if (res != ERROR_SUCCESS)
1989 continue;
1992 if (*idx < index)
1994 (*idx)++;
1995 continue;
1998 r = ERROR_SUCCESS;
1999 if (targetprod)
2000 lstrcpyW(targetprod, prodcode);
2002 if (targetctx)
2003 *targetctx = context;
2005 if (targetsid)
2007 lstrcpynW(targetsid, usersid, *sidsize);
2008 if (lstrlenW(usersid) >= *sidsize)
2009 r = ERROR_MORE_DATA;
2012 if (sidsize)
2014 *sidsize = lstrlenW(usersid);
2015 if (!targetsid)
2016 *sidsize *= sizeof(WCHAR);
2020 done:
2021 RegCloseKey(prod);
2022 msi_free(patches);
2024 return r;
2027 static UINT msi_enum_patches(LPCWSTR szProductCode, LPCWSTR szUserSid,
2028 DWORD dwContext, DWORD dwFilter, DWORD dwIndex, DWORD *idx,
2029 LPWSTR szPatchCode, LPWSTR szTargetProductCode,
2030 MSIINSTALLCONTEXT *pdwTargetProductContext, LPWSTR szTargetUserSid,
2031 LPDWORD pcchTargetUserSid, LPWSTR *szTransforms)
2033 LPWSTR usersid = NULL;
2034 UINT r = ERROR_INVALID_PARAMETER;
2036 if (!szUserSid)
2038 szUserSid = usersid = get_user_sid();
2039 if (!usersid) return ERROR_FUNCTION_FAILED;
2042 if (dwContext & MSIINSTALLCONTEXT_USERMANAGED)
2044 r = msi_check_product_patches(szProductCode, szUserSid,
2045 MSIINSTALLCONTEXT_USERMANAGED, dwFilter,
2046 dwIndex, idx, szPatchCode,
2047 szTargetProductCode,
2048 pdwTargetProductContext, szTargetUserSid,
2049 pcchTargetUserSid, szTransforms);
2050 if (r != ERROR_NO_MORE_ITEMS)
2051 goto done;
2054 if (dwContext & MSIINSTALLCONTEXT_USERUNMANAGED)
2056 r = msi_check_product_patches(szProductCode, szUserSid,
2057 MSIINSTALLCONTEXT_USERUNMANAGED, dwFilter,
2058 dwIndex, idx, szPatchCode,
2059 szTargetProductCode,
2060 pdwTargetProductContext, szTargetUserSid,
2061 pcchTargetUserSid, szTransforms);
2062 if (r != ERROR_NO_MORE_ITEMS)
2063 goto done;
2066 if (dwContext & MSIINSTALLCONTEXT_MACHINE)
2068 r = msi_check_product_patches(szProductCode, szUserSid,
2069 MSIINSTALLCONTEXT_MACHINE, dwFilter,
2070 dwIndex, idx, szPatchCode,
2071 szTargetProductCode,
2072 pdwTargetProductContext, szTargetUserSid,
2073 pcchTargetUserSid, szTransforms);
2074 if (r != ERROR_NO_MORE_ITEMS)
2075 goto done;
2078 done:
2079 LocalFree(usersid);
2080 return r;
2083 /***********************************************************************
2084 * MsiEnumPatchesExW [MSI.@]
2086 UINT WINAPI MsiEnumPatchesExW(LPCWSTR szProductCode, LPCWSTR szUserSid,
2087 DWORD dwContext, DWORD dwFilter, DWORD dwIndex, LPWSTR szPatchCode,
2088 LPWSTR szTargetProductCode, MSIINSTALLCONTEXT *pdwTargetProductContext,
2089 LPWSTR szTargetUserSid, LPDWORD pcchTargetUserSid)
2091 WCHAR squished_pc[GUID_SIZE];
2092 DWORD idx = 0;
2093 UINT r;
2095 static DWORD last_index;
2097 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n",
2098 debugstr_w(szProductCode), debugstr_w(szUserSid), dwContext, dwFilter,
2099 dwIndex, szPatchCode, szTargetProductCode, pdwTargetProductContext,
2100 szTargetUserSid, pcchTargetUserSid);
2102 if (!szProductCode || !squash_guid(szProductCode, squished_pc))
2103 return ERROR_INVALID_PARAMETER;
2105 if (szUserSid && !strcmpW( szUserSid, szLocalSid ))
2106 return ERROR_INVALID_PARAMETER;
2108 if (dwContext & MSIINSTALLCONTEXT_MACHINE && szUserSid)
2109 return ERROR_INVALID_PARAMETER;
2111 if (dwContext <= MSIINSTALLCONTEXT_NONE ||
2112 dwContext > MSIINSTALLCONTEXT_ALL)
2113 return ERROR_INVALID_PARAMETER;
2115 if (dwFilter <= MSIPATCHSTATE_INVALID || dwFilter > MSIPATCHSTATE_ALL)
2116 return ERROR_INVALID_PARAMETER;
2118 if (dwIndex && dwIndex - last_index != 1)
2119 return ERROR_INVALID_PARAMETER;
2121 if (dwIndex == 0)
2122 last_index = 0;
2124 r = msi_enum_patches(szProductCode, szUserSid, dwContext, dwFilter,
2125 dwIndex, &idx, szPatchCode, szTargetProductCode,
2126 pdwTargetProductContext, szTargetUserSid,
2127 pcchTargetUserSid, NULL);
2129 if (r == ERROR_SUCCESS)
2130 last_index = dwIndex;
2131 else
2132 last_index = 0;
2134 return r;
2137 /***********************************************************************
2138 * MsiEnumPatchesA [MSI.@]
2140 UINT WINAPI MsiEnumPatchesA(LPCSTR szProduct, DWORD iPatchIndex,
2141 LPSTR lpPatchBuf, LPSTR lpTransformsBuf, LPDWORD pcchTransformsBuf)
2143 LPWSTR product, transforms;
2144 WCHAR patch[GUID_SIZE];
2145 DWORD len;
2146 UINT r;
2148 TRACE("(%s %d %p %p %p)\n", debugstr_a(szProduct), iPatchIndex,
2149 lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);
2151 if (!szProduct || !lpPatchBuf || !lpTransformsBuf || !pcchTransformsBuf)
2152 return ERROR_INVALID_PARAMETER;
2154 product = strdupAtoW(szProduct);
2155 if (!product)
2156 return ERROR_OUTOFMEMORY;
2158 len = *pcchTransformsBuf;
2159 transforms = msi_alloc( len * sizeof(WCHAR) );
2160 if (!transforms)
2162 r = ERROR_OUTOFMEMORY;
2163 goto done;
2166 r = MsiEnumPatchesW(product, iPatchIndex, patch, transforms, &len);
2167 if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
2168 goto done;
2170 WideCharToMultiByte(CP_ACP, 0, patch, -1, lpPatchBuf,
2171 GUID_SIZE, NULL, NULL);
2173 if (!WideCharToMultiByte(CP_ACP, 0, transforms, -1, lpTransformsBuf,
2174 *pcchTransformsBuf, NULL, NULL))
2175 r = ERROR_MORE_DATA;
2177 if (r == ERROR_MORE_DATA)
2179 lpTransformsBuf[*pcchTransformsBuf - 1] = '\0';
2180 *pcchTransformsBuf = len * 2;
2182 else
2183 *pcchTransformsBuf = strlen( lpTransformsBuf );
2185 done:
2186 msi_free(transforms);
2187 msi_free(product);
2189 return r;
2192 /***********************************************************************
2193 * MsiEnumPatchesW [MSI.@]
2195 UINT WINAPI MsiEnumPatchesW(LPCWSTR szProduct, DWORD iPatchIndex,
2196 LPWSTR lpPatchBuf, LPWSTR lpTransformsBuf, LPDWORD pcchTransformsBuf)
2198 WCHAR squished_pc[GUID_SIZE];
2199 LPWSTR transforms = NULL;
2200 HKEY prod;
2201 DWORD idx = 0;
2202 UINT r;
2204 TRACE("(%s %d %p %p %p)\n", debugstr_w(szProduct), iPatchIndex,
2205 lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);
2207 if (!szProduct || !squash_guid(szProduct, squished_pc))
2208 return ERROR_INVALID_PARAMETER;
2210 if (!lpPatchBuf || !lpTransformsBuf || !pcchTransformsBuf)
2211 return ERROR_INVALID_PARAMETER;
2213 if (MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERMANAGED,
2214 &prod, FALSE) != ERROR_SUCCESS &&
2215 MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERUNMANAGED,
2216 &prod, FALSE) != ERROR_SUCCESS &&
2217 MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_MACHINE,
2218 &prod, FALSE) != ERROR_SUCCESS)
2219 return ERROR_UNKNOWN_PRODUCT;
2221 RegCloseKey(prod);
2223 r = msi_enum_patches(szProduct, NULL, MSIINSTALLCONTEXT_ALL,
2224 MSIPATCHSTATE_ALL, iPatchIndex, &idx, lpPatchBuf,
2225 NULL, NULL, NULL, NULL, &transforms);
2226 if (r != ERROR_SUCCESS)
2227 goto done;
2229 lstrcpynW(lpTransformsBuf, transforms, *pcchTransformsBuf);
2230 if (*pcchTransformsBuf <= lstrlenW(transforms))
2232 r = ERROR_MORE_DATA;
2233 *pcchTransformsBuf = lstrlenW(transforms);
2235 else
2236 *pcchTransformsBuf = lstrlenW(transforms);
2238 done:
2239 msi_free(transforms);
2240 return r;
2243 UINT WINAPI MsiEnumProductsExA( LPCSTR product, LPCSTR usersid, DWORD ctx, DWORD index,
2244 CHAR installed_product[GUID_SIZE],
2245 MSIINSTALLCONTEXT *installed_ctx, LPSTR sid, LPDWORD sid_len )
2247 UINT r;
2248 WCHAR installed_productW[GUID_SIZE], *productW = NULL, *usersidW = NULL, *sidW = NULL;
2250 TRACE("%s, %s, %u, %u, %p, %p, %p, %p\n", debugstr_a(product), debugstr_a(usersid),
2251 ctx, index, installed_product, installed_ctx, sid, sid_len);
2253 if (sid && !sid_len) return ERROR_INVALID_PARAMETER;
2254 if (product && !(productW = strdupAtoW( product ))) return ERROR_OUTOFMEMORY;
2255 if (usersid && !(usersidW = strdupAtoW( usersid )))
2257 msi_free( productW );
2258 return ERROR_OUTOFMEMORY;
2260 if (sid && !(sidW = msi_alloc( *sid_len * sizeof(WCHAR) )))
2262 msi_free( usersidW );
2263 msi_free( productW );
2264 return ERROR_OUTOFMEMORY;
2266 r = MsiEnumProductsExW( productW, usersidW, ctx, index, installed_productW,
2267 installed_ctx, sidW, sid_len );
2268 if (r == ERROR_SUCCESS)
2270 if (installed_product) WideCharToMultiByte( CP_ACP, 0, installed_productW, GUID_SIZE,
2271 installed_product, GUID_SIZE, NULL, NULL );
2272 if (sid) WideCharToMultiByte( CP_ACP, 0, sidW, *sid_len + 1, sid, *sid_len + 1, NULL, NULL );
2274 msi_free( productW );
2275 msi_free( usersidW );
2276 msi_free( sidW );
2277 return r;
2280 static UINT fetch_machine_product( const WCHAR *match, DWORD index, DWORD *idx,
2281 WCHAR installed_product[GUID_SIZE],
2282 MSIINSTALLCONTEXT *installed_ctx, WCHAR *sid, DWORD *sid_len )
2284 static const WCHAR productsW[] =
2285 {'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
2286 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
2287 UINT r;
2288 WCHAR product[GUID_SIZE];
2289 DWORD i = 0, len;
2290 REGSAM access = KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY;
2291 HKEY key;
2293 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, productsW, 0, access, &key ))
2294 return ERROR_NO_MORE_ITEMS;
2296 len = sizeof(product)/sizeof(product[0]);
2297 while (!RegEnumKeyExW( key, i, product, &len, NULL, NULL, NULL, NULL ))
2299 if (match && strcmpW( match, product ))
2301 i++;
2302 len = sizeof(product)/sizeof(product[0]);
2303 continue;
2305 if (*idx == index) goto found;
2306 (*idx)++;
2307 len = sizeof(product)/sizeof(product[0]);
2308 i++;
2310 RegCloseKey( key );
2311 return ERROR_NO_MORE_ITEMS;
2313 found:
2314 if (sid_len && *sid_len < 1)
2316 *sid_len = 1;
2317 r = ERROR_MORE_DATA;
2319 else
2321 if (installed_product) unsquash_guid( product, installed_product );
2322 if (installed_ctx) *installed_ctx = MSIINSTALLCONTEXT_MACHINE;
2323 if (sid)
2325 sid[0] = 0;
2326 *sid_len = 0;
2328 r = ERROR_SUCCESS;
2330 RegCloseKey( key );
2331 return r;
2334 static UINT fetch_user_product( const WCHAR *match, const WCHAR *usersid, DWORD ctx, DWORD index,
2335 DWORD *idx, WCHAR installed_product[GUID_SIZE],
2336 MSIINSTALLCONTEXT *installed_ctx, WCHAR *sid, DWORD *sid_len )
2338 static const WCHAR managedW[] =
2339 {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
2340 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s',
2341 'i','o','n','\\','I','n','s','t','a','l','l','e','r','\\','M','a','n','a','g','e','d',0};
2342 static const WCHAR managed_productsW[] =
2343 {'\\','I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
2344 static const WCHAR unmanaged_productsW[] =
2345 {'\\','S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
2346 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
2347 UINT r;
2348 const WCHAR *subkey;
2349 WCHAR path[MAX_PATH], product[GUID_SIZE], user[128];
2350 DWORD i = 0, j = 0, len_product, len_user;
2351 REGSAM access = KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY;
2352 HKEY key_users, key_products;
2354 if (ctx == MSIINSTALLCONTEXT_USERMANAGED)
2356 subkey = managed_productsW;
2357 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, managedW, 0, access, &key_users ))
2358 return ERROR_NO_MORE_ITEMS;
2360 else if (ctx == MSIINSTALLCONTEXT_USERUNMANAGED)
2362 subkey = unmanaged_productsW;
2363 if (RegOpenKeyExW( HKEY_USERS, NULL, 0, access, &key_users ))
2364 return ERROR_NO_MORE_ITEMS;
2366 else return ERROR_INVALID_PARAMETER;
2368 len_user = sizeof(user)/sizeof(user[0]);
2369 while (!RegEnumKeyExW( key_users, i, user, &len_user, NULL, NULL, NULL, NULL ))
2371 if (strcmpW( usersid, user ) && strcmpW( usersid, szAllSid ))
2373 i++;
2374 len_user = sizeof(user)/sizeof(user[0]);
2375 continue;
2377 strcpyW( path, user );
2378 strcatW( path, subkey );
2379 if (RegOpenKeyExW( key_users, path, 0, access, &key_products ))
2381 i++;
2382 len_user = sizeof(user)/sizeof(user[0]);
2383 continue;
2385 len_product = sizeof(product)/sizeof(product[0]);
2386 while (!RegEnumKeyExW( key_products, j, product, &len_product, NULL, NULL, NULL, NULL ))
2388 if (match && strcmpW( match, product ))
2390 j++;
2391 len_product = sizeof(product)/sizeof(product[0]);
2392 continue;
2394 if (*idx == index) goto found;
2395 (*idx)++;
2396 len_product = sizeof(product)/sizeof(product[0]);
2397 j++;
2399 RegCloseKey( key_products );
2400 len_user = sizeof(user)/sizeof(user[0]);
2401 i++;
2403 RegCloseKey( key_users );
2404 return ERROR_NO_MORE_ITEMS;
2406 found:
2407 if (sid_len && *sid_len <= len_user)
2409 *sid_len = len_user;
2410 r = ERROR_MORE_DATA;
2412 else
2414 if (installed_product) unsquash_guid( product, installed_product );
2415 if (installed_ctx) *installed_ctx = ctx;
2416 if (sid)
2418 strcpyW( sid, user );
2419 *sid_len = len_user;
2421 r = ERROR_SUCCESS;
2423 RegCloseKey( key_products );
2424 RegCloseKey( key_users );
2425 return r;
2428 static UINT enum_products( const WCHAR *product, const WCHAR *usersid, DWORD ctx, DWORD index,
2429 DWORD *idx, WCHAR installed_product[GUID_SIZE],
2430 MSIINSTALLCONTEXT *installed_ctx, WCHAR *sid, DWORD *sid_len )
2432 UINT r = ERROR_NO_MORE_ITEMS;
2433 WCHAR *user = NULL;
2435 if (!usersid)
2437 usersid = user = get_user_sid();
2438 if (!user) return ERROR_FUNCTION_FAILED;
2440 if (ctx & MSIINSTALLCONTEXT_MACHINE)
2442 r = fetch_machine_product( product, index, idx, installed_product, installed_ctx,
2443 sid, sid_len );
2444 if (r != ERROR_NO_MORE_ITEMS) goto done;
2446 if (ctx & MSIINSTALLCONTEXT_USERUNMANAGED)
2448 r = fetch_user_product( product, usersid, MSIINSTALLCONTEXT_USERUNMANAGED, index,
2449 idx, installed_product, installed_ctx, sid, sid_len );
2450 if (r != ERROR_NO_MORE_ITEMS) goto done;
2452 if (ctx & MSIINSTALLCONTEXT_USERMANAGED)
2454 r = fetch_user_product( product, usersid, MSIINSTALLCONTEXT_USERMANAGED, index,
2455 idx, installed_product, installed_ctx, sid, sid_len );
2456 if (r != ERROR_NO_MORE_ITEMS) goto done;
2459 done:
2460 LocalFree( user );
2461 return r;
2464 UINT WINAPI MsiEnumProductsExW( LPCWSTR product, LPCWSTR usersid, DWORD ctx, DWORD index,
2465 WCHAR installed_product[GUID_SIZE],
2466 MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid, LPDWORD sid_len )
2468 UINT r;
2469 DWORD idx = 0;
2470 static DWORD last_index;
2472 TRACE("%s, %s, %u, %u, %p, %p, %p, %p\n", debugstr_w(product), debugstr_w(usersid),
2473 ctx, index, installed_product, installed_ctx, sid, sid_len);
2475 if ((sid && !sid_len) || !ctx || (usersid && ctx == MSIINSTALLCONTEXT_MACHINE))
2476 return ERROR_INVALID_PARAMETER;
2478 if (index && index - last_index != 1)
2479 return ERROR_INVALID_PARAMETER;
2481 if (!index) last_index = 0;
2483 r = enum_products( product, usersid, ctx, index, &idx, installed_product, installed_ctx,
2484 sid, sid_len );
2485 if (r == ERROR_SUCCESS)
2486 last_index = index;
2487 else
2488 last_index = 0;
2490 return r;