makefiles: Add resource files to the standard object files list.
[wine.git] / dlls / msi / registry.c
blob3ba34d12072aa2e69c808f095425c924a7b68f5a
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 szInstallProperties_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 'I','n','s','t','a','l','l','P','r','o','p','e','r','t','i','e','s',0};
196 static const WCHAR szInstaller_LocalClassesProd[] = {
197 'S','o','f','t','w','a','r','e','\\',
198 'C','l','a','s','s','e','s','\\',
199 'I','n','s','t','a','l','l','e','r','\\',
200 'P','r','o','d','u','c','t','s',0};
202 static const WCHAR szInstaller_LocalClassesProd_fmt[] = {
203 'S','o','f','t','w','a','r','e','\\',
204 'C','l','a','s','s','e','s','\\',
205 'I','n','s','t','a','l','l','e','r','\\',
206 'P','r','o','d','u','c','t','s','\\','%','s',0};
208 static const WCHAR szInstaller_LocalClassesFeat_fmt[] = {
209 'S','o','f','t','w','a','r','e','\\',
210 'C','l','a','s','s','e','s','\\',
211 'I','n','s','t','a','l','l','e','r','\\',
212 'F','e','a','t','u','r','e','s','\\','%','s',0};
214 static const WCHAR szInstaller_LocalManaged_fmt[] = {
215 'S','o','f','t','w','a','r','e','\\',
216 'M','i','c','r','o','s','o','f','t','\\',
217 'W','i','n','d','o','w','s','\\',
218 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
219 'I','n','s','t','a','l','l','e','r','\\',
220 'M','a','n','a','g','e','d','\\','%','s','\\',
221 'I','n','s','t','a','l','l','e','r','\\',
222 'P','r','o','d','u','c','t','s',0};
224 static const WCHAR szInstaller_LocalManagedProd_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','\\','%','s',0};
234 static const WCHAR szInstaller_LocalManagedFeat_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 'F','e','a','t','u','r','e','s','\\','%','s',0};
244 static const WCHAR szInstaller_ClassesUpgrade_fmt[] = {
245 'I','n','s','t','a','l','l','e','r','\\',
246 'U','p','g','r','a','d','e','C','o','d','e','s','\\',
247 '%','s',0};
249 BOOL unsquash_guid(LPCWSTR in, LPWSTR out)
251 DWORD i,n=0;
253 if (lstrlenW(in) != 32)
254 return FALSE;
256 out[n++]='{';
257 for(i=0; i<8; i++)
258 out[n++] = in[7-i];
259 out[n++]='-';
260 for(i=0; i<4; i++)
261 out[n++] = in[11-i];
262 out[n++]='-';
263 for(i=0; i<4; i++)
264 out[n++] = in[15-i];
265 out[n++]='-';
266 for(i=0; i<2; i++)
268 out[n++] = in[17+i*2];
269 out[n++] = in[16+i*2];
271 out[n++]='-';
272 for( ; i<8; i++)
274 out[n++] = in[17+i*2];
275 out[n++] = in[16+i*2];
277 out[n++]='}';
278 out[n]=0;
279 return TRUE;
282 BOOL squash_guid(LPCWSTR in, LPWSTR out)
284 DWORD i,n=1;
285 GUID guid;
287 out[0] = 0;
289 if (FAILED(CLSIDFromString((LPOLESTR)in, &guid)))
290 return FALSE;
292 for(i=0; i<8; i++)
293 out[7-i] = in[n++];
294 n++;
295 for(i=0; i<4; i++)
296 out[11-i] = in[n++];
297 n++;
298 for(i=0; i<4; i++)
299 out[15-i] = in[n++];
300 n++;
301 for(i=0; i<2; i++)
303 out[17+i*2] = in[n++];
304 out[16+i*2] = in[n++];
306 n++;
307 for( ; i<8; i++)
309 out[17+i*2] = in[n++];
310 out[16+i*2] = in[n++];
312 out[32]=0;
313 return TRUE;
317 /* tables for encoding and decoding base85 */
318 static const unsigned char table_dec85[0x80] = {
319 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
320 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
321 0xff,0x00,0xff,0xff,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0xff,
322 0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0xff,0xff,0xff,0x16,0xff,0x17,
323 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
324 0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0xff,0x34,0x35,0x36,
325 0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x42,0x43,0x44,0x45,0x46,
326 0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0xff,0x53,0x54,0xff,
329 static const char table_enc85[] =
330 "!$%&'()*+,-.0123456789=?@ABCDEFGHIJKLMNO"
331 "PQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwx"
332 "yz{}~";
335 * Converts a base85 encoded guid into a GUID pointer
336 * Base85 encoded GUIDs should be 20 characters long.
338 * returns TRUE if successful, FALSE if not
340 BOOL decode_base85_guid( LPCWSTR str, GUID *guid )
342 DWORD i, val = 0, base = 1, *p;
344 if (!str)
345 return FALSE;
347 p = (DWORD*) guid;
348 for( i=0; i<20; i++ )
350 if( (i%5) == 0 )
352 val = 0;
353 base = 1;
355 val += table_dec85[str[i]] * base;
356 if( str[i] >= 0x80 )
357 return FALSE;
358 if( table_dec85[str[i]] == 0xff )
359 return FALSE;
360 if( (i%5) == 4 )
361 p[i/5] = val;
362 base *= 85;
364 return TRUE;
368 * Encodes a base85 guid given a GUID pointer
369 * Caller should provide a 21 character buffer for the encoded string.
371 * returns TRUE if successful, FALSE if not
373 BOOL encode_base85_guid( GUID *guid, LPWSTR str )
375 unsigned int x, *p, i;
377 p = (unsigned int*) guid;
378 for( i=0; i<4; i++ )
380 x = p[i];
381 *str++ = table_enc85[x%85];
382 x = x/85;
383 *str++ = table_enc85[x%85];
384 x = x/85;
385 *str++ = table_enc85[x%85];
386 x = x/85;
387 *str++ = table_enc85[x%85];
388 x = x/85;
389 *str++ = table_enc85[x%85];
391 *str = 0;
393 return TRUE;
396 DWORD msi_version_str_to_dword(LPCWSTR p)
398 DWORD major, minor = 0, build = 0, version = 0;
400 if (!p)
401 return version;
403 major = atoiW(p);
405 p = strchrW(p, '.');
406 if (p)
408 minor = atoiW(p+1);
409 p = strchrW(p+1, '.');
410 if (p)
411 build = atoiW(p+1);
414 return MAKELONG(build, MAKEWORD(minor, major));
417 LONG msi_reg_set_val_str( HKEY hkey, LPCWSTR name, LPCWSTR value )
419 static const WCHAR emptyW[] = {0};
420 DWORD len;
421 if (!value) value = emptyW;
422 len = (lstrlenW(value) + 1) * sizeof (WCHAR);
423 return RegSetValueExW( hkey, name, 0, REG_SZ, (const BYTE *)value, len );
426 LONG msi_reg_set_val_multi_str( HKEY hkey, LPCWSTR name, LPCWSTR value )
428 LPCWSTR p = value;
429 while (*p) p += lstrlenW(p) + 1;
430 return RegSetValueExW( hkey, name, 0, REG_MULTI_SZ,
431 (const BYTE *)value, (p + 1 - value) * sizeof(WCHAR) );
434 LONG msi_reg_set_val_dword( HKEY hkey, LPCWSTR name, DWORD val )
436 return RegSetValueExW( hkey, name, 0, REG_DWORD, (LPBYTE)&val, sizeof (DWORD) );
439 LONG msi_reg_set_subkey_val( HKEY hkey, LPCWSTR path, LPCWSTR name, LPCWSTR val )
441 HKEY hsubkey = 0;
442 LONG r;
444 r = RegCreateKeyW( hkey, path, &hsubkey );
445 if (r != ERROR_SUCCESS)
446 return r;
447 r = msi_reg_set_val_str( hsubkey, name, val );
448 RegCloseKey( hsubkey );
449 return r;
452 LPWSTR msi_reg_get_val_str( HKEY hkey, LPCWSTR name )
454 DWORD len = 0;
455 LPWSTR val;
456 LONG r;
458 r = RegQueryValueExW(hkey, name, NULL, NULL, NULL, &len);
459 if (r != ERROR_SUCCESS)
460 return NULL;
462 len += sizeof (WCHAR);
463 val = msi_alloc( len );
464 if (!val)
465 return NULL;
466 val[0] = 0;
467 RegQueryValueExW(hkey, name, NULL, NULL, (LPBYTE) val, &len);
468 return val;
471 BOOL msi_reg_get_val_dword( HKEY hkey, LPCWSTR name, DWORD *val)
473 DWORD type, len = sizeof (DWORD);
474 LONG r = RegQueryValueExW(hkey, name, NULL, &type, (LPBYTE) val, &len);
475 return r == ERROR_SUCCESS && type == REG_DWORD;
478 static UINT get_user_sid(LPWSTR *usersid)
480 HANDLE token;
481 BYTE buf[1024];
482 DWORD size;
483 PTOKEN_USER user;
485 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token))
486 return ERROR_FUNCTION_FAILED;
488 size = sizeof(buf);
489 if (!GetTokenInformation(token, TokenUser, buf, size, &size)) {
490 CloseHandle(token);
491 return ERROR_FUNCTION_FAILED;
494 user = (PTOKEN_USER)buf;
495 if (!ConvertSidToStringSidW(user->User.Sid, usersid)) {
496 CloseHandle(token);
497 return ERROR_FUNCTION_FAILED;
499 CloseHandle(token);
500 return ERROR_SUCCESS;
503 UINT MSIREG_OpenUninstallKey(LPCWSTR szProduct, HKEY* key, BOOL create)
505 UINT rc;
506 WCHAR keypath[0x200];
507 TRACE("%s\n",debugstr_w(szProduct));
509 sprintfW(keypath,szUninstall_fmt,szProduct);
511 if (create)
512 rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
513 else
514 rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
516 return rc;
519 UINT MSIREG_DeleteUninstallKey(LPCWSTR szProduct)
521 WCHAR keypath[0x200];
522 TRACE("%s\n",debugstr_w(szProduct));
524 sprintfW(keypath,szUninstall_fmt,szProduct);
526 return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
529 UINT MSIREG_OpenProductKey(LPCWSTR szProduct, LPCWSTR szUserSid,
530 MSIINSTALLCONTEXT context, HKEY *key, BOOL create)
532 UINT r;
533 LPWSTR usersid = NULL;
534 HKEY root = HKEY_LOCAL_MACHINE;
535 WCHAR squished_pc[GUID_SIZE];
536 WCHAR keypath[MAX_PATH];
538 TRACE("(%s, %d, %d)\n", debugstr_w(szProduct), context, create);
540 if (!squash_guid(szProduct, squished_pc))
541 return ERROR_FUNCTION_FAILED;
543 TRACE("squished (%s)\n", debugstr_w(squished_pc));
545 if (context == MSIINSTALLCONTEXT_MACHINE)
547 sprintfW(keypath, szInstaller_LocalClassesProd_fmt, squished_pc);
549 else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
551 root = HKEY_CURRENT_USER;
552 sprintfW(keypath, szUserProduct_fmt, squished_pc);
554 else
556 if (!szUserSid)
558 r = get_user_sid(&usersid);
559 if (r != ERROR_SUCCESS || !usersid)
561 ERR("Failed to retrieve user SID: %d\n", r);
562 return r;
565 szUserSid = usersid;
568 sprintfW(keypath, szInstaller_LocalManagedProd_fmt,
569 szUserSid, squished_pc);
570 LocalFree(usersid);
573 if (create)
574 return RegCreateKeyW(root, keypath, key);
576 return RegOpenKeyW(root, keypath, key);
579 UINT MSIREG_DeleteUserProductKey(LPCWSTR szProduct)
581 WCHAR squished_pc[GUID_SIZE];
582 WCHAR keypath[0x200];
584 TRACE("%s\n",debugstr_w(szProduct));
585 if (!squash_guid(szProduct,squished_pc))
586 return ERROR_FUNCTION_FAILED;
587 TRACE("squished (%s)\n", debugstr_w(squished_pc));
589 sprintfW(keypath,szUserProduct_fmt,squished_pc);
591 return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
594 UINT MSIREG_OpenUserPatchesKey(LPCWSTR szPatch, HKEY* key, BOOL create)
596 UINT rc;
597 WCHAR squished_pc[GUID_SIZE];
598 WCHAR keypath[0x200];
600 TRACE("%s\n",debugstr_w(szPatch));
601 if (!squash_guid(szPatch,squished_pc))
602 return ERROR_FUNCTION_FAILED;
603 TRACE("squished (%s)\n", debugstr_w(squished_pc));
605 sprintfW(keypath,szUserPatch_fmt,squished_pc);
607 if (create)
608 rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
609 else
610 rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
612 return rc;
615 UINT MSIREG_OpenFeaturesKey(LPCWSTR szProduct, MSIINSTALLCONTEXT context,
616 HKEY *key, BOOL create)
618 UINT r;
619 LPWSTR usersid;
620 HKEY root = HKEY_LOCAL_MACHINE;
621 WCHAR squished_pc[GUID_SIZE];
622 WCHAR keypath[MAX_PATH];
624 TRACE("(%s, %d, %d)\n", debugstr_w(szProduct), context, create);
626 if (!squash_guid(szProduct, squished_pc))
627 return ERROR_FUNCTION_FAILED;
629 TRACE("squished (%s)\n", debugstr_w(squished_pc));
631 if (context == MSIINSTALLCONTEXT_MACHINE)
633 sprintfW(keypath, szInstaller_LocalClassesFeat_fmt, squished_pc);
635 else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
637 root = HKEY_CURRENT_USER;
638 sprintfW(keypath, szUserFeatures_fmt, squished_pc);
640 else
642 r = get_user_sid(&usersid);
643 if (r != ERROR_SUCCESS || !usersid)
645 ERR("Failed to retrieve user SID: %d\n", r);
646 return r;
649 sprintfW(keypath, szInstaller_LocalManagedFeat_fmt, usersid, squished_pc);
650 LocalFree(usersid);
653 if (create)
654 return RegCreateKeyW(root, keypath, key);
656 return RegOpenKeyW(root, keypath, key);
659 UINT MSIREG_DeleteUserFeaturesKey(LPCWSTR szProduct)
661 WCHAR squished_pc[GUID_SIZE];
662 WCHAR keypath[0x200];
664 TRACE("%s\n",debugstr_w(szProduct));
665 if (!squash_guid(szProduct,squished_pc))
666 return ERROR_FUNCTION_FAILED;
667 TRACE("squished (%s)\n", debugstr_w(squished_pc));
669 sprintfW(keypath,szUserFeatures_fmt,squished_pc);
670 return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
673 static UINT MSIREG_OpenInstallerFeaturesKey(LPCWSTR szProduct, HKEY* key, BOOL create)
675 UINT rc;
676 WCHAR squished_pc[GUID_SIZE];
677 WCHAR keypath[0x200];
679 TRACE("%s\n",debugstr_w(szProduct));
680 if (!squash_guid(szProduct,squished_pc))
681 return ERROR_FUNCTION_FAILED;
682 TRACE("squished (%s)\n", debugstr_w(squished_pc));
684 sprintfW(keypath,szInstaller_Features_fmt,squished_pc);
686 if (create)
687 rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
688 else
689 rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
691 return rc;
694 UINT MSIREG_OpenUserDataFeaturesKey(LPCWSTR szProduct, MSIINSTALLCONTEXT context,
695 HKEY *key, BOOL create)
697 UINT r;
698 LPWSTR usersid;
699 WCHAR squished_pc[GUID_SIZE];
700 WCHAR keypath[0x200];
702 TRACE("(%s, %d, %d)\n", debugstr_w(szProduct), context, create);
704 if (!squash_guid(szProduct, squished_pc))
705 return ERROR_FUNCTION_FAILED;
707 TRACE("squished (%s)\n", debugstr_w(squished_pc));
709 if (context == MSIINSTALLCONTEXT_MACHINE)
711 sprintfW(keypath, szUserDataFeatures_fmt, szLocalSid, squished_pc);
713 else
715 r = get_user_sid(&usersid);
716 if (r != ERROR_SUCCESS || !usersid)
718 ERR("Failed to retrieve user SID: %d\n", r);
719 return r;
722 sprintfW(keypath, szUserDataFeatures_fmt, usersid, squished_pc);
723 LocalFree(usersid);
726 if (create)
727 return RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
729 return RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
732 UINT MSIREG_OpenUserComponentsKey(LPCWSTR szComponent, HKEY* key, BOOL create)
734 UINT rc;
735 WCHAR squished_cc[GUID_SIZE];
736 WCHAR keypath[0x200];
738 TRACE("%s\n",debugstr_w(szComponent));
739 if (!squash_guid(szComponent,squished_cc))
740 return ERROR_FUNCTION_FAILED;
741 TRACE("squished (%s)\n", debugstr_w(squished_cc));
743 sprintfW(keypath,szUser_Components_fmt,squished_cc);
745 if (create)
746 rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
747 else
748 rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
750 return rc;
753 UINT MSIREG_OpenUserDataComponentKey(LPCWSTR szComponent, LPCWSTR szUserSid,
754 HKEY *key, BOOL create)
756 UINT rc;
757 WCHAR comp[GUID_SIZE];
758 WCHAR keypath[0x200];
759 LPWSTR usersid;
761 TRACE("%s\n", debugstr_w(szComponent));
762 if (!squash_guid(szComponent, comp))
763 return ERROR_FUNCTION_FAILED;
764 TRACE("squished (%s)\n", debugstr_w(comp));
766 if (!szUserSid)
768 rc = get_user_sid(&usersid);
769 if (rc != ERROR_SUCCESS || !usersid)
771 ERR("Failed to retrieve user SID: %d\n", rc);
772 return rc;
775 sprintfW(keypath, szUserDataComp_fmt, usersid, comp);
776 LocalFree(usersid);
778 else
779 sprintfW(keypath, szUserDataComp_fmt, szUserSid, comp);
781 if (create)
782 rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
783 else
784 rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
786 return rc;
789 UINT MSIREG_DeleteUserDataComponentKey(LPCWSTR szComponent, LPCWSTR szUserSid)
791 UINT rc;
792 WCHAR comp[GUID_SIZE];
793 WCHAR keypath[0x200];
794 LPWSTR usersid;
796 TRACE("%s\n", debugstr_w(szComponent));
797 if (!squash_guid(szComponent, comp))
798 return ERROR_FUNCTION_FAILED;
799 TRACE("squished (%s)\n", debugstr_w(comp));
801 if (!szUserSid)
803 rc = get_user_sid(&usersid);
804 if (rc != ERROR_SUCCESS || !usersid)
806 ERR("Failed to retrieve user SID: %d\n", rc);
807 return rc;
810 sprintfW(keypath, szUserDataComp_fmt, usersid, comp);
811 LocalFree(usersid);
813 else
814 sprintfW(keypath, szUserDataComp_fmt, szUserSid, comp);
816 return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
819 UINT MSIREG_OpenUserDataProductKey(LPCWSTR szProduct, MSIINSTALLCONTEXT dwContext,
820 LPCWSTR szUserSid, HKEY *key, BOOL create)
822 UINT rc;
823 WCHAR squished_pc[GUID_SIZE];
824 WCHAR keypath[0x200];
825 LPWSTR usersid;
827 TRACE("%s\n", debugstr_w(szProduct));
828 if (!squash_guid(szProduct, squished_pc))
829 return ERROR_FUNCTION_FAILED;
830 TRACE("squished (%s)\n", debugstr_w(squished_pc));
832 if (dwContext == MSIINSTALLCONTEXT_MACHINE)
833 sprintfW(keypath, szUserDataProd_fmt, szLocalSid, squished_pc);
834 else if (szUserSid)
835 sprintfW(keypath, szUserDataProd_fmt, szUserSid, squished_pc);
836 else
838 rc = get_user_sid(&usersid);
839 if (rc != ERROR_SUCCESS || !usersid)
841 ERR("Failed to retrieve user SID: %d\n", rc);
842 return rc;
845 sprintfW(keypath, szUserDataProd_fmt, usersid, squished_pc);
846 LocalFree(usersid);
849 if (create)
850 rc = RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
851 else
852 rc = RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
854 return rc;
857 UINT MSIREG_OpenUserDataPatchKey(LPCWSTR szPatch, MSIINSTALLCONTEXT dwContext,
858 HKEY *key, BOOL create)
860 UINT rc;
861 WCHAR squished_patch[GUID_SIZE];
862 WCHAR keypath[0x200];
863 LPWSTR usersid;
865 TRACE("%s\n", debugstr_w(szPatch));
866 if (!squash_guid(szPatch, squished_patch))
867 return ERROR_FUNCTION_FAILED;
868 TRACE("squished (%s)\n", debugstr_w(squished_patch));
870 if (dwContext == MSIINSTALLCONTEXT_MACHINE)
871 sprintfW(keypath, szUserDataPatch_fmt, szLocalSid, squished_patch);
872 else
874 rc = get_user_sid(&usersid);
875 if (rc != ERROR_SUCCESS || !usersid)
877 ERR("Failed to retrieve user SID: %d\n", rc);
878 return rc;
881 sprintfW(keypath, szUserDataPatch_fmt, usersid, squished_patch);
882 LocalFree(usersid);
885 if (create)
886 return RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
888 return RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
891 UINT MSIREG_OpenInstallProps(LPCWSTR szProduct, MSIINSTALLCONTEXT dwContext,
892 LPCWSTR szUserSid, HKEY *key, BOOL create)
894 UINT rc;
895 LPWSTR usersid;
896 WCHAR squished_pc[GUID_SIZE];
897 WCHAR keypath[0x200];
899 TRACE("%s\n", debugstr_w(szProduct));
900 if (!squash_guid(szProduct, squished_pc))
901 return ERROR_FUNCTION_FAILED;
902 TRACE("squished (%s)\n", debugstr_w(squished_pc));
904 if (dwContext == MSIINSTALLCONTEXT_MACHINE)
905 sprintfW(keypath, szInstallProperties_fmt, szLocalSid, squished_pc);
906 else if (szUserSid)
907 sprintfW(keypath, szInstallProperties_fmt, szUserSid, squished_pc);
908 else
910 rc = get_user_sid(&usersid);
911 if (rc != ERROR_SUCCESS || !usersid)
913 ERR("Failed to retrieve user SID: %d\n", rc);
914 return rc;
917 sprintfW(keypath, szInstallProperties_fmt, usersid, squished_pc);
918 LocalFree(usersid);
921 if (create)
922 return RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, key);
924 return RegOpenKeyW(HKEY_LOCAL_MACHINE, keypath, key);
927 UINT MSIREG_DeleteUserDataProductKey(LPCWSTR szProduct)
929 UINT rc;
930 WCHAR squished_pc[GUID_SIZE];
931 WCHAR keypath[0x200];
932 LPWSTR usersid;
934 TRACE("%s\n", debugstr_w(szProduct));
935 if (!squash_guid(szProduct, squished_pc))
936 return ERROR_FUNCTION_FAILED;
937 TRACE("squished (%s)\n", debugstr_w(squished_pc));
939 rc = get_user_sid(&usersid);
940 if (rc != ERROR_SUCCESS || !usersid)
942 ERR("Failed to retrieve user SID: %d\n", rc);
943 return rc;
946 sprintfW(keypath, szUserDataProd_fmt, usersid, squished_pc);
948 LocalFree(usersid);
949 return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
952 UINT MSIREG_DeleteProductKey(LPCWSTR szProduct)
954 WCHAR squished_pc[GUID_SIZE];
955 WCHAR keypath[0x200];
957 TRACE("%s\n", debugstr_w(szProduct));
958 if (!squash_guid(szProduct, squished_pc))
959 return ERROR_FUNCTION_FAILED;
960 TRACE("squished (%s)\n", debugstr_w(squished_pc));
962 sprintfW(keypath, szInstaller_Products_fmt, squished_pc);
964 return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
967 UINT MSIREG_OpenPatchesKey(LPCWSTR szPatch, HKEY* key, BOOL create)
969 UINT rc;
970 WCHAR squished_pc[GUID_SIZE];
971 WCHAR keypath[0x200];
973 TRACE("%s\n",debugstr_w(szPatch));
974 if (!squash_guid(szPatch,squished_pc))
975 return ERROR_FUNCTION_FAILED;
976 TRACE("squished (%s)\n", debugstr_w(squished_pc));
978 sprintfW(keypath,szInstaller_Patches_fmt,squished_pc);
980 if (create)
981 rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
982 else
983 rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
985 return rc;
988 UINT MSIREG_OpenUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY* key, BOOL create)
990 UINT rc;
991 WCHAR squished_pc[GUID_SIZE];
992 WCHAR keypath[0x200];
994 TRACE("%s\n",debugstr_w(szUpgradeCode));
995 if (!squash_guid(szUpgradeCode,squished_pc))
996 return ERROR_FUNCTION_FAILED;
997 TRACE("squished (%s)\n", debugstr_w(squished_pc));
999 sprintfW(keypath,szInstaller_UpgradeCodes_fmt,squished_pc);
1001 if (create)
1002 rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,keypath,key);
1003 else
1004 rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,keypath,key);
1006 return rc;
1009 UINT MSIREG_OpenUserUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY* key, BOOL create)
1011 UINT rc;
1012 WCHAR squished_pc[GUID_SIZE];
1013 WCHAR keypath[0x200];
1015 TRACE("%s\n",debugstr_w(szUpgradeCode));
1016 if (!squash_guid(szUpgradeCode,squished_pc))
1017 return ERROR_FUNCTION_FAILED;
1018 TRACE("squished (%s)\n", debugstr_w(squished_pc));
1020 sprintfW(keypath,szInstaller_UserUpgradeCodes_fmt,squished_pc);
1022 if (create)
1023 rc = RegCreateKeyW(HKEY_CURRENT_USER,keypath,key);
1024 else
1025 rc = RegOpenKeyW(HKEY_CURRENT_USER,keypath,key);
1027 return rc;
1030 UINT MSIREG_DeleteUserUpgradeCodesKey(LPCWSTR szUpgradeCode)
1032 WCHAR squished_pc[GUID_SIZE];
1033 WCHAR keypath[0x200];
1035 TRACE("%s\n",debugstr_w(szUpgradeCode));
1036 if (!squash_guid(szUpgradeCode,squished_pc))
1037 return ERROR_FUNCTION_FAILED;
1038 TRACE("squished (%s)\n", debugstr_w(squished_pc));
1040 sprintfW(keypath,szInstaller_UserUpgradeCodes_fmt,squished_pc);
1042 return RegDeleteTreeW(HKEY_CURRENT_USER, keypath);
1045 UINT MSIREG_DeleteLocalClassesProductKey(LPCWSTR szProductCode)
1047 WCHAR squished_pc[GUID_SIZE];
1048 WCHAR keypath[0x200];
1050 TRACE("%s\n", debugstr_w(szProductCode));
1052 if (!squash_guid(szProductCode, squished_pc))
1053 return ERROR_FUNCTION_FAILED;
1055 TRACE("squished (%s)\n", debugstr_w(squished_pc));
1057 sprintfW(keypath, szInstaller_LocalClassesProd_fmt, squished_pc);
1059 return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
1062 UINT MSIREG_DeleteLocalClassesFeaturesKey(LPCWSTR szProductCode)
1064 WCHAR squished_pc[GUID_SIZE];
1065 WCHAR keypath[0x200];
1067 TRACE("%s\n", debugstr_w(szProductCode));
1069 if (!squash_guid(szProductCode, squished_pc))
1070 return ERROR_FUNCTION_FAILED;
1072 TRACE("squished (%s)\n", debugstr_w(squished_pc));
1074 sprintfW(keypath, szInstaller_LocalClassesFeat_fmt, squished_pc);
1076 return RegDeleteTreeW(HKEY_LOCAL_MACHINE, keypath);
1079 UINT MSIREG_OpenClassesUpgradeCodesKey(LPCWSTR szUpgradeCode, HKEY* key, BOOL create)
1081 WCHAR squished_pc[GUID_SIZE];
1082 WCHAR keypath[0x200];
1084 TRACE("%s\n", debugstr_w(szUpgradeCode));
1085 if (!squash_guid(szUpgradeCode, squished_pc))
1086 return ERROR_FUNCTION_FAILED;
1087 TRACE("squished (%s)\n", debugstr_w(squished_pc));
1089 sprintfW(keypath, szInstaller_ClassesUpgrade_fmt, squished_pc);
1091 if (create)
1092 return RegCreateKeyW(HKEY_CLASSES_ROOT, keypath, key);
1094 return RegOpenKeyW(HKEY_CLASSES_ROOT, keypath, key);
1097 /*************************************************************************
1098 * MsiDecomposeDescriptorW [MSI.@]
1100 * Decomposes an MSI descriptor into product, feature and component parts.
1101 * An MSI descriptor is a string of the form:
1102 * [base 85 guid] [feature code] '>' [base 85 guid]
1104 * PARAMS
1105 * szDescriptor [I] the descriptor to decompose
1106 * szProduct [O] buffer of MAX_FEATURE_CHARS+1 for the product guid
1107 * szFeature [O] buffer of MAX_FEATURE_CHARS+1 for the feature code
1108 * szComponent [O] buffer of MAX_FEATURE_CHARS+1 for the component guid
1109 * pUsed [O] the length of the descriptor
1111 * RETURNS
1112 * ERROR_SUCCESS if everything worked correctly
1113 * ERROR_INVALID_PARAMETER if the descriptor was invalid
1116 UINT WINAPI MsiDecomposeDescriptorW( LPCWSTR szDescriptor, LPWSTR szProduct,
1117 LPWSTR szFeature, LPWSTR szComponent, LPDWORD pUsed )
1119 UINT r, len;
1120 LPWSTR p;
1121 GUID product, component;
1123 TRACE("%s %p %p %p %p\n", debugstr_w(szDescriptor), szProduct,
1124 szFeature, szComponent, pUsed);
1126 r = decode_base85_guid( szDescriptor, &product );
1127 if( !r )
1128 return ERROR_INVALID_PARAMETER;
1130 TRACE("product %s\n", debugstr_guid( &product ));
1132 p = strchrW(&szDescriptor[20],'>');
1133 if( !p )
1134 return ERROR_INVALID_PARAMETER;
1136 len = (p - &szDescriptor[20]);
1137 if( len > MAX_FEATURE_CHARS )
1138 return ERROR_INVALID_PARAMETER;
1140 TRACE("feature %s\n", debugstr_wn( &szDescriptor[20], len ));
1142 r = decode_base85_guid( p+1, &component );
1143 if( !r )
1144 return ERROR_INVALID_PARAMETER;
1146 TRACE("component %s\n", debugstr_guid( &component ));
1148 if (szProduct)
1149 StringFromGUID2( &product, szProduct, MAX_FEATURE_CHARS+1 );
1150 if (szComponent)
1151 StringFromGUID2( &component, szComponent, MAX_FEATURE_CHARS+1 );
1152 if (szFeature)
1154 memcpy( szFeature, &szDescriptor[20], len*sizeof(WCHAR) );
1155 szFeature[len] = 0;
1157 len = ( &p[21] - szDescriptor );
1159 TRACE("length = %d\n", len);
1160 *pUsed = len;
1162 return ERROR_SUCCESS;
1165 UINT WINAPI MsiDecomposeDescriptorA( LPCSTR szDescriptor, LPSTR szProduct,
1166 LPSTR szFeature, LPSTR szComponent, LPDWORD pUsed )
1168 WCHAR product[MAX_FEATURE_CHARS+1];
1169 WCHAR feature[MAX_FEATURE_CHARS+1];
1170 WCHAR component[MAX_FEATURE_CHARS+1];
1171 LPWSTR str = NULL, p = NULL, f = NULL, c = NULL;
1172 UINT r;
1174 TRACE("%s %p %p %p %p\n", debugstr_a(szDescriptor), szProduct,
1175 szFeature, szComponent, pUsed);
1177 str = strdupAtoW( szDescriptor );
1178 if( szDescriptor && !str )
1179 return ERROR_OUTOFMEMORY;
1181 if (szProduct)
1182 p = product;
1183 if (szFeature)
1184 f = feature;
1185 if (szComponent)
1186 c = component;
1188 r = MsiDecomposeDescriptorW( str, p, f, c, pUsed );
1190 if (r == ERROR_SUCCESS)
1192 WideCharToMultiByte( CP_ACP, 0, p, -1,
1193 szProduct, MAX_FEATURE_CHARS+1, NULL, NULL );
1194 WideCharToMultiByte( CP_ACP, 0, f, -1,
1195 szFeature, MAX_FEATURE_CHARS+1, NULL, NULL );
1196 WideCharToMultiByte( CP_ACP, 0, c, -1,
1197 szComponent, MAX_FEATURE_CHARS+1, NULL, NULL );
1200 msi_free( str );
1202 return r;
1205 UINT WINAPI MsiEnumProductsA(DWORD index, LPSTR lpguid)
1207 DWORD r;
1208 WCHAR szwGuid[GUID_SIZE];
1210 TRACE("%d %p\n", index, lpguid);
1212 if (NULL == lpguid)
1213 return ERROR_INVALID_PARAMETER;
1214 r = MsiEnumProductsW(index, szwGuid);
1215 if( r == ERROR_SUCCESS )
1216 WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
1218 return r;
1221 UINT WINAPI MsiEnumProductsW(DWORD index, LPWSTR lpguid)
1223 UINT r;
1224 WCHAR szKeyName[SQUISH_GUID_SIZE];
1225 HKEY key;
1226 DWORD machine_count, managed_count, unmanaged_count;
1227 WCHAR keypath[MAX_PATH];
1228 LPWSTR usersid = NULL;
1230 static DWORD last_index;
1232 TRACE("%d %p\n", index, lpguid);
1234 if (NULL == lpguid)
1235 return ERROR_INVALID_PARAMETER;
1237 if (index && index - last_index != 1)
1238 return ERROR_INVALID_PARAMETER;
1240 r = RegCreateKeyW(HKEY_LOCAL_MACHINE, szInstaller_LocalClassesProd, &key);
1241 if( r != ERROR_SUCCESS )
1242 return ERROR_NO_MORE_ITEMS;
1244 r = RegQueryInfoKeyW(key, NULL, NULL, NULL, &machine_count, NULL, NULL,
1245 NULL, NULL, NULL, NULL, NULL);
1246 if( r != ERROR_SUCCESS )
1248 RegCloseKey(key);
1249 return ERROR_NO_MORE_ITEMS;
1252 if (machine_count && index <= machine_count)
1254 r = RegEnumKeyW(key, index, szKeyName, SQUISH_GUID_SIZE);
1255 if( r == ERROR_SUCCESS )
1257 unsquash_guid(szKeyName, lpguid);
1258 last_index = index;
1259 RegCloseKey(key);
1260 return ERROR_SUCCESS;
1263 RegCloseKey(key);
1265 r = get_user_sid(&usersid);
1266 if (r != ERROR_SUCCESS || !usersid)
1268 ERR("Failed to retrieve user SID: %d\n", r);
1269 return r;
1271 sprintfW(keypath, szInstaller_LocalManaged_fmt, usersid);
1272 LocalFree(usersid);
1274 r = RegCreateKeyW(HKEY_LOCAL_MACHINE, keypath, &key);
1275 if( r != ERROR_SUCCESS )
1276 return ERROR_NO_MORE_ITEMS;
1278 r = RegQueryInfoKeyW(key, NULL, NULL, NULL, &managed_count, NULL, NULL,
1279 NULL, NULL, NULL, NULL, NULL);
1280 if( r != ERROR_SUCCESS )
1282 RegCloseKey(key);
1283 return ERROR_NO_MORE_ITEMS;
1286 if (managed_count && index <= machine_count + managed_count)
1288 r = RegEnumKeyW(key, index - machine_count, szKeyName, SQUISH_GUID_SIZE);
1289 if( r == ERROR_SUCCESS )
1291 unsquash_guid(szKeyName, lpguid);
1292 last_index = index;
1293 RegCloseKey(key);
1294 return ERROR_SUCCESS;
1297 RegCloseKey(key);
1299 r = RegCreateKeyW(HKEY_CURRENT_USER, szUserProduct, &key);
1300 if( r != ERROR_SUCCESS )
1301 return ERROR_NO_MORE_ITEMS;
1303 r = RegQueryInfoKeyW(key, NULL, NULL, NULL, &unmanaged_count, NULL, NULL,
1304 NULL, NULL, NULL, NULL, NULL);
1305 if( r != ERROR_SUCCESS )
1307 RegCloseKey(key);
1308 return ERROR_NO_MORE_ITEMS;
1311 if (unmanaged_count && index <= machine_count + managed_count + unmanaged_count)
1313 r = RegEnumKeyW(key, index - machine_count - managed_count, szKeyName, SQUISH_GUID_SIZE);
1314 if( r == ERROR_SUCCESS )
1316 unsquash_guid(szKeyName, lpguid);
1317 last_index = index;
1318 RegCloseKey(key);
1319 return ERROR_SUCCESS;
1322 RegCloseKey(key);
1324 return ERROR_NO_MORE_ITEMS;
1327 UINT WINAPI MsiEnumFeaturesA(LPCSTR szProduct, DWORD index,
1328 LPSTR szFeature, LPSTR szParent)
1330 DWORD r;
1331 WCHAR szwFeature[GUID_SIZE], szwParent[GUID_SIZE];
1332 LPWSTR szwProduct = NULL;
1334 TRACE("%s %d %p %p\n", debugstr_a(szProduct), index, szFeature, szParent);
1336 if( szProduct )
1338 szwProduct = strdupAtoW( szProduct );
1339 if( !szwProduct )
1340 return ERROR_OUTOFMEMORY;
1343 r = MsiEnumFeaturesW(szwProduct, index, szwFeature, szwParent);
1344 if( r == ERROR_SUCCESS )
1346 WideCharToMultiByte(CP_ACP, 0, szwFeature, -1,
1347 szFeature, GUID_SIZE, NULL, NULL);
1348 WideCharToMultiByte(CP_ACP, 0, szwParent, -1,
1349 szParent, GUID_SIZE, NULL, NULL);
1352 msi_free( szwProduct);
1354 return r;
1357 UINT WINAPI MsiEnumFeaturesW(LPCWSTR szProduct, DWORD index,
1358 LPWSTR szFeature, LPWSTR szParent)
1360 HKEY hkeyProduct = 0;
1361 DWORD r, sz;
1363 TRACE("%s %d %p %p\n", debugstr_w(szProduct), index, szFeature, szParent);
1365 if( !szProduct )
1366 return ERROR_INVALID_PARAMETER;
1368 r = MSIREG_OpenInstallerFeaturesKey(szProduct,&hkeyProduct,FALSE);
1369 if( r != ERROR_SUCCESS )
1370 return ERROR_NO_MORE_ITEMS;
1372 sz = GUID_SIZE;
1373 r = RegEnumValueW(hkeyProduct, index, szFeature, &sz, NULL, NULL, NULL, NULL);
1374 RegCloseKey(hkeyProduct);
1376 return r;
1379 UINT WINAPI MsiEnumComponentsA(DWORD index, LPSTR lpguid)
1381 DWORD r;
1382 WCHAR szwGuid[GUID_SIZE];
1384 TRACE("%d %p\n", index, lpguid);
1386 r = MsiEnumComponentsW(index, szwGuid);
1387 if( r == ERROR_SUCCESS )
1388 WideCharToMultiByte(CP_ACP, 0, szwGuid, -1, lpguid, GUID_SIZE, NULL, NULL);
1390 return r;
1393 UINT WINAPI MsiEnumComponentsW(DWORD index, LPWSTR lpguid)
1395 HKEY hkeyComponents = 0;
1396 DWORD r;
1397 WCHAR szKeyName[SQUISH_GUID_SIZE];
1399 TRACE("%d %p\n", index, lpguid);
1401 r = RegCreateKeyW(HKEY_LOCAL_MACHINE, szInstaller_Components, &hkeyComponents);
1402 if( r != ERROR_SUCCESS )
1403 return ERROR_NO_MORE_ITEMS;
1405 r = RegEnumKeyW(hkeyComponents, index, szKeyName, SQUISH_GUID_SIZE);
1406 if( r == ERROR_SUCCESS )
1407 unsquash_guid(szKeyName, lpguid);
1408 RegCloseKey(hkeyComponents);
1410 return r;
1413 UINT WINAPI MsiEnumClientsA(LPCSTR szComponent, DWORD index, LPSTR szProduct)
1415 DWORD r;
1416 WCHAR szwProduct[GUID_SIZE];
1417 LPWSTR szwComponent = NULL;
1419 TRACE("%s %d %p\n", debugstr_a(szComponent), index, szProduct);
1421 if ( !szProduct )
1422 return ERROR_INVALID_PARAMETER;
1424 if( szComponent )
1426 szwComponent = strdupAtoW( szComponent );
1427 if( !szwComponent )
1428 return ERROR_OUTOFMEMORY;
1431 r = MsiEnumClientsW(szComponent?szwComponent:NULL, index, szwProduct);
1432 if( r == ERROR_SUCCESS )
1434 WideCharToMultiByte(CP_ACP, 0, szwProduct, -1,
1435 szProduct, GUID_SIZE, NULL, NULL);
1438 msi_free( szwComponent);
1440 return r;
1443 UINT WINAPI MsiEnumClientsW(LPCWSTR szComponent, DWORD index, LPWSTR szProduct)
1445 HKEY hkeyComp = 0;
1446 DWORD r, sz;
1447 WCHAR szValName[SQUISH_GUID_SIZE];
1449 TRACE("%s %d %p\n", debugstr_w(szComponent), index, szProduct);
1451 if (!szComponent || !*szComponent || !szProduct)
1452 return ERROR_INVALID_PARAMETER;
1454 if (MSIREG_OpenUserDataComponentKey(szComponent, NULL, &hkeyComp, FALSE) != ERROR_SUCCESS &&
1455 MSIREG_OpenUserDataComponentKey(szComponent, szLocalSid, &hkeyComp, FALSE) != ERROR_SUCCESS)
1456 return ERROR_UNKNOWN_COMPONENT;
1458 /* see if there are any products at all */
1459 sz = SQUISH_GUID_SIZE;
1460 r = RegEnumValueW(hkeyComp, 0, szValName, &sz, NULL, NULL, NULL, NULL);
1461 if (r != ERROR_SUCCESS)
1463 RegCloseKey(hkeyComp);
1465 if (index != 0)
1466 return ERROR_INVALID_PARAMETER;
1468 return ERROR_UNKNOWN_COMPONENT;
1471 sz = SQUISH_GUID_SIZE;
1472 r = RegEnumValueW(hkeyComp, index, szValName, &sz, NULL, NULL, NULL, NULL);
1473 if( r == ERROR_SUCCESS )
1474 unsquash_guid(szValName, szProduct);
1476 RegCloseKey(hkeyComp);
1478 return r;
1481 static UINT MSI_EnumComponentQualifiers( LPCWSTR szComponent, DWORD iIndex,
1482 awstring *lpQualBuf, LPDWORD pcchQual,
1483 awstring *lpAppBuf, LPDWORD pcchAppBuf )
1485 DWORD name_sz, val_sz, name_max, val_max, type, ofs;
1486 LPWSTR name = NULL, val = NULL;
1487 UINT r, r2;
1488 HKEY key;
1490 TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
1491 lpQualBuf, pcchQual, lpAppBuf, pcchAppBuf);
1493 if (!szComponent)
1494 return ERROR_INVALID_PARAMETER;
1496 r = MSIREG_OpenUserComponentsKey( szComponent, &key, FALSE );
1497 if (r != ERROR_SUCCESS)
1498 return ERROR_UNKNOWN_COMPONENT;
1500 /* figure out how big the name is we want to return */
1501 name_max = 0x10;
1502 r = ERROR_OUTOFMEMORY;
1503 name = msi_alloc( name_max * sizeof(WCHAR) );
1504 if (!name)
1505 goto end;
1507 val_max = 0x10;
1508 r = ERROR_OUTOFMEMORY;
1509 val = msi_alloc( val_max );
1510 if (!val)
1511 goto end;
1513 /* loop until we allocate enough memory */
1514 while (1)
1516 name_sz = name_max;
1517 val_sz = val_max;
1518 r = RegEnumValueW( key, iIndex, name, &name_sz,
1519 NULL, &type, (LPBYTE)val, &val_sz );
1520 if (r == ERROR_SUCCESS)
1521 break;
1522 if (r != ERROR_MORE_DATA)
1523 goto end;
1525 if (type != REG_MULTI_SZ)
1527 ERR("component data has wrong type (%d)\n", type);
1528 goto end;
1531 r = ERROR_OUTOFMEMORY;
1532 if ((name_sz+1) >= name_max)
1534 name_max *= 2;
1535 msi_free( name );
1536 name = msi_alloc( name_max * sizeof (WCHAR) );
1537 if (!name)
1538 goto end;
1539 continue;
1541 if (val_sz > val_max)
1543 val_max = val_sz + sizeof (WCHAR);
1544 msi_free( val );
1545 val = msi_alloc( val_max * sizeof (WCHAR) );
1546 if (!val)
1547 goto end;
1548 continue;
1550 ERR("should be enough data, but isn't %d %d\n", name_sz, val_sz );
1551 goto end;
1554 ofs = 0;
1555 r = MsiDecomposeDescriptorW( val, NULL, NULL, NULL, &ofs );
1556 if (r != ERROR_SUCCESS)
1557 goto end;
1559 TRACE("Providing %s and %s\n", debugstr_w(name), debugstr_w(val+ofs));
1561 r = msi_strcpy_to_awstring( name, lpQualBuf, pcchQual );
1562 r2 = msi_strcpy_to_awstring( val+ofs, lpAppBuf, pcchAppBuf );
1564 if (r2 != ERROR_SUCCESS)
1565 r = r2;
1567 end:
1568 msi_free(val);
1569 msi_free(name);
1570 RegCloseKey(key);
1572 return r;
1575 /*************************************************************************
1576 * MsiEnumComponentQualifiersA [MSI.@]
1578 UINT WINAPI MsiEnumComponentQualifiersA( LPCSTR szComponent, DWORD iIndex,
1579 LPSTR lpQualifierBuf, LPDWORD pcchQualifierBuf,
1580 LPSTR lpApplicationDataBuf, LPDWORD pcchApplicationDataBuf )
1582 awstring qual, appdata;
1583 LPWSTR comp;
1584 UINT r;
1586 TRACE("%s %08x %p %p %p %p\n", debugstr_a(szComponent), iIndex,
1587 lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
1588 pcchApplicationDataBuf);
1590 comp = strdupAtoW( szComponent );
1591 if (szComponent && !comp)
1592 return ERROR_OUTOFMEMORY;
1594 qual.unicode = FALSE;
1595 qual.str.a = lpQualifierBuf;
1597 appdata.unicode = FALSE;
1598 appdata.str.a = lpApplicationDataBuf;
1600 r = MSI_EnumComponentQualifiers( comp, iIndex,
1601 &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
1602 msi_free( comp );
1603 return r;
1606 /*************************************************************************
1607 * MsiEnumComponentQualifiersW [MSI.@]
1609 UINT WINAPI MsiEnumComponentQualifiersW( LPCWSTR szComponent, DWORD iIndex,
1610 LPWSTR lpQualifierBuf, LPDWORD pcchQualifierBuf,
1611 LPWSTR lpApplicationDataBuf, LPDWORD pcchApplicationDataBuf )
1613 awstring qual, appdata;
1615 TRACE("%s %08x %p %p %p %p\n", debugstr_w(szComponent), iIndex,
1616 lpQualifierBuf, pcchQualifierBuf, lpApplicationDataBuf,
1617 pcchApplicationDataBuf);
1619 qual.unicode = TRUE;
1620 qual.str.w = lpQualifierBuf;
1622 appdata.unicode = TRUE;
1623 appdata.str.w = lpApplicationDataBuf;
1625 return MSI_EnumComponentQualifiers( szComponent, iIndex,
1626 &qual, pcchQualifierBuf, &appdata, pcchApplicationDataBuf );
1629 /*************************************************************************
1630 * MsiEnumRelatedProductsW [MSI.@]
1633 UINT WINAPI MsiEnumRelatedProductsW(LPCWSTR szUpgradeCode, DWORD dwReserved,
1634 DWORD iProductIndex, LPWSTR lpProductBuf)
1636 UINT r;
1637 HKEY hkey;
1638 DWORD dwSize = SQUISH_GUID_SIZE;
1639 WCHAR szKeyName[SQUISH_GUID_SIZE];
1641 TRACE("%s %u %u %p\n", debugstr_w(szUpgradeCode), dwReserved,
1642 iProductIndex, lpProductBuf);
1644 if (NULL == szUpgradeCode)
1645 return ERROR_INVALID_PARAMETER;
1646 if (NULL == lpProductBuf)
1647 return ERROR_INVALID_PARAMETER;
1649 r = MSIREG_OpenUpgradeCodesKey(szUpgradeCode, &hkey, FALSE);
1650 if (r != ERROR_SUCCESS)
1651 return ERROR_NO_MORE_ITEMS;
1653 r = RegEnumValueW(hkey, iProductIndex, szKeyName, &dwSize, NULL, NULL, NULL, NULL);
1654 if( r == ERROR_SUCCESS )
1655 unsquash_guid(szKeyName, lpProductBuf);
1656 RegCloseKey(hkey);
1658 return r;
1661 /*************************************************************************
1662 * MsiEnumRelatedProductsA [MSI.@]
1665 UINT WINAPI MsiEnumRelatedProductsA(LPCSTR szUpgradeCode, DWORD dwReserved,
1666 DWORD iProductIndex, LPSTR lpProductBuf)
1668 LPWSTR szwUpgradeCode = NULL;
1669 WCHAR productW[GUID_SIZE];
1670 UINT r;
1672 TRACE("%s %u %u %p\n", debugstr_a(szUpgradeCode), dwReserved,
1673 iProductIndex, lpProductBuf);
1675 if (szUpgradeCode)
1677 szwUpgradeCode = strdupAtoW( szUpgradeCode );
1678 if( !szwUpgradeCode )
1679 return ERROR_OUTOFMEMORY;
1682 r = MsiEnumRelatedProductsW( szwUpgradeCode, dwReserved,
1683 iProductIndex, productW );
1684 if (r == ERROR_SUCCESS)
1686 WideCharToMultiByte( CP_ACP, 0, productW, GUID_SIZE,
1687 lpProductBuf, GUID_SIZE, NULL, NULL );
1689 msi_free( szwUpgradeCode);
1690 return r;
1693 /***********************************************************************
1694 * MsiEnumPatchesExA [MSI.@]
1696 UINT WINAPI MsiEnumPatchesExA(LPCSTR szProductCode, LPCSTR szUserSid,
1697 DWORD dwContext, DWORD dwFilter, DWORD dwIndex, LPSTR szPatchCode,
1698 LPSTR szTargetProductCode, MSIINSTALLCONTEXT *pdwTargetProductContext,
1699 LPSTR szTargetUserSid, LPDWORD pcchTargetUserSid)
1701 LPWSTR prodcode = NULL;
1702 LPWSTR usersid = NULL;
1703 LPWSTR targsid = NULL;
1704 WCHAR patch[GUID_SIZE];
1705 WCHAR targprod[GUID_SIZE];
1706 DWORD len;
1707 UINT r;
1709 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n",
1710 debugstr_a(szProductCode), debugstr_a(szUserSid), dwContext, dwFilter,
1711 dwIndex, szPatchCode, szTargetProductCode, pdwTargetProductContext,
1712 szTargetUserSid, pcchTargetUserSid);
1714 if (szTargetUserSid && !pcchTargetUserSid)
1715 return ERROR_INVALID_PARAMETER;
1717 if (szProductCode) prodcode = strdupAtoW(szProductCode);
1718 if (szUserSid) usersid = strdupAtoW(szUserSid);
1720 r = MsiEnumPatchesExW(prodcode, usersid, dwContext, dwFilter, dwIndex,
1721 patch, targprod, pdwTargetProductContext,
1722 NULL, &len);
1723 if (r != ERROR_SUCCESS)
1724 goto done;
1726 WideCharToMultiByte(CP_ACP, 0, patch, -1, szPatchCode,
1727 GUID_SIZE, NULL, NULL);
1728 WideCharToMultiByte(CP_ACP, 0, targprod, -1, szTargetProductCode,
1729 GUID_SIZE, NULL, NULL);
1731 if (!szTargetUserSid)
1733 if (pcchTargetUserSid)
1734 *pcchTargetUserSid = len;
1736 goto done;
1739 targsid = msi_alloc(++len * sizeof(WCHAR));
1740 if (!targsid)
1742 r = ERROR_OUTOFMEMORY;
1743 goto done;
1746 r = MsiEnumPatchesExW(prodcode, usersid, dwContext, dwFilter, dwIndex,
1747 patch, targprod, pdwTargetProductContext,
1748 targsid, &len);
1749 if (r != ERROR_SUCCESS || !szTargetUserSid)
1750 goto done;
1752 WideCharToMultiByte(CP_ACP, 0, targsid, -1, szTargetUserSid,
1753 *pcchTargetUserSid, NULL, NULL);
1755 len = lstrlenW(targsid);
1756 if (*pcchTargetUserSid < len + 1)
1758 r = ERROR_MORE_DATA;
1759 *pcchTargetUserSid = len * sizeof(WCHAR);
1761 else
1762 *pcchTargetUserSid = len;
1764 done:
1765 msi_free(prodcode);
1766 msi_free(usersid);
1767 msi_free(targsid);
1769 return r;
1772 static UINT msi_get_patch_state(LPCWSTR prodcode, LPCWSTR usersid,
1773 MSIINSTALLCONTEXT context,
1774 LPWSTR patch, MSIPATCHSTATE *state)
1776 DWORD type, val, size;
1777 HKEY prod, hkey = 0;
1778 HKEY udpatch = 0;
1779 LONG res;
1780 UINT r = ERROR_NO_MORE_ITEMS;
1782 static const WCHAR szPatches[] = {'P','a','t','c','h','e','s',0};
1783 static const WCHAR szState[] = {'S','t','a','t','e',0};
1785 *state = MSIPATCHSTATE_INVALID;
1787 r = MSIREG_OpenUserDataProductKey(prodcode, context,
1788 usersid, &prod, FALSE);
1789 if (r != ERROR_SUCCESS)
1790 return ERROR_NO_MORE_ITEMS;
1792 res = RegOpenKeyExW(prod, szPatches, 0, KEY_READ, &hkey);
1793 if (res != ERROR_SUCCESS)
1794 goto done;
1796 res = RegOpenKeyExW(hkey, patch, 0, KEY_READ, &udpatch);
1797 if (res != ERROR_SUCCESS)
1798 goto done;
1800 size = sizeof(DWORD);
1801 res = RegGetValueW(udpatch, NULL, szState, RRF_RT_DWORD, &type, &val, &size);
1802 if (res != ERROR_SUCCESS ||
1803 val < MSIPATCHSTATE_APPLIED || val > MSIPATCHSTATE_REGISTERED)
1805 r = ERROR_BAD_CONFIGURATION;
1806 goto done;
1809 *state = val;
1810 r = ERROR_SUCCESS;
1812 done:
1813 RegCloseKey(udpatch);
1814 RegCloseKey(hkey);
1815 RegCloseKey(prod);
1817 return r;
1820 static UINT msi_check_product_patches(LPCWSTR prodcode, LPCWSTR usersid,
1821 MSIINSTALLCONTEXT context, DWORD filter, DWORD index, DWORD *idx,
1822 LPWSTR patch, LPWSTR targetprod, MSIINSTALLCONTEXT *targetctx,
1823 LPWSTR targetsid, DWORD *sidsize, LPWSTR *transforms)
1825 MSIPATCHSTATE state;
1826 LPWSTR ptr, patches = NULL;
1827 HKEY prod, patchkey = 0;
1828 HKEY localprod = 0, localpatch = 0;
1829 DWORD type, size;
1830 LONG res;
1831 UINT temp, r = ERROR_NO_MORE_ITEMS;
1833 static const WCHAR szPatches[] = {'P','a','t','c','h','e','s',0};
1834 static const WCHAR szState[] = {'S','t','a','t','e',0};
1835 static const WCHAR szEmpty[] = {0};
1837 if (MSIREG_OpenProductKey(prodcode, usersid, context,
1838 &prod, FALSE) != ERROR_SUCCESS)
1839 return ERROR_NO_MORE_ITEMS;
1841 size = 0;
1842 res = RegGetValueW(prod, szPatches, szPatches, RRF_RT_ANY, &type, NULL,
1843 &size);
1844 if (res != ERROR_SUCCESS)
1845 goto done;
1847 if (type != REG_MULTI_SZ)
1849 r = ERROR_BAD_CONFIGURATION;
1850 goto done;
1853 patches = msi_alloc(size);
1854 if (!patches)
1856 r = ERROR_OUTOFMEMORY;
1857 goto done;
1860 res = RegGetValueW(prod, szPatches, szPatches, RRF_RT_ANY, &type,
1861 patches, &size);
1862 if (res != ERROR_SUCCESS)
1863 goto done;
1865 ptr = patches;
1866 for (ptr = patches; *ptr && r == ERROR_NO_MORE_ITEMS; ptr += lstrlenW(ptr))
1868 if (!unsquash_guid(ptr, patch))
1870 r = ERROR_BAD_CONFIGURATION;
1871 goto done;
1874 size = 0;
1875 res = RegGetValueW(prod, szPatches, ptr, RRF_RT_REG_SZ,
1876 &type, NULL, &size);
1877 if (res != ERROR_SUCCESS)
1878 continue;
1880 if (transforms)
1882 *transforms = msi_alloc(size);
1883 if (!*transforms)
1885 r = ERROR_OUTOFMEMORY;
1886 goto done;
1889 res = RegGetValueW(prod, szPatches, ptr, RRF_RT_REG_SZ,
1890 &type, *transforms, &size);
1891 if (res != ERROR_SUCCESS)
1892 continue;
1895 if (context == MSIINSTALLCONTEXT_USERMANAGED)
1897 if (!(filter & MSIPATCHSTATE_APPLIED))
1899 temp = msi_get_patch_state(prodcode, usersid, context,
1900 ptr, &state);
1901 if (temp == ERROR_BAD_CONFIGURATION)
1903 r = ERROR_BAD_CONFIGURATION;
1904 goto done;
1907 if (temp != ERROR_SUCCESS || !(filter & state))
1908 continue;
1911 else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
1913 if (!(filter & MSIPATCHSTATE_APPLIED))
1915 temp = msi_get_patch_state(prodcode, usersid, context,
1916 ptr, &state);
1917 if (temp == ERROR_BAD_CONFIGURATION)
1919 r = ERROR_BAD_CONFIGURATION;
1920 goto done;
1923 if (temp != ERROR_SUCCESS || !(filter & state))
1924 continue;
1926 else
1928 temp = MSIREG_OpenUserDataPatchKey(patch, context,
1929 &patchkey, FALSE);
1930 RegCloseKey(patchkey);
1931 if (temp != ERROR_SUCCESS)
1932 continue;
1935 else if (context == MSIINSTALLCONTEXT_MACHINE)
1937 usersid = szEmpty;
1939 if (MSIREG_OpenUserDataProductKey(prodcode, context, NULL, &localprod, FALSE) == ERROR_SUCCESS &&
1940 RegOpenKeyExW(localprod, szPatches, 0, KEY_READ, &localpatch) == ERROR_SUCCESS &&
1941 RegOpenKeyExW(localpatch, ptr, 0, KEY_READ, &patchkey) == ERROR_SUCCESS)
1943 res = RegGetValueW(patchkey, NULL, szState, RRF_RT_REG_DWORD,
1944 &type, &state, &size);
1946 if (!(filter & state))
1947 res = ERROR_NO_MORE_ITEMS;
1949 RegCloseKey(patchkey);
1952 RegCloseKey(localpatch);
1953 RegCloseKey(localprod);
1955 if (res != ERROR_SUCCESS)
1956 continue;
1959 if (*idx < index)
1961 (*idx)++;
1962 continue;
1965 r = ERROR_SUCCESS;
1966 if (targetprod)
1967 lstrcpyW(targetprod, prodcode);
1969 if (targetctx)
1970 *targetctx = context;
1972 if (targetsid)
1974 lstrcpynW(targetsid, usersid, *sidsize);
1975 if (lstrlenW(usersid) >= *sidsize)
1976 r = ERROR_MORE_DATA;
1979 if (sidsize)
1981 *sidsize = lstrlenW(usersid);
1982 if (!targetsid)
1983 *sidsize *= sizeof(WCHAR);
1987 done:
1988 RegCloseKey(prod);
1989 msi_free(patches);
1991 return r;
1994 static UINT msi_enum_patches(LPCWSTR szProductCode, LPCWSTR szUserSid,
1995 DWORD dwContext, DWORD dwFilter, DWORD dwIndex, DWORD *idx,
1996 LPWSTR szPatchCode, LPWSTR szTargetProductCode,
1997 MSIINSTALLCONTEXT *pdwTargetProductContext, LPWSTR szTargetUserSid,
1998 LPDWORD pcchTargetUserSid, LPWSTR *szTransforms)
2000 LPWSTR usersid = NULL;
2001 UINT r = ERROR_INVALID_PARAMETER;
2003 if (!szUserSid)
2005 get_user_sid(&usersid);
2006 szUserSid = usersid;
2009 if (dwContext & MSIINSTALLCONTEXT_USERMANAGED)
2011 r = msi_check_product_patches(szProductCode, szUserSid,
2012 MSIINSTALLCONTEXT_USERMANAGED, dwFilter,
2013 dwIndex, idx, szPatchCode,
2014 szTargetProductCode,
2015 pdwTargetProductContext, szTargetUserSid,
2016 pcchTargetUserSid, szTransforms);
2017 if (r != ERROR_NO_MORE_ITEMS)
2018 goto done;
2021 if (dwContext & MSIINSTALLCONTEXT_USERUNMANAGED)
2023 r = msi_check_product_patches(szProductCode, szUserSid,
2024 MSIINSTALLCONTEXT_USERUNMANAGED, dwFilter,
2025 dwIndex, idx, szPatchCode,
2026 szTargetProductCode,
2027 pdwTargetProductContext, szTargetUserSid,
2028 pcchTargetUserSid, szTransforms);
2029 if (r != ERROR_NO_MORE_ITEMS)
2030 goto done;
2033 if (dwContext & MSIINSTALLCONTEXT_MACHINE)
2035 r = msi_check_product_patches(szProductCode, szUserSid,
2036 MSIINSTALLCONTEXT_MACHINE, dwFilter,
2037 dwIndex, idx, szPatchCode,
2038 szTargetProductCode,
2039 pdwTargetProductContext, szTargetUserSid,
2040 pcchTargetUserSid, szTransforms);
2041 if (r != ERROR_NO_MORE_ITEMS)
2042 goto done;
2045 done:
2046 LocalFree(usersid);
2047 return r;
2050 /***********************************************************************
2051 * MsiEnumPatchesExW [MSI.@]
2053 UINT WINAPI MsiEnumPatchesExW(LPCWSTR szProductCode, LPCWSTR szUserSid,
2054 DWORD dwContext, DWORD dwFilter, DWORD dwIndex, LPWSTR szPatchCode,
2055 LPWSTR szTargetProductCode, MSIINSTALLCONTEXT *pdwTargetProductContext,
2056 LPWSTR szTargetUserSid, LPDWORD pcchTargetUserSid)
2058 WCHAR squished_pc[GUID_SIZE];
2059 DWORD idx = 0;
2060 UINT r;
2062 static int last_index = 0;
2064 TRACE("(%s, %s, %d, %d, %d, %p, %p, %p, %p, %p)\n",
2065 debugstr_w(szProductCode), debugstr_w(szUserSid), dwContext, dwFilter,
2066 dwIndex, szPatchCode, szTargetProductCode, pdwTargetProductContext,
2067 szTargetUserSid, pcchTargetUserSid);
2069 if (!szProductCode || !squash_guid(szProductCode, squished_pc))
2070 return ERROR_INVALID_PARAMETER;
2072 if (!lstrcmpW(szUserSid, szLocalSid))
2073 return ERROR_INVALID_PARAMETER;
2075 if (dwContext & MSIINSTALLCONTEXT_MACHINE && szUserSid)
2076 return ERROR_INVALID_PARAMETER;
2078 if (dwContext <= MSIINSTALLCONTEXT_NONE ||
2079 dwContext > MSIINSTALLCONTEXT_ALL)
2080 return ERROR_INVALID_PARAMETER;
2082 if (dwFilter <= MSIPATCHSTATE_INVALID || dwFilter > MSIPATCHSTATE_ALL)
2083 return ERROR_INVALID_PARAMETER;
2085 if (dwIndex && dwIndex - last_index != 1)
2086 return ERROR_INVALID_PARAMETER;
2088 if (dwIndex == 0)
2089 last_index = 0;
2091 r = msi_enum_patches(szProductCode, szUserSid, dwContext, dwFilter,
2092 dwIndex, &idx, szPatchCode, szTargetProductCode,
2093 pdwTargetProductContext, szTargetUserSid,
2094 pcchTargetUserSid, NULL);
2096 if (r == ERROR_SUCCESS)
2097 last_index = dwIndex;
2099 return r;
2102 /***********************************************************************
2103 * MsiEnumPatchesA [MSI.@]
2105 UINT WINAPI MsiEnumPatchesA(LPCSTR szProduct, DWORD iPatchIndex,
2106 LPSTR lpPatchBuf, LPSTR lpTransformsBuf, LPDWORD pcchTransformsBuf)
2108 LPWSTR product, transforms;
2109 WCHAR patch[GUID_SIZE];
2110 DWORD len;
2111 UINT r;
2113 TRACE("(%s %d %p %p %p)\n", debugstr_a(szProduct), iPatchIndex,
2114 lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);
2116 if (!szProduct || !lpPatchBuf || !lpTransformsBuf || !pcchTransformsBuf)
2117 return ERROR_INVALID_PARAMETER;
2119 product = strdupAtoW(szProduct);
2120 if (!product)
2121 return ERROR_OUTOFMEMORY;
2123 len = *pcchTransformsBuf;
2124 transforms = msi_alloc( len * sizeof(WCHAR) );
2125 if (!transforms)
2127 r = ERROR_OUTOFMEMORY;
2128 goto done;
2131 r = MsiEnumPatchesW(product, iPatchIndex, patch, transforms, &len);
2132 if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
2133 goto done;
2135 WideCharToMultiByte(CP_ACP, 0, patch, -1, lpPatchBuf,
2136 GUID_SIZE, NULL, NULL);
2138 if (!WideCharToMultiByte(CP_ACP, 0, transforms, -1, lpTransformsBuf,
2139 *pcchTransformsBuf, NULL, NULL))
2140 r = ERROR_MORE_DATA;
2142 if (r == ERROR_MORE_DATA)
2144 lpTransformsBuf[*pcchTransformsBuf - 1] = '\0';
2145 *pcchTransformsBuf = len * 2;
2147 else
2148 *pcchTransformsBuf = strlen( lpTransformsBuf );
2150 done:
2151 msi_free(transforms);
2152 msi_free(product);
2154 return r;
2157 /***********************************************************************
2158 * MsiEnumPatchesW [MSI.@]
2160 UINT WINAPI MsiEnumPatchesW(LPCWSTR szProduct, DWORD iPatchIndex,
2161 LPWSTR lpPatchBuf, LPWSTR lpTransformsBuf, LPDWORD pcchTransformsBuf)
2163 WCHAR squished_pc[GUID_SIZE];
2164 LPWSTR transforms = NULL;
2165 HKEY prod;
2166 DWORD idx = 0;
2167 UINT r;
2169 TRACE("(%s %d %p %p %p)\n", debugstr_w(szProduct), iPatchIndex,
2170 lpPatchBuf, lpTransformsBuf, pcchTransformsBuf);
2172 if (!szProduct || !squash_guid(szProduct, squished_pc))
2173 return ERROR_INVALID_PARAMETER;
2175 if (!lpPatchBuf || !lpTransformsBuf || !pcchTransformsBuf)
2176 return ERROR_INVALID_PARAMETER;
2178 if (MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERMANAGED,
2179 &prod, FALSE) != ERROR_SUCCESS &&
2180 MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERUNMANAGED,
2181 &prod, FALSE) != ERROR_SUCCESS &&
2182 MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_MACHINE,
2183 &prod, FALSE) != ERROR_SUCCESS)
2184 return ERROR_UNKNOWN_PRODUCT;
2186 RegCloseKey(prod);
2188 r = msi_enum_patches(szProduct, NULL, MSIINSTALLCONTEXT_ALL,
2189 MSIPATCHSTATE_ALL, iPatchIndex, &idx, lpPatchBuf,
2190 NULL, NULL, NULL, NULL, &transforms);
2191 if (r != ERROR_SUCCESS)
2192 goto done;
2194 lstrcpynW(lpTransformsBuf, transforms, *pcchTransformsBuf);
2195 if (*pcchTransformsBuf <= lstrlenW(transforms))
2197 r = ERROR_MORE_DATA;
2198 *pcchTransformsBuf = lstrlenW(transforms);
2200 else
2201 *pcchTransformsBuf = lstrlenW(transforms);
2203 done:
2204 msi_free(transforms);
2205 return r;
2208 UINT WINAPI MsiEnumProductsExA( LPCSTR szProductCode, LPCSTR szUserSid,
2209 DWORD dwContext, DWORD dwIndex, CHAR szInstalledProductCode[39],
2210 MSIINSTALLCONTEXT* pdwInstalledContext, LPSTR szSid, LPDWORD pcchSid)
2212 FIXME("%s %s %d %d %p %p %p %p\n", debugstr_a(szProductCode), debugstr_a(szUserSid),
2213 dwContext, dwIndex, szInstalledProductCode, pdwInstalledContext,
2214 szSid, pcchSid);
2215 return ERROR_NO_MORE_ITEMS;
2218 UINT WINAPI MsiEnumProductsExW( LPCWSTR szProductCode, LPCWSTR szUserSid,
2219 DWORD dwContext, DWORD dwIndex, WCHAR szInstalledProductCode[39],
2220 MSIINSTALLCONTEXT* pdwInstalledContext, LPWSTR szSid, LPDWORD pcchSid)
2222 FIXME("%s %s %d %d %p %p %p %p\n", debugstr_w(szProductCode), debugstr_w(szUserSid),
2223 dwContext, dwIndex, szInstalledProductCode, pdwInstalledContext,
2224 szSid, pcchSid);
2225 return ERROR_NO_MORE_ITEMS;