configure: Changes from running autconf after previous patch.
[wine/hacks.git] / dlls / msi / registry.c
blob518303f5da04f3818ac759f1dfcaa640815e8541
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);
44 /*
45 * This module will be all the helper functions for registry access by the
46 * installer bits.
48 static const WCHAR szUserFeatures_fmt[] = {
49 'S','o','f','t','w','a','r','e','\\',
50 'M','i','c','r','o','s','o','f','t','\\',
51 'I','n','s','t','a','l','l','e','r','\\',
52 'F','e','a','t','u','r','e','s','\\',
53 '%','s',0};
55 static const WCHAR szUserDataFeatures_fmt[] = {
56 'S','o','f','t','w','a','r','e','\\',
57 'M','i','c','r','o','s','o','f','t','\\',
58 'W','i','n','d','o','w','s','\\',
59 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
60 'I','n','s','t','a','l','l','e','r','\\',
61 'U','s','e','r','D','a','t','a','\\',
62 '%','s','\\','P','r','o','d','u','c','t','s','\\',
63 '%','s','\\','F','e','a','t','u','r','e','s',0};
65 static const WCHAR szInstaller_Features_fmt[] = {
66 'S','o','f','t','w','a','r','e','\\',
67 'M','i','c','r','o','s','o','f','t','\\',
68 'W','i','n','d','o','w','s','\\',
69 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
70 'I','n','s','t','a','l','l','e','r','\\',
71 'F','e','a','t','u','r','e','s','\\',
72 '%','s',0};
74 static const WCHAR szInstaller_Components[] = {
75 'S','o','f','t','w','a','r','e','\\',
76 'M','i','c','r','o','s','o','f','t','\\',
77 'W','i','n','d','o','w','s','\\',
78 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
79 'I','n','s','t','a','l','l','e','r','\\',
80 'C','o','m','p','o','n','e','n','t','s',0 };
82 static const WCHAR szUser_Components_fmt[] = {
83 'S','o','f','t','w','a','r','e','\\',
84 'M','i','c','r','o','s','o','f','t','\\',
85 'I','n','s','t','a','l','l','e','r','\\',
86 'C','o','m','p','o','n','e','n','t','s','\\',
87 '%','s',0};
89 static const WCHAR szUserDataComp_fmt[] = {
90 'S','o','f','t','w','a','r','e','\\',
91 'M','i','c','r','o','s','o','f','t','\\',
92 'W','i','n','d','o','w','s','\\',
93 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
94 'I','n','s','t','a','l','l','e','r','\\',
95 'U','s','e','r','D','a','t','a','\\',
96 '%','s','\\','C','o','m','p','o','n','e','n','t','s','\\','%','s',0};
98 static const WCHAR szUninstall_fmt[] = {
99 'S','o','f','t','w','a','r','e','\\',
100 'M','i','c','r','o','s','o','f','t','\\',
101 'W','i','n','d','o','w','s','\\',
102 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
103 'U','n','i','n','s','t','a','l','l','\\',
104 '%','s',0 };
106 static const WCHAR szUserProduct[] = {
107 'S','o','f','t','w','a','r','e','\\',
108 'M','i','c','r','o','s','o','f','t','\\',
109 'I','n','s','t','a','l','l','e','r','\\',
110 'P','r','o','d','u','c','t','s',0};
112 static const WCHAR szUserProduct_fmt[] = {
113 'S','o','f','t','w','a','r','e','\\',
114 'M','i','c','r','o','s','o','f','t','\\',
115 'I','n','s','t','a','l','l','e','r','\\',
116 'P','r','o','d','u','c','t','s','\\',
117 '%','s',0};
119 static const WCHAR szUserPatch_fmt[] = {
120 'S','o','f','t','w','a','r','e','\\',
121 'M','i','c','r','o','s','o','f','t','\\',
122 'I','n','s','t','a','l','l','e','r','\\',
123 'P','a','t','c','h','e','s','\\',
124 '%','s',0};
126 static const WCHAR szInstaller_Products[] = {
127 'S','o','f','t','w','a','r','e','\\',
128 'M','i','c','r','o','s','o','f','t','\\',
129 'W','i','n','d','o','w','s','\\',
130 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
131 'I','n','s','t','a','l','l','e','r','\\',
132 'P','r','o','d','u','c','t','s',0};
134 static const WCHAR szInstaller_Products_fmt[] = {
135 'S','o','f','t','w','a','r','e','\\',
136 'M','i','c','r','o','s','o','f','t','\\',
137 'W','i','n','d','o','w','s','\\',
138 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
139 'I','n','s','t','a','l','l','e','r','\\',
140 'P','r','o','d','u','c','t','s','\\',
141 '%','s',0};
143 static const WCHAR szInstaller_Patches_fmt[] = {
144 'S','o','f','t','w','a','r','e','\\',
145 'M','i','c','r','o','s','o','f','t','\\',
146 'W','i','n','d','o','w','s','\\',
147 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
148 'I','n','s','t','a','l','l','e','r','\\',
149 'P','a','t','c','h','e','s','\\',
150 '%','s',0};
152 static const WCHAR szInstaller_UpgradeCodes_fmt[] = {
153 'S','o','f','t','w','a','r','e','\\',
154 'M','i','c','r','o','s','o','f','t','\\',
155 'W','i','n','d','o','w','s','\\',
156 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
157 'I','n','s','t','a','l','l','e','r','\\',
158 'U','p','g','r','a','d','e','C','o','d','e','s','\\',
159 '%','s',0};
161 static const WCHAR szInstaller_UserUpgradeCodes_fmt[] = {
162 'S','o','f','t','w','a','r','e','\\',
163 'M','i','c','r','o','s','o','f','t','\\',
164 'I','n','s','t','a','l','l','e','r','\\',
165 'U','p','g','r','a','d','e','C','o','d','e','s','\\',
166 '%','s',0};
168 static const WCHAR szUserDataProd_fmt[] = {
169 'S','o','f','t','w','a','r','e','\\',
170 'M','i','c','r','o','s','o','f','t','\\',
171 'W','i','n','d','o','w','s','\\',
172 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
173 'I','n','s','t','a','l','l','e','r','\\',
174 'U','s','e','r','D','a','t','a','\\',
175 '%','s','\\','P','r','o','d','u','c','t','s','\\','%','s',0};
177 static const WCHAR szUserDataPatch_fmt[] = {
178 'S','o','f','t','w','a','r','e','\\',
179 'M','i','c','r','o','s','o','f','t','\\',
180 'W','i','n','d','o','w','s','\\',
181 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
182 'I','n','s','t','a','l','l','e','r','\\',
183 'U','s','e','r','D','a','t','a','\\',
184 '%','s','\\','P','a','t','c','h','e','s','\\','%','s',0};
186 static const WCHAR szUserDataProductPatches_fmt[] = {
187 'S','o','f','t','w','a','r','e','\\',
188 'M','i','c','r','o','s','o','f','t','\\',
189 'W','i','n','d','o','w','s','\\',
190 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
191 'I','n','s','t','a','l','l','e','r','\\',
192 'U','s','e','r','D','a','t','a','\\',
193 '%','s','\\','P','r','o','d','u','c','t','s','\\','%','s','\\',
194 'P','a','t','c','h','e','s',0};
196 static const WCHAR szInstallProperties_fmt[] = {
197 'S','o','f','t','w','a','r','e','\\',
198 'M','i','c','r','o','s','o','f','t','\\',
199 'W','i','n','d','o','w','s','\\',
200 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
201 'I','n','s','t','a','l','l','e','r','\\',
202 'U','s','e','r','D','a','t','a','\\',
203 '%','s','\\','P','r','o','d','u','c','t','s','\\','%','s','\\',
204 'I','n','s','t','a','l','l','P','r','o','p','e','r','t','i','e','s',0};
206 static const WCHAR szInstaller_LocalClassesProd[] = {
207 'S','o','f','t','w','a','r','e','\\',
208 'C','l','a','s','s','e','s','\\',
209 'I','n','s','t','a','l','l','e','r','\\',
210 'P','r','o','d','u','c','t','s',0};
212 static const WCHAR szInstaller_LocalClassesProd_fmt[] = {
213 'S','o','f','t','w','a','r','e','\\',
214 'C','l','a','s','s','e','s','\\',
215 'I','n','s','t','a','l','l','e','r','\\',
216 'P','r','o','d','u','c','t','s','\\','%','s',0};
218 static const WCHAR szInstaller_LocalClassesFeat_fmt[] = {
219 'S','o','f','t','w','a','r','e','\\',
220 'C','l','a','s','s','e','s','\\',
221 'I','n','s','t','a','l','l','e','r','\\',
222 'F','e','a','t','u','r','e','s','\\','%','s',0};
224 static const WCHAR szInstaller_LocalManaged_fmt[] = {
225 'S','o','f','t','w','a','r','e','\\',
226 'M','i','c','r','o','s','o','f','t','\\',
227 'W','i','n','d','o','w','s','\\',
228 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
229 'I','n','s','t','a','l','l','e','r','\\',
230 'M','a','n','a','g','e','d','\\','%','s','\\',
231 'I','n','s','t','a','l','l','e','r','\\',
232 'P','r','o','d','u','c','t','s',0};
234 static const WCHAR szInstaller_LocalManagedProd_fmt[] = {
235 'S','o','f','t','w','a','r','e','\\',
236 'M','i','c','r','o','s','o','f','t','\\',
237 'W','i','n','d','o','w','s','\\',
238 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
239 'I','n','s','t','a','l','l','e','r','\\',
240 'M','a','n','a','g','e','d','\\','%','s','\\',
241 'I','n','s','t','a','l','l','e','r','\\',
242 'P','r','o','d','u','c','t','s','\\','%','s',0};
244 static const WCHAR szInstaller_LocalManagedFeat_fmt[] = {
245 'S','o','f','t','w','a','r','e','\\',
246 'M','i','c','r','o','s','o','f','t','\\',
247 'W','i','n','d','o','w','s','\\',
248 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
249 'I','n','s','t','a','l','l','e','r','\\',
250 'M','a','n','a','g','e','d','\\','%','s','\\',
251 'I','n','s','t','a','l','l','e','r','\\',
252 'F','e','a','t','u','r','e','s','\\','%','s',0};
254 static const WCHAR szInstaller_ClassesUpgrade_fmt[] = {
255 'I','n','s','t','a','l','l','e','r','\\',
256 'U','p','g','r','a','d','e','C','o','d','e','s','\\',
257 '%','s',0};
259 BOOL unsquash_guid(LPCWSTR in, LPWSTR out)
261 DWORD i,n=0;
263 if (lstrlenW(in) != 32)
264 return FALSE;
266 out[n++]='{';
267 for(i=0; i<8; i++)
268 out[n++] = in[7-i];
269 out[n++]='-';
270 for(i=0; i<4; i++)
271 out[n++] = in[11-i];
272 out[n++]='-';
273 for(i=0; i<4; i++)
274 out[n++] = in[15-i];
275 out[n++]='-';
276 for(i=0; i<2; i++)
278 out[n++] = in[17+i*2];
279 out[n++] = in[16+i*2];
281 out[n++]='-';
282 for( ; i<8; i++)
284 out[n++] = in[17+i*2];
285 out[n++] = in[16+i*2];
287 out[n++]='}';
288 out[n]=0;
289 return TRUE;
292 BOOL squash_guid(LPCWSTR in, LPWSTR out)
294 DWORD i,n=1;
295 GUID guid;
297 out[0] = 0;
299 if (FAILED(CLSIDFromString((LPCOLESTR)in, &guid)))
300 return FALSE;
302 for(i=0; i<8; i++)
303 out[7-i] = in[n++];
304 n++;
305 for(i=0; i<4; i++)
306 out[11-i] = in[n++];
307 n++;
308 for(i=0; i<4; i++)
309 out[15-i] = in[n++];
310 n++;
311 for(i=0; i<2; i++)
313 out[17+i*2] = in[n++];
314 out[16+i*2] = in[n++];
316 n++;
317 for( ; i<8; i++)
319 out[17+i*2] = in[n++];
320 out[16+i*2] = in[n++];
322 out[32]=0;
323 return TRUE;
327 /* tables for encoding and decoding base85 */
328 static const unsigned char table_dec85[0x80] = {
329 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
330 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
331 0xff,0x00,0xff,0xff,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0xff,
332 0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0xff,0xff,0xff,0x16,0xff,0x17,
333 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
334 0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0xff,0x34,0x35,0x36,
335 0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x42,0x43,0x44,0x45,0x46,
336 0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0xff,0x53,0x54,0xff,
339 static const char table_enc85[] =
340 "!$%&'()*+,-.0123456789=?@ABCDEFGHIJKLMNO"
341 "PQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwx"
342 "yz{}~";
345 * Converts a base85 encoded guid into a GUID pointer
346 * Base85 encoded GUIDs should be 20 characters long.
348 * returns TRUE if successful, FALSE if not
350 BOOL decode_base85_guid( LPCWSTR str, GUID *guid )
352 DWORD i, val = 0, base = 1, *p;
354 if (!str)
355 return FALSE;
357 p = (DWORD*) guid;
358 for( i=0; i<20; i++ )
360 if( (i%5) == 0 )
362 val = 0;
363 base = 1;
365 val += table_dec85[str[i]] * base;
366 if( str[i] >= 0x80 )
367 return FALSE;
368 if( table_dec85[str[i]] == 0xff )
369 return FALSE;
370 if( (i%5) == 4 )
371 p[i/5] = val;
372 base *= 85;
374 return TRUE;
378 * Encodes a base85 guid given a GUID pointer
379 * Caller should provide a 21 character buffer for the encoded string.
381 * returns TRUE if successful, FALSE if not
383 BOOL encode_base85_guid( GUID *guid, LPWSTR str )
385 unsigned int x, *p, i;
387 p = (unsigned int*) guid;
388 for( i=0; i<4; i++ )
390 x = p[i];
391 *str++ = table_enc85[x%85];
392 x = x/85;
393 *str++ = table_enc85[x%85];
394 x = x/85;
395 *str++ = table_enc85[x%85];
396 x = x/85;
397 *str++ = table_enc85[x%85];
398 x = x/85;
399 *str++ = table_enc85[x%85];
401 *str = 0;
403 return TRUE;
406 DWORD msi_version_str_to_dword(LPCWSTR p)
408 DWORD major, minor = 0, build = 0, version = 0;
410 if (!p)
411 return version;
413 major = atoiW(p);
415 p = strchrW(p, '.');
416 if (p)
418 minor = atoiW(p+1);
419 p = strchrW(p+1, '.');
420 if (p)
421 build = atoiW(p+1);
424 return MAKELONG(build, MAKEWORD(minor, major));
427 LONG msi_reg_set_val_str( HKEY hkey, LPCWSTR name, LPCWSTR value )
429 DWORD len;
430 if (!value) value = szEmpty;
431 len = (lstrlenW(value) + 1) * sizeof (WCHAR);
432 return RegSetValueExW( hkey, name, 0, REG_SZ, (const BYTE *)value, len );
435 LONG msi_reg_set_val_multi_str( HKEY hkey, LPCWSTR name, LPCWSTR value )
437 LPCWSTR p = value;
438 while (*p) p += lstrlenW(p) + 1;
439 return RegSetValueExW( hkey, name, 0, REG_MULTI_SZ,
440 (const BYTE *)value, (p + 1 - value) * sizeof(WCHAR) );
443 LONG msi_reg_set_val_dword( HKEY hkey, LPCWSTR name, DWORD val )
445 return RegSetValueExW( hkey, name, 0, REG_DWORD, (LPBYTE)&val, sizeof (DWORD) );
448 LONG msi_reg_set_subkey_val( HKEY hkey, LPCWSTR path, LPCWSTR name, LPCWSTR val )
450 HKEY hsubkey = 0;
451 LONG r;
453 r = RegCreateKeyW( hkey, path, &hsubkey );
454 if (r != ERROR_SUCCESS)
455 return r;
456 r = msi_reg_set_val_str( hsubkey, name, val );
457 RegCloseKey( hsubkey );
458 return r;
461 LPWSTR msi_reg_get_val_str( HKEY hkey, LPCWSTR name )
463 DWORD len = 0;
464 LPWSTR val;
465 LONG r;
467 r = RegQueryValueExW(hkey, name, NULL, NULL, NULL, &len);
468 if (r != ERROR_SUCCESS)
469 return NULL;
471 len += sizeof (WCHAR);
472 val = msi_alloc( len );
473 if (!val)
474 return NULL;
475 val[0] = 0;
476 RegQueryValueExW(hkey, name, NULL, NULL, (LPBYTE) val, &len);
477 return val;
480 BOOL msi_reg_get_val_dword( HKEY hkey, LPCWSTR name, DWORD *val)
482 DWORD type, len = sizeof (DWORD);
483 LONG r = RegQueryValueExW(hkey, name, NULL, &type, (LPBYTE) val, &len);
484 return r == ERROR_SUCCESS && type == REG_DWORD;
487 static UINT get_user_sid(LPWSTR *usersid)
489 HANDLE token;
490 BYTE buf[1024];
491 DWORD size;
492 PTOKEN_USER user;
494 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token))
495 return ERROR_FUNCTION_FAILED;
497 size = sizeof(buf);
498 if (!GetTokenInformation(token, TokenUser, buf, size, &size)) {
499 CloseHandle(token);
500 return ERROR_FUNCTION_FAILED;
503 user = (PTOKEN_USER)buf;
504 if (!ConvertSidToStringSidW(user->User.Sid, usersid)) {
505 CloseHandle(token);
506 return ERROR_FUNCTION_FAILED;
508 CloseHandle(token);
509 return ERROR_SUCCESS;
512 UINT MSIREG_OpenUninstallKey(LPCWSTR szProduct, HKEY* key, BOOL create)
514 UINT rc;
515 WCHAR keypath[0x200];
516 TRACE("%s\n",debugstr_w(szProduct));
518 sprintfW(keypath,szUninstall_fmt,szProduct);
520 if (create)
521 rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
522 else
523 rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
525 return rc;
528 UINT MSIREG_DeleteUninstallKey(LPCWSTR szProduct)
530 WCHAR keypath[0x200];
531 TRACE("%s\n",debugstr_w(szProduct));
533 sprintfW(keypath,szUninstall_fmt,szProduct);
535 return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
538 UINT MSIREG_OpenProductKey(LPCWSTR szProduct, LPCWSTR szUserSid,
539 MSIINSTALLCONTEXT context, HKEY *key, BOOL create)
541 UINT r;
542 LPWSTR usersid = NULL;
543 HKEY root = HKEY_LOCAL_MACHINE;
544 WCHAR squished_pc[GUID_SIZE];
545 WCHAR keypath[MAX_PATH];
547 TRACE("(%s, %d, %d)\n", debugstr_w(szProduct), context, create);
549 if (!squash_guid(szProduct, squished_pc))
550 return ERROR_FUNCTION_FAILED;
552 TRACE("squished (%s)\n", debugstr_w(squished_pc));
554 if (context == MSIINSTALLCONTEXT_MACHINE)
556 sprintfW(keypath, szInstaller_LocalClassesProd_fmt, squished_pc);
558 else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
560 root = HKEY_CURRENT_USER;
561 sprintfW(keypath, szUserProduct_fmt, squished_pc);
563 else
565 if (!szUserSid)
567 r = get_user_sid(&usersid);
568 if (r != ERROR_SUCCESS || !usersid)
570 ERR("Failed to retrieve user SID: %d\n", r);
571 return r;
574 szUserSid = usersid;
577 sprintfW(keypath, szInstaller_LocalManagedProd_fmt,
578 szUserSid, squished_pc);
579 LocalFree(usersid);
582 if (create)
583 return RegCreateKeyW(root, keypath, key);
585 return RegOpenKeyW(root, keypath, key);
588 UINT MSIREG_DeleteUserProductKey(LPCWSTR szProduct)
590 WCHAR squished_pc[GUID_SIZE];
591 WCHAR keypath[0x200];
593 TRACE("%s\n",debugstr_w(szProduct));
594 if (!squash_guid(szProduct,squished_pc))
595 return ERROR_FUNCTION_FAILED;
596 TRACE("squished (%s)\n", debugstr_w(squished_pc));
598 sprintfW(keypath,szUserProduct_fmt,squished_pc);
600 return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
603 UINT MSIREG_OpenUserPatchesKey(LPCWSTR szPatch, HKEY* key, BOOL create)
605 UINT rc;
606 WCHAR squished_pc[GUID_SIZE];
607 WCHAR keypath[0x200];
609 TRACE("%s\n",debugstr_w(szPatch));
610 if (!squash_guid(szPatch,squished_pc))
611 return ERROR_FUNCTION_FAILED;
612 TRACE("squished (%s)\n", debugstr_w(squished_pc));
614 sprintfW(keypath,szUserPatch_fmt,squished_pc);
616 if (create)
617 rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
618 else
619 rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
621 return rc;
624 UINT MSIREG_OpenFeaturesKey(LPCWSTR szProduct, MSIINSTALLCONTEXT context,
625 HKEY *key, BOOL create)
627 UINT r;
628 LPWSTR usersid;
629 HKEY root = HKEY_LOCAL_MACHINE;
630 WCHAR squished_pc[GUID_SIZE];
631 WCHAR keypath[MAX_PATH];
633 TRACE("(%s, %d, %d)\n", debugstr_w(szProduct), context, create);
635 if (!squash_guid(szProduct, squished_pc))
636 return ERROR_FUNCTION_FAILED;
638 TRACE("squished (%s)\n", debugstr_w(squished_pc));
640 if (context == MSIINSTALLCONTEXT_MACHINE)
642 sprintfW(keypath, szInstaller_LocalClassesFeat_fmt, squished_pc);
644 else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
646 root = HKEY_CURRENT_USER;
647 sprintfW(keypath, szUserFeatures_fmt, squished_pc);
649 else
651 r = get_user_sid(&usersid);
652 if (r != ERROR_SUCCESS || !usersid)
654 ERR("Failed to retrieve user SID: %d\n", r);
655 return r;
658 sprintfW(keypath, szInstaller_LocalManagedFeat_fmt, usersid, squished_pc);
659 LocalFree(usersid);
662 if (create)
663 return RegCreateKeyW(root, keypath, key);
665 return RegOpenKeyW(root, keypath, key);
668 UINT MSIREG_DeleteUserFeaturesKey(LPCWSTR szProduct)
670 WCHAR squished_pc[GUID_SIZE];
671 WCHAR keypath[0x200];
673 TRACE("%s\n",debugstr_w(szProduct));
674 if (!squash_guid(szProduct,squished_pc))
675 return ERROR_FUNCTION_FAILED;
676 TRACE("squished (%s)\n", debugstr_w(squished_pc));
678 sprintfW(keypath,szUserFeatures_fmt,squished_pc);
679 return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
682 static UINT MSIREG_OpenInstallerFeaturesKey(LPCWSTR szProduct, HKEY* key, BOOL create)
684 UINT rc;
685 WCHAR squished_pc[GUID_SIZE];
686 WCHAR keypath[0x200];
688 TRACE("%s\n",debugstr_w(szProduct));
689 if (!squash_guid(szProduct,squished_pc))
690 return ERROR_FUNCTION_FAILED;
691 TRACE("squished (%s)\n", debugstr_w(squished_pc));
693 sprintfW(keypath,szInstaller_Features_fmt,squished_pc);
695 if (create)
696 rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
697 else
698 rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
700 return rc;
703 UINT MSIREG_OpenUserDataFeaturesKey(LPCWSTR szProduct, MSIINSTALLCONTEXT context,
704 HKEY *key, BOOL create)
706 UINT r;
707 LPWSTR usersid;
708 WCHAR squished_pc[GUID_SIZE];
709 WCHAR keypath[0x200];
711 TRACE("(%s, %d, %d)\n", debugstr_w(szProduct), context, create);
713 if (!squash_guid(szProduct, squished_pc))
714 return ERROR_FUNCTION_FAILED;
716 TRACE("squished (%s)\n", debugstr_w(squished_pc));
718 if (context == MSIINSTALLCONTEXT_MACHINE)
720 sprintfW(keypath, szUserDataFeatures_fmt, szLocalSid, squished_pc);
722 else
724 r = get_user_sid(&usersid);
725 if (r != ERROR_SUCCESS || !usersid)
727 ERR("Failed to retrieve user SID: %d\n", r);
728 return r;
731 sprintfW(keypath, szUserDataFeatures_fmt, usersid, squished_pc);
732 LocalFree(usersid);
735 if (create)
736 return RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
738 return RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
741 UINT MSIREG_OpenUserComponentsKey(LPCWSTR szComponent, HKEY* key, BOOL create)
743 UINT rc;
744 WCHAR squished_cc[GUID_SIZE];
745 WCHAR keypath[0x200];
747 TRACE("%s\n",debugstr_w(szComponent));
748 if (!squash_guid(szComponent,squished_cc))
749 return ERROR_FUNCTION_FAILED;
750 TRACE("squished (%s)\n", debugstr_w(squished_cc));
752 sprintfW(keypath,szUser_Components_fmt,squished_cc);
754 if (create)
755 rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
756 else
757 rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
759 return rc;
762 UINT MSIREG_OpenUserDataComponentKey(LPCWSTR szComponent, LPCWSTR szUserSid,
763 HKEY *key, BOOL create)
765 UINT rc;
766 WCHAR comp[GUID_SIZE];
767 WCHAR keypath[0x200];
768 LPWSTR usersid;
770 TRACE("%s\n", debugstr_w(szComponent));
771 if (!squash_guid(szComponent, comp))
772 return ERROR_FUNCTION_FAILED;
773 TRACE("squished (%s)\n", debugstr_w(comp));
775 if (!szUserSid)
777 rc = get_user_sid(&usersid);
778 if (rc != ERROR_SUCCESS || !usersid)
780 ERR("Failed to retrieve user SID: %d\n", rc);
781 return rc;
784 sprintfW(keypath, szUserDataComp_fmt, usersid, comp);
785 LocalFree(usersid);
787 else
788 sprintfW(keypath, szUserDataComp_fmt, szUserSid, comp);
790 if (create)
791 rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
792 else
793 rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
795 return rc;
798 UINT MSIREG_DeleteUserDataComponentKey(LPCWSTR szComponent, LPCWSTR szUserSid)
800 UINT rc;
801 WCHAR comp[GUID_SIZE];
802 WCHAR keypath[0x200];
803 LPWSTR usersid;
805 TRACE("%s\n", debugstr_w(szComponent));
806 if (!squash_guid(szComponent, comp))
807 return ERROR_FUNCTION_FAILED;
808 TRACE("squished (%s)\n", debugstr_w(comp));
810 if (!szUserSid)
812 rc = get_user_sid(&usersid);
813 if (rc != ERROR_SUCCESS || !usersid)
815 ERR("Failed to retrieve user SID: %d\n", rc);
816 return rc;
819 sprintfW(keypath, szUserDataComp_fmt, usersid, comp);
820 LocalFree(usersid);
822 else
823 sprintfW(keypath, szUserDataComp_fmt, szUserSid, comp);
825 return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
828 UINT MSIREG_OpenUserDataProductKey(LPCWSTR szProduct, MSIINSTALLCONTEXT dwContext,
829 LPCWSTR szUserSid, HKEY *key, BOOL create)
831 UINT rc;
832 WCHAR squished_pc[GUID_SIZE];
833 WCHAR keypath[0x200];
834 LPWSTR usersid;
836 TRACE("%s\n", debugstr_w(szProduct));
837 if (!squash_guid(szProduct, squished_pc))
838 return ERROR_FUNCTION_FAILED;
839 TRACE("squished (%s)\n", debugstr_w(squished_pc));
841 if (dwContext == MSIINSTALLCONTEXT_MACHINE)
842 sprintfW(keypath, szUserDataProd_fmt, szLocalSid, squished_pc);
843 else if (szUserSid)
844 sprintfW(keypath, szUserDataProd_fmt, szUserSid, squished_pc);
845 else
847 rc = get_user_sid(&usersid);
848 if (rc != ERROR_SUCCESS || !usersid)
850 ERR("Failed to retrieve user SID: %d\n", rc);
851 return rc;
854 sprintfW(keypath, szUserDataProd_fmt, usersid, squished_pc);
855 LocalFree(usersid);
858 if (create)
859 rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
860 else
861 rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
863 return rc;
866 UINT MSIREG_OpenUserDataPatchKey(LPCWSTR szPatch, MSIINSTALLCONTEXT dwContext,
867 HKEY *key, BOOL create)
869 UINT rc;
870 WCHAR squished_patch[GUID_SIZE];
871 WCHAR keypath[0x200];
872 LPWSTR usersid;
874 TRACE("%s\n", debugstr_w(szPatch));
875 if (!squash_guid(szPatch, squished_patch))
876 return ERROR_FUNCTION_FAILED;
877 TRACE("squished (%s)\n", debugstr_w(squished_patch));
879 if (dwContext == MSIINSTALLCONTEXT_MACHINE)
880 sprintfW(keypath, szUserDataPatch_fmt, szLocalSid, squished_patch);
881 else
883 rc = get_user_sid(&usersid);
884 if (rc != ERROR_SUCCESS || !usersid)
886 ERR("Failed to retrieve user SID: %d\n", rc);
887 return rc;
890 sprintfW(keypath, szUserDataPatch_fmt, usersid, squished_patch);
891 LocalFree(usersid);
894 if (create)
895 return RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
897 return RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
900 UINT MSIREG_DeleteUserDataPatchKey(LPCWSTR patch, MSIINSTALLCONTEXT context)
902 UINT r;
903 WCHAR squished_patch[GUID_SIZE];
904 WCHAR keypath[0x200];
905 LPWSTR usersid;
907 TRACE("%s\n", debugstr_w(patch));
908 if (!squash_guid(patch, squished_patch))
909 return ERROR_FUNCTION_FAILED;
910 TRACE("squished (%s)\n", debugstr_w(squished_patch));
912 if (context == MSIINSTALLCONTEXT_MACHINE)
913 sprintfW(keypath, szUserDataPatch_fmt, szLocalSid, squished_patch);
914 else
916 r = get_user_sid(&usersid);
917 if (r != ERROR_SUCCESS || !usersid)
919 ERR("Failed to retrieve user SID: %d\n", r);
920 return r;
923 sprintfW(keypath, szUserDataPatch_fmt, usersid, squished_patch);
924 LocalFree(usersid);
927 return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
930 UINT MSIREG_OpenUserDataProductPatchesKey(LPCWSTR product, MSIINSTALLCONTEXT context,
931 HKEY *key, BOOL create)
933 UINT rc;
934 WCHAR squished_product[GUID_SIZE];
935 WCHAR keypath[0x200];
936 LPWSTR usersid;
938 TRACE("%s\n", debugstr_w(product));
939 if (!squash_guid(product, squished_product))
940 return ERROR_FUNCTION_FAILED;
942 if (context == MSIINSTALLCONTEXT_MACHINE)
943 sprintfW(keypath, szUserDataProductPatches_fmt, szLocalSid, squished_product);
944 else
946 rc = get_user_sid(&usersid);
947 if (rc != ERROR_SUCCESS || !usersid)
949 ERR("Failed to retrieve user SID: %d\n", rc);
950 return rc;
953 sprintfW(keypath, szUserDataProductPatches_fmt, usersid, squished_product);
954 LocalFree(usersid);
957 if (create)
958 return RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
960 return RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
963 UINT MSIREG_OpenInstallProps(LPCWSTR szProduct, MSIINSTALLCONTEXT dwContext,
964 LPCWSTR szUserSid, HKEY *key, BOOL create)
966 UINT rc;
967 LPWSTR usersid;
968 WCHAR squished_pc[GUID_SIZE];
969 WCHAR keypath[0x200];
971 TRACE("%s\n", debugstr_w(szProduct));
972 if (!squash_guid(szProduct, squished_pc))
973 return ERROR_FUNCTION_FAILED;
974 TRACE("squished (%s)\n", debugstr_w(squished_pc));
976 if (dwContext == MSIINSTALLCONTEXT_MACHINE)
977 sprintfW(keypath, szInstallProperties_fmt, szLocalSid, squished_pc);
978 else if (szUserSid)
979 sprintfW(keypath, szInstallProperties_fmt, szUserSid, squished_pc);
980 else
982 rc = get_user_sid(&usersid);
983 if (rc != ERROR_SUCCESS || !usersid)
985 ERR("Failed to retrieve user SID: %d\n", rc);
986 return rc;
989 sprintfW(keypath, szInstallProperties_fmt, usersid, squished_pc);
990 LocalFree(usersid);
993 if (create)
994 return RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
996 return RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
999 UINT MSIREG_DeleteUserDataProductKey(LPCWSTR szProduct)
1001 UINT rc;
1002 WCHAR squished_pc[GUID_SIZE];
1003 WCHAR keypath[0x200];
1004 LPWSTR usersid;
1006 TRACE("%s\n", debugstr_w(szProduct));
1007 if (!squash_guid(szProduct, squished_pc))
1008 return ERROR_FUNCTION_FAILED;
1009 TRACE("squished (%s)\n", debugstr_w(squished_pc));
1011 rc = get_user_sid(&usersid);
1012 if (rc != ERROR_SUCCESS || !usersid)
1014 ERR("Failed to retrieve user SID: %d\n", rc);
1015 return rc;
1018 sprintfW(keypath, szUserDataProd_fmt, usersid, squished_pc);
1020 LocalFree(usersid);
1021 return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
1024 UINT MSIREG_DeleteProductKey(LPCWSTR szProduct)
1026 WCHAR squished_pc[GUID_SIZE];
1027 WCHAR keypath[0x200];
1029 TRACE("%s\n", debugstr_w(szProduct));
1030 if (!squash_guid(szProduct, squished_pc))
1031 return ERROR_FUNCTION_FAILED;
1032 TRACE("squished (%s)\n", debugstr_w(squished_pc));
1034 sprintfW(keypath, szInstaller_Products_fmt, squished_pc);
1036 return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
1039 UINT MSIREG_OpenPatchesKey(LPCWSTR szPatch, HKEY* key, BOOL create)
1041 UINT rc;
1042 WCHAR squished_pc[GUID_SIZE];
1043 WCHAR keypath[0x200];
1045 TRACE("%s\n",debugstr_w(szPatch));
1046 if (!squash_guid(szPatch,squished_pc))
1047 return ERROR_FUNCTION_FAILED;
1048 TRACE("squished (%s)\n", debugstr_w(squished_pc));
1050 sprintfW(keypath,szInstaller_Patches_fmt,squished_pc);
1052 if (create)
1053 rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
1054 else
1055 rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
1057 return rc;
1060 UINT MSIREG_OpenUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY* key, BOOL create)
1062 UINT rc;
1063 WCHAR squished_pc[GUID_SIZE];
1064 WCHAR keypath[0x200];
1066 TRACE("%s\n",debugstr_w(szUpgradeCode));
1067 if (!squash_guid(szUpgradeCode,squished_pc))
1068 return ERROR_FUNCTION_FAILED;
1069 TRACE("squished (%s)\n", debugstr_w(squished_pc));
1071 sprintfW(keypath,szInstaller_UpgradeCodes_fmt,squished_pc);
1073 if (create)
1074 rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
1075 else
1076 rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
1078 return rc;
1081 UINT MSIREG_OpenUserUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY* key, BOOL create)
1083 UINT rc;
1084 WCHAR squished_pc[GUID_SIZE];
1085 WCHAR keypath[0x200];
1087 TRACE("%s\n",debugstr_w(szUpgradeCode));
1088 if (!squash_guid(szUpgradeCode,squished_pc))
1089 return ERROR_FUNCTION_FAILED;
1090 TRACE("squished (%s)\n", debugstr_w(squished_pc));
1092 sprintfW(keypath,szInstaller_UserUpgradeCodes_fmt,squished_pc);
1094 if (create)
1095 rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
1096 else
1097 rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
1099 return rc;
1102 UINT MSIREG_DeleteUserUpgradeCodesKey(LPCWSTR szUpgradeCode)
1104 WCHAR squished_pc[GUID_SIZE];
1105 WCHAR keypath[0x200];
1107 TRACE("%s\n",debugstr_w(szUpgradeCode));
1108 if (!squash_guid(szUpgradeCode,squished_pc))
1109 return ERROR_FUNCTION_FAILED;
1110 TRACE("squished (%s)\n", debugstr_w(squished_pc));
1112 sprintfW(keypath,szInstaller_UserUpgradeCodes_fmt,squished_pc);
1114 return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
1117 UINT MSIREG_DeleteLocalClassesProductKey(LPCWSTR szProductCode)
1119 WCHAR squished_pc[GUID_SIZE];
1120 WCHAR keypath[0x200];
1122 TRACE("%s\n", debugstr_w(szProductCode));
1124 if (!squash_guid(szProductCode, squished_pc))
1125 return ERROR_FUNCTION_FAILED;
1127 TRACE("squished (%s)\n", debugstr_w(squished_pc));
1129 sprintfW(keypath, szInstaller_LocalClassesProd_fmt, squished_pc);
1131 return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
1134 UINT MSIREG_DeleteLocalClassesFeaturesKey(LPCWSTR szProductCode)
1136 WCHAR squished_pc[GUID_SIZE];
1137 WCHAR keypath[0x200];
1139 TRACE("%s\n", debugstr_w(szProductCode));
1141 if (!squash_guid(szProductCode, squished_pc))
1142 return ERROR_FUNCTION_FAILED;
1144 TRACE("squished (%s)\n", debugstr_w(squished_pc));
1146 sprintfW(keypath, szInstaller_LocalClassesFeat_fmt, squished_pc);
1148 return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
1151 UINT MSIREG_OpenClassesUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY* key, BOOL create)
1153 WCHAR squished_pc[GUID_SIZE];
1154 WCHAR keypath[0x200];
1156 TRACE("%s\n", debugstr_w(szUpgradeCode));
1157 if (!squash_guid(szUpgradeCode, squished_pc))
1158 return ERROR_FUNCTION_FAILED;
1159 TRACE("squished (%s)\n", debugstr_w(squished_pc));
1161 sprintfW(keypath, szInstaller_ClassesUpgrade_fmt, squished_pc);
1163 if (create)
1164 return RegCreateKeyW(HKEY_CLASSES_ROOT, keypath, key);
1166 return RegOpenKeyW(HKEY_CLASSES_ROOT, keypath, key);
1169 /*************************************************************************
1170 * MsiDecomposeDescriptorW [MSI.@]
1172 * Decomposes an MSI descriptor into product, feature and component parts.
1173 * An MSI descriptor is a string of the form:
1174 * [base 85 guid] [feature code] '>' [base 85 guid]
1176 * PARAMS
1177 * szDescriptor [I] the descriptor to decompose
1178 * szProduct [O] buffer of MAX_FEATURE_CHARS+1 for the product guid
1179 * szFeature [O] buffer of MAX_FEATURE_CHARS+1 for the feature code
1180 * szComponent [O] buffer of MAX_FEATURE_CHARS+1 for the component guid
1181 * pUsed [O] the length of the descriptor
1183 * RETURNS
1184 * ERROR_SUCCESS if everything worked correctly
1185 * ERROR_INVALID_PARAMETER if the descriptor was invalid
1188 UINT WINAPI MsiDecomposeDescriptorW( LPCWSTR szDescriptor, LPWSTR szProduct,
1189 LPWSTR szFeature, LPWSTR szComponent, LPDWORD pUsed )
1191 UINT r, len;
1192 LPWSTR p;
1193 GUID product, component;
1195 TRACE("%s %p %p %p %p\n", debugstr_w(szDescriptor), szProduct,
1196 szFeature, szComponent, pUsed);
1198 r = decode_base85_guid( szDescriptor, &product );
1199 if( !r )
1200 return ERROR_INVALID_PARAMETER;
1202 TRACE("product %s\n", debugstr_guid( &product ));
1204 p = strchrW(&szDescriptor[20],'>');
1205 if( !p )
1206 return ERROR_INVALID_PARAMETER;
1208 len = (p - &szDescriptor[20]);
1209 if( len > MAX_FEATURE_CHARS )
1210 return ERROR_INVALID_PARAMETER;
1212 TRACE("feature %s\n", debugstr_wn( &szDescriptor[20], len ));
1214 r = decode_base85_guid( p+1, &component );
1215 if( !r )
1216 return ERROR_INVALID_PARAMETER;
1218 TRACE("component %s\n", debugstr_guid( &component ));
1220 if (szProduct)
1221 StringFromGUID2( &product, szProduct, MAX_FEATURE_CHARS+1 );
1222 if (szComponent)
1223 StringFromGUID2( &component, szComponent, MAX_FEATURE_CHARS+1 );
1224 if (szFeature)
1226 memcpy( szFeature, &szDescriptor[20], len*sizeof(WCHAR) );
1227 szFeature[len] = 0;
1229 len = ( &p[21] - szDescriptor );
1231 TRACE("length = %d\n", len);
1232 if (pUsed) *pUsed = len;
1234 return ERROR_SUCCESS;
1237 UINT WINAPI MsiDecomposeDescriptorA( LPCSTR szDescriptor, LPSTR szProduct,
1238 LPSTR szFeature, LPSTR szComponent, LPDWORD pUsed )
1240 WCHAR product[MAX_FEATURE_CHARS+1];
1241 WCHAR feature[MAX_FEATURE_CHARS+1];
1242 WCHAR component[MAX_FEATURE_CHARS+1];
1243 LPWSTR str = NULL, p = NULL, f = NULL, c = NULL;
1244 UINT r;
1246 TRACE("%s %p %p %p %p\n", debugstr_a(szDescriptor), szProduct,
1247 szFeature, szComponent, pUsed);
1249 str = strdupAtoW( szDescriptor );
1250 if( szDescriptor && !str )
1251 return ERROR_OUTOFMEMORY;
1253 if (szProduct)
1254 p = product;
1255 if (szFeature)
1256 f = feature;
1257 if (szComponent)
1258 c = component;
1260 r = MsiDecomposeDescriptorW( str, p, f, c, pUsed );
1262 if (r == ERROR_SUCCESS)
1264 WideCharToMultiByte( CP_ACP, 0, p, -1,
1265 szProduct, MAX_FEATURE_CHARS+1, NULL, NULL );
1266 WideCharToMultiByte( CP_ACP, 0, f, -1,
1267 szFeature, MAX_FEATURE_CHARS+1, NULL, NULL );
1268 WideCharToMultiByte( CP_ACP, 0, c, -1,
1269 szComponent, MAX_FEATURE_CHARS+1, NULL, NULL );
1272 msi_free( str );
1274 return r;
1277 UINT WINAPI MsiEnumProductsA(DWORD index, LPSTR lpguid)
1279 DWORD r;
1280 WCHAR szwGuid[GUID_SIZE];
1282 TRACE("%d %p\n", index, lpguid);
1284 if (NULL == lpguid)
1285 return ERROR_INVALID_PARAMETER;
1286 r = MsiEnumProductsW(index, szwGuid);
1287 if( r == ERROR_SUCCESS )
1288 WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
1290 return r;
1293 UINT WINAPI MsiEnumProductsW(DWORD index, LPWSTR lpguid)
1295 UINT r;
1296 WCHAR szKeyName[SQUISH_GUID_SIZE];
1297 HKEY key;
1298 DWORD machine_count, managed_count, unmanaged_count;
1299 WCHAR keypath[MAX_PATH];
1300 LPWSTR usersid = NULL;
1302 static DWORD last_index;
1304 TRACE("%d %p\n", index, lpguid);
1306 if (NULL == lpguid)
1307 return ERROR_INVALID_PARAMETER;
1309 if (index && index - last_index != 1)
1310 return ERROR_INVALID_PARAMETER;
1312 key = 0;
1313 r = RegCreateKeyW(HKEY_LOCAL_MACHINE, szInstaller_LocalClassesProd, &key);
1314 if( r != ERROR_SUCCESS ) goto failed;
1316 r = RegQueryInfoKeyW(key, NULL, NULL, NULL, &machine_count, NULL, NULL,
1317 NULL, NULL, NULL, NULL, NULL);
1318 if( r != ERROR_SUCCESS ) goto failed;
1320 if (machine_count && index <= machine_count)
1322 r = RegEnumKeyW(key, index, szKeyName, SQUISH_GUID_SIZE);
1323 if( r == ERROR_SUCCESS )
1325 unsquash_guid(szKeyName, lpguid);
1326 last_index = index;
1327 RegCloseKey(key);
1328 return ERROR_SUCCESS;
1331 RegCloseKey(key);
1333 key = 0;
1334 r = get_user_sid(&usersid);
1335 if (r != ERROR_SUCCESS || !usersid)
1337 ERR("Failed to retrieve user SID: %d\n", r);
1338 last_index = 0;
1339 return r;
1341 sprintfW(keypath, szInstaller_LocalManaged_fmt, usersid);
1342 LocalFree(usersid);
1344 r = RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, &key);
1345 if( r != ERROR_SUCCESS ) goto failed;
1347 r = RegQueryInfoKeyW(key, NULL, NULL, NULL, &managed_count, NULL, NULL,
1348 NULL, NULL, NULL, NULL, NULL);
1349 if( r != ERROR_SUCCESS ) goto failed;
1351 if (managed_count && index <= machine_count + managed_count)
1353 r = RegEnumKeyW(key, index - machine_count, szKeyName, SQUISH_GUID_SIZE);
1354 if( r == ERROR_SUCCESS )
1356 unsquash_guid(szKeyName, lpguid);
1357 last_index = index;
1358 RegCloseKey(key);
1359 return ERROR_SUCCESS;
1362 RegCloseKey(key);
1364 key = 0;
1365 r = RegCreateKeyW(HKEY_CURRENT_USER, szUserProduct, &key);
1366 if( r != ERROR_SUCCESS ) goto failed;
1368 r = RegQueryInfoKeyW(key, NULL, NULL, NULL, &unmanaged_count, NULL, NULL,
1369 NULL, NULL, NULL, NULL, NULL);
1370 if( r != ERROR_SUCCESS ) goto failed;
1372 if (unmanaged_count && index <= machine_count + managed_count + unmanaged_count)
1374 r = RegEnumKeyW(key, index - machine_count - managed_count, szKeyName, SQUISH_GUID_SIZE);
1375 if( r == ERROR_SUCCESS )
1377 unsquash_guid(szKeyName, lpguid);
1378 last_index = index;
1379 RegCloseKey(key);
1380 return ERROR_SUCCESS;
1383 failed:
1384 RegCloseKey(key);
1385 last_index = 0;
1386 return ERROR_NO_MORE_ITEMS;
1389 UINT WINAPI MsiEnumFeaturesA(LPCSTR szProduct, DWORD index,
1390 LPSTR szFeature, LPSTR szParent)
1392 DWORD r;
1393 WCHAR szwFeature[GUID_SIZE], szwParent[GUID_SIZE];
1394 LPWSTR szwProduct = NULL;
1396 TRACE("%s %d %p %p\n", debugstr_a(szProduct), index, szFeature, szParent);
1398 if( szProduct )
1400 szwProduct = strdupAtoW( szProduct );
1401 if( !szwProduct )
1402 return ERROR_OUTOFMEMORY;
1405 r = MsiEnumFeaturesW(szwProduct, index, szwFeature, szwParent);
1406 if( r == ERROR_SUCCESS )
1408 WideCharToMultiByte(CP_ACP, 0, szwFeature, -1,
1409 szFeature, GUID_SIZE, NULL, NULL);
1410 WideCharToMultiByte(CP_ACP, 0, szwParent, -1,
1411 szParent, GUID_SIZE, NULL, NULL);
1414 msi_free( szwProduct);
1416 return r;
1419 UINT WINAPI MsiEnumFeaturesW(LPCWSTR szProduct, DWORD index,
1420 LPWSTR szFeature, LPWSTR szParent)
1422 HKEY hkeyProduct = 0;
1423 DWORD r, sz;
1425 TRACE("%s %d %p %p\n", debugstr_w(szProduct), index, szFeature, szParent);
1427 if( !szProduct )
1428 return ERROR_INVALID_PARAMETER;
1430 r = MSIREG_OpenInstallerFeaturesKey(szProduct,&hkeyProduct,FALSE);
1431 if( r != ERROR_SUCCESS )
1432 return ERROR_NO_MORE_ITEMS;
1434 sz = GUID_SIZE;
1435 r = RegEnumValueW(hkeyProduct, index, szFeature, &sz, NULL, NULL, NULL, NULL);
1436 RegCloseKey(hkeyProduct);
1438 return r;
1441 UINT WINAPI MsiEnumComponentsA(DWORD index, LPSTR lpguid)
1443 DWORD r;
1444 WCHAR szwGuid[GUID_SIZE];
1446 TRACE("%d %p\n", index, lpguid);
1448 r = MsiEnumComponentsW(index, szwGuid);
1449 if( r == ERROR_SUCCESS )
1450 WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
1452 return r;
1455 UINT WINAPI MsiEnumComponentsW(DWORD index, LPWSTR lpguid)
1457 HKEY hkeyComponents = 0;
1458 DWORD r;
1459 WCHAR szKeyName[SQUISH_GUID_SIZE];
1461 TRACE("%d %p\n", index, lpguid);
1463 r = RegCreateKeyW(HKEY_LOCAL_MACHINE, szInstaller_Components, &hkeyComponents);
1464 if( r != ERROR_SUCCESS )
1465 return ERROR_NO_MORE_ITEMS;
1467 r = RegEnumKeyW(hkeyComponents, index, szKeyName, SQUISH_GUID_SIZE);
1468 if( r == ERROR_SUCCESS )
1469 unsquash_guid(szKeyName, lpguid);
1470 RegCloseKey(hkeyComponents);
1472 return r;
1475 UINT WINAPI MsiEnumClientsA(LPCSTR szComponent, DWORD index, LPSTR szProduct)
1477 DWORD r;
1478 WCHAR szwProduct[GUID_SIZE];
1479 LPWSTR szwComponent = NULL;
1481 TRACE("%s %d %p\n", debugstr_a(szComponent), index, szProduct);
1483 if ( !szProduct )
1484 return ERROR_INVALID_PARAMETER;
1486 if( szComponent )
1488 szwComponent = strdupAtoW( szComponent );
1489 if( !szwComponent )
1490 return ERROR_OUTOFMEMORY;
1493 r = MsiEnumClientsW(szComponent?szwComponent:NULL, index, szwProduct);
1494 if( r == ERROR_SUCCESS )
1496 WideCharToMultiByte(CP_ACP, 0, szwProduct, -1,
1497 szProduct, GUID_SIZE, NULL, NULL);
1500 msi_free( szwComponent);
1502 return r;
1505 UINT WINAPI MsiEnumClientsW(LPCWSTR szComponent, DWORD index, LPWSTR szProduct)
1507 HKEY hkeyComp = 0;
1508 DWORD r, sz;
1509 WCHAR szValName[SQUISH_GUID_SIZE];
1511 TRACE("%s %d %p\n", debugstr_w(szComponent), index, szProduct);
1513 if (!szComponent || !*szComponent || !szProduct)
1514 return ERROR_INVALID_PARAMETER;
1516 if (MSIREG_OpenUserDataComponentKey(szComponent, NULL, &hkeyComp, FALSE) != ERROR_SUCCESS &&
1517 MSIREG_OpenUserDataComponentKey(szComponent, szLocalSid, &hkeyComp, FALSE) != ERROR_SUCCESS)
1518 return ERROR_UNKNOWN_COMPONENT;
1520 /* see if there are any products at all */
1521 sz = SQUISH_GUID_SIZE;
1522 r = RegEnumValueW(hkeyComp, 0, szValName, &sz, NULL, NULL, NULL, NULL);
1523 if (r != ERROR_SUCCESS)
1525 RegCloseKey(hkeyComp);
1527 if (index != 0)
1528 return ERROR_INVALID_PARAMETER;
1530 return ERROR_UNKNOWN_COMPONENT;
1533 sz = SQUISH_GUID_SIZE;
1534 r = RegEnumValueW(hkeyComp, index, szValName, &sz, NULL, NULL, NULL, NULL);
1535 if( r == ERROR_SUCCESS )
1536 unsquash_guid(szValName, szProduct);
1538 RegCloseKey(hkeyComp);
1540 return r;
1543 static UINT MSI_EnumComponentQualifiers( LPCWSTR szComponent, DWORD iIndex,
1544 awstring *lpQualBuf, LPDWORD pcchQual,
1545 awstring *lpAppBuf, LPDWORD pcchAppBuf )
1547 DWORD name_sz, val_sz, name_max, val_max, type, ofs;
1548 LPWSTR name = NULL, val = NULL;
1549 UINT r, r2;
1550 HKEY key;
1552 TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
1553 lpQualBuf, pcchQual, lpAppBuf, pcchAppBuf);
1555 if (!szComponent)
1556 return ERROR_INVALID_PARAMETER;
1558 r = MSIREG_OpenUserComponentsKey( szComponent, &key, FALSE );
1559 if (r != ERROR_SUCCESS)
1560 return ERROR_UNKNOWN_COMPONENT;
1562 /* figure out how big the name is we want to return */
1563 name_max = 0x10;
1564 r = ERROR_OUTOFMEMORY;
1565 name = msi_alloc( name_max * sizeof(WCHAR) );
1566 if (!name)
1567 goto end;
1569 val_max = 0x10;
1570 r = ERROR_OUTOFMEMORY;
1571 val = msi_alloc( val_max );
1572 if (!val)
1573 goto end;
1575 /* loop until we allocate enough memory */
1576 while (1)
1578 name_sz = name_max;
1579 val_sz = val_max;
1580 r = RegEnumValueW( key, iIndex, name, &name_sz,
1581 NULL, &type, (LPBYTE)val, &val_sz );
1582 if (r == ERROR_SUCCESS)
1583 break;
1584 if (r != ERROR_MORE_DATA)
1585 goto end;
1587 if (type != REG_MULTI_SZ)
1589 ERR("component data has wrong type (%d)\n", type);
1590 goto end;
1593 r = ERROR_OUTOFMEMORY;
1594 if ((name_sz+1) >= name_max)
1596 name_max *= 2;
1597 msi_free( name );
1598 name = msi_alloc( name_max * sizeof (WCHAR) );
1599 if (!name)
1600 goto end;
1601 continue;
1603 if (val_sz > val_max)
1605 val_max = val_sz + sizeof (WCHAR);
1606 msi_free( val );
1607 val = msi_alloc( val_max * sizeof (WCHAR) );
1608 if (!val)
1609 goto end;
1610 continue;
1612 ERR("should be enough data, but isn't %d %d\n", name_sz, val_sz );
1613 goto end;
1616 ofs = 0;
1617 r = MsiDecomposeDescriptorW( val, NULL, NULL, NULL, &ofs );
1618 if (r != ERROR_SUCCESS)
1619 goto end;
1621 TRACE("Providing %s and %s\n", debugstr_w(name), debugstr_w(val+ofs));
1623 r = msi_strcpy_to_awstring( name, lpQualBuf, pcchQual );
1624 r2 = msi_strcpy_to_awstring( val+ofs, lpAppBuf, pcchAppBuf );
1626 if (r2 != ERROR_SUCCESS)
1627 r = r2;
1629 end:
1630 msi_free(val);
1631 msi_free(name);
1632 RegCloseKey(key);
1634 return r;
1637 /*************************************************************************
1638 * MsiEnumComponentQualifiersA [MSI.@]
1640 UINT WINAPI MsiEnumComponentQualifiersA( LPCSTR szComponent, DWORD iIndex,
1641 LPSTR lpQualifierBuf, LPDWORD pcchQualifierBuf,
1642 LPSTR lpApplicationDataBuf, LPDWORD pcchApplicationDataBuf )
1644 awstring qual, appdata;
1645 LPWSTR comp;
1646 UINT r;
1648 TRACE("%s %08x %p %p %p %p\n", debugstr_a(szComponent), iIndex,
1649 lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
1650 pcchApplicationDataBuf);
1652 comp = strdupAtoW( szComponent );
1653 if (szComponent && !comp)
1654 return ERROR_OUTOFMEMORY;
1656 qual.unicode = FALSE;
1657 qual.str.a = lpQualifierBuf;
1659 appdata.unicode = FALSE;
1660 appdata.str.a = lpApplicationDataBuf;
1662 r = MSI_EnumComponentQualifiers( comp, iIndex,
1663 &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
1664 msi_free( comp );
1665 return r;
1668 /*************************************************************************
1669 * MsiEnumComponentQualifiersW [MSI.@]
1671 UINT WINAPI MsiEnumComponentQualifiersW( LPCWSTR szComponent, DWORD iIndex,
1672 LPWSTR lpQualifierBuf, LPDWORD pcchQualifierBuf,
1673 LPWSTR lpApplicationDataBuf, LPDWORD pcchApplicationDataBuf )
1675 awstring qual, appdata;
1677 TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
1678 lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
1679 pcchApplicationDataBuf);
1681 qual.unicode = TRUE;
1682 qual.str.w = lpQualifierBuf;
1684 appdata.unicode = TRUE;
1685 appdata.str.w = lpApplicationDataBuf;
1687 return MSI_EnumComponentQualifiers( szComponent, iIndex,
1688 &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
1691 /*************************************************************************
1692 * MsiEnumRelatedProductsW [MSI.@]
1695 UINT WINAPI MsiEnumRelatedProductsW(LPCWSTR szUpgradeCode, DWORD dwReserved,
1696 DWORD iProductIndex, LPWSTR lpProductBuf)
1698 UINT r;
1699 HKEY hkey;
1700 DWORD dwSize = SQUISH_GUID_SIZE;
1701 WCHAR szKeyName[SQUISH_GUID_SIZE];
1703 TRACE("%s %u %u %p\n", debugstr_w(szUpgradeCode), dwReserved,
1704 iProductIndex, lpProductBuf);
1706 if (NULL == szUpgradeCode)
1707 return ERROR_INVALID_PARAMETER;
1708 if (NULL == lpProductBuf)
1709 return ERROR_INVALID_PARAMETER;
1711 r = MSIREG_OpenUpgradeCodesKey(szUpgradeCode, &hkey, FALSE);
1712 if (r != ERROR_SUCCESS)
1713 return ERROR_NO_MORE_ITEMS;
1715 r = RegEnumValueW(hkey, iProductIndex, szKeyName, &dwSize, NULL, NULL, NULL, NULL);
1716 if( r == ERROR_SUCCESS )
1717 unsquash_guid(szKeyName, lpProductBuf);
1718 RegCloseKey(hkey);
1720 return r;
1723 /*************************************************************************
1724 * MsiEnumRelatedProductsA [MSI.@]
1727 UINT WINAPI MsiEnumRelatedProductsA(LPCSTR szUpgradeCode, DWORD dwReserved,
1728 DWORD iProductIndex, LPSTR lpProductBuf)
1730 LPWSTR szwUpgradeCode = NULL;
1731 WCHAR productW[GUID_SIZE];
1732 UINT r;
1734 TRACE("%s %u %u %p\n", debugstr_a(szUpgradeCode), dwReserved,
1735 iProductIndex, lpProductBuf);
1737 if (szUpgradeCode)
1739 szwUpgradeCode = strdupAtoW( szUpgradeCode );
1740 if( !szwUpgradeCode )
1741 return ERROR_OUTOFMEMORY;
1744 r = MsiEnumRelatedProductsW( szwUpgradeCode, dwReserved,
1745 iProductIndex, productW );
1746 if (r == ERROR_SUCCESS)
1748 WideCharToMultiByte( CP_ACP, 0, productW, GUID_SIZE,
1749 lpProductBuf, GUID_SIZE, NULL, NULL );
1751 msi_free( szwUpgradeCode);
1752 return r;
1755 /***********************************************************************
1756 * MsiEnumPatchesExA [MSI.@]
1758 UINT WINAPI MsiEnumPatchesExA(LPCSTR szProductCode, LPCSTR szUserSid,
1759 DWORD dwContext, DWORD dwFilter, DWORD dwIndex, LPSTR szPatchCode,
1760 LPSTR szTargetProductCode, MSIINSTALLCONTEXT *pdwTargetProductContext,
1761 LPSTR szTargetUserSid, LPDWORD pcchTargetUserSid)
1763 LPWSTR prodcode = NULL;
1764 LPWSTR usersid = NULL;
1765 LPWSTR targsid = NULL;
1766 WCHAR patch[GUID_SIZE];
1767 WCHAR targprod[GUID_SIZE];
1768 DWORD len;
1769 UINT r;
1771 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n",
1772 debugstr_a(szProductCode), debugstr_a(szUserSid), dwContext, dwFilter,
1773 dwIndex, szPatchCode, szTargetProductCode, pdwTargetProductContext,
1774 szTargetUserSid, pcchTargetUserSid);
1776 if (szTargetUserSid && !pcchTargetUserSid)
1777 return ERROR_INVALID_PARAMETER;
1779 if (szProductCode) prodcode = strdupAtoW(szProductCode);
1780 if (szUserSid) usersid = strdupAtoW(szUserSid);
1782 r = MsiEnumPatchesExW(prodcode, usersid, dwContext, dwFilter, dwIndex,
1783 patch, targprod, pdwTargetProductContext,
1784 NULL, &len);
1785 if (r != ERROR_SUCCESS)
1786 goto done;
1788 WideCharToMultiByte(CP_ACP, 0, patch, -1, szPatchCode,
1789 GUID_SIZE, NULL, NULL);
1790 WideCharToMultiByte(CP_ACP, 0, targprod, -1, szTargetProductCode,
1791 GUID_SIZE, NULL, NULL);
1793 if (!szTargetUserSid)
1795 if (pcchTargetUserSid)
1796 *pcchTargetUserSid = len;
1798 goto done;
1801 targsid = msi_alloc(++len * sizeof(WCHAR));
1802 if (!targsid)
1804 r = ERROR_OUTOFMEMORY;
1805 goto done;
1808 r = MsiEnumPatchesExW(prodcode, usersid, dwContext, dwFilter, dwIndex,
1809 patch, targprod, pdwTargetProductContext,
1810 targsid, &len);
1811 if (r != ERROR_SUCCESS || !szTargetUserSid)
1812 goto done;
1814 WideCharToMultiByte(CP_ACP, 0, targsid, -1, szTargetUserSid,
1815 *pcchTargetUserSid, NULL, NULL);
1817 len = lstrlenW(targsid);
1818 if (*pcchTargetUserSid < len + 1)
1820 r = ERROR_MORE_DATA;
1821 *pcchTargetUserSid = len * sizeof(WCHAR);
1823 else
1824 *pcchTargetUserSid = len;
1826 done:
1827 msi_free(prodcode);
1828 msi_free(usersid);
1829 msi_free(targsid);
1831 return r;
1834 static UINT msi_get_patch_state(LPCWSTR prodcode, LPCWSTR usersid,
1835 MSIINSTALLCONTEXT context,
1836 LPWSTR patch, MSIPATCHSTATE *state)
1838 DWORD type, val, size;
1839 HKEY prod, hkey = 0;
1840 HKEY udpatch = 0;
1841 LONG res;
1842 UINT r = ERROR_NO_MORE_ITEMS;
1844 *state = MSIPATCHSTATE_INVALID;
1846 r = MSIREG_OpenUserDataProductKey(prodcode, context,
1847 usersid, &prod, FALSE);
1848 if (r != ERROR_SUCCESS)
1849 return ERROR_NO_MORE_ITEMS;
1851 res = RegOpenKeyExW(prod, szPatches, 0, KEY_READ, &hkey);
1852 if (res != ERROR_SUCCESS)
1853 goto done;
1855 res = RegOpenKeyExW(hkey, patch, 0, KEY_READ, &udpatch);
1856 if (res != ERROR_SUCCESS)
1857 goto done;
1859 size = sizeof(DWORD);
1860 res = RegGetValueW(udpatch, NULL, szState, RRF_RT_DWORD, &type, &val, &size);
1861 if (res != ERROR_SUCCESS ||
1862 val < MSIPATCHSTATE_APPLIED || val > MSIPATCHSTATE_REGISTERED)
1864 r = ERROR_BAD_CONFIGURATION;
1865 goto done;
1868 *state = val;
1869 r = ERROR_SUCCESS;
1871 done:
1872 RegCloseKey(udpatch);
1873 RegCloseKey(hkey);
1874 RegCloseKey(prod);
1876 return r;
1879 static UINT msi_check_product_patches(LPCWSTR prodcode, LPCWSTR usersid,
1880 MSIINSTALLCONTEXT context, DWORD filter, DWORD index, DWORD *idx,
1881 LPWSTR patch, LPWSTR targetprod, MSIINSTALLCONTEXT *targetctx,
1882 LPWSTR targetsid, DWORD *sidsize, LPWSTR *transforms)
1884 MSIPATCHSTATE state = MSIPATCHSTATE_INVALID;
1885 LPWSTR ptr, patches = NULL;
1886 HKEY prod, patchkey = 0;
1887 HKEY localprod = 0, localpatch = 0;
1888 DWORD type, size;
1889 LONG res;
1890 UINT temp, r = ERROR_NO_MORE_ITEMS;
1892 if (MSIREG_OpenProductKey(prodcode, usersid, context,
1893 &prod, FALSE) != ERROR_SUCCESS)
1894 return ERROR_NO_MORE_ITEMS;
1896 size = 0;
1897 res = RegGetValueW(prod, szPatches, szPatches, RRF_RT_ANY, &type, NULL,
1898 &size);
1899 if (res != ERROR_SUCCESS)
1900 goto done;
1902 if (type != REG_MULTI_SZ)
1904 r = ERROR_BAD_CONFIGURATION;
1905 goto done;
1908 patches = msi_alloc(size);
1909 if (!patches)
1911 r = ERROR_OUTOFMEMORY;
1912 goto done;
1915 res = RegGetValueW(prod, szPatches, szPatches, RRF_RT_ANY, &type,
1916 patches, &size);
1917 if (res != ERROR_SUCCESS)
1918 goto done;
1920 ptr = patches;
1921 for (ptr = patches; *ptr && r == ERROR_NO_MORE_ITEMS; ptr += lstrlenW(ptr))
1923 if (!unsquash_guid(ptr, patch))
1925 r = ERROR_BAD_CONFIGURATION;
1926 goto done;
1929 size = 0;
1930 res = RegGetValueW(prod, szPatches, ptr, RRF_RT_REG_SZ,
1931 &type, NULL, &size);
1932 if (res != ERROR_SUCCESS)
1933 continue;
1935 if (transforms)
1937 *transforms = msi_alloc(size);
1938 if (!*transforms)
1940 r = ERROR_OUTOFMEMORY;
1941 goto done;
1944 res = RegGetValueW(prod, szPatches, ptr, RRF_RT_REG_SZ,
1945 &type, *transforms, &size);
1946 if (res != ERROR_SUCCESS)
1947 continue;
1950 if (context == MSIINSTALLCONTEXT_USERMANAGED)
1952 if (!(filter & MSIPATCHSTATE_APPLIED))
1954 temp = msi_get_patch_state(prodcode, usersid, context,
1955 ptr, &state);
1956 if (temp == ERROR_BAD_CONFIGURATION)
1958 r = ERROR_BAD_CONFIGURATION;
1959 goto done;
1962 if (temp != ERROR_SUCCESS || !(filter & state))
1963 continue;
1966 else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
1968 if (!(filter & MSIPATCHSTATE_APPLIED))
1970 temp = msi_get_patch_state(prodcode, usersid, context,
1971 ptr, &state);
1972 if (temp == ERROR_BAD_CONFIGURATION)
1974 r = ERROR_BAD_CONFIGURATION;
1975 goto done;
1978 if (temp != ERROR_SUCCESS || !(filter & state))
1979 continue;
1981 else
1983 temp = MSIREG_OpenUserDataPatchKey(patch, context,
1984 &patchkey, FALSE);
1985 RegCloseKey(patchkey);
1986 if (temp != ERROR_SUCCESS)
1987 continue;
1990 else if (context == MSIINSTALLCONTEXT_MACHINE)
1992 usersid = szEmpty;
1994 if (MSIREG_OpenUserDataProductKey(prodcode, context, NULL, &localprod, FALSE) == ERROR_SUCCESS &&
1995 RegOpenKeyExW(localprod, szPatches, 0, KEY_READ, &localpatch) == ERROR_SUCCESS &&
1996 RegOpenKeyExW(localpatch, ptr, 0, KEY_READ, &patchkey) == ERROR_SUCCESS)
1998 res = RegGetValueW(patchkey, NULL, szState, RRF_RT_REG_DWORD,
1999 &type, &state, &size);
2001 if (!(filter & state))
2002 res = ERROR_NO_MORE_ITEMS;
2004 RegCloseKey(patchkey);
2007 RegCloseKey(localpatch);
2008 RegCloseKey(localprod);
2010 if (res != ERROR_SUCCESS)
2011 continue;
2014 if (*idx < index)
2016 (*idx)++;
2017 continue;
2020 r = ERROR_SUCCESS;
2021 if (targetprod)
2022 lstrcpyW(targetprod, prodcode);
2024 if (targetctx)
2025 *targetctx = context;
2027 if (targetsid)
2029 lstrcpynW(targetsid, usersid, *sidsize);
2030 if (lstrlenW(usersid) >= *sidsize)
2031 r = ERROR_MORE_DATA;
2034 if (sidsize)
2036 *sidsize = lstrlenW(usersid);
2037 if (!targetsid)
2038 *sidsize *= sizeof(WCHAR);
2042 done:
2043 RegCloseKey(prod);
2044 msi_free(patches);
2046 return r;
2049 static UINT msi_enum_patches(LPCWSTR szProductCode, LPCWSTR szUserSid,
2050 DWORD dwContext, DWORD dwFilter, DWORD dwIndex, DWORD *idx,
2051 LPWSTR szPatchCode, LPWSTR szTargetProductCode,
2052 MSIINSTALLCONTEXT *pdwTargetProductContext, LPWSTR szTargetUserSid,
2053 LPDWORD pcchTargetUserSid, LPWSTR *szTransforms)
2055 LPWSTR usersid = NULL;
2056 UINT r = ERROR_INVALID_PARAMETER;
2058 if (!szUserSid)
2060 get_user_sid(&usersid);
2061 szUserSid = usersid;
2064 if (dwContext & MSIINSTALLCONTEXT_USERMANAGED)
2066 r = msi_check_product_patches(szProductCode, szUserSid,
2067 MSIINSTALLCONTEXT_USERMANAGED, dwFilter,
2068 dwIndex, idx, szPatchCode,
2069 szTargetProductCode,
2070 pdwTargetProductContext, szTargetUserSid,
2071 pcchTargetUserSid, szTransforms);
2072 if (r != ERROR_NO_MORE_ITEMS)
2073 goto done;
2076 if (dwContext & MSIINSTALLCONTEXT_USERUNMANAGED)
2078 r = msi_check_product_patches(szProductCode, szUserSid,
2079 MSIINSTALLCONTEXT_USERUNMANAGED, dwFilter,
2080 dwIndex, idx, szPatchCode,
2081 szTargetProductCode,
2082 pdwTargetProductContext, szTargetUserSid,
2083 pcchTargetUserSid, szTransforms);
2084 if (r != ERROR_NO_MORE_ITEMS)
2085 goto done;
2088 if (dwContext & MSIINSTALLCONTEXT_MACHINE)
2090 r = msi_check_product_patches(szProductCode, szUserSid,
2091 MSIINSTALLCONTEXT_MACHINE, dwFilter,
2092 dwIndex, idx, szPatchCode,
2093 szTargetProductCode,
2094 pdwTargetProductContext, szTargetUserSid,
2095 pcchTargetUserSid, szTransforms);
2096 if (r != ERROR_NO_MORE_ITEMS)
2097 goto done;
2100 done:
2101 LocalFree(usersid);
2102 return r;
2105 /***********************************************************************
2106 * MsiEnumPatchesExW [MSI.@]
2108 UINT WINAPI MsiEnumPatchesExW(LPCWSTR szProductCode, LPCWSTR szUserSid,
2109 DWORD dwContext, DWORD dwFilter, DWORD dwIndex, LPWSTR szPatchCode,
2110 LPWSTR szTargetProductCode, MSIINSTALLCONTEXT *pdwTargetProductContext,
2111 LPWSTR szTargetUserSid, LPDWORD pcchTargetUserSid)
2113 WCHAR squished_pc[GUID_SIZE];
2114 DWORD idx = 0;
2115 UINT r;
2117 static DWORD last_index;
2119 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n",
2120 debugstr_w(szProductCode), debugstr_w(szUserSid), dwContext, dwFilter,
2121 dwIndex, szPatchCode, szTargetProductCode, pdwTargetProductContext,
2122 szTargetUserSid, pcchTargetUserSid);
2124 if (!szProductCode || !squash_guid(szProductCode, squished_pc))
2125 return ERROR_INVALID_PARAMETER;
2127 if (!lstrcmpW(szUserSid, szLocalSid))
2128 return ERROR_INVALID_PARAMETER;
2130 if (dwContext & MSIINSTALLCONTEXT_MACHINE && szUserSid)
2131 return ERROR_INVALID_PARAMETER;
2133 if (dwContext <= MSIINSTALLCONTEXT_NONE ||
2134 dwContext > MSIINSTALLCONTEXT_ALL)
2135 return ERROR_INVALID_PARAMETER;
2137 if (dwFilter <= MSIPATCHSTATE_INVALID || dwFilter > MSIPATCHSTATE_ALL)
2138 return ERROR_INVALID_PARAMETER;
2140 if (dwIndex && dwIndex - last_index != 1)
2141 return ERROR_INVALID_PARAMETER;
2143 if (dwIndex == 0)
2144 last_index = 0;
2146 r = msi_enum_patches(szProductCode, szUserSid, dwContext, dwFilter,
2147 dwIndex, &idx, szPatchCode, szTargetProductCode,
2148 pdwTargetProductContext, szTargetUserSid,
2149 pcchTargetUserSid, NULL);
2151 if (r == ERROR_SUCCESS)
2152 last_index = dwIndex;
2153 else
2154 last_index = 0;
2156 return r;
2159 /***********************************************************************
2160 * MsiEnumPatchesA [MSI.@]
2162 UINT WINAPI MsiEnumPatchesA(LPCSTR szProduct, DWORD iPatchIndex,
2163 LPSTR lpPatchBuf, LPSTR lpTransformsBuf, LPDWORD pcchTransformsBuf)
2165 LPWSTR product, transforms;
2166 WCHAR patch[GUID_SIZE];
2167 DWORD len;
2168 UINT r;
2170 TRACE("(%s %d %p %p %p)\n", debugstr_a(szProduct), iPatchIndex,
2171 lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);
2173 if (!szProduct || !lpPatchBuf || !lpTransformsBuf || !pcchTransformsBuf)
2174 return ERROR_INVALID_PARAMETER;
2176 product = strdupAtoW(szProduct);
2177 if (!product)
2178 return ERROR_OUTOFMEMORY;
2180 len = *pcchTransformsBuf;
2181 transforms = msi_alloc( len * sizeof(WCHAR) );
2182 if (!transforms)
2184 r = ERROR_OUTOFMEMORY;
2185 goto done;
2188 r = MsiEnumPatchesW(product, iPatchIndex, patch, transforms, &len);
2189 if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
2190 goto done;
2192 WideCharToMultiByte(CP_ACP, 0, patch, -1, lpPatchBuf,
2193 GUID_SIZE, NULL, NULL);
2195 if (!WideCharToMultiByte(CP_ACP, 0, transforms, -1, lpTransformsBuf,
2196 *pcchTransformsBuf, NULL, NULL))
2197 r = ERROR_MORE_DATA;
2199 if (r == ERROR_MORE_DATA)
2201 lpTransformsBuf[*pcchTransformsBuf - 1] = '\0';
2202 *pcchTransformsBuf = len * 2;
2204 else
2205 *pcchTransformsBuf = strlen( lpTransformsBuf );
2207 done:
2208 msi_free(transforms);
2209 msi_free(product);
2211 return r;
2214 /***********************************************************************
2215 * MsiEnumPatchesW [MSI.@]
2217 UINT WINAPI MsiEnumPatchesW(LPCWSTR szProduct, DWORD iPatchIndex,
2218 LPWSTR lpPatchBuf, LPWSTR lpTransformsBuf, LPDWORD pcchTransformsBuf)
2220 WCHAR squished_pc[GUID_SIZE];
2221 LPWSTR transforms = NULL;
2222 HKEY prod;
2223 DWORD idx = 0;
2224 UINT r;
2226 TRACE("(%s %d %p %p %p)\n", debugstr_w(szProduct), iPatchIndex,
2227 lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);
2229 if (!szProduct || !squash_guid(szProduct, squished_pc))
2230 return ERROR_INVALID_PARAMETER;
2232 if (!lpPatchBuf || !lpTransformsBuf || !pcchTransformsBuf)
2233 return ERROR_INVALID_PARAMETER;
2235 if (MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERMANAGED,
2236 &prod, FALSE) != ERROR_SUCCESS &&
2237 MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERUNMANAGED,
2238 &prod, FALSE) != ERROR_SUCCESS &&
2239 MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_MACHINE,
2240 &prod, FALSE) != ERROR_SUCCESS)
2241 return ERROR_UNKNOWN_PRODUCT;
2243 RegCloseKey(prod);
2245 r = msi_enum_patches(szProduct, NULL, MSIINSTALLCONTEXT_ALL,
2246 MSIPATCHSTATE_ALL, iPatchIndex, &idx, lpPatchBuf,
2247 NULL, NULL, NULL, NULL, &transforms);
2248 if (r != ERROR_SUCCESS)
2249 goto done;
2251 lstrcpynW(lpTransformsBuf, transforms, *pcchTransformsBuf);
2252 if (*pcchTransformsBuf <= lstrlenW(transforms))
2254 r = ERROR_MORE_DATA;
2255 *pcchTransformsBuf = lstrlenW(transforms);
2257 else
2258 *pcchTransformsBuf = lstrlenW(transforms);
2260 done:
2261 msi_free(transforms);
2262 return r;
2265 UINT WINAPI MsiEnumProductsExA( LPCSTR szProductCode, LPCSTR szUserSid,
2266 DWORD dwContext, DWORD dwIndex, CHAR szInstalledProductCode[39],
2267 MSIINSTALLCONTEXT* pdwInstalledContext, LPSTR szSid, LPDWORD pcchSid)
2269 FIXME("%s %s %d %d %p %p %p %p\n", debugstr_a(szProductCode), debugstr_a(szUserSid),
2270 dwContext, dwIndex, szInstalledProductCode, pdwInstalledContext,
2271 szSid, pcchSid);
2272 return ERROR_NO_MORE_ITEMS;
2275 UINT WINAPI MsiEnumProductsExW( LPCWSTR szProductCode, LPCWSTR szUserSid,
2276 DWORD dwContext, DWORD dwIndex, WCHAR szInstalledProductCode[39],
2277 MSIINSTALLCONTEXT* pdwInstalledContext, LPWSTR szSid, LPDWORD pcchSid)
2279 FIXME("%s %s %d %d %p %p %p %p\n", debugstr_w(szProductCode), debugstr_w(szUserSid),
2280 dwContext, dwIndex, szInstalledProductCode, pdwInstalledContext,
2281 szSid, pcchSid);
2282 return ERROR_NO_MORE_ITEMS;