strmbase: Implement BaseControlWindow.
[wine/multimedia.git] / dlls / msi / registry.c
blob0c726484d4acf825e260c3891f44e6025cd7b7e8
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_LocalManaged_fmt[] = {
104 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
105 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
106 'I','n','s','t','a','l','l','e','r','\\','M','a','n','a','g','e','d','\\','%','s','\\',
107 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
109 static const WCHAR szInstaller_LocalManagedProd_fmt[] = {
110 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
111 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
112 'I','n','s','t','a','l','l','e','r','\\','M','a','n','a','g','e','d','\\','%','s','\\',
113 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s','\\','%','s',0};
115 static const WCHAR szInstaller_LocalManagedFeat_fmt[] = {
116 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
117 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
118 'I','n','s','t','a','l','l','e','r','\\','M','a','n','a','g','e','d','\\','%','s','\\',
119 'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s','\\','%','s',0};
121 static const WCHAR szInstaller_Products[] = {
122 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
123 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
124 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
126 static const WCHAR szInstaller_Patches[] = {
127 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
128 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
129 'I','n','s','t','a','l','l','e','r','\\','P','a','t','c','h','e','s',0};
131 static const WCHAR szInstaller_Components[] = {
132 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
133 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
134 'I','n','s','t','a','l','l','e','r','\\','C','o','m','p','o','n','e','n','t','s',0};
136 static const WCHAR szInstaller_LocalClassesProducts[] = {
137 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
138 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
140 static const WCHAR szInstaller_LocalClassesFeatures[] = {
141 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
142 'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s',0};
144 static const WCHAR szInstaller_LocalClassesProd[] = {
145 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
146 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s','\\',0};
148 static const WCHAR szInstaller_LocalClassesFeat[] = {
149 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
150 'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s','\\',0};
152 static const WCHAR szInstaller_ClassesUpgradeCode[] = {
153 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
154 'I','n','s','t','a','l','l','e','r','\\','U','p','g','r','a','d','e','C','o','d','e','s','\\',0};
156 static const WCHAR szInstaller_ClassesUpgradeCodes[] = {
157 'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
158 'I','n','s','t','a','l','l','e','r','\\','U','p','g','r','a','d','e','C','o','d','e','s',0};
160 static const WCHAR szInstaller_Features[] = {
161 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
162 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
163 'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s','\\',0};
165 static const WCHAR szInstaller_UpgradeCodes[] = {
166 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
167 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
168 'I','n','s','t','a','l','l','e','r','\\','U','p','g','r','a','d','e','C','o','d','e','s','\\',0};
170 static const WCHAR szInstaller_UserUpgradeCodes[] = {
171 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
172 'I','n','s','t','a','l','l','e','r','\\','U','p','g','r','a','d','e','C','o','d','e','s','\\',0};
174 static const WCHAR szUninstall[] = {
175 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
176 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
177 'U','n','i','n','s','t','a','l','l','\\',0};
179 static const WCHAR szUninstall_32node[] = {
180 'S','o','f','t','w','a','r','e','\\','W','o','w','6','4','3','2','N','o','d','e','\\',
181 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
182 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','U','n','i','n','s','t','a','l','l','\\',0};
184 static const WCHAR szUserComponents[] = {
185 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
186 'I','n','s','t','a','l','l','e','r','\\','C','o','m','p','o','n','e','n','t','s','\\',0};
188 static const WCHAR szUserFeatures[] = {
189 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
190 'I','n','s','t','a','l','l','e','r','\\','F','e','a','t','u','r','e','s','\\',0};
192 static const WCHAR szUserProducts[] = {
193 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
194 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s','\\',0};
196 static const WCHAR szUserPatches[] = {
197 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
198 'I','n','s','t','a','l','l','e','r','\\','P','a','t','c','h','e','s','\\',0};
200 BOOL unsquash_guid(LPCWSTR in, LPWSTR out)
202 DWORD i,n=0;
204 if (lstrlenW(in) != 32)
205 return FALSE;
207 out[n++]='{';
208 for(i=0; i<8; i++)
209 out[n++] = in[7-i];
210 out[n++]='-';
211 for(i=0; i<4; i++)
212 out[n++] = in[11-i];
213 out[n++]='-';
214 for(i=0; i<4; i++)
215 out[n++] = in[15-i];
216 out[n++]='-';
217 for(i=0; i<2; i++)
219 out[n++] = in[17+i*2];
220 out[n++] = in[16+i*2];
222 out[n++]='-';
223 for( ; i<8; i++)
225 out[n++] = in[17+i*2];
226 out[n++] = in[16+i*2];
228 out[n++]='}';
229 out[n]=0;
230 return TRUE;
233 BOOL squash_guid(LPCWSTR in, LPWSTR out)
235 DWORD i,n=1;
236 GUID guid;
238 out[0] = 0;
240 if (FAILED(CLSIDFromString((LPCOLESTR)in, &guid)))
241 return FALSE;
243 for(i=0; i<8; i++)
244 out[7-i] = in[n++];
245 n++;
246 for(i=0; i<4; i++)
247 out[11-i] = in[n++];
248 n++;
249 for(i=0; i<4; i++)
250 out[15-i] = in[n++];
251 n++;
252 for(i=0; i<2; i++)
254 out[17+i*2] = in[n++];
255 out[16+i*2] = in[n++];
257 n++;
258 for( ; i<8; i++)
260 out[17+i*2] = in[n++];
261 out[16+i*2] = in[n++];
263 out[32]=0;
264 return TRUE;
268 /* tables for encoding and decoding base85 */
269 static const unsigned char table_dec85[0x80] = {
270 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
271 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
272 0xff,0x00,0xff,0xff,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0xff,
273 0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0xff,0xff,0xff,0x16,0xff,0x17,
274 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
275 0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0xff,0x34,0x35,0x36,
276 0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x42,0x43,0x44,0x45,0x46,
277 0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0xff,0x53,0x54,0xff,
280 static const char table_enc85[] =
281 "!$%&'()*+,-.0123456789=?@ABCDEFGHIJKLMNO"
282 "PQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwx"
283 "yz{}~";
286 * Converts a base85 encoded guid into a GUID pointer
287 * Base85 encoded GUIDs should be 20 characters long.
289 * returns TRUE if successful, FALSE if not
291 BOOL decode_base85_guid( LPCWSTR str, GUID *guid )
293 DWORD i, val = 0, base = 1, *p;
295 if (!str)
296 return FALSE;
298 p = (DWORD*) guid;
299 for( i=0; i<20; i++ )
301 if( (i%5) == 0 )
303 val = 0;
304 base = 1;
306 val += table_dec85[str[i]] * base;
307 if( str[i] >= 0x80 )
308 return FALSE;
309 if( table_dec85[str[i]] == 0xff )
310 return FALSE;
311 if( (i%5) == 4 )
312 p[i/5] = val;
313 base *= 85;
315 return TRUE;
319 * Encodes a base85 guid given a GUID pointer
320 * Caller should provide a 21 character buffer for the encoded string.
322 * returns TRUE if successful, FALSE if not
324 BOOL encode_base85_guid( GUID *guid, LPWSTR str )
326 unsigned int x, *p, i;
328 p = (unsigned int*) guid;
329 for( i=0; i<4; i++ )
331 x = p[i];
332 *str++ = table_enc85[x%85];
333 x = x/85;
334 *str++ = table_enc85[x%85];
335 x = x/85;
336 *str++ = table_enc85[x%85];
337 x = x/85;
338 *str++ = table_enc85[x%85];
339 x = x/85;
340 *str++ = table_enc85[x%85];
342 *str = 0;
344 return TRUE;
347 DWORD msi_version_str_to_dword(LPCWSTR p)
349 DWORD major, minor = 0, build = 0, version = 0;
351 if (!p)
352 return version;
354 major = atoiW(p);
356 p = strchrW(p, '.');
357 if (p)
359 minor = atoiW(p+1);
360 p = strchrW(p+1, '.');
361 if (p)
362 build = atoiW(p+1);
365 return MAKELONG(build, MAKEWORD(minor, major));
368 LONG msi_reg_set_val_str( HKEY hkey, LPCWSTR name, LPCWSTR value )
370 DWORD len;
371 if (!value) value = szEmpty;
372 len = (lstrlenW(value) + 1) * sizeof (WCHAR);
373 return RegSetValueExW( hkey, name, 0, REG_SZ, (const BYTE *)value, len );
376 LONG msi_reg_set_val_multi_str( HKEY hkey, LPCWSTR name, LPCWSTR value )
378 LPCWSTR p = value;
379 while (*p) p += lstrlenW(p) + 1;
380 return RegSetValueExW( hkey, name, 0, REG_MULTI_SZ,
381 (const BYTE *)value, (p + 1 - value) * sizeof(WCHAR) );
384 LONG msi_reg_set_val_dword( HKEY hkey, LPCWSTR name, DWORD val )
386 return RegSetValueExW( hkey, name, 0, REG_DWORD, (LPBYTE)&val, sizeof (DWORD) );
389 LONG msi_reg_set_subkey_val( HKEY hkey, LPCWSTR path, LPCWSTR name, LPCWSTR val )
391 HKEY hsubkey = 0;
392 LONG r;
394 r = RegCreateKeyW( hkey, path, &hsubkey );
395 if (r != ERROR_SUCCESS)
396 return r;
397 r = msi_reg_set_val_str( hsubkey, name, val );
398 RegCloseKey( hsubkey );
399 return r;
402 LPWSTR msi_reg_get_val_str( HKEY hkey, LPCWSTR name )
404 DWORD len = 0;
405 LPWSTR val;
406 LONG r;
408 r = RegQueryValueExW(hkey, name, NULL, NULL, NULL, &len);
409 if (r != ERROR_SUCCESS)
410 return NULL;
412 len += sizeof (WCHAR);
413 val = msi_alloc( len );
414 if (!val)
415 return NULL;
416 val[0] = 0;
417 RegQueryValueExW(hkey, name, NULL, NULL, (LPBYTE) val, &len);
418 return val;
421 BOOL msi_reg_get_val_dword( HKEY hkey, LPCWSTR name, DWORD *val)
423 DWORD type, len = sizeof (DWORD);
424 LONG r = RegQueryValueExW(hkey, name, NULL, &type, (LPBYTE) val, &len);
425 return r == ERROR_SUCCESS && type == REG_DWORD;
428 static WCHAR *get_user_sid(void)
430 HANDLE token;
431 DWORD size = 256;
432 TOKEN_USER *user;
433 WCHAR *ret;
435 if (!OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &token )) return NULL;
436 if (!(user = msi_alloc( size )))
438 CloseHandle( token );
439 return NULL;
441 if (!GetTokenInformation( token, TokenUser, user, size, &size ))
443 msi_free( user );
444 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER || !(user = msi_alloc( size )))
446 CloseHandle( token );
447 return NULL;
449 GetTokenInformation( token, TokenUser, user, size, &size );
451 CloseHandle( token );
452 if (!ConvertSidToStringSidW( user->User.Sid, &ret ))
454 msi_free( user );
455 return NULL;
457 msi_free( user );
458 return ret;
461 UINT MSIREG_OpenUninstallKey(const WCHAR *product, enum platform platform, HKEY *key, BOOL create)
463 WCHAR keypath[0x200];
465 TRACE("%s\n", debugstr_w(product));
467 if (is_64bit && platform == PLATFORM_INTEL)
469 strcpyW(keypath, szUninstall_32node);
470 strcatW(keypath, product);
472 else
474 strcpyW(keypath, szUninstall);
475 strcatW(keypath, product);
477 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, KEY_ALL_ACCESS, NULL, key, NULL);
478 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, KEY_ALL_ACCESS, key);
481 UINT MSIREG_DeleteUninstallKey(const WCHAR *product, enum platform platform)
483 WCHAR keypath[0x200];
485 TRACE("%s\n", debugstr_w(product));
487 if (is_64bit && platform == PLATFORM_INTEL)
489 strcpyW(keypath, szUninstall_32node);
490 strcatW(keypath, product);
492 else
494 strcpyW(keypath, szUninstall);
495 strcatW(keypath, product);
497 return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
500 UINT MSIREG_OpenProductKey(LPCWSTR szProduct, LPCWSTR szUserSid, MSIINSTALLCONTEXT context, HKEY *key, BOOL create)
502 LPWSTR usersid = NULL;
503 HKEY root = HKEY_LOCAL_MACHINE;
504 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
505 WCHAR squished_pc[GUID_SIZE], keypath[MAX_PATH];
507 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
508 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
510 if (context == MSIINSTALLCONTEXT_MACHINE)
512 strcpyW(keypath, szInstaller_LocalClassesProd);
513 strcatW(keypath, squished_pc);
515 else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
517 root = HKEY_CURRENT_USER;
518 strcpyW(keypath, szUserProducts);
519 strcatW(keypath, squished_pc);
521 else
523 if (!szUserSid)
525 if (!(usersid = get_user_sid()))
527 ERR("Failed to retrieve user SID\n");
528 return ERROR_FUNCTION_FAILED;
530 szUserSid = usersid;
532 sprintfW(keypath, szInstaller_LocalManagedProd_fmt, szUserSid, squished_pc);
533 LocalFree(usersid);
535 if (create) return RegCreateKeyExW(root, keypath, 0, NULL, 0, access, NULL, key, NULL);
536 return RegOpenKeyExW(root, keypath, 0, access, key);
539 UINT MSIREG_DeleteUserProductKey(LPCWSTR szProduct)
541 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
543 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
544 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
546 strcpyW(keypath, szUserProducts);
547 strcatW(keypath, squished_pc);
548 return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
551 UINT MSIREG_OpenUserPatchesKey(LPCWSTR szPatch, HKEY *key, BOOL create)
553 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
555 if (!squash_guid(szPatch, squished_pc)) return ERROR_FUNCTION_FAILED;
556 TRACE("%s squished %s\n", debugstr_w(szPatch), debugstr_w(squished_pc));
558 strcpyW(keypath, szUserPatches);
559 strcatW(keypath, squished_pc);
561 if (create) return RegCreateKeyW(HKEY_CURRENT_USER, keypath, key);
562 return RegOpenKeyW(HKEY_CURRENT_USER, keypath, key);
565 UINT MSIREG_OpenFeaturesKey(LPCWSTR szProduct, MSIINSTALLCONTEXT context, HKEY *key, BOOL create)
567 LPWSTR usersid;
568 HKEY root = HKEY_LOCAL_MACHINE;
569 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
570 WCHAR squished_pc[GUID_SIZE], keypath[MAX_PATH];
572 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
573 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
575 if (context == MSIINSTALLCONTEXT_MACHINE)
577 strcpyW(keypath, szInstaller_LocalClassesFeat);
578 strcatW(keypath, squished_pc);
580 else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
582 root = HKEY_CURRENT_USER;
583 strcpyW(keypath, szUserFeatures);
584 strcatW(keypath, squished_pc);
586 else
588 if (!(usersid = get_user_sid()))
590 ERR("Failed to retrieve user SID\n");
591 return ERROR_FUNCTION_FAILED;
593 sprintfW(keypath, szInstaller_LocalManagedFeat_fmt, usersid, squished_pc);
594 LocalFree(usersid);
596 if (create) return RegCreateKeyExW(root, keypath, 0, NULL, 0, access, NULL, key, NULL);
597 return RegOpenKeyExW(root, keypath, 0, access, key);
600 UINT MSIREG_DeleteUserFeaturesKey(LPCWSTR szProduct)
602 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
604 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
605 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
607 strcpyW(keypath, szUserFeatures);
608 strcatW(keypath, squished_pc);
609 return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
612 static UINT MSIREG_OpenInstallerFeaturesKey(LPCWSTR szProduct, HKEY *key, BOOL create)
614 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
615 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
617 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
618 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
620 strcpyW(keypath, szInstaller_Features);
621 strcatW(keypath, squished_pc);
623 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
624 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
627 UINT MSIREG_OpenUserDataFeaturesKey(LPCWSTR szProduct, MSIINSTALLCONTEXT context, HKEY *key, BOOL create)
629 LPWSTR usersid;
630 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
631 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
633 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
634 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
636 if (context == MSIINSTALLCONTEXT_MACHINE)
638 sprintfW(keypath, szUserDataFeatures_fmt, szLocalSid, squished_pc);
640 else
642 if (!(usersid = get_user_sid()))
644 ERR("Failed to retrieve user SID\n");
645 return ERROR_FUNCTION_FAILED;
647 sprintfW(keypath, szUserDataFeatures_fmt, usersid, squished_pc);
648 LocalFree(usersid);
650 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
651 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
654 UINT MSIREG_OpenUserComponentsKey(LPCWSTR szComponent, HKEY *key, BOOL create)
656 WCHAR squished_cc[GUID_SIZE], keypath[0x200];
658 if (!squash_guid(szComponent, squished_cc)) return ERROR_FUNCTION_FAILED;
659 TRACE("%s squished %s\n", debugstr_w(szComponent), debugstr_w(squished_cc));
661 strcpyW(keypath, szUserComponents);
662 strcatW(keypath, squished_cc);
664 if (create) return RegCreateKeyW(HKEY_CURRENT_USER, keypath, key);
665 return RegOpenKeyW(HKEY_CURRENT_USER, keypath, key);
668 UINT MSIREG_OpenUserDataComponentKey(LPCWSTR szComponent, LPCWSTR szUserSid, HKEY *key, BOOL create)
670 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
671 WCHAR comp[GUID_SIZE], keypath[0x200];
672 LPWSTR usersid;
674 if (!squash_guid(szComponent, comp)) return ERROR_FUNCTION_FAILED;
675 TRACE("%s squished %s\n", debugstr_w(szComponent), debugstr_w(comp));
677 if (!szUserSid)
679 if (!(usersid = get_user_sid()))
681 ERR("Failed to retrieve user SID\n");
682 return ERROR_FUNCTION_FAILED;
684 sprintfW(keypath, szUserDataComp_fmt, usersid, comp);
685 LocalFree(usersid);
687 else
688 sprintfW(keypath, szUserDataComp_fmt, szUserSid, comp);
690 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
691 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
694 UINT MSIREG_DeleteUserDataComponentKey(LPCWSTR szComponent, LPCWSTR szUserSid)
696 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
697 WCHAR comp[GUID_SIZE], keypath[0x200];
698 LPWSTR usersid;
699 HKEY hkey;
700 LONG r;
702 if (!squash_guid(szComponent, comp)) return ERROR_FUNCTION_FAILED;
703 TRACE("%s squished %s\n", debugstr_w(szComponent), debugstr_w(comp));
705 if (!szUserSid)
707 if (!(usersid = get_user_sid()))
709 ERR("Failed to retrieve user SID\n");
710 return ERROR_FUNCTION_FAILED;
712 sprintfW(keypath, szUserDataComponents_fmt, usersid);
713 LocalFree(usersid);
715 else
716 sprintfW(keypath, szUserDataComponents_fmt, szUserSid);
718 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, &hkey)) return ERROR_SUCCESS;
719 r = RegDeleteTreeW(hkey, comp);
720 RegCloseKey(hkey);
721 return r;
724 UINT MSIREG_OpenUserDataProductKey(LPCWSTR szProduct, MSIINSTALLCONTEXT dwContext, LPCWSTR szUserSid, HKEY *key, BOOL create)
726 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
727 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
728 LPWSTR usersid;
730 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
731 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
733 if (dwContext == MSIINSTALLCONTEXT_MACHINE)
734 sprintfW(keypath, szUserDataProd_fmt, szLocalSid, squished_pc);
735 else if (szUserSid)
736 sprintfW(keypath, szUserDataProd_fmt, szUserSid, squished_pc);
737 else
739 if (!(usersid = get_user_sid()))
741 ERR("Failed to retrieve user SID\n");
742 return ERROR_FUNCTION_FAILED;
744 sprintfW(keypath, szUserDataProd_fmt, usersid, squished_pc);
745 LocalFree(usersid);
747 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
748 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
751 UINT MSIREG_OpenUserDataPatchKey(LPCWSTR szPatch, MSIINSTALLCONTEXT dwContext, HKEY *key, BOOL create)
753 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
754 WCHAR squished_patch[GUID_SIZE], keypath[0x200];
755 LPWSTR usersid;
757 if (!squash_guid(szPatch, squished_patch)) return ERROR_FUNCTION_FAILED;
758 TRACE("%s squished %s\n", debugstr_w(szPatch), debugstr_w(squished_patch));
760 if (dwContext == MSIINSTALLCONTEXT_MACHINE)
761 sprintfW(keypath, szUserDataPatch_fmt, szLocalSid, squished_patch);
762 else
764 if (!(usersid = get_user_sid()))
766 ERR("Failed to retrieve user SID\n");
767 return ERROR_FUNCTION_FAILED;
769 sprintfW(keypath, szUserDataPatch_fmt, usersid, squished_patch);
770 LocalFree(usersid);
772 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
773 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
776 UINT MSIREG_DeleteUserDataPatchKey(LPCWSTR patch, MSIINSTALLCONTEXT context)
778 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
779 WCHAR squished_patch[GUID_SIZE], keypath[0x200];
780 LPWSTR usersid;
781 HKEY hkey;
782 LONG r;
784 if (!squash_guid(patch, squished_patch)) return ERROR_FUNCTION_FAILED;
785 TRACE("%s squished %s\n", debugstr_w(patch), debugstr_w(squished_patch));
787 if (context == MSIINSTALLCONTEXT_MACHINE)
788 sprintfW(keypath, szUserDataPatches_fmt, szLocalSid);
789 else
791 if (!(usersid = get_user_sid()))
793 ERR("Failed to retrieve user SID\n");
794 return ERROR_FUNCTION_FAILED;
796 sprintfW(keypath, szUserDataPatches_fmt, usersid);
797 LocalFree(usersid);
799 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, &hkey)) return ERROR_SUCCESS;
800 r = RegDeleteTreeW(hkey, squished_patch);
801 RegCloseKey(hkey);
802 return r;
805 UINT MSIREG_OpenUserDataProductPatchesKey(LPCWSTR product, MSIINSTALLCONTEXT context, HKEY *key, BOOL create)
807 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
808 WCHAR squished_product[GUID_SIZE], keypath[0x200];
809 LPWSTR usersid;
811 if (!squash_guid(product, squished_product)) return ERROR_FUNCTION_FAILED;
812 TRACE("%s squished %s\n", debugstr_w(product), debugstr_w(squished_product));
814 if (context == MSIINSTALLCONTEXT_MACHINE)
815 sprintfW(keypath, szUserDataProductPatches_fmt, szLocalSid, squished_product);
816 else
818 if (!(usersid = get_user_sid()))
820 ERR("Failed to retrieve user SID\n");
821 return ERROR_FUNCTION_FAILED;
823 sprintfW(keypath, szUserDataProductPatches_fmt, usersid, squished_product);
824 LocalFree(usersid);
826 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
827 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
830 UINT MSIREG_OpenInstallProps(LPCWSTR szProduct, MSIINSTALLCONTEXT dwContext, LPCWSTR szUserSid, HKEY *key, BOOL create)
832 LPWSTR usersid;
833 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
834 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
836 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
837 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
839 if (dwContext == MSIINSTALLCONTEXT_MACHINE)
840 sprintfW(keypath, szInstallProperties_fmt, szLocalSid, squished_pc);
841 else if (szUserSid)
842 sprintfW(keypath, szInstallProperties_fmt, szUserSid, squished_pc);
843 else
845 if (!(usersid = get_user_sid()))
847 ERR("Failed to retrieve user SID\n");
848 return ERROR_FUNCTION_FAILED;
850 sprintfW(keypath, szInstallProperties_fmt, usersid, squished_pc);
851 LocalFree(usersid);
853 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
854 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
857 UINT MSIREG_DeleteUserDataProductKey(LPCWSTR szProduct)
859 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
860 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
861 LPWSTR usersid;
862 HKEY hkey;
863 LONG r;
865 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
866 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
868 if (!(usersid = get_user_sid()))
870 ERR("Failed to retrieve user SID\n");
871 return ERROR_FUNCTION_FAILED;
873 sprintfW(keypath, szUserDataProducts_fmt, usersid);
874 LocalFree(usersid);
876 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, &hkey)) return ERROR_SUCCESS;
877 r = RegDeleteTreeW(hkey, squished_pc);
878 RegCloseKey(hkey);
879 return r;
882 UINT MSIREG_DeleteProductKey(LPCWSTR szProduct)
884 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
885 WCHAR squished_pc[GUID_SIZE];
886 HKEY hkey;
887 LONG r;
889 if (!squash_guid(szProduct, squished_pc)) return ERROR_FUNCTION_FAILED;
890 TRACE("%s squished %s\n", debugstr_w(szProduct), debugstr_w(squished_pc));
892 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_Products, 0, access, &hkey)) return ERROR_SUCCESS;
893 r = RegDeleteTreeW(hkey, squished_pc);
894 RegCloseKey(hkey);
895 return r;
898 UINT MSIREG_OpenPatchesKey(LPCWSTR szPatch, HKEY *key, BOOL create)
900 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
901 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
903 if (!squash_guid(szPatch, squished_pc)) return ERROR_FUNCTION_FAILED;
904 TRACE("%s squished %s\n", debugstr_w(szPatch), debugstr_w(squished_pc));
906 sprintfW(keypath, szInstaller_Patches, squished_pc);
908 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
909 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
912 UINT MSIREG_OpenUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY *key, BOOL create)
914 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
915 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
917 if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
918 TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
920 strcpyW(keypath, szInstaller_UpgradeCodes);
921 strcatW(keypath, squished_pc);
923 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
924 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
927 UINT MSIREG_OpenUserUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY* key, BOOL create)
929 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
931 if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
932 TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
934 strcpyW(keypath, szInstaller_UserUpgradeCodes);
935 strcatW(keypath, squished_pc);
937 if (create) return RegCreateKeyW(HKEY_CURRENT_USER, keypath, key);
938 return RegOpenKeyW(HKEY_CURRENT_USER, keypath, key);
941 UINT MSIREG_DeleteUserUpgradeCodesKey(LPCWSTR szUpgradeCode)
943 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
945 if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
946 TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
948 strcpyW(keypath, szInstaller_UserUpgradeCodes);
949 strcatW(keypath, squished_pc);
950 return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
953 UINT MSIREG_DeleteLocalClassesProductKey(LPCWSTR szProductCode)
955 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
956 WCHAR squished_pc[GUID_SIZE];
957 HKEY hkey;
958 LONG r;
960 if (!squash_guid(szProductCode, squished_pc)) return ERROR_FUNCTION_FAILED;
961 TRACE("%s squished %s\n", debugstr_w(szProductCode), debugstr_w(squished_pc));
963 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_LocalClassesProducts, 0, access, &hkey)) return ERROR_SUCCESS;
964 r = RegDeleteTreeW(hkey, squished_pc);
965 RegCloseKey(hkey);
966 return r;
969 UINT MSIREG_DeleteLocalClassesFeaturesKey(LPCWSTR szProductCode)
971 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
972 WCHAR squished_pc[GUID_SIZE];
973 HKEY hkey;
974 LONG r;
976 if (!squash_guid(szProductCode, squished_pc)) return ERROR_FUNCTION_FAILED;
977 TRACE("%s squished %s\n", debugstr_w(szProductCode), debugstr_w(squished_pc));
979 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_LocalClassesFeatures, 0, access, &hkey)) return ERROR_SUCCESS;
980 r = RegDeleteTreeW(hkey, squished_pc);
981 RegCloseKey(hkey);
982 return r;
985 UINT MSIREG_OpenClassesUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY *key, BOOL create)
987 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
988 WCHAR squished_pc[GUID_SIZE], keypath[0x200];
990 if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
991 TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
993 strcpyW(keypath, szInstaller_ClassesUpgradeCode);
994 strcatW(keypath, squished_pc);
996 if (create) return RegCreateKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, NULL, 0, access, NULL, key, NULL);
997 return RegOpenKeyExW(HKEY_LOCAL_MACHINE, keypath, 0, access, key);
1000 UINT MSIREG_DeleteClassesUpgradeCodesKey(LPCWSTR szUpgradeCode)
1002 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
1003 WCHAR squished_pc[GUID_SIZE];
1004 HKEY hkey;
1005 LONG r;
1007 if (!squash_guid(szUpgradeCode, squished_pc)) return ERROR_FUNCTION_FAILED;
1008 TRACE("%s squished %s\n", debugstr_w(szUpgradeCode), debugstr_w(squished_pc));
1010 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, szInstaller_ClassesUpgradeCodes, 0, access, &hkey)) return ERROR_SUCCESS;
1011 r = RegDeleteTreeW(hkey, squished_pc);
1012 RegCloseKey(hkey);
1013 return r;
1016 /*************************************************************************
1017 * MsiDecomposeDescriptorW [MSI.@]
1019 * Decomposes an MSI descriptor into product, feature and component parts.
1020 * An MSI descriptor is a string of the form:
1021 * [base 85 guid] [feature code] '>' [base 85 guid]
1023 * PARAMS
1024 * szDescriptor [I] the descriptor to decompose
1025 * szProduct [O] buffer of MAX_FEATURE_CHARS+1 for the product guid
1026 * szFeature [O] buffer of MAX_FEATURE_CHARS+1 for the feature code
1027 * szComponent [O] buffer of MAX_FEATURE_CHARS+1 for the component guid
1028 * pUsed [O] the length of the descriptor
1030 * RETURNS
1031 * ERROR_SUCCESS if everything worked correctly
1032 * ERROR_INVALID_PARAMETER if the descriptor was invalid
1035 UINT WINAPI MsiDecomposeDescriptorW( LPCWSTR szDescriptor, LPWSTR szProduct,
1036 LPWSTR szFeature, LPWSTR szComponent, LPDWORD pUsed )
1038 UINT r, len;
1039 LPWSTR p;
1040 GUID product, component;
1042 TRACE("%s %p %p %p %p\n", debugstr_w(szDescriptor), szProduct,
1043 szFeature, szComponent, pUsed);
1045 r = decode_base85_guid( szDescriptor, &product );
1046 if( !r )
1047 return ERROR_INVALID_PARAMETER;
1049 TRACE("product %s\n", debugstr_guid( &product ));
1051 p = strchrW(&szDescriptor[20],'>');
1052 if( !p )
1053 return ERROR_INVALID_PARAMETER;
1055 len = (p - &szDescriptor[20]);
1056 if( len > MAX_FEATURE_CHARS )
1057 return ERROR_INVALID_PARAMETER;
1059 TRACE("feature %s\n", debugstr_wn( &szDescriptor[20], len ));
1061 r = decode_base85_guid( p+1, &component );
1062 if( !r )
1063 return ERROR_INVALID_PARAMETER;
1065 TRACE("component %s\n", debugstr_guid( &component ));
1067 if (szProduct)
1068 StringFromGUID2( &product, szProduct, MAX_FEATURE_CHARS+1 );
1069 if (szComponent)
1070 StringFromGUID2( &component, szComponent, MAX_FEATURE_CHARS+1 );
1071 if (szFeature)
1073 memcpy( szFeature, &szDescriptor[20], len*sizeof(WCHAR) );
1074 szFeature[len] = 0;
1076 len = ( &p[21] - szDescriptor );
1078 TRACE("length = %d\n", len);
1079 if (pUsed) *pUsed = len;
1081 return ERROR_SUCCESS;
1084 UINT WINAPI MsiDecomposeDescriptorA( LPCSTR szDescriptor, LPSTR szProduct,
1085 LPSTR szFeature, LPSTR szComponent, LPDWORD pUsed )
1087 WCHAR product[MAX_FEATURE_CHARS+1];
1088 WCHAR feature[MAX_FEATURE_CHARS+1];
1089 WCHAR component[MAX_FEATURE_CHARS+1];
1090 LPWSTR str = NULL, p = NULL, f = NULL, c = NULL;
1091 UINT r;
1093 TRACE("%s %p %p %p %p\n", debugstr_a(szDescriptor), szProduct,
1094 szFeature, szComponent, pUsed);
1096 str = strdupAtoW( szDescriptor );
1097 if( szDescriptor && !str )
1098 return ERROR_OUTOFMEMORY;
1100 if (szProduct)
1101 p = product;
1102 if (szFeature)
1103 f = feature;
1104 if (szComponent)
1105 c = component;
1107 r = MsiDecomposeDescriptorW( str, p, f, c, pUsed );
1109 if (r == ERROR_SUCCESS)
1111 WideCharToMultiByte( CP_ACP, 0, p, -1,
1112 szProduct, MAX_FEATURE_CHARS+1, NULL, NULL );
1113 WideCharToMultiByte( CP_ACP, 0, f, -1,
1114 szFeature, MAX_FEATURE_CHARS+1, NULL, NULL );
1115 WideCharToMultiByte( CP_ACP, 0, c, -1,
1116 szComponent, MAX_FEATURE_CHARS+1, NULL, NULL );
1119 msi_free( str );
1121 return r;
1124 UINT WINAPI MsiEnumProductsA(DWORD index, LPSTR lpguid)
1126 DWORD r;
1127 WCHAR szwGuid[GUID_SIZE];
1129 TRACE("%d %p\n", index, lpguid);
1131 if (NULL == lpguid)
1132 return ERROR_INVALID_PARAMETER;
1133 r = MsiEnumProductsW(index, szwGuid);
1134 if( r == ERROR_SUCCESS )
1135 WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
1137 return r;
1140 UINT WINAPI MsiEnumProductsW(DWORD index, LPWSTR lpguid)
1142 TRACE("%d %p\n", index, lpguid);
1144 if (NULL == lpguid)
1145 return ERROR_INVALID_PARAMETER;
1147 return MsiEnumProductsExW( NULL, szAllSid, MSIINSTALLCONTEXT_ALL, index, lpguid,
1148 NULL, NULL, NULL );
1151 UINT WINAPI MsiEnumFeaturesA(LPCSTR szProduct, DWORD index,
1152 LPSTR szFeature, LPSTR szParent)
1154 DWORD r;
1155 WCHAR szwFeature[GUID_SIZE], szwParent[GUID_SIZE];
1156 LPWSTR szwProduct = NULL;
1158 TRACE("%s %d %p %p\n", debugstr_a(szProduct), index, szFeature, szParent);
1160 if( szProduct )
1162 szwProduct = strdupAtoW( szProduct );
1163 if( !szwProduct )
1164 return ERROR_OUTOFMEMORY;
1167 r = MsiEnumFeaturesW(szwProduct, index, szwFeature, szwParent);
1168 if( r == ERROR_SUCCESS )
1170 WideCharToMultiByte(CP_ACP, 0, szwFeature, -1,
1171 szFeature, GUID_SIZE, NULL, NULL);
1172 WideCharToMultiByte(CP_ACP, 0, szwParent, -1,
1173 szParent, GUID_SIZE, NULL, NULL);
1176 msi_free( szwProduct);
1178 return r;
1181 UINT WINAPI MsiEnumFeaturesW(LPCWSTR szProduct, DWORD index,
1182 LPWSTR szFeature, LPWSTR szParent)
1184 HKEY hkeyProduct = 0;
1185 DWORD r, sz;
1187 TRACE("%s %d %p %p\n", debugstr_w(szProduct), index, szFeature, szParent);
1189 if( !szProduct )
1190 return ERROR_INVALID_PARAMETER;
1192 r = MSIREG_OpenInstallerFeaturesKey(szProduct,&hkeyProduct,FALSE);
1193 if( r != ERROR_SUCCESS )
1194 return ERROR_NO_MORE_ITEMS;
1196 sz = GUID_SIZE;
1197 r = RegEnumValueW(hkeyProduct, index, szFeature, &sz, NULL, NULL, NULL, NULL);
1198 RegCloseKey(hkeyProduct);
1200 return r;
1203 UINT WINAPI MsiEnumComponentsA(DWORD index, LPSTR lpguid)
1205 DWORD r;
1206 WCHAR szwGuid[GUID_SIZE];
1208 TRACE("%d %p\n", index, lpguid);
1210 r = MsiEnumComponentsW(index, szwGuid);
1211 if( r == ERROR_SUCCESS )
1212 WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
1214 return r;
1217 UINT WINAPI MsiEnumComponentsW(DWORD index, LPWSTR lpguid)
1219 HKEY hkey;
1220 REGSAM access = KEY_WOW64_64KEY | KEY_ALL_ACCESS;
1221 WCHAR szKeyName[SQUISH_GUID_SIZE];
1222 DWORD r;
1224 TRACE("%d %p\n", index, lpguid);
1226 r = RegCreateKeyExW(HKEY_LOCAL_MACHINE, szInstaller_Components, 0, NULL, 0, access, NULL, &hkey, NULL);
1227 if( r != ERROR_SUCCESS )
1228 return ERROR_NO_MORE_ITEMS;
1230 r = RegEnumKeyW(hkey, index, szKeyName, SQUISH_GUID_SIZE);
1231 if( r == ERROR_SUCCESS )
1232 unsquash_guid(szKeyName, lpguid);
1234 RegCloseKey(hkey);
1235 return r;
1238 UINT WINAPI MsiEnumClientsA(LPCSTR szComponent, DWORD index, LPSTR szProduct)
1240 DWORD r;
1241 WCHAR szwProduct[GUID_SIZE];
1242 LPWSTR szwComponent = NULL;
1244 TRACE("%s %d %p\n", debugstr_a(szComponent), index, szProduct);
1246 if ( !szProduct )
1247 return ERROR_INVALID_PARAMETER;
1249 if( szComponent )
1251 szwComponent = strdupAtoW( szComponent );
1252 if( !szwComponent )
1253 return ERROR_OUTOFMEMORY;
1256 r = MsiEnumClientsW(szComponent?szwComponent:NULL, index, szwProduct);
1257 if( r == ERROR_SUCCESS )
1259 WideCharToMultiByte(CP_ACP, 0, szwProduct, -1,
1260 szProduct, GUID_SIZE, NULL, NULL);
1263 msi_free( szwComponent);
1265 return r;
1268 UINT WINAPI MsiEnumClientsW(LPCWSTR szComponent, DWORD index, LPWSTR szProduct)
1270 HKEY hkeyComp = 0;
1271 DWORD r, sz;
1272 WCHAR szValName[SQUISH_GUID_SIZE];
1274 TRACE("%s %d %p\n", debugstr_w(szComponent), index, szProduct);
1276 if (!szComponent || !*szComponent || !szProduct)
1277 return ERROR_INVALID_PARAMETER;
1279 if (MSIREG_OpenUserDataComponentKey(szComponent, NULL, &hkeyComp, FALSE) != ERROR_SUCCESS &&
1280 MSIREG_OpenUserDataComponentKey(szComponent, szLocalSid, &hkeyComp, FALSE) != ERROR_SUCCESS)
1281 return ERROR_UNKNOWN_COMPONENT;
1283 /* see if there are any products at all */
1284 sz = SQUISH_GUID_SIZE;
1285 r = RegEnumValueW(hkeyComp, 0, szValName, &sz, NULL, NULL, NULL, NULL);
1286 if (r != ERROR_SUCCESS)
1288 RegCloseKey(hkeyComp);
1290 if (index != 0)
1291 return ERROR_INVALID_PARAMETER;
1293 return ERROR_UNKNOWN_COMPONENT;
1296 sz = SQUISH_GUID_SIZE;
1297 r = RegEnumValueW(hkeyComp, index, szValName, &sz, NULL, NULL, NULL, NULL);
1298 if( r == ERROR_SUCCESS )
1300 unsquash_guid(szValName, szProduct);
1301 TRACE("-> %s\n", debugstr_w(szProduct));
1303 RegCloseKey(hkeyComp);
1304 return r;
1307 static UINT MSI_EnumComponentQualifiers( LPCWSTR szComponent, DWORD iIndex,
1308 awstring *lpQualBuf, LPDWORD pcchQual,
1309 awstring *lpAppBuf, LPDWORD pcchAppBuf )
1311 DWORD name_sz, val_sz, name_max, val_max, type, ofs;
1312 LPWSTR name = NULL, val = NULL;
1313 UINT r, r2;
1314 HKEY key;
1316 TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
1317 lpQualBuf, pcchQual, lpAppBuf, pcchAppBuf);
1319 if (!szComponent)
1320 return ERROR_INVALID_PARAMETER;
1322 r = MSIREG_OpenUserComponentsKey( szComponent, &key, FALSE );
1323 if (r != ERROR_SUCCESS)
1324 return ERROR_UNKNOWN_COMPONENT;
1326 /* figure out how big the name is we want to return */
1327 name_max = 0x10;
1328 r = ERROR_OUTOFMEMORY;
1329 name = msi_alloc( name_max * sizeof(WCHAR) );
1330 if (!name)
1331 goto end;
1333 val_max = 0x10;
1334 r = ERROR_OUTOFMEMORY;
1335 val = msi_alloc( val_max );
1336 if (!val)
1337 goto end;
1339 /* loop until we allocate enough memory */
1340 while (1)
1342 name_sz = name_max;
1343 val_sz = val_max;
1344 r = RegEnumValueW( key, iIndex, name, &name_sz,
1345 NULL, &type, (LPBYTE)val, &val_sz );
1346 if (r == ERROR_SUCCESS)
1347 break;
1348 if (r != ERROR_MORE_DATA)
1349 goto end;
1351 if (type != REG_MULTI_SZ)
1353 ERR("component data has wrong type (%d)\n", type);
1354 goto end;
1357 r = ERROR_OUTOFMEMORY;
1358 if ((name_sz+1) >= name_max)
1360 name_max *= 2;
1361 msi_free( name );
1362 name = msi_alloc( name_max * sizeof (WCHAR) );
1363 if (!name)
1364 goto end;
1365 continue;
1367 if (val_sz > val_max)
1369 val_max = val_sz + sizeof (WCHAR);
1370 msi_free( val );
1371 val = msi_alloc( val_max * sizeof (WCHAR) );
1372 if (!val)
1373 goto end;
1374 continue;
1376 ERR("should be enough data, but isn't %d %d\n", name_sz, val_sz );
1377 goto end;
1380 ofs = 0;
1381 r = MsiDecomposeDescriptorW( val, NULL, NULL, NULL, &ofs );
1382 if (r != ERROR_SUCCESS)
1383 goto end;
1385 TRACE("Providing %s and %s\n", debugstr_w(name), debugstr_w(val+ofs));
1387 r = msi_strcpy_to_awstring( name, lpQualBuf, pcchQual );
1388 r2 = msi_strcpy_to_awstring( val+ofs, lpAppBuf, pcchAppBuf );
1390 if (r2 != ERROR_SUCCESS)
1391 r = r2;
1393 end:
1394 msi_free(val);
1395 msi_free(name);
1396 RegCloseKey(key);
1398 return r;
1401 /*************************************************************************
1402 * MsiEnumComponentQualifiersA [MSI.@]
1404 UINT WINAPI MsiEnumComponentQualifiersA( LPCSTR szComponent, DWORD iIndex,
1405 LPSTR lpQualifierBuf, LPDWORD pcchQualifierBuf,
1406 LPSTR lpApplicationDataBuf, LPDWORD pcchApplicationDataBuf )
1408 awstring qual, appdata;
1409 LPWSTR comp;
1410 UINT r;
1412 TRACE("%s %08x %p %p %p %p\n", debugstr_a(szComponent), iIndex,
1413 lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
1414 pcchApplicationDataBuf);
1416 comp = strdupAtoW( szComponent );
1417 if (szComponent && !comp)
1418 return ERROR_OUTOFMEMORY;
1420 qual.unicode = FALSE;
1421 qual.str.a = lpQualifierBuf;
1423 appdata.unicode = FALSE;
1424 appdata.str.a = lpApplicationDataBuf;
1426 r = MSI_EnumComponentQualifiers( comp, iIndex,
1427 &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
1428 msi_free( comp );
1429 return r;
1432 /*************************************************************************
1433 * MsiEnumComponentQualifiersW [MSI.@]
1435 UINT WINAPI MsiEnumComponentQualifiersW( LPCWSTR szComponent, DWORD iIndex,
1436 LPWSTR lpQualifierBuf, LPDWORD pcchQualifierBuf,
1437 LPWSTR lpApplicationDataBuf, LPDWORD pcchApplicationDataBuf )
1439 awstring qual, appdata;
1441 TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
1442 lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
1443 pcchApplicationDataBuf);
1445 qual.unicode = TRUE;
1446 qual.str.w = lpQualifierBuf;
1448 appdata.unicode = TRUE;
1449 appdata.str.w = lpApplicationDataBuf;
1451 return MSI_EnumComponentQualifiers( szComponent, iIndex,
1452 &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
1455 /*************************************************************************
1456 * MsiEnumRelatedProductsW [MSI.@]
1459 UINT WINAPI MsiEnumRelatedProductsW(LPCWSTR szUpgradeCode, DWORD dwReserved,
1460 DWORD iProductIndex, LPWSTR lpProductBuf)
1462 UINT r;
1463 HKEY hkey;
1464 DWORD dwSize = SQUISH_GUID_SIZE;
1465 WCHAR szKeyName[SQUISH_GUID_SIZE];
1467 TRACE("%s %u %u %p\n", debugstr_w(szUpgradeCode), dwReserved,
1468 iProductIndex, lpProductBuf);
1470 if (NULL == szUpgradeCode)
1471 return ERROR_INVALID_PARAMETER;
1472 if (NULL == lpProductBuf)
1473 return ERROR_INVALID_PARAMETER;
1475 r = MSIREG_OpenUpgradeCodesKey(szUpgradeCode, &hkey, FALSE);
1476 if (r != ERROR_SUCCESS)
1477 return ERROR_NO_MORE_ITEMS;
1479 r = RegEnumValueW(hkey, iProductIndex, szKeyName, &dwSize, NULL, NULL, NULL, NULL);
1480 if( r == ERROR_SUCCESS )
1481 unsquash_guid(szKeyName, lpProductBuf);
1482 RegCloseKey(hkey);
1484 return r;
1487 /*************************************************************************
1488 * MsiEnumRelatedProductsA [MSI.@]
1491 UINT WINAPI MsiEnumRelatedProductsA(LPCSTR szUpgradeCode, DWORD dwReserved,
1492 DWORD iProductIndex, LPSTR lpProductBuf)
1494 LPWSTR szwUpgradeCode = NULL;
1495 WCHAR productW[GUID_SIZE];
1496 UINT r;
1498 TRACE("%s %u %u %p\n", debugstr_a(szUpgradeCode), dwReserved,
1499 iProductIndex, lpProductBuf);
1501 if (szUpgradeCode)
1503 szwUpgradeCode = strdupAtoW( szUpgradeCode );
1504 if( !szwUpgradeCode )
1505 return ERROR_OUTOFMEMORY;
1508 r = MsiEnumRelatedProductsW( szwUpgradeCode, dwReserved,
1509 iProductIndex, productW );
1510 if (r == ERROR_SUCCESS)
1512 WideCharToMultiByte( CP_ACP, 0, productW, GUID_SIZE,
1513 lpProductBuf, GUID_SIZE, NULL, NULL );
1515 msi_free( szwUpgradeCode);
1516 return r;
1519 /***********************************************************************
1520 * MsiEnumPatchesExA [MSI.@]
1522 UINT WINAPI MsiEnumPatchesExA(LPCSTR szProductCode, LPCSTR szUserSid,
1523 DWORD dwContext, DWORD dwFilter, DWORD dwIndex, LPSTR szPatchCode,
1524 LPSTR szTargetProductCode, MSIINSTALLCONTEXT *pdwTargetProductContext,
1525 LPSTR szTargetUserSid, LPDWORD pcchTargetUserSid)
1527 LPWSTR prodcode = NULL;
1528 LPWSTR usersid = NULL;
1529 LPWSTR targsid = NULL;
1530 WCHAR patch[GUID_SIZE];
1531 WCHAR targprod[GUID_SIZE];
1532 DWORD len;
1533 UINT r;
1535 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n",
1536 debugstr_a(szProductCode), debugstr_a(szUserSid), dwContext, dwFilter,
1537 dwIndex, szPatchCode, szTargetProductCode, pdwTargetProductContext,
1538 szTargetUserSid, pcchTargetUserSid);
1540 if (szTargetUserSid && !pcchTargetUserSid)
1541 return ERROR_INVALID_PARAMETER;
1543 if (szProductCode) prodcode = strdupAtoW(szProductCode);
1544 if (szUserSid) usersid = strdupAtoW(szUserSid);
1546 r = MsiEnumPatchesExW(prodcode, usersid, dwContext, dwFilter, dwIndex,
1547 patch, targprod, pdwTargetProductContext,
1548 NULL, &len);
1549 if (r != ERROR_SUCCESS)
1550 goto done;
1552 WideCharToMultiByte(CP_ACP, 0, patch, -1, szPatchCode,
1553 GUID_SIZE, NULL, NULL);
1554 WideCharToMultiByte(CP_ACP, 0, targprod, -1, szTargetProductCode,
1555 GUID_SIZE, NULL, NULL);
1557 if (!szTargetUserSid)
1559 if (pcchTargetUserSid)
1560 *pcchTargetUserSid = len;
1562 goto done;
1565 targsid = msi_alloc(++len * sizeof(WCHAR));
1566 if (!targsid)
1568 r = ERROR_OUTOFMEMORY;
1569 goto done;
1572 r = MsiEnumPatchesExW(prodcode, usersid, dwContext, dwFilter, dwIndex,
1573 patch, targprod, pdwTargetProductContext,
1574 targsid, &len);
1575 if (r != ERROR_SUCCESS || !szTargetUserSid)
1576 goto done;
1578 WideCharToMultiByte(CP_ACP, 0, targsid, -1, szTargetUserSid,
1579 *pcchTargetUserSid, NULL, NULL);
1581 len = lstrlenW(targsid);
1582 if (*pcchTargetUserSid < len + 1)
1584 r = ERROR_MORE_DATA;
1585 *pcchTargetUserSid = len * sizeof(WCHAR);
1587 else
1588 *pcchTargetUserSid = len;
1590 done:
1591 msi_free(prodcode);
1592 msi_free(usersid);
1593 msi_free(targsid);
1595 return r;
1598 static UINT msi_get_patch_state(LPCWSTR prodcode, LPCWSTR usersid,
1599 MSIINSTALLCONTEXT context,
1600 LPWSTR patch, MSIPATCHSTATE *state)
1602 DWORD type, val, size;
1603 HKEY prod, hkey = 0;
1604 HKEY udpatch = 0;
1605 LONG res;
1606 UINT r = ERROR_NO_MORE_ITEMS;
1608 *state = MSIPATCHSTATE_INVALID;
1610 r = MSIREG_OpenUserDataProductKey(prodcode, context,
1611 usersid, &prod, FALSE);
1612 if (r != ERROR_SUCCESS)
1613 return ERROR_NO_MORE_ITEMS;
1615 res = RegOpenKeyExW(prod, szPatches, 0, KEY_READ, &hkey);
1616 if (res != ERROR_SUCCESS)
1617 goto done;
1619 res = RegOpenKeyExW(hkey, patch, 0, KEY_READ, &udpatch);
1620 if (res != ERROR_SUCCESS)
1621 goto done;
1623 size = sizeof(DWORD);
1624 res = RegGetValueW(udpatch, NULL, szState, RRF_RT_DWORD, &type, &val, &size);
1625 if (res != ERROR_SUCCESS ||
1626 val < MSIPATCHSTATE_APPLIED || val > MSIPATCHSTATE_REGISTERED)
1628 r = ERROR_BAD_CONFIGURATION;
1629 goto done;
1632 *state = val;
1633 r = ERROR_SUCCESS;
1635 done:
1636 RegCloseKey(udpatch);
1637 RegCloseKey(hkey);
1638 RegCloseKey(prod);
1640 return r;
1643 static UINT msi_check_product_patches(LPCWSTR prodcode, LPCWSTR usersid,
1644 MSIINSTALLCONTEXT context, DWORD filter, DWORD index, DWORD *idx,
1645 LPWSTR patch, LPWSTR targetprod, MSIINSTALLCONTEXT *targetctx,
1646 LPWSTR targetsid, DWORD *sidsize, LPWSTR *transforms)
1648 MSIPATCHSTATE state = MSIPATCHSTATE_INVALID;
1649 LPWSTR ptr, patches = NULL;
1650 HKEY prod, patchkey = 0;
1651 HKEY localprod = 0, localpatch = 0;
1652 DWORD type, size;
1653 LONG res;
1654 UINT temp, r = ERROR_NO_MORE_ITEMS;
1656 if (MSIREG_OpenProductKey(prodcode, usersid, context,
1657 &prod, FALSE) != ERROR_SUCCESS)
1658 return ERROR_NO_MORE_ITEMS;
1660 size = 0;
1661 res = RegGetValueW(prod, szPatches, szPatches, RRF_RT_ANY, &type, NULL,
1662 &size);
1663 if (res != ERROR_SUCCESS)
1664 goto done;
1666 if (type != REG_MULTI_SZ)
1668 r = ERROR_BAD_CONFIGURATION;
1669 goto done;
1672 patches = msi_alloc(size);
1673 if (!patches)
1675 r = ERROR_OUTOFMEMORY;
1676 goto done;
1679 res = RegGetValueW(prod, szPatches, szPatches, RRF_RT_ANY, &type,
1680 patches, &size);
1681 if (res != ERROR_SUCCESS)
1682 goto done;
1684 for (ptr = patches; *ptr && r == ERROR_NO_MORE_ITEMS; ptr += lstrlenW(ptr) + 1)
1686 if (!unsquash_guid(ptr, patch))
1688 r = ERROR_BAD_CONFIGURATION;
1689 goto done;
1692 size = 0;
1693 res = RegGetValueW(prod, szPatches, ptr, RRF_RT_REG_SZ,
1694 &type, NULL, &size);
1695 if (res != ERROR_SUCCESS)
1696 continue;
1698 if (transforms)
1700 *transforms = msi_alloc(size);
1701 if (!*transforms)
1703 r = ERROR_OUTOFMEMORY;
1704 goto done;
1707 res = RegGetValueW(prod, szPatches, ptr, RRF_RT_REG_SZ,
1708 &type, *transforms, &size);
1709 if (res != ERROR_SUCCESS)
1710 continue;
1713 if (context == MSIINSTALLCONTEXT_USERMANAGED)
1715 if (!(filter & MSIPATCHSTATE_APPLIED))
1717 temp = msi_get_patch_state(prodcode, usersid, context,
1718 ptr, &state);
1719 if (temp == ERROR_BAD_CONFIGURATION)
1721 r = ERROR_BAD_CONFIGURATION;
1722 goto done;
1725 if (temp != ERROR_SUCCESS || !(filter & state))
1726 continue;
1729 else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
1731 if (!(filter & MSIPATCHSTATE_APPLIED))
1733 temp = msi_get_patch_state(prodcode, usersid, context,
1734 ptr, &state);
1735 if (temp == ERROR_BAD_CONFIGURATION)
1737 r = ERROR_BAD_CONFIGURATION;
1738 goto done;
1741 if (temp != ERROR_SUCCESS || !(filter & state))
1742 continue;
1744 else
1746 temp = MSIREG_OpenUserDataPatchKey(patch, context,
1747 &patchkey, FALSE);
1748 RegCloseKey(patchkey);
1749 if (temp != ERROR_SUCCESS)
1750 continue;
1753 else if (context == MSIINSTALLCONTEXT_MACHINE)
1755 usersid = szEmpty;
1757 if (MSIREG_OpenUserDataProductKey(prodcode, context, NULL, &localprod, FALSE) == ERROR_SUCCESS &&
1758 RegOpenKeyExW(localprod, szPatches, 0, KEY_READ, &localpatch) == ERROR_SUCCESS &&
1759 RegOpenKeyExW(localpatch, ptr, 0, KEY_READ, &patchkey) == ERROR_SUCCESS)
1761 res = RegGetValueW(patchkey, NULL, szState, RRF_RT_REG_DWORD,
1762 &type, &state, &size);
1764 if (!(filter & state))
1765 res = ERROR_NO_MORE_ITEMS;
1767 RegCloseKey(patchkey);
1770 RegCloseKey(localpatch);
1771 RegCloseKey(localprod);
1773 if (res != ERROR_SUCCESS)
1774 continue;
1777 if (*idx < index)
1779 (*idx)++;
1780 continue;
1783 r = ERROR_SUCCESS;
1784 if (targetprod)
1785 lstrcpyW(targetprod, prodcode);
1787 if (targetctx)
1788 *targetctx = context;
1790 if (targetsid)
1792 lstrcpynW(targetsid, usersid, *sidsize);
1793 if (lstrlenW(usersid) >= *sidsize)
1794 r = ERROR_MORE_DATA;
1797 if (sidsize)
1799 *sidsize = lstrlenW(usersid);
1800 if (!targetsid)
1801 *sidsize *= sizeof(WCHAR);
1805 done:
1806 RegCloseKey(prod);
1807 msi_free(patches);
1809 return r;
1812 static UINT msi_enum_patches(LPCWSTR szProductCode, LPCWSTR szUserSid,
1813 DWORD dwContext, DWORD dwFilter, DWORD dwIndex, DWORD *idx,
1814 LPWSTR szPatchCode, LPWSTR szTargetProductCode,
1815 MSIINSTALLCONTEXT *pdwTargetProductContext, LPWSTR szTargetUserSid,
1816 LPDWORD pcchTargetUserSid, LPWSTR *szTransforms)
1818 LPWSTR usersid = NULL;
1819 UINT r = ERROR_INVALID_PARAMETER;
1821 if (!szUserSid)
1823 szUserSid = usersid = get_user_sid();
1824 if (!usersid) return ERROR_FUNCTION_FAILED;
1827 if (dwContext & MSIINSTALLCONTEXT_USERMANAGED)
1829 r = msi_check_product_patches(szProductCode, szUserSid,
1830 MSIINSTALLCONTEXT_USERMANAGED, dwFilter,
1831 dwIndex, idx, szPatchCode,
1832 szTargetProductCode,
1833 pdwTargetProductContext, szTargetUserSid,
1834 pcchTargetUserSid, szTransforms);
1835 if (r != ERROR_NO_MORE_ITEMS)
1836 goto done;
1839 if (dwContext & MSIINSTALLCONTEXT_USERUNMANAGED)
1841 r = msi_check_product_patches(szProductCode, szUserSid,
1842 MSIINSTALLCONTEXT_USERUNMANAGED, dwFilter,
1843 dwIndex, idx, szPatchCode,
1844 szTargetProductCode,
1845 pdwTargetProductContext, szTargetUserSid,
1846 pcchTargetUserSid, szTransforms);
1847 if (r != ERROR_NO_MORE_ITEMS)
1848 goto done;
1851 if (dwContext & MSIINSTALLCONTEXT_MACHINE)
1853 r = msi_check_product_patches(szProductCode, szUserSid,
1854 MSIINSTALLCONTEXT_MACHINE, dwFilter,
1855 dwIndex, idx, szPatchCode,
1856 szTargetProductCode,
1857 pdwTargetProductContext, szTargetUserSid,
1858 pcchTargetUserSid, szTransforms);
1859 if (r != ERROR_NO_MORE_ITEMS)
1860 goto done;
1863 done:
1864 LocalFree(usersid);
1865 return r;
1868 /***********************************************************************
1869 * MsiEnumPatchesExW [MSI.@]
1871 UINT WINAPI MsiEnumPatchesExW(LPCWSTR szProductCode, LPCWSTR szUserSid,
1872 DWORD dwContext, DWORD dwFilter, DWORD dwIndex, LPWSTR szPatchCode,
1873 LPWSTR szTargetProductCode, MSIINSTALLCONTEXT *pdwTargetProductContext,
1874 LPWSTR szTargetUserSid, LPDWORD pcchTargetUserSid)
1876 WCHAR squished_pc[GUID_SIZE];
1877 DWORD idx = 0;
1878 UINT r;
1880 static DWORD last_index;
1882 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n",
1883 debugstr_w(szProductCode), debugstr_w(szUserSid), dwContext, dwFilter,
1884 dwIndex, szPatchCode, szTargetProductCode, pdwTargetProductContext,
1885 szTargetUserSid, pcchTargetUserSid);
1887 if (!szProductCode || !squash_guid(szProductCode, squished_pc))
1888 return ERROR_INVALID_PARAMETER;
1890 if (szUserSid && !strcmpW( szUserSid, szLocalSid ))
1891 return ERROR_INVALID_PARAMETER;
1893 if (dwContext & MSIINSTALLCONTEXT_MACHINE && szUserSid)
1894 return ERROR_INVALID_PARAMETER;
1896 if (dwContext <= MSIINSTALLCONTEXT_NONE ||
1897 dwContext > MSIINSTALLCONTEXT_ALL)
1898 return ERROR_INVALID_PARAMETER;
1900 if (dwFilter <= MSIPATCHSTATE_INVALID || dwFilter > MSIPATCHSTATE_ALL)
1901 return ERROR_INVALID_PARAMETER;
1903 if (dwIndex && dwIndex - last_index != 1)
1904 return ERROR_INVALID_PARAMETER;
1906 if (dwIndex == 0)
1907 last_index = 0;
1909 r = msi_enum_patches(szProductCode, szUserSid, dwContext, dwFilter,
1910 dwIndex, &idx, szPatchCode, szTargetProductCode,
1911 pdwTargetProductContext, szTargetUserSid,
1912 pcchTargetUserSid, NULL);
1914 if (r == ERROR_SUCCESS)
1915 last_index = dwIndex;
1916 else
1917 last_index = 0;
1919 return r;
1922 /***********************************************************************
1923 * MsiEnumPatchesA [MSI.@]
1925 UINT WINAPI MsiEnumPatchesA(LPCSTR szProduct, DWORD iPatchIndex,
1926 LPSTR lpPatchBuf, LPSTR lpTransformsBuf, LPDWORD pcchTransformsBuf)
1928 LPWSTR product, transforms;
1929 WCHAR patch[GUID_SIZE];
1930 DWORD len;
1931 UINT r;
1933 TRACE("(%s %d %p %p %p)\n", debugstr_a(szProduct), iPatchIndex,
1934 lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);
1936 if (!szProduct || !lpPatchBuf || !lpTransformsBuf || !pcchTransformsBuf)
1937 return ERROR_INVALID_PARAMETER;
1939 product = strdupAtoW(szProduct);
1940 if (!product)
1941 return ERROR_OUTOFMEMORY;
1943 len = *pcchTransformsBuf;
1944 transforms = msi_alloc( len * sizeof(WCHAR) );
1945 if (!transforms)
1947 r = ERROR_OUTOFMEMORY;
1948 goto done;
1951 r = MsiEnumPatchesW(product, iPatchIndex, patch, transforms, &len);
1952 if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
1953 goto done;
1955 WideCharToMultiByte(CP_ACP, 0, patch, -1, lpPatchBuf,
1956 GUID_SIZE, NULL, NULL);
1958 if (!WideCharToMultiByte(CP_ACP, 0, transforms, -1, lpTransformsBuf,
1959 *pcchTransformsBuf, NULL, NULL))
1960 r = ERROR_MORE_DATA;
1962 if (r == ERROR_MORE_DATA)
1964 lpTransformsBuf[*pcchTransformsBuf - 1] = '\0';
1965 *pcchTransformsBuf = len * 2;
1967 else
1968 *pcchTransformsBuf = strlen( lpTransformsBuf );
1970 done:
1971 msi_free(transforms);
1972 msi_free(product);
1974 return r;
1977 /***********************************************************************
1978 * MsiEnumPatchesW [MSI.@]
1980 UINT WINAPI MsiEnumPatchesW(LPCWSTR szProduct, DWORD iPatchIndex,
1981 LPWSTR lpPatchBuf, LPWSTR lpTransformsBuf, LPDWORD pcchTransformsBuf)
1983 WCHAR squished_pc[GUID_SIZE];
1984 LPWSTR transforms = NULL;
1985 HKEY prod;
1986 DWORD idx = 0;
1987 UINT r;
1989 TRACE("(%s %d %p %p %p)\n", debugstr_w(szProduct), iPatchIndex,
1990 lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);
1992 if (!szProduct || !squash_guid(szProduct, squished_pc))
1993 return ERROR_INVALID_PARAMETER;
1995 if (!lpPatchBuf || !lpTransformsBuf || !pcchTransformsBuf)
1996 return ERROR_INVALID_PARAMETER;
1998 if (MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERMANAGED,
1999 &prod, FALSE) != ERROR_SUCCESS &&
2000 MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERUNMANAGED,
2001 &prod, FALSE) != ERROR_SUCCESS &&
2002 MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_MACHINE,
2003 &prod, FALSE) != ERROR_SUCCESS)
2004 return ERROR_UNKNOWN_PRODUCT;
2006 RegCloseKey(prod);
2008 r = msi_enum_patches(szProduct, NULL, MSIINSTALLCONTEXT_ALL,
2009 MSIPATCHSTATE_ALL, iPatchIndex, &idx, lpPatchBuf,
2010 NULL, NULL, NULL, NULL, &transforms);
2011 if (r != ERROR_SUCCESS)
2012 goto done;
2014 lstrcpynW(lpTransformsBuf, transforms, *pcchTransformsBuf);
2015 if (*pcchTransformsBuf <= lstrlenW(transforms))
2017 r = ERROR_MORE_DATA;
2018 *pcchTransformsBuf = lstrlenW(transforms);
2020 else
2021 *pcchTransformsBuf = lstrlenW(transforms);
2023 done:
2024 msi_free(transforms);
2025 return r;
2028 UINT WINAPI MsiEnumProductsExA( LPCSTR product, LPCSTR usersid, DWORD ctx, DWORD index,
2029 CHAR installed_product[GUID_SIZE],
2030 MSIINSTALLCONTEXT *installed_ctx, LPSTR sid, LPDWORD sid_len )
2032 UINT r;
2033 WCHAR installed_productW[GUID_SIZE], *productW = NULL, *usersidW = NULL, *sidW = NULL;
2035 TRACE("%s, %s, %u, %u, %p, %p, %p, %p\n", debugstr_a(product), debugstr_a(usersid),
2036 ctx, index, installed_product, installed_ctx, sid, sid_len);
2038 if (sid && !sid_len) return ERROR_INVALID_PARAMETER;
2039 if (product && !(productW = strdupAtoW( product ))) return ERROR_OUTOFMEMORY;
2040 if (usersid && !(usersidW = strdupAtoW( usersid )))
2042 msi_free( productW );
2043 return ERROR_OUTOFMEMORY;
2045 if (sid && !(sidW = msi_alloc( *sid_len * sizeof(WCHAR) )))
2047 msi_free( usersidW );
2048 msi_free( productW );
2049 return ERROR_OUTOFMEMORY;
2051 r = MsiEnumProductsExW( productW, usersidW, ctx, index, installed_productW,
2052 installed_ctx, sidW, sid_len );
2053 if (r == ERROR_SUCCESS)
2055 if (installed_product) WideCharToMultiByte( CP_ACP, 0, installed_productW, GUID_SIZE,
2056 installed_product, GUID_SIZE, NULL, NULL );
2057 if (sid) WideCharToMultiByte( CP_ACP, 0, sidW, *sid_len + 1, sid, *sid_len + 1, NULL, NULL );
2059 msi_free( productW );
2060 msi_free( usersidW );
2061 msi_free( sidW );
2062 return r;
2065 static UINT fetch_machine_product( const WCHAR *match, DWORD index, DWORD *idx,
2066 WCHAR installed_product[GUID_SIZE],
2067 MSIINSTALLCONTEXT *installed_ctx, WCHAR *sid, DWORD *sid_len )
2069 static const WCHAR productsW[] =
2070 {'S','o','f','t','w','a','r','e','\\','C','l','a','s','s','e','s','\\',
2071 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
2072 UINT r;
2073 WCHAR product[GUID_SIZE];
2074 DWORD i = 0, len;
2075 REGSAM access = KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY;
2076 HKEY key;
2078 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, productsW, 0, access, &key ))
2079 return ERROR_NO_MORE_ITEMS;
2081 len = sizeof(product)/sizeof(product[0]);
2082 while (!RegEnumKeyExW( key, i, product, &len, NULL, NULL, NULL, NULL ))
2084 if (match && strcmpW( match, product ))
2086 i++;
2087 len = sizeof(product)/sizeof(product[0]);
2088 continue;
2090 if (*idx == index) goto found;
2091 (*idx)++;
2092 len = sizeof(product)/sizeof(product[0]);
2093 i++;
2095 RegCloseKey( key );
2096 return ERROR_NO_MORE_ITEMS;
2098 found:
2099 if (sid_len && *sid_len < 1)
2101 *sid_len = 1;
2102 r = ERROR_MORE_DATA;
2104 else
2106 if (installed_product) unsquash_guid( product, installed_product );
2107 if (installed_ctx) *installed_ctx = MSIINSTALLCONTEXT_MACHINE;
2108 if (sid)
2110 sid[0] = 0;
2111 *sid_len = 0;
2113 r = ERROR_SUCCESS;
2115 RegCloseKey( key );
2116 return r;
2119 static UINT fetch_user_product( const WCHAR *match, const WCHAR *usersid, DWORD ctx, DWORD index,
2120 DWORD *idx, WCHAR installed_product[GUID_SIZE],
2121 MSIINSTALLCONTEXT *installed_ctx, WCHAR *sid, DWORD *sid_len )
2123 static const WCHAR managedW[] =
2124 {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
2125 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s',
2126 'i','o','n','\\','I','n','s','t','a','l','l','e','r','\\','M','a','n','a','g','e','d',0};
2127 static const WCHAR managed_productsW[] =
2128 {'\\','I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
2129 static const WCHAR unmanaged_productsW[] =
2130 {'\\','S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
2131 'I','n','s','t','a','l','l','e','r','\\','P','r','o','d','u','c','t','s',0};
2132 UINT r;
2133 const WCHAR *subkey;
2134 WCHAR path[MAX_PATH], product[GUID_SIZE], user[128];
2135 DWORD i = 0, j = 0, len_product, len_user;
2136 REGSAM access = KEY_ENUMERATE_SUB_KEYS | KEY_WOW64_64KEY;
2137 HKEY key_users, key_products;
2139 if (ctx == MSIINSTALLCONTEXT_USERMANAGED)
2141 subkey = managed_productsW;
2142 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, managedW, 0, access, &key_users ))
2143 return ERROR_NO_MORE_ITEMS;
2145 else if (ctx == MSIINSTALLCONTEXT_USERUNMANAGED)
2147 subkey = unmanaged_productsW;
2148 if (RegOpenKeyExW( HKEY_USERS, NULL, 0, access, &key_users ))
2149 return ERROR_NO_MORE_ITEMS;
2151 else return ERROR_INVALID_PARAMETER;
2153 len_user = sizeof(user)/sizeof(user[0]);
2154 while (!RegEnumKeyExW( key_users, i, user, &len_user, NULL, NULL, NULL, NULL ))
2156 if (strcmpW( usersid, user ) && strcmpW( usersid, szAllSid ))
2158 i++;
2159 len_user = sizeof(user)/sizeof(user[0]);
2160 continue;
2162 strcpyW( path, user );
2163 strcatW( path, subkey );
2164 if ((r = RegOpenKeyExW( key_users, path, 0, access, &key_products )))
2166 i++;
2167 len_user = sizeof(user)/sizeof(user[0]);
2168 continue;
2170 len_product = sizeof(product)/sizeof(product[0]);
2171 while (!RegEnumKeyExW( key_products, j, product, &len_product, NULL, NULL, NULL, NULL ))
2173 if (match && strcmpW( match, product ))
2175 j++;
2176 len_product = sizeof(product)/sizeof(product[0]);
2177 continue;
2179 if (*idx == index) goto found;
2180 (*idx)++;
2181 len_product = sizeof(product)/sizeof(product[0]);
2182 j++;
2184 RegCloseKey( key_products );
2185 len_user = sizeof(user)/sizeof(user[0]);
2186 i++;
2188 RegCloseKey( key_users );
2189 return ERROR_NO_MORE_ITEMS;
2191 found:
2192 if (sid_len && *sid_len <= len_user)
2194 *sid_len = len_user;
2195 r = ERROR_MORE_DATA;
2197 else
2199 if (installed_product) unsquash_guid( product, installed_product );
2200 if (installed_ctx) *installed_ctx = ctx;
2201 if (sid)
2203 strcpyW( sid, user );
2204 *sid_len = len_user;
2206 r = ERROR_SUCCESS;
2208 RegCloseKey( key_products );
2209 RegCloseKey( key_users );
2210 return r;
2213 static UINT enum_products( const WCHAR *product, const WCHAR *usersid, DWORD ctx, DWORD index,
2214 DWORD *idx, WCHAR installed_product[GUID_SIZE],
2215 MSIINSTALLCONTEXT *installed_ctx, WCHAR *sid, DWORD *sid_len )
2217 UINT r = ERROR_NO_MORE_ITEMS;
2218 WCHAR *user = NULL;
2220 if (!usersid)
2222 usersid = user = get_user_sid();
2223 if (!user) return ERROR_FUNCTION_FAILED;
2225 if (ctx & MSIINSTALLCONTEXT_MACHINE)
2227 r = fetch_machine_product( product, index, idx, installed_product, installed_ctx,
2228 sid, sid_len );
2229 if (r != ERROR_NO_MORE_ITEMS) goto done;
2231 if (ctx & MSIINSTALLCONTEXT_USERUNMANAGED)
2233 r = fetch_user_product( product, usersid, MSIINSTALLCONTEXT_USERUNMANAGED, index,
2234 idx, installed_product, installed_ctx, sid, sid_len );
2235 if (r != ERROR_NO_MORE_ITEMS) goto done;
2237 if (ctx & MSIINSTALLCONTEXT_USERMANAGED)
2239 r = fetch_user_product( product, usersid, MSIINSTALLCONTEXT_USERMANAGED, index,
2240 idx, installed_product, installed_ctx, sid, sid_len );
2241 if (r != ERROR_NO_MORE_ITEMS) goto done;
2244 done:
2245 LocalFree( user );
2246 return r;
2249 UINT WINAPI MsiEnumProductsExW( LPCWSTR product, LPCWSTR usersid, DWORD ctx, DWORD index,
2250 WCHAR installed_product[GUID_SIZE],
2251 MSIINSTALLCONTEXT *installed_ctx, LPWSTR sid, LPDWORD sid_len )
2253 UINT r;
2254 DWORD idx = 0;
2255 static DWORD last_index;
2257 TRACE("%s, %s, %u, %u, %p, %p, %p, %p\n", debugstr_w(product), debugstr_w(usersid),
2258 ctx, index, installed_product, installed_ctx, sid, sid_len);
2260 if ((sid && !sid_len) || !ctx || (usersid && ctx == MSIINSTALLCONTEXT_MACHINE))
2261 return ERROR_INVALID_PARAMETER;
2263 if (index && index - last_index != 1)
2264 return ERROR_INVALID_PARAMETER;
2266 if (!index) last_index = 0;
2268 r = enum_products( product, usersid, ctx, index, &idx, installed_product, installed_ctx,
2269 sid, sid_len );
2270 if (r == ERROR_SUCCESS)
2271 last_index = index;
2272 else
2273 last_index = 0;
2275 return r;