msi: Don't reinstall components shared with other products.
[wine] / dlls / msi / msi.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2002,2003,2004,2005 Mike McCormack for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24 #define NONAMELESSUNION
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winnls.h"
30 #include "shlwapi.h"
31 #include "msi.h"
32 #include "msidefs.h"
33 #include "msiquery.h"
34 #include "msipriv.h"
35 #include "msiserver.h"
36 #include "wincrypt.h"
37 #include "winver.h"
38 #include "winuser.h"
39 #include "shlobj.h"
40 #include "shobjidl.h"
41 #include "objidl.h"
42 #include "wintrust.h"
43 #include "softpub.h"
44
45 #include "initguid.h"
46 #include "msxml2.h"
47
48 #include "wine/debug.h"
49 #include "wine/unicode.h"
50
51 WINE_DEFAULT_DEBUG_CHANNEL(msi);
52
53 static const WCHAR installerW[] = {'\\','I','n','s','t','a','l','l','e','r',0};
54
55 UINT msi_locate_product(LPCWSTR szProduct, MSIINSTALLCONTEXT *context)
56 {
57     HKEY hkey = NULL;
58
59     *context = MSIINSTALLCONTEXT_NONE;
60     if (!szProduct) return ERROR_UNKNOWN_PRODUCT;
61
62     if (MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERMANAGED,
63                               &hkey, FALSE) == ERROR_SUCCESS)
64         *context = MSIINSTALLCONTEXT_USERMANAGED;
65     else if (MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_MACHINE,
66                                    &hkey, FALSE) == ERROR_SUCCESS)
67         *context = MSIINSTALLCONTEXT_MACHINE;
68     else if (MSIREG_OpenProductKey(szProduct, NULL,
69                                    MSIINSTALLCONTEXT_USERUNMANAGED,
70                                    &hkey, FALSE) == ERROR_SUCCESS)
71         *context = MSIINSTALLCONTEXT_USERUNMANAGED;
72
73     RegCloseKey(hkey);
74
75     if (*context == MSIINSTALLCONTEXT_NONE)
76         return ERROR_UNKNOWN_PRODUCT;
77
78     return ERROR_SUCCESS;
79 }
80
81 UINT WINAPI MsiOpenProductA(LPCSTR szProduct, MSIHANDLE *phProduct)
82 {
83     UINT r;
84     LPWSTR szwProd = NULL;
85
86     TRACE("%s %p\n",debugstr_a(szProduct), phProduct);
87
88     if( szProduct )
89     {
90         szwProd = strdupAtoW( szProduct );
91         if( !szwProd )
92             return ERROR_OUTOFMEMORY;
93     }
94
95     r = MsiOpenProductW( szwProd, phProduct );
96
97     msi_free( szwProd );
98
99     return r;
100 }
101
102 static UINT MSI_OpenProductW(LPCWSTR szProduct, MSIPACKAGE **package)
103 {
104     UINT r;
105     HKEY props;
106     LPWSTR path;
107     MSIINSTALLCONTEXT context;
108
109     static const WCHAR managed[] = {
110         'M','a','n','a','g','e','d','L','o','c','a','l','P','a','c','k','a','g','e',0};
111     static const WCHAR local[] = {'L','o','c','a','l','P','a','c','k','a','g','e',0};
112
113     TRACE("%s %p\n", debugstr_w(szProduct), package);
114
115     r = msi_locate_product(szProduct, &context);
116     if (r != ERROR_SUCCESS)
117         return r;
118
119     r = MSIREG_OpenInstallProps(szProduct, context, NULL, &props, FALSE);
120     if (r != ERROR_SUCCESS)
121         return ERROR_UNKNOWN_PRODUCT;
122
123     if (context == MSIINSTALLCONTEXT_USERMANAGED)
124         path = msi_reg_get_val_str(props, managed);
125     else
126         path = msi_reg_get_val_str(props, local);
127
128     r = ERROR_UNKNOWN_PRODUCT;
129
130     if (!path || GetFileAttributesW(path) == INVALID_FILE_ATTRIBUTES)
131         goto done;
132
133     if (PathIsRelativeW(path))
134     {
135         r = ERROR_INSTALL_PACKAGE_OPEN_FAILED;
136         goto done;
137     }
138
139     r = MSI_OpenPackageW(path, package);
140
141 done:
142     RegCloseKey(props);
143     msi_free(path);
144     return r;
145 }
146
147 UINT WINAPI MsiOpenProductW(LPCWSTR szProduct, MSIHANDLE *phProduct)
148 {
149     MSIPACKAGE *package = NULL;
150     WCHAR squished_pc[GUID_SIZE];
151     UINT r;
152
153     if (!szProduct || !squash_guid(szProduct, squished_pc))
154         return ERROR_INVALID_PARAMETER;
155
156     if (!phProduct)
157         return ERROR_INVALID_PARAMETER;
158
159     r = MSI_OpenProductW(szProduct, &package);
160     if (r != ERROR_SUCCESS)
161         return r;
162
163     *phProduct = alloc_msihandle(&package->hdr);
164     if (!*phProduct)
165         r = ERROR_NOT_ENOUGH_MEMORY;
166
167     msiobj_release(&package->hdr);
168     return r;
169 }
170
171 UINT WINAPI MsiAdvertiseProductA(LPCSTR szPackagePath, LPCSTR szScriptfilePath,
172                 LPCSTR szTransforms, LANGID lgidLanguage)
173 {
174     FIXME("%s %s %s %08x\n",debugstr_a(szPackagePath),
175           debugstr_a(szScriptfilePath), debugstr_a(szTransforms), lgidLanguage);
176     return ERROR_CALL_NOT_IMPLEMENTED;
177 }
178
179 UINT WINAPI MsiAdvertiseProductW(LPCWSTR szPackagePath, LPCWSTR szScriptfilePath,
180                 LPCWSTR szTransforms, LANGID lgidLanguage)
181 {
182     FIXME("%s %s %s %08x\n",debugstr_w(szPackagePath),
183           debugstr_w(szScriptfilePath), debugstr_w(szTransforms), lgidLanguage);
184     return ERROR_CALL_NOT_IMPLEMENTED;
185 }
186
187 UINT WINAPI MsiAdvertiseProductExA(LPCSTR szPackagePath, LPCSTR szScriptfilePath,
188       LPCSTR szTransforms, LANGID lgidLanguage, DWORD dwPlatform, DWORD dwOptions)
189 {
190     FIXME("%s %s %s %08x %08x %08x\n", debugstr_a(szPackagePath),
191           debugstr_a(szScriptfilePath), debugstr_a(szTransforms),
192           lgidLanguage, dwPlatform, dwOptions);
193     return ERROR_CALL_NOT_IMPLEMENTED;
194 }
195
196 UINT WINAPI MsiAdvertiseProductExW( LPCWSTR szPackagePath, LPCWSTR szScriptfilePath,
197       LPCWSTR szTransforms, LANGID lgidLanguage, DWORD dwPlatform, DWORD dwOptions)
198 {
199     FIXME("%s %s %s %08x %08x %08x\n", debugstr_w(szPackagePath),
200           debugstr_w(szScriptfilePath), debugstr_w(szTransforms),
201           lgidLanguage, dwPlatform, dwOptions);
202     return ERROR_CALL_NOT_IMPLEMENTED;
203 }
204
205 UINT WINAPI MsiInstallProductA(LPCSTR szPackagePath, LPCSTR szCommandLine)
206 {
207     LPWSTR szwPath = NULL, szwCommand = NULL;
208     UINT r = ERROR_OUTOFMEMORY;
209
210     TRACE("%s %s\n",debugstr_a(szPackagePath), debugstr_a(szCommandLine));
211
212     if( szPackagePath )
213     {
214         szwPath = strdupAtoW( szPackagePath );
215         if( !szwPath )
216             goto end;
217     }
218
219     if( szCommandLine )
220     {
221         szwCommand = strdupAtoW( szCommandLine );
222         if( !szwCommand )
223             goto end;
224     }
225
226     r = MsiInstallProductW( szwPath, szwCommand );
227
228 end:
229     msi_free( szwPath );
230     msi_free( szwCommand );
231
232     return r;
233 }
234
235 UINT WINAPI MsiInstallProductW(LPCWSTR szPackagePath, LPCWSTR szCommandLine)
236 {
237     MSIPACKAGE *package = NULL;
238     UINT r;
239
240     TRACE("%s %s\n",debugstr_w(szPackagePath), debugstr_w(szCommandLine));
241
242     if (!szPackagePath)
243         return ERROR_INVALID_PARAMETER;
244
245     if (!*szPackagePath)
246         return ERROR_PATH_NOT_FOUND;
247
248     r = MSI_OpenPackageW( szPackagePath, &package );
249     if (r == ERROR_SUCCESS)
250     {
251         r = MSI_InstallPackage( package, szPackagePath, szCommandLine );
252         msiobj_release( &package->hdr );
253     }
254
255     return r;
256 }
257
258 UINT WINAPI MsiReinstallProductA(LPCSTR szProduct, DWORD dwReinstallMode)
259 {
260     LPWSTR wszProduct;
261     UINT rc;
262
263     TRACE("%s %08x\n", debugstr_a(szProduct), dwReinstallMode);
264
265     wszProduct = strdupAtoW(szProduct);
266
267     rc = MsiReinstallProductW(wszProduct, dwReinstallMode);
268
269     msi_free(wszProduct);
270     return rc;
271 }
272
273 UINT WINAPI MsiReinstallProductW(LPCWSTR szProduct, DWORD dwReinstallMode)
274 {
275     TRACE("%s %08x\n", debugstr_w(szProduct), dwReinstallMode);
276
277     return MsiReinstallFeatureW(szProduct, szAll, dwReinstallMode);
278 }
279
280 UINT WINAPI MsiApplyPatchA(LPCSTR szPatchPackage, LPCSTR szInstallPackage,
281         INSTALLTYPE eInstallType, LPCSTR szCommandLine)
282 {
283     LPWSTR patch_package = NULL;
284     LPWSTR install_package = NULL;
285     LPWSTR command_line = NULL;
286     UINT r = ERROR_OUTOFMEMORY;
287
288     TRACE("%s %s %d %s\n", debugstr_a(szPatchPackage), debugstr_a(szInstallPackage),
289           eInstallType, debugstr_a(szCommandLine));
290
291     if (szPatchPackage && !(patch_package = strdupAtoW(szPatchPackage)))
292         goto done;
293
294     if (szInstallPackage && !(install_package = strdupAtoW(szInstallPackage)))
295         goto done;
296
297     if (szCommandLine && !(command_line = strdupAtoW(szCommandLine)))
298         goto done;
299
300     r = MsiApplyPatchW(patch_package, install_package, eInstallType, command_line);
301
302 done:
303     msi_free(patch_package);
304     msi_free(install_package);
305     msi_free(command_line);
306
307     return r;
308 }
309
310 static UINT get_patch_product_codes( LPCWSTR szPatchPackage, WCHAR ***product_codes )
311 {
312     MSIHANDLE patch, info = 0;
313     UINT r, type;
314     DWORD size;
315     static WCHAR empty[] = {0};
316     WCHAR *codes = NULL;
317
318     r = MsiOpenDatabaseW( szPatchPackage, MSIDBOPEN_READONLY, &patch );
319     if (r != ERROR_SUCCESS)
320         return r;
321
322     r = MsiGetSummaryInformationW( patch, NULL, 0, &info );
323     if (r != ERROR_SUCCESS)
324         goto done;
325
326     size = 0;
327     r = MsiSummaryInfoGetPropertyW( info, PID_TEMPLATE, &type, NULL, NULL, empty, &size );
328     if (r != ERROR_MORE_DATA || !size || type != VT_LPSTR)
329     {
330         ERR("Failed to read product codes from patch\n");
331         r = ERROR_FUNCTION_FAILED;
332         goto done;
333     }
334
335     codes = msi_alloc( ++size * sizeof(WCHAR) );
336     if (!codes)
337     {
338         r = ERROR_OUTOFMEMORY;
339         goto done;
340     }
341
342     r = MsiSummaryInfoGetPropertyW( info, PID_TEMPLATE, &type, NULL, NULL, codes, &size );
343     if (r == ERROR_SUCCESS)
344         *product_codes = msi_split_string( codes, ';' );
345
346 done:
347     MsiCloseHandle( info );
348     MsiCloseHandle( patch );
349     msi_free( codes );
350     return r;
351 }
352
353 static UINT MSI_ApplyPatchW(LPCWSTR szPatchPackage, LPCWSTR szProductCode, LPCWSTR szCommandLine)
354 {
355     UINT i, r = ERROR_FUNCTION_FAILED;
356     DWORD size;
357     LPCWSTR cmd_ptr = szCommandLine;
358     LPWSTR cmd, *codes = NULL;
359     BOOL succeeded = FALSE;
360
361     static const WCHAR fmt[] = {'%','s',' ','P','A','T','C','H','=','"','%','s','"',0};
362     static WCHAR empty[] = {0};
363
364     if (!szPatchPackage || !szPatchPackage[0])
365         return ERROR_INVALID_PARAMETER;
366
367     if (!szProductCode && (r = get_patch_product_codes( szPatchPackage, &codes )))
368         return r;
369
370     if (!szCommandLine)
371         cmd_ptr = empty;
372
373     size = strlenW(cmd_ptr) + strlenW(fmt) + strlenW(szPatchPackage) + 1;
374     cmd = msi_alloc(size * sizeof(WCHAR));
375     if (!cmd)
376     {
377         msi_free(codes);
378         return ERROR_OUTOFMEMORY;
379     }
380     sprintfW(cmd, fmt, cmd_ptr, szPatchPackage);
381
382     if (szProductCode)
383         r = MsiConfigureProductExW(szProductCode, INSTALLLEVEL_DEFAULT, INSTALLSTATE_DEFAULT, cmd);
384     else
385     {
386         for (i = 0; codes[i]; i++)
387         {
388             r = MsiConfigureProductExW(codes[i], INSTALLLEVEL_DEFAULT, INSTALLSTATE_DEFAULT, cmd);
389             if (r == ERROR_SUCCESS)
390             {
391                 TRACE("patch applied\n");
392                 succeeded = TRUE;
393             }
394         }
395
396         if (succeeded)
397             r = ERROR_SUCCESS;
398     }
399
400     msi_free(cmd);
401     msi_free(codes);
402     return r;
403 }
404
405 UINT WINAPI MsiApplyPatchW(LPCWSTR szPatchPackage, LPCWSTR szInstallPackage,
406          INSTALLTYPE eInstallType, LPCWSTR szCommandLine)
407 {
408     TRACE("%s %s %d %s\n", debugstr_w(szPatchPackage), debugstr_w(szInstallPackage),
409           eInstallType, debugstr_w(szCommandLine));
410
411     if (szInstallPackage || eInstallType == INSTALLTYPE_NETWORK_IMAGE ||
412         eInstallType == INSTALLTYPE_SINGLE_INSTANCE)
413     {
414         FIXME("Only reading target products from patch\n");
415         return ERROR_CALL_NOT_IMPLEMENTED;
416     }
417
418     return MSI_ApplyPatchW(szPatchPackage, NULL, szCommandLine);
419 }
420
421 UINT WINAPI MsiApplyMultiplePatchesA(LPCSTR szPatchPackages,
422         LPCSTR szProductCode, LPCSTR szPropertiesList)
423 {
424     LPWSTR patch_packages = NULL;
425     LPWSTR product_code = NULL;
426     LPWSTR properties_list = NULL;
427     UINT r = ERROR_OUTOFMEMORY;
428
429     TRACE("%s %s %s\n", debugstr_a(szPatchPackages), debugstr_a(szProductCode),
430           debugstr_a(szPropertiesList));
431
432     if (!szPatchPackages || !szPatchPackages[0])
433         return ERROR_INVALID_PARAMETER;
434
435     if (!(patch_packages = strdupAtoW(szPatchPackages)))
436         return ERROR_OUTOFMEMORY;
437
438     if (szProductCode && !(product_code = strdupAtoW(szProductCode)))
439         goto done;
440
441     if (szPropertiesList && !(properties_list = strdupAtoW(szPropertiesList)))
442         goto done;
443
444     r = MsiApplyMultiplePatchesW(patch_packages, product_code, properties_list);
445
446 done:
447     msi_free(patch_packages);
448     msi_free(product_code);
449     msi_free(properties_list);
450
451     return r;
452 }
453
454 UINT WINAPI MsiApplyMultiplePatchesW(LPCWSTR szPatchPackages,
455         LPCWSTR szProductCode, LPCWSTR szPropertiesList)
456 {
457     UINT r = ERROR_SUCCESS;
458     LPCWSTR beg, end;
459
460     TRACE("%s %s %s\n", debugstr_w(szPatchPackages), debugstr_w(szProductCode),
461           debugstr_w(szPropertiesList));
462
463     if (!szPatchPackages || !szPatchPackages[0])
464         return ERROR_INVALID_PARAMETER;
465
466     beg = end = szPatchPackages;
467     while (*beg)
468     {
469         DWORD len;
470         LPWSTR patch;
471
472         while (*beg == ' ') beg++;
473         while (*end && *end != ';') end++;
474
475         len = end - beg;
476         while (len && beg[len - 1] == ' ') len--;
477
478         if (!len) return ERROR_INVALID_NAME;
479
480         patch = msi_alloc((len + 1) * sizeof(WCHAR));
481         if (!patch)
482             return ERROR_OUTOFMEMORY;
483
484         memcpy(patch, beg, len * sizeof(WCHAR));
485         patch[len] = '\0';
486
487         r = MSI_ApplyPatchW(patch, szProductCode, szPropertiesList);
488         msi_free(patch);
489
490         if (r != ERROR_SUCCESS)
491             break;
492
493         beg = ++end;
494     }
495     return r;
496 }
497
498 static void free_patchinfo( DWORD count, MSIPATCHSEQUENCEINFOW *info )
499 {
500     DWORD i;
501     for (i = 0; i < count; i++) msi_free( (WCHAR *)info[i].szPatchData );
502     msi_free( info );
503 }
504
505 static MSIPATCHSEQUENCEINFOW *patchinfoAtoW( DWORD count, const MSIPATCHSEQUENCEINFOA *info )
506 {
507     DWORD i;
508     MSIPATCHSEQUENCEINFOW *ret;
509
510     if (!(ret = msi_alloc( count * sizeof(MSIPATCHSEQUENCEINFOW) ))) return NULL;
511     for (i = 0; i < count; i++)
512     {
513         if (info[i].szPatchData && !(ret[i].szPatchData = strdupAtoW( info[i].szPatchData )))
514         {
515             free_patchinfo( i, ret );
516             return NULL;
517         }
518         ret[i].ePatchDataType = info[i].ePatchDataType;
519         ret[i].dwOrder = info[i].dwOrder;
520         ret[i].uStatus = info[i].uStatus;
521     }
522     return ret;
523 }
524
525 UINT WINAPI MsiDetermineApplicablePatchesA(LPCSTR szProductPackagePath,
526         DWORD cPatchInfo, PMSIPATCHSEQUENCEINFOA pPatchInfo)
527 {
528     UINT i, r;
529     WCHAR *package_path = NULL;
530     MSIPATCHSEQUENCEINFOW *psi;
531
532     TRACE("%s, %u, %p\n", debugstr_a(szProductPackagePath), cPatchInfo, pPatchInfo);
533
534     if (szProductPackagePath && !(package_path = strdupAtoW( szProductPackagePath )))
535         return ERROR_OUTOFMEMORY;
536
537     if (!(psi = patchinfoAtoW( cPatchInfo, pPatchInfo )))
538     {
539         msi_free( package_path );
540         return ERROR_OUTOFMEMORY;
541     }
542     r = MsiDetermineApplicablePatchesW( package_path, cPatchInfo, psi );
543     if (r == ERROR_SUCCESS)
544     {
545         for (i = 0; i < cPatchInfo; i++)
546         {
547             pPatchInfo[i].dwOrder = psi[i].dwOrder;
548             pPatchInfo[i].uStatus = psi[i].uStatus;
549         }
550     }
551     msi_free( package_path );
552     free_patchinfo( cPatchInfo, psi );
553     return r;
554 }
555
556 static UINT MSI_ApplicablePatchW( MSIPACKAGE *package, LPCWSTR patch )
557 {
558     MSISUMMARYINFO *si;
559     MSIDATABASE *patch_db;
560     UINT r = ERROR_SUCCESS;
561
562     r = MSI_OpenDatabaseW( patch, MSIDBOPEN_READONLY, &patch_db );
563     if (r != ERROR_SUCCESS)
564     {
565         WARN("failed to open patch file %s\n", debugstr_w(patch));
566         return r;
567     }
568
569     si = MSI_GetSummaryInformationW( patch_db->storage, 0 );
570     if (!si)
571     {
572         msiobj_release( &patch_db->hdr );
573         return ERROR_FUNCTION_FAILED;
574     }
575
576     r = msi_check_patch_applicable( package, si );
577     if (r != ERROR_SUCCESS)
578         TRACE("patch not applicable\n");
579
580     msiobj_release( &patch_db->hdr );
581     msiobj_release( &si->hdr );
582     return r;
583 }
584
585 /* IXMLDOMDocument should be set to XPath mode already */
586 static UINT MSI_ApplicablePatchXML( MSIPACKAGE *package, IXMLDOMDocument *desc )
587 {
588     static const WCHAR queryW[] = {'M','s','i','P','a','t','c','h','/',
589                                    'T','a','r','g','e','t','P','r','o','d','u','c','t','/',
590                                    'T','a','r','g','e','t','P','r','o','d','u','c','t','C','o','d','e',0};
591     UINT r = ERROR_FUNCTION_FAILED;
592     IXMLDOMNodeList *list;
593     LPWSTR product_code;
594     IXMLDOMNode *node;
595     HRESULT hr;
596     BSTR s;
597
598     product_code = msi_dup_property( package->db, szProductCode );
599     if (!product_code)
600     {
601         /* FIXME: the property ProductCode should be written into the DB somewhere */
602         ERR("no product code to check\n");
603         return ERROR_SUCCESS;
604     }
605
606     s = SysAllocString(queryW);
607     hr = IXMLDOMDocument_selectNodes( desc, s, &list );
608     SysFreeString(s);
609     if (hr != S_OK)
610         return ERROR_INVALID_PATCH_XML;
611
612     while (IXMLDOMNodeList_nextNode( list, &node ) == S_OK && r != ERROR_SUCCESS)
613     {
614         hr = IXMLDOMNode_get_text( node, &s );
615         IXMLDOMNode_Release( node );
616         if (hr == S_OK)
617         {
618             if (!strcmpW( s, product_code )) r = ERROR_SUCCESS;
619             SysFreeString( s );
620         }
621     }
622     IXMLDOMNodeList_Release( list );
623
624     if (r != ERROR_SUCCESS)
625         TRACE("patch not applicable\n");
626
627     msi_free( product_code );
628     return r;
629 }
630
631 static UINT determine_patch_sequence( MSIPACKAGE *package, DWORD count, MSIPATCHSEQUENCEINFOW *info )
632 {
633     IXMLDOMDocument *desc = NULL;
634     DWORD i;
635
636     if (count > 1)
637         FIXME("patch ordering not supported\n");
638
639     for (i = 0; i < count; i++)
640     {
641         switch (info[i].ePatchDataType)
642         {
643         case MSIPATCH_DATATYPE_PATCHFILE:
644         {
645             if (MSI_ApplicablePatchW( package, info[i].szPatchData ) != ERROR_SUCCESS)
646             {
647                 info[i].dwOrder = ~0u;
648                 info[i].uStatus = ERROR_PATCH_TARGET_NOT_FOUND;
649             }
650             else
651             {
652                 info[i].dwOrder = i;
653                 info[i].uStatus = ERROR_SUCCESS;
654             }
655             break;
656         }
657         case MSIPATCH_DATATYPE_XMLPATH:
658         case MSIPATCH_DATATYPE_XMLBLOB:
659         {
660             VARIANT_BOOL b;
661             HRESULT hr;
662             BSTR s;
663
664             if (!desc)
665             {
666                 hr = CoCreateInstance( &CLSID_DOMDocument30, NULL, CLSCTX_INPROC_SERVER,
667                     &IID_IXMLDOMDocument, (void**)&desc );
668                 if (hr != S_OK)
669                 {
670                     ERR("failed to create DOMDocument30 instance, 0x%08x\n", hr);
671                     return ERROR_FUNCTION_FAILED;
672                 }
673             }
674
675             s = SysAllocString( info[i].szPatchData );
676             if (info[i].ePatchDataType == MSIPATCH_DATATYPE_XMLPATH)
677             {
678                 VARIANT src;
679
680                 V_VT(&src) = VT_BSTR;
681                 V_BSTR(&src) = s;
682                 hr = IXMLDOMDocument_load( desc, src, &b );
683             }
684             else
685                 hr = IXMLDOMDocument_loadXML( desc, s, &b );
686             SysFreeString( s );
687             if ( hr != S_OK )
688             {
689                 ERR("failed to parse patch description\n");
690                 IXMLDOMDocument_Release( desc );
691                 break;
692             }
693
694             if (MSI_ApplicablePatchXML( package, desc ) != ERROR_SUCCESS)
695             {
696                 info[i].dwOrder = ~0u;
697                 info[i].uStatus = ERROR_PATCH_TARGET_NOT_FOUND;
698             }
699             else
700             {
701                 info[i].dwOrder = i;
702                 info[i].uStatus = ERROR_SUCCESS;
703             }
704             break;
705         }
706         default:
707         {
708             FIXME("unknown patch data type %u\n", info[i].ePatchDataType);
709             info[i].dwOrder = i;
710             info[i].uStatus = ERROR_SUCCESS;
711             break;
712         }
713         }
714
715         TRACE("szPatchData: %s\n", debugstr_w(info[i].szPatchData));
716         TRACE("ePatchDataType: %u\n", info[i].ePatchDataType);
717         TRACE("dwOrder: %u\n", info[i].dwOrder);
718         TRACE("uStatus: %u\n", info[i].uStatus);
719     }
720
721     if (desc) IXMLDOMDocument_Release( desc );
722
723     return ERROR_SUCCESS;
724 }
725
726 UINT WINAPI MsiDetermineApplicablePatchesW(LPCWSTR szProductPackagePath,
727         DWORD cPatchInfo, PMSIPATCHSEQUENCEINFOW pPatchInfo)
728 {
729     UINT r;
730     MSIPACKAGE *package;
731
732     TRACE("%s, %u, %p\n", debugstr_w(szProductPackagePath), cPatchInfo, pPatchInfo);
733
734     r = MSI_OpenPackageW( szProductPackagePath, &package );
735     if (r != ERROR_SUCCESS)
736     {
737         ERR("failed to open package %u\n", r);
738         return r;
739     }
740     r = determine_patch_sequence( package, cPatchInfo, pPatchInfo );
741     msiobj_release( &package->hdr );
742     return r;
743 }
744
745 UINT WINAPI MsiDeterminePatchSequenceA( LPCSTR product, LPCSTR usersid,
746     MSIINSTALLCONTEXT context, DWORD count, PMSIPATCHSEQUENCEINFOA patchinfo )
747 {
748     UINT i, r;
749     WCHAR *productW, *usersidW = NULL;
750     MSIPATCHSEQUENCEINFOW *patchinfoW;
751
752     TRACE("%s, %s, %d, %d, %p\n", debugstr_a(product), debugstr_a(usersid),
753           context, count, patchinfo);
754
755     if (!product) return ERROR_INVALID_PARAMETER;
756     if (!(productW = strdupAtoW( product ))) return ERROR_OUTOFMEMORY;
757     if (usersid && !(usersidW = strdupAtoW( usersid )))
758     {
759         msi_free( productW );
760         return ERROR_OUTOFMEMORY;
761     }
762     if (!(patchinfoW = patchinfoAtoW( count, patchinfo )))
763     {
764         msi_free( productW );
765         msi_free( usersidW );
766         return ERROR_OUTOFMEMORY;
767     }
768     r = MsiDeterminePatchSequenceW( productW, usersidW, context, count, patchinfoW );
769     if (r == ERROR_SUCCESS)
770     {
771         for (i = 0; i < count; i++)
772         {
773             patchinfo[i].dwOrder = patchinfoW[i].dwOrder;
774             patchinfo[i].uStatus = patchinfoW[i].uStatus;
775         }
776     }
777     msi_free( productW );
778     msi_free( usersidW );
779     free_patchinfo( count, patchinfoW );
780     return r;
781 }
782
783 static UINT open_package( const WCHAR *product, const WCHAR *usersid,
784                           MSIINSTALLCONTEXT context, MSIPACKAGE **package )
785 {
786     UINT r;
787     HKEY props;
788     WCHAR *localpath, sourcepath[MAX_PATH], filename[MAX_PATH];
789
790     r = MSIREG_OpenInstallProps( product, context, usersid, &props, FALSE );
791     if (r != ERROR_SUCCESS) return ERROR_BAD_CONFIGURATION;
792
793     if ((localpath = msi_reg_get_val_str( props, szLocalPackage )))
794     {
795         strcpyW( sourcepath, localpath );
796         msi_free( localpath );
797     }
798     RegCloseKey( props );
799     if (!localpath || GetFileAttributesW( sourcepath ) == INVALID_FILE_ATTRIBUTES)
800     {
801         DWORD sz = sizeof(sourcepath);
802         MsiSourceListGetInfoW( product, usersid, context, MSICODE_PRODUCT,
803                                INSTALLPROPERTY_LASTUSEDSOURCEW, sourcepath, &sz );
804         sz = sizeof(filename);
805         MsiSourceListGetInfoW( product, usersid, context, MSICODE_PRODUCT,
806                                INSTALLPROPERTY_PACKAGENAMEW, filename, &sz );
807         strcatW( sourcepath, filename );
808     }
809     if (GetFileAttributesW( sourcepath ) == INVALID_FILE_ATTRIBUTES)
810         return ERROR_INSTALL_SOURCE_ABSENT;
811
812     return MSI_OpenPackageW( sourcepath, package );
813 }
814
815 UINT WINAPI MsiDeterminePatchSequenceW( LPCWSTR product, LPCWSTR usersid,
816     MSIINSTALLCONTEXT context, DWORD count, PMSIPATCHSEQUENCEINFOW patchinfo )
817 {
818     UINT r;
819     MSIPACKAGE *package;
820
821     TRACE("%s, %s, %d, %d, %p\n", debugstr_w(product), debugstr_w(usersid),
822           context, count, patchinfo);
823
824     if (!product) return ERROR_INVALID_PARAMETER;
825     r = open_package( product, usersid, context, &package );
826     if (r != ERROR_SUCCESS) return r;
827
828     r = determine_patch_sequence( package, count, patchinfo );
829     msiobj_release( &package->hdr );
830     return r;
831 }
832
833 UINT WINAPI MsiConfigureProductExW(LPCWSTR szProduct, int iInstallLevel,
834                         INSTALLSTATE eInstallState, LPCWSTR szCommandLine)
835 {
836     MSIPACKAGE* package = NULL;
837     MSIINSTALLCONTEXT context;
838     UINT r;
839     DWORD sz;
840     WCHAR sourcepath[MAX_PATH], filename[MAX_PATH];
841     LPWSTR commandline;
842
843     static const WCHAR szInstalled[] = {
844         ' ','I','n','s','t','a','l','l','e','d','=','1',0};
845     static const WCHAR szMaxInstallLevel[] = {
846         ' ','I','N','S','T','A','L','L','L','E','V','E','L','=','3','2','7','6','7',0};
847     static const WCHAR szRemoveAll[] = {
848         ' ','R','E','M','O','V','E','=','A','L','L',0};
849     static const WCHAR szMachine[] = {
850         ' ','A','L','L','U','S','E','R','S','=','1',0};
851
852     TRACE("%s %d %d %s\n",debugstr_w(szProduct), iInstallLevel, eInstallState,
853           debugstr_w(szCommandLine));
854
855     if (!szProduct || lstrlenW(szProduct) != GUID_SIZE - 1)
856         return ERROR_INVALID_PARAMETER;
857
858     if (eInstallState == INSTALLSTATE_ADVERTISED ||
859         eInstallState == INSTALLSTATE_SOURCE)
860     {
861         FIXME("State %d not implemented\n", eInstallState);
862         return ERROR_CALL_NOT_IMPLEMENTED;
863     }
864
865     r = msi_locate_product(szProduct, &context);
866     if (r != ERROR_SUCCESS)
867         return r;
868
869     r = open_package(szProduct, NULL, context, &package);
870     if (r != ERROR_SUCCESS)
871         return r;
872
873     sz = lstrlenW(szInstalled) + 1;
874
875     if (szCommandLine)
876         sz += lstrlenW(szCommandLine);
877
878     if (eInstallState != INSTALLSTATE_DEFAULT)
879         sz += lstrlenW(szMaxInstallLevel);
880
881     if (eInstallState == INSTALLSTATE_ABSENT)
882         sz += lstrlenW(szRemoveAll);
883
884     if (context == MSIINSTALLCONTEXT_MACHINE)
885         sz += lstrlenW(szMachine);
886
887     commandline = msi_alloc(sz * sizeof(WCHAR));
888     if (!commandline)
889     {
890         r = ERROR_OUTOFMEMORY;
891         goto end;
892     }
893
894     commandline[0] = 0;
895     if (szCommandLine)
896         lstrcpyW(commandline,szCommandLine);
897
898     if (eInstallState != INSTALLSTATE_DEFAULT)
899         lstrcatW(commandline, szMaxInstallLevel);
900
901     if (eInstallState == INSTALLSTATE_ABSENT)
902         lstrcatW(commandline, szRemoveAll);
903
904     if (context == MSIINSTALLCONTEXT_MACHINE)
905         lstrcatW(commandline, szMachine);
906
907     sz = sizeof(sourcepath);
908     MsiSourceListGetInfoW(szProduct, NULL, context, MSICODE_PRODUCT,
909                           INSTALLPROPERTY_LASTUSEDSOURCEW, sourcepath, &sz);
910
911     sz = sizeof(filename);
912     MsiSourceListGetInfoW(szProduct, NULL, context, MSICODE_PRODUCT,
913                           INSTALLPROPERTY_PACKAGENAMEW, filename, &sz);
914
915     strcatW(sourcepath, filename);
916
917     r = MSI_InstallPackage( package, sourcepath, commandline );
918
919     msi_free(commandline);
920
921 end:
922     msiobj_release( &package->hdr );
923
924     return r;
925 }
926
927 UINT WINAPI MsiConfigureProductExA(LPCSTR szProduct, int iInstallLevel,
928                         INSTALLSTATE eInstallState, LPCSTR szCommandLine)
929 {
930     LPWSTR szwProduct = NULL;
931     LPWSTR szwCommandLine = NULL;
932     UINT r = ERROR_OUTOFMEMORY;
933
934     if( szProduct )
935     {
936         szwProduct = strdupAtoW( szProduct );
937         if( !szwProduct )
938             goto end;
939     }
940
941     if( szCommandLine)
942     {
943         szwCommandLine = strdupAtoW( szCommandLine );
944         if( !szwCommandLine)
945             goto end;
946     }
947
948     r = MsiConfigureProductExW( szwProduct, iInstallLevel, eInstallState,
949                                 szwCommandLine );
950 end:
951     msi_free( szwProduct );
952     msi_free( szwCommandLine);
953
954     return r;
955 }
956
957 UINT WINAPI MsiConfigureProductA(LPCSTR szProduct, int iInstallLevel,
958                                  INSTALLSTATE eInstallState)
959 {
960     LPWSTR szwProduct = NULL;
961     UINT r;
962
963     TRACE("%s %d %d\n",debugstr_a(szProduct), iInstallLevel, eInstallState);
964
965     if( szProduct )
966     {
967         szwProduct = strdupAtoW( szProduct );
968         if( !szwProduct )
969             return ERROR_OUTOFMEMORY;
970     }
971
972     r = MsiConfigureProductW( szwProduct, iInstallLevel, eInstallState );
973     msi_free( szwProduct );
974
975     return r;
976 }
977
978 UINT WINAPI MsiConfigureProductW(LPCWSTR szProduct, int iInstallLevel,
979                                  INSTALLSTATE eInstallState)
980 {
981     return MsiConfigureProductExW(szProduct, iInstallLevel, eInstallState, NULL);
982 }
983
984 UINT WINAPI MsiGetProductCodeA(LPCSTR szComponent, LPSTR szBuffer)
985 {
986     LPWSTR szwComponent = NULL;
987     UINT r;
988     WCHAR szwBuffer[GUID_SIZE];
989
990     TRACE("%s %p\n", debugstr_a(szComponent), szBuffer);
991
992     if( szComponent )
993     {
994         szwComponent = strdupAtoW( szComponent );
995         if( !szwComponent )
996             return ERROR_OUTOFMEMORY;
997     }
998
999     *szwBuffer = '\0';
1000     r = MsiGetProductCodeW( szwComponent, szwBuffer );
1001
1002     if(*szwBuffer)
1003         WideCharToMultiByte(CP_ACP, 0, szwBuffer, -1, szBuffer, GUID_SIZE, NULL, NULL);
1004
1005     msi_free( szwComponent );
1006
1007     return r;
1008 }
1009
1010 UINT WINAPI MsiGetProductCodeW(LPCWSTR szComponent, LPWSTR szBuffer)
1011 {
1012     UINT rc, index;
1013     HKEY compkey, prodkey;
1014     WCHAR squished_comp[GUID_SIZE];
1015     WCHAR squished_prod[GUID_SIZE];
1016     DWORD sz = GUID_SIZE;
1017
1018     TRACE("%s %p\n", debugstr_w(szComponent), szBuffer);
1019
1020     if (!szComponent || !*szComponent)
1021         return ERROR_INVALID_PARAMETER;
1022
1023     if (!squash_guid(szComponent, squished_comp))
1024         return ERROR_INVALID_PARAMETER;
1025
1026     if (MSIREG_OpenUserDataComponentKey(szComponent, NULL, &compkey, FALSE) != ERROR_SUCCESS &&
1027         MSIREG_OpenUserDataComponentKey(szComponent, szLocalSid, &compkey, FALSE) != ERROR_SUCCESS)
1028     {
1029         return ERROR_UNKNOWN_COMPONENT;
1030     }
1031
1032     rc = RegEnumValueW(compkey, 0, squished_prod, &sz, NULL, NULL, NULL, NULL);
1033     if (rc != ERROR_SUCCESS)
1034     {
1035         RegCloseKey(compkey);
1036         return ERROR_UNKNOWN_COMPONENT;
1037     }
1038
1039     /* check simple case, only one product */
1040     rc = RegEnumValueW(compkey, 1, squished_prod, &sz, NULL, NULL, NULL, NULL);
1041     if (rc == ERROR_NO_MORE_ITEMS)
1042     {
1043         rc = ERROR_SUCCESS;
1044         goto done;
1045     }
1046
1047     index = 0;
1048     while ((rc = RegEnumValueW(compkey, index, squished_prod, &sz,
1049            NULL, NULL, NULL, NULL)) != ERROR_NO_MORE_ITEMS)
1050     {
1051         index++;
1052         sz = GUID_SIZE;
1053         unsquash_guid(squished_prod, szBuffer);
1054
1055         if (MSIREG_OpenProductKey(szBuffer, NULL,
1056                                   MSIINSTALLCONTEXT_USERMANAGED,
1057                                   &prodkey, FALSE) == ERROR_SUCCESS ||
1058             MSIREG_OpenProductKey(szBuffer, NULL,
1059                                   MSIINSTALLCONTEXT_USERUNMANAGED,
1060                                   &prodkey, FALSE) == ERROR_SUCCESS ||
1061             MSIREG_OpenProductKey(szBuffer, NULL,
1062                                   MSIINSTALLCONTEXT_MACHINE,
1063                                   &prodkey, FALSE) == ERROR_SUCCESS)
1064         {
1065             RegCloseKey(prodkey);
1066             rc = ERROR_SUCCESS;
1067             goto done;
1068         }
1069     }
1070
1071     rc = ERROR_INSTALL_FAILURE;
1072
1073 done:
1074     RegCloseKey(compkey);
1075     unsquash_guid(squished_prod, szBuffer);
1076     return rc;
1077 }
1078
1079 static LPWSTR msi_reg_get_value(HKEY hkey, LPCWSTR name, DWORD *type)
1080 {
1081     DWORD dval;
1082     LONG res;
1083     WCHAR temp[20];
1084
1085     static const WCHAR format[] = {'%','d',0};
1086
1087     res = RegQueryValueExW(hkey, name, NULL, type, NULL, NULL);
1088     if (res != ERROR_SUCCESS)
1089         return NULL;
1090
1091     if (*type == REG_SZ)
1092         return msi_reg_get_val_str(hkey, name);
1093
1094     if (!msi_reg_get_val_dword(hkey, name, &dval))
1095         return NULL;
1096
1097     sprintfW(temp, format, dval);
1098     return strdupW(temp);
1099 }
1100
1101 static UINT MSI_GetProductInfo(LPCWSTR szProduct, LPCWSTR szAttribute,
1102                                awstring *szValue, LPDWORD pcchValueBuf)
1103 {
1104     MSIINSTALLCONTEXT context = MSIINSTALLCONTEXT_USERUNMANAGED;
1105     UINT r = ERROR_UNKNOWN_PROPERTY;
1106     HKEY prodkey, userdata, source;
1107     LPWSTR val = NULL;
1108     WCHAR squished_pc[GUID_SIZE];
1109     WCHAR packagecode[GUID_SIZE];
1110     BOOL badconfig = FALSE;
1111     LONG res;
1112     DWORD type = REG_NONE;
1113
1114     static WCHAR empty[] = {0};
1115     static const WCHAR sourcelist[] = {
1116         'S','o','u','r','c','e','L','i','s','t',0};
1117     static const WCHAR display_name[] = {
1118         'D','i','s','p','l','a','y','N','a','m','e',0};
1119     static const WCHAR display_version[] = {
1120         'D','i','s','p','l','a','y','V','e','r','s','i','o','n',0};
1121     static const WCHAR assignment[] = {
1122         'A','s','s','i','g','n','m','e','n','t',0};
1123
1124     TRACE("%s %s %p %p\n", debugstr_w(szProduct),
1125           debugstr_w(szAttribute), szValue, pcchValueBuf);
1126
1127     if ((szValue->str.w && !pcchValueBuf) || !szProduct || !szAttribute)
1128         return ERROR_INVALID_PARAMETER;
1129
1130     if (!squash_guid(szProduct, squished_pc))
1131         return ERROR_INVALID_PARAMETER;
1132
1133     if ((r = MSIREG_OpenProductKey(szProduct, NULL,
1134                                    MSIINSTALLCONTEXT_USERMANAGED,
1135                                    &prodkey, FALSE)) != ERROR_SUCCESS &&
1136         (r = MSIREG_OpenProductKey(szProduct, NULL,
1137                                    MSIINSTALLCONTEXT_USERUNMANAGED,
1138                                    &prodkey, FALSE)) != ERROR_SUCCESS &&
1139         (r = MSIREG_OpenProductKey(szProduct, NULL,
1140                                    MSIINSTALLCONTEXT_MACHINE,
1141                                     &prodkey, FALSE)) == ERROR_SUCCESS)
1142     {
1143         context = MSIINSTALLCONTEXT_MACHINE;
1144     }
1145
1146     MSIREG_OpenInstallProps(szProduct, context, NULL, &userdata, FALSE);
1147
1148     if (!strcmpW( szAttribute, INSTALLPROPERTY_HELPLINKW ) ||
1149         !strcmpW( szAttribute, INSTALLPROPERTY_HELPTELEPHONEW ) ||
1150         !strcmpW( szAttribute, INSTALLPROPERTY_INSTALLDATEW ) ||
1151         !strcmpW( szAttribute, INSTALLPROPERTY_INSTALLEDPRODUCTNAMEW ) ||
1152         !strcmpW( szAttribute, INSTALLPROPERTY_INSTALLLOCATIONW ) ||
1153         !strcmpW( szAttribute, INSTALLPROPERTY_INSTALLSOURCEW ) ||
1154         !strcmpW( szAttribute, INSTALLPROPERTY_LOCALPACKAGEW ) ||
1155         !strcmpW( szAttribute, INSTALLPROPERTY_PUBLISHERW ) ||
1156         !strcmpW( szAttribute, INSTALLPROPERTY_URLINFOABOUTW ) ||
1157         !strcmpW( szAttribute, INSTALLPROPERTY_URLUPDATEINFOW ) ||
1158         !strcmpW( szAttribute, INSTALLPROPERTY_VERSIONMINORW ) ||
1159         !strcmpW( szAttribute, INSTALLPROPERTY_VERSIONMAJORW ) ||
1160         !strcmpW( szAttribute, INSTALLPROPERTY_VERSIONSTRINGW ) ||
1161         !strcmpW( szAttribute, INSTALLPROPERTY_PRODUCTIDW ) ||
1162         !strcmpW( szAttribute, INSTALLPROPERTY_REGCOMPANYW ) ||
1163         !strcmpW( szAttribute, INSTALLPROPERTY_REGOWNERW ))
1164     {
1165         if (!prodkey)
1166         {
1167             r = ERROR_UNKNOWN_PRODUCT;
1168             goto done;
1169         }
1170
1171         if (!userdata)
1172             return ERROR_UNKNOWN_PROPERTY;
1173
1174         if (!strcmpW( szAttribute, INSTALLPROPERTY_INSTALLEDPRODUCTNAMEW ))
1175             szAttribute = display_name;
1176         else if (!strcmpW( szAttribute, INSTALLPROPERTY_VERSIONSTRINGW ))
1177             szAttribute = display_version;
1178
1179         val = msi_reg_get_value(userdata, szAttribute, &type);
1180         if (!val)
1181             val = empty;
1182     }
1183     else if (!strcmpW( szAttribute, INSTALLPROPERTY_INSTANCETYPEW ) ||
1184              !strcmpW( szAttribute, INSTALLPROPERTY_TRANSFORMSW ) ||
1185              !strcmpW( szAttribute, INSTALLPROPERTY_LANGUAGEW ) ||
1186              !strcmpW( szAttribute, INSTALLPROPERTY_PRODUCTNAMEW ) ||
1187              !strcmpW( szAttribute, INSTALLPROPERTY_ASSIGNMENTTYPEW ) ||
1188              !strcmpW( szAttribute, INSTALLPROPERTY_PACKAGECODEW ) ||
1189              !strcmpW( szAttribute, INSTALLPROPERTY_VERSIONW ) ||
1190              !strcmpW( szAttribute, INSTALLPROPERTY_PRODUCTICONW ) ||
1191              !strcmpW( szAttribute, INSTALLPROPERTY_PACKAGENAMEW ) ||
1192              !strcmpW( szAttribute, INSTALLPROPERTY_AUTHORIZED_LUA_APPW ))
1193     {
1194         if (!prodkey)
1195         {
1196             r = ERROR_UNKNOWN_PRODUCT;
1197             goto done;
1198         }
1199
1200         if (!strcmpW( szAttribute, INSTALLPROPERTY_ASSIGNMENTTYPEW ))
1201             szAttribute = assignment;
1202
1203         if (!strcmpW( szAttribute, INSTALLPROPERTY_PACKAGENAMEW ))
1204         {
1205             res = RegOpenKeyW(prodkey, sourcelist, &source);
1206             if (res != ERROR_SUCCESS)
1207             {
1208                 r = ERROR_UNKNOWN_PRODUCT;
1209                 goto done;
1210             }
1211
1212             val = msi_reg_get_value(source, szAttribute, &type);
1213             if (!val)
1214                 val = empty;
1215
1216             RegCloseKey(source);
1217         }
1218         else
1219         {
1220             val = msi_reg_get_value(prodkey, szAttribute, &type);
1221             if (!val)
1222                 val = empty;
1223         }
1224
1225         if (val != empty && type != REG_DWORD &&
1226             !strcmpW( szAttribute, INSTALLPROPERTY_PACKAGECODEW ))
1227         {
1228             if (lstrlenW(val) != SQUISH_GUID_SIZE - 1)
1229                 badconfig = TRUE;
1230             else
1231             {
1232                 unsquash_guid(val, packagecode);
1233                 msi_free(val);
1234                 val = strdupW(packagecode);
1235             }
1236         }
1237     }
1238
1239     if (!val)
1240     {
1241         r = ERROR_UNKNOWN_PROPERTY;
1242         goto done;
1243     }
1244
1245     if (pcchValueBuf)
1246     {
1247         /* If szBuffer (szValue->str) is NULL, there's no need to copy the value
1248          * out.  Also, *pcchValueBuf may be uninitialized in this case, so we
1249          * can't rely on its value.
1250          */
1251         if (szValue->str.a || szValue->str.w)
1252         {
1253             DWORD size = *pcchValueBuf;
1254             if (strlenW(val) < size)
1255                 r = msi_strcpy_to_awstring(val, szValue, &size);
1256             else
1257             {
1258                 r = ERROR_MORE_DATA;
1259             }
1260         }
1261
1262         if (!badconfig)
1263             *pcchValueBuf = lstrlenW(val);
1264     }
1265
1266     if (badconfig)
1267         r = ERROR_BAD_CONFIGURATION;
1268
1269     if (val != empty)
1270         msi_free(val);
1271
1272 done:
1273     RegCloseKey(prodkey);
1274     RegCloseKey(userdata);
1275     return r;
1276 }
1277
1278 UINT WINAPI MsiGetProductInfoA(LPCSTR szProduct, LPCSTR szAttribute,
1279                                LPSTR szBuffer, LPDWORD pcchValueBuf)
1280 {
1281     LPWSTR szwProduct, szwAttribute = NULL;
1282     UINT r = ERROR_OUTOFMEMORY;
1283     awstring buffer;
1284
1285     TRACE("%s %s %p %p\n", debugstr_a(szProduct), debugstr_a(szAttribute),
1286           szBuffer, pcchValueBuf);
1287
1288     szwProduct = strdupAtoW( szProduct );
1289     if( szProduct && !szwProduct )
1290         goto end;
1291
1292     szwAttribute = strdupAtoW( szAttribute );
1293     if( szAttribute && !szwAttribute )
1294         goto end;
1295
1296     buffer.unicode = FALSE;
1297     buffer.str.a = szBuffer;
1298
1299     r = MSI_GetProductInfo( szwProduct, szwAttribute,
1300                             &buffer, pcchValueBuf );
1301
1302 end:
1303     msi_free( szwProduct );
1304     msi_free( szwAttribute );
1305
1306     return r;
1307 }
1308
1309 UINT WINAPI MsiGetProductInfoW(LPCWSTR szProduct, LPCWSTR szAttribute,
1310                                LPWSTR szBuffer, LPDWORD pcchValueBuf)
1311 {
1312     awstring buffer;
1313
1314     TRACE("%s %s %p %p\n", debugstr_w(szProduct), debugstr_w(szAttribute),
1315           szBuffer, pcchValueBuf);
1316
1317     buffer.unicode = TRUE;
1318     buffer.str.w = szBuffer;
1319
1320     return MSI_GetProductInfo( szProduct, szAttribute,
1321                                &buffer, pcchValueBuf );
1322 }
1323
1324 UINT WINAPI MsiGetProductInfoExA(LPCSTR szProductCode, LPCSTR szUserSid,
1325                                  MSIINSTALLCONTEXT dwContext, LPCSTR szProperty,
1326                                  LPSTR szValue, LPDWORD pcchValue)
1327 {
1328     LPWSTR product = NULL;
1329     LPWSTR usersid = NULL;
1330     LPWSTR property = NULL;
1331     LPWSTR value = NULL;
1332     DWORD len = 0;
1333     UINT r;
1334
1335     TRACE("(%s, %s, %d, %s, %p, %p)\n", debugstr_a(szProductCode),
1336           debugstr_a(szUserSid), dwContext, debugstr_a(szProperty),
1337            szValue, pcchValue);
1338
1339     if (szValue && !pcchValue)
1340         return ERROR_INVALID_PARAMETER;
1341
1342     if (szProductCode) product = strdupAtoW(szProductCode);
1343     if (szUserSid) usersid = strdupAtoW(szUserSid);
1344     if (szProperty) property = strdupAtoW(szProperty);
1345
1346     r = MsiGetProductInfoExW(product, usersid, dwContext, property,
1347                              NULL, &len);
1348     if (r != ERROR_SUCCESS)
1349         goto done;
1350
1351     value = msi_alloc(++len * sizeof(WCHAR));
1352     if (!value)
1353     {
1354         r = ERROR_OUTOFMEMORY;
1355         goto done;
1356     }
1357
1358     r = MsiGetProductInfoExW(product, usersid, dwContext, property,
1359                              value, &len);
1360     if (r != ERROR_SUCCESS)
1361         goto done;
1362
1363     if (!pcchValue)
1364         goto done;
1365
1366     len = WideCharToMultiByte(CP_ACP, 0, value, -1, NULL, 0, NULL, NULL);
1367     if (*pcchValue >= len)
1368         WideCharToMultiByte(CP_ACP, 0, value, -1, szValue, len, NULL, NULL);
1369     else if (szValue)
1370     {
1371         r = ERROR_MORE_DATA;
1372         if (*pcchValue > 0)
1373             *szValue = '\0';
1374     }
1375
1376     if (*pcchValue <= len || !szValue)
1377         len = len * sizeof(WCHAR) - 1;
1378
1379     *pcchValue = len - 1;
1380
1381 done:
1382     msi_free(product);
1383     msi_free(usersid);
1384     msi_free(property);
1385     msi_free(value);
1386
1387     return r;
1388 }
1389
1390 static UINT msi_copy_outval(LPWSTR val, LPWSTR out, LPDWORD size)
1391 {
1392     UINT r = ERROR_SUCCESS;
1393
1394     if (!val)
1395         return ERROR_UNKNOWN_PROPERTY;
1396
1397     if (out)
1398     {
1399         if (strlenW(val) >= *size)
1400         {
1401             r = ERROR_MORE_DATA;
1402             if (*size > 0)
1403                 *out = '\0';
1404         }
1405         else
1406             lstrcpyW(out, val);
1407     }
1408
1409     if (size)
1410         *size = lstrlenW(val);
1411
1412     return r;
1413 }
1414
1415 UINT WINAPI MsiGetProductInfoExW(LPCWSTR szProductCode, LPCWSTR szUserSid,
1416                                  MSIINSTALLCONTEXT dwContext, LPCWSTR szProperty,
1417                                  LPWSTR szValue, LPDWORD pcchValue)
1418 {
1419     WCHAR squished_pc[GUID_SIZE];
1420     LPWSTR val = NULL;
1421     LPCWSTR package = NULL;
1422     HKEY props = NULL, prod;
1423     HKEY classes = NULL, managed;
1424     HKEY hkey = NULL;
1425     DWORD type;
1426     UINT r = ERROR_UNKNOWN_PRODUCT;
1427
1428     static const WCHAR five[] = {'5',0};
1429     static const WCHAR displayname[] = {
1430         'D','i','s','p','l','a','y','N','a','m','e',0};
1431     static const WCHAR displayversion[] = {
1432         'D','i','s','p','l','a','y','V','e','r','s','i','o','n',0};
1433     static const WCHAR managed_local_package[] = {
1434         'M','a','n','a','g','e','d','L','o','c','a','l',
1435         'P','a','c','k','a','g','e',0};
1436
1437     TRACE("(%s, %s, %d, %s, %p, %p)\n", debugstr_w(szProductCode),
1438           debugstr_w(szUserSid), dwContext, debugstr_w(szProperty),
1439            szValue, pcchValue);
1440
1441     if (!szProductCode || !squash_guid(szProductCode, squished_pc))
1442         return ERROR_INVALID_PARAMETER;
1443
1444     if (szValue && !pcchValue)
1445         return ERROR_INVALID_PARAMETER;
1446
1447     if (dwContext != MSIINSTALLCONTEXT_USERUNMANAGED &&
1448         dwContext != MSIINSTALLCONTEXT_USERMANAGED &&
1449         dwContext != MSIINSTALLCONTEXT_MACHINE)
1450         return ERROR_INVALID_PARAMETER;
1451
1452     if (!szProperty || !*szProperty)
1453         return ERROR_INVALID_PARAMETER;
1454
1455     if (dwContext == MSIINSTALLCONTEXT_MACHINE && szUserSid)
1456         return ERROR_INVALID_PARAMETER;
1457
1458     /* FIXME: dwContext is provided, no need to search for it */
1459     MSIREG_OpenProductKey(szProductCode, NULL,MSIINSTALLCONTEXT_USERMANAGED,
1460                           &managed, FALSE);
1461     MSIREG_OpenProductKey(szProductCode, NULL, MSIINSTALLCONTEXT_USERUNMANAGED,
1462                           &prod, FALSE);
1463
1464     MSIREG_OpenInstallProps(szProductCode, dwContext, NULL, &props, FALSE);
1465
1466     if (dwContext == MSIINSTALLCONTEXT_USERUNMANAGED)
1467     {
1468         package = INSTALLPROPERTY_LOCALPACKAGEW;
1469
1470         if (!props && !prod)
1471             goto done;
1472     }
1473     else if (dwContext == MSIINSTALLCONTEXT_USERMANAGED)
1474     {
1475         package = managed_local_package;
1476
1477         if (!props && !managed)
1478             goto done;
1479     }
1480     else if (dwContext == MSIINSTALLCONTEXT_MACHINE)
1481     {
1482         package = INSTALLPROPERTY_LOCALPACKAGEW;
1483         MSIREG_OpenProductKey(szProductCode, NULL, dwContext, &classes, FALSE);
1484
1485         if (!props && !classes)
1486             goto done;
1487     }
1488
1489     if (!strcmpW( szProperty, INSTALLPROPERTY_HELPLINKW ) ||
1490         !strcmpW( szProperty, INSTALLPROPERTY_HELPTELEPHONEW ) ||
1491         !strcmpW( szProperty, INSTALLPROPERTY_INSTALLDATEW ) ||
1492         !strcmpW( szProperty, INSTALLPROPERTY_INSTALLEDPRODUCTNAMEW ) ||
1493         !strcmpW( szProperty, INSTALLPROPERTY_INSTALLLOCATIONW ) ||
1494         !strcmpW( szProperty, INSTALLPROPERTY_INSTALLSOURCEW ) ||
1495         !strcmpW( szProperty, INSTALLPROPERTY_LOCALPACKAGEW ) ||
1496         !strcmpW( szProperty, INSTALLPROPERTY_PUBLISHERW ) ||
1497         !strcmpW( szProperty, INSTALLPROPERTY_URLINFOABOUTW ) ||
1498         !strcmpW( szProperty, INSTALLPROPERTY_URLUPDATEINFOW ) ||
1499         !strcmpW( szProperty, INSTALLPROPERTY_VERSIONMINORW ) ||
1500         !strcmpW( szProperty, INSTALLPROPERTY_VERSIONMAJORW ) ||
1501         !strcmpW( szProperty, INSTALLPROPERTY_VERSIONSTRINGW ) ||
1502         !strcmpW( szProperty, INSTALLPROPERTY_PRODUCTIDW ) ||
1503         !strcmpW( szProperty, INSTALLPROPERTY_REGCOMPANYW ) ||
1504         !strcmpW( szProperty, INSTALLPROPERTY_REGOWNERW ) ||
1505         !strcmpW( szProperty, INSTALLPROPERTY_INSTANCETYPEW ))
1506     {
1507         val = msi_reg_get_value(props, package, &type);
1508         if (!val)
1509         {
1510             if (prod || classes)
1511                 r = ERROR_UNKNOWN_PROPERTY;
1512
1513             goto done;
1514         }
1515
1516         msi_free(val);
1517
1518         if (!strcmpW( szProperty, INSTALLPROPERTY_INSTALLEDPRODUCTNAMEW ))
1519             szProperty = displayname;
1520         else if (!strcmpW( szProperty, INSTALLPROPERTY_VERSIONSTRINGW ))
1521             szProperty = displayversion;
1522
1523         val = msi_reg_get_value(props, szProperty, &type);
1524         if (!val)
1525             val = strdupW(szEmpty);
1526
1527         r = msi_copy_outval(val, szValue, pcchValue);
1528     }
1529     else if (!strcmpW( szProperty, INSTALLPROPERTY_TRANSFORMSW ) ||
1530              !strcmpW( szProperty, INSTALLPROPERTY_LANGUAGEW ) ||
1531              !strcmpW( szProperty, INSTALLPROPERTY_PRODUCTNAMEW ) ||
1532              !strcmpW( szProperty, INSTALLPROPERTY_PACKAGECODEW ) ||
1533              !strcmpW( szProperty, INSTALLPROPERTY_VERSIONW ) ||
1534              !strcmpW( szProperty, INSTALLPROPERTY_PRODUCTICONW ) ||
1535              !strcmpW( szProperty, INSTALLPROPERTY_PACKAGENAMEW ) ||
1536              !strcmpW( szProperty, INSTALLPROPERTY_AUTHORIZED_LUA_APPW ))
1537     {
1538         if (!prod && !classes)
1539             goto done;
1540
1541         if (dwContext == MSIINSTALLCONTEXT_USERUNMANAGED)
1542             hkey = prod;
1543         else if (dwContext == MSIINSTALLCONTEXT_USERMANAGED)
1544             hkey = managed;
1545         else if (dwContext == MSIINSTALLCONTEXT_MACHINE)
1546             hkey = classes;
1547
1548         val = msi_reg_get_value(hkey, szProperty, &type);
1549         if (!val)
1550             val = strdupW(szEmpty);
1551
1552         r = msi_copy_outval(val, szValue, pcchValue);
1553     }
1554     else if (!strcmpW( szProperty, INSTALLPROPERTY_PRODUCTSTATEW ))
1555     {
1556         if (dwContext == MSIINSTALLCONTEXT_MACHINE)
1557         {
1558             if (props)
1559             {
1560                 val = msi_reg_get_value(props, package, &type);
1561                 if (!val)
1562                     goto done;
1563
1564                 msi_free(val);
1565                 val = strdupW(five);
1566             }
1567             else
1568                 val = strdupW(szOne);
1569
1570             r = msi_copy_outval(val, szValue, pcchValue);
1571             goto done;
1572         }
1573         else if (props && (val = msi_reg_get_value(props, package, &type)))
1574         {
1575             msi_free(val);
1576             val = strdupW(five);
1577             r = msi_copy_outval(val, szValue, pcchValue);
1578             goto done;
1579         }
1580
1581         if (prod || managed)
1582             val = strdupW(szOne);
1583         else
1584             goto done;
1585
1586         r = msi_copy_outval(val, szValue, pcchValue);
1587     }
1588     else if (!strcmpW( szProperty, INSTALLPROPERTY_ASSIGNMENTTYPEW ))
1589     {
1590         if (!prod && !classes)
1591             goto done;
1592
1593         /* FIXME */
1594         val = strdupW(szEmpty);
1595         r = msi_copy_outval(val, szValue, pcchValue);
1596     }
1597     else
1598         r = ERROR_UNKNOWN_PROPERTY;
1599
1600 done:
1601     RegCloseKey(props);
1602     RegCloseKey(prod);
1603     RegCloseKey(managed);
1604     RegCloseKey(classes);
1605     msi_free(val);
1606
1607     return r;
1608 }
1609
1610 UINT WINAPI MsiGetPatchInfoExA(LPCSTR szPatchCode, LPCSTR szProductCode,
1611                                LPCSTR szUserSid, MSIINSTALLCONTEXT dwContext,
1612                                LPCSTR szProperty, LPSTR lpValue, DWORD *pcchValue)
1613 {
1614     LPWSTR patch = NULL, product = NULL, usersid = NULL;
1615     LPWSTR property = NULL, val = NULL;
1616     DWORD len;
1617     UINT r;
1618
1619     TRACE("(%s, %s, %s, %d, %s, %p, %p)\n", debugstr_a(szPatchCode),
1620           debugstr_a(szProductCode), debugstr_a(szUserSid), dwContext,
1621           debugstr_a(szProperty), lpValue, pcchValue);
1622
1623     if (lpValue && !pcchValue)
1624         return ERROR_INVALID_PARAMETER;
1625
1626     if (szPatchCode) patch = strdupAtoW(szPatchCode);
1627     if (szProductCode) product = strdupAtoW(szProductCode);
1628     if (szUserSid) usersid = strdupAtoW(szUserSid);
1629     if (szProperty) property = strdupAtoW(szProperty);
1630
1631     len = 0;
1632     r = MsiGetPatchInfoExW(patch, product, usersid, dwContext, property,
1633                            NULL, &len);
1634     if (r != ERROR_SUCCESS)
1635         goto done;
1636
1637     val = msi_alloc(++len * sizeof(WCHAR));
1638     if (!val)
1639     {
1640         r = ERROR_OUTOFMEMORY;
1641         goto done;
1642     }
1643
1644     r = MsiGetPatchInfoExW(patch, product, usersid, dwContext, property,
1645                            val, &len);
1646     if (r != ERROR_SUCCESS || !pcchValue)
1647         goto done;
1648
1649     if (lpValue)
1650         WideCharToMultiByte(CP_ACP, 0, val, -1, lpValue,
1651                             *pcchValue - 1, NULL, NULL);
1652
1653     len = lstrlenW(val);
1654     if ((*val && *pcchValue < len + 1) || !lpValue)
1655     {
1656         if (lpValue)
1657         {
1658             r = ERROR_MORE_DATA;
1659             lpValue[*pcchValue - 1] = '\0';
1660         }
1661
1662         *pcchValue = len * sizeof(WCHAR);
1663     }
1664     else
1665         *pcchValue = len;
1666
1667 done:
1668     msi_free(val);
1669     msi_free(patch);
1670     msi_free(product);
1671     msi_free(usersid);
1672     msi_free(property);
1673
1674     return r;
1675 }
1676
1677 UINT WINAPI MsiGetPatchInfoExW(LPCWSTR szPatchCode, LPCWSTR szProductCode,
1678                                LPCWSTR szUserSid, MSIINSTALLCONTEXT dwContext,
1679                                LPCWSTR szProperty, LPWSTR lpValue, DWORD *pcchValue)
1680 {
1681     WCHAR squished_pc[GUID_SIZE];
1682     WCHAR squished_patch[GUID_SIZE];
1683     HKEY udprod = 0, prod = 0, props = 0;
1684     HKEY patch = 0, patches = 0;
1685     HKEY udpatch = 0, datakey = 0;
1686     HKEY prodpatches = 0;
1687     LPWSTR val = NULL;
1688     UINT r = ERROR_UNKNOWN_PRODUCT;
1689     DWORD len;
1690     LONG res;
1691
1692     static const WCHAR szManagedPackage[] = {'M','a','n','a','g','e','d',
1693         'L','o','c','a','l','P','a','c','k','a','g','e',0};
1694
1695     TRACE("(%s, %s, %s, %d, %s, %p, %p)\n", debugstr_w(szPatchCode),
1696           debugstr_w(szProductCode), debugstr_w(szUserSid), dwContext,
1697           debugstr_w(szProperty), lpValue, pcchValue);
1698
1699     if (!szProductCode || !squash_guid(szProductCode, squished_pc))
1700         return ERROR_INVALID_PARAMETER;
1701
1702     if (!szPatchCode || !squash_guid(szPatchCode, squished_patch))
1703         return ERROR_INVALID_PARAMETER;
1704
1705     if (!szProperty)
1706         return ERROR_INVALID_PARAMETER;
1707
1708     if (lpValue && !pcchValue)
1709         return ERROR_INVALID_PARAMETER;
1710
1711     if (dwContext != MSIINSTALLCONTEXT_USERMANAGED &&
1712         dwContext != MSIINSTALLCONTEXT_USERUNMANAGED &&
1713         dwContext != MSIINSTALLCONTEXT_MACHINE)
1714         return ERROR_INVALID_PARAMETER;
1715
1716     if (dwContext == MSIINSTALLCONTEXT_MACHINE && szUserSid)
1717         return ERROR_INVALID_PARAMETER;
1718
1719     if (szUserSid && !strcmpW( szUserSid, szLocalSid ))
1720         return ERROR_INVALID_PARAMETER;
1721
1722     if (MSIREG_OpenUserDataProductKey(szProductCode, dwContext, NULL,
1723                                       &udprod, FALSE) != ERROR_SUCCESS)
1724         goto done;
1725
1726     if (MSIREG_OpenInstallProps(szProductCode, dwContext, NULL,
1727                                 &props, FALSE) != ERROR_SUCCESS)
1728         goto done;
1729
1730     r = ERROR_UNKNOWN_PATCH;
1731
1732     res = RegOpenKeyExW(udprod, szPatches, 0, KEY_WOW64_64KEY|KEY_READ, &patches);
1733     if (res != ERROR_SUCCESS)
1734         goto done;
1735
1736     res = RegOpenKeyExW(patches, squished_patch, 0, KEY_WOW64_64KEY|KEY_READ, &patch);
1737     if (res != ERROR_SUCCESS)
1738         goto done;
1739
1740     if (!strcmpW( szProperty, INSTALLPROPERTY_TRANSFORMSW ))
1741     {
1742         if (MSIREG_OpenProductKey(szProductCode, NULL, dwContext,
1743                                   &prod, FALSE) != ERROR_SUCCESS)
1744             goto done;
1745
1746         res = RegOpenKeyExW(prod, szPatches, 0, KEY_WOW64_64KEY|KEY_ALL_ACCESS, &prodpatches);
1747         if (res != ERROR_SUCCESS)
1748             goto done;
1749
1750         datakey = prodpatches;
1751         szProperty = squished_patch;
1752     }
1753     else
1754     {
1755         if (MSIREG_OpenUserDataPatchKey(szPatchCode, dwContext,
1756                                         &udpatch, FALSE) != ERROR_SUCCESS)
1757             goto done;
1758
1759         if (!strcmpW( szProperty, INSTALLPROPERTY_LOCALPACKAGEW ))
1760         {
1761             if (dwContext == MSIINSTALLCONTEXT_USERMANAGED)
1762                 szProperty = szManagedPackage;
1763             datakey = udpatch;
1764         }
1765         else if (!strcmpW( szProperty, INSTALLPROPERTY_INSTALLDATEW ))
1766         {
1767             datakey = patch;
1768             szProperty = szInstalled;
1769         }
1770         else if (!strcmpW( szProperty, INSTALLPROPERTY_LOCALPACKAGEW ))
1771         {
1772             datakey = udpatch;
1773         }
1774         else if (!strcmpW( szProperty, INSTALLPROPERTY_UNINSTALLABLEW ) ||
1775                  !strcmpW( szProperty, INSTALLPROPERTY_PATCHSTATEW ) ||
1776                  !strcmpW( szProperty, INSTALLPROPERTY_DISPLAYNAMEW ) ||
1777                  !strcmpW( szProperty, INSTALLPROPERTY_MOREINFOURLW ))
1778         {
1779             datakey = patch;
1780         }
1781         else
1782         {
1783             r = ERROR_UNKNOWN_PROPERTY;
1784             goto done;
1785         }
1786     }
1787
1788     val = msi_reg_get_val_str(datakey, szProperty);
1789     if (!val)
1790         val = strdupW(szEmpty);
1791
1792     r = ERROR_SUCCESS;
1793
1794     if (!pcchValue)
1795         goto done;
1796
1797     if (lpValue)
1798         lstrcpynW(lpValue, val, *pcchValue);
1799
1800     len = lstrlenW(val);
1801     if ((*val && *pcchValue < len + 1) || !lpValue)
1802     {
1803         if (lpValue)
1804             r = ERROR_MORE_DATA;
1805
1806         *pcchValue = len * sizeof(WCHAR);
1807     }
1808
1809     *pcchValue = len;
1810
1811 done:
1812     msi_free(val);
1813     RegCloseKey(prodpatches);
1814     RegCloseKey(prod);
1815     RegCloseKey(patch);
1816     RegCloseKey(patches);
1817     RegCloseKey(udpatch);
1818     RegCloseKey(props);
1819     RegCloseKey(udprod);
1820
1821     return r;
1822 }
1823
1824 UINT WINAPI MsiGetPatchInfoA( LPCSTR patch, LPCSTR attr, LPSTR buffer, LPDWORD buflen )
1825 {
1826     UINT r = ERROR_OUTOFMEMORY;
1827     DWORD size;
1828     LPWSTR patchW = NULL, attrW = NULL, bufferW = NULL;
1829
1830     TRACE("%s %s %p %p\n", debugstr_a(patch), debugstr_a(attr), buffer, buflen);
1831
1832     if (!patch || !attr)
1833         return ERROR_INVALID_PARAMETER;
1834
1835     if (!(patchW = strdupAtoW( patch )))
1836         goto done;
1837
1838     if (!(attrW = strdupAtoW( attr )))
1839         goto done;
1840
1841     size = 0;
1842     r = MsiGetPatchInfoW( patchW, attrW, NULL, &size );
1843     if (r != ERROR_SUCCESS)
1844         goto done;
1845
1846     size++;
1847     if (!(bufferW = msi_alloc( size * sizeof(WCHAR) )))
1848     {
1849         r = ERROR_OUTOFMEMORY;
1850         goto done;
1851     }
1852
1853     r = MsiGetPatchInfoW( patchW, attrW, bufferW, &size );
1854     if (r == ERROR_SUCCESS)
1855     {
1856         int len = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
1857         if (len > *buflen)
1858             r = ERROR_MORE_DATA;
1859         else if (buffer)
1860             WideCharToMultiByte( CP_ACP, 0, bufferW, -1, buffer, *buflen, NULL, NULL );
1861
1862         *buflen = len - 1;
1863     }
1864
1865 done:
1866     msi_free( patchW );
1867     msi_free( attrW );
1868     msi_free( bufferW );
1869     return r;
1870 }
1871
1872 UINT WINAPI MsiGetPatchInfoW( LPCWSTR patch, LPCWSTR attr, LPWSTR buffer, LPDWORD buflen )
1873 {
1874     UINT r;
1875     WCHAR product[GUID_SIZE];
1876     DWORD index;
1877
1878     TRACE("%s %s %p %p\n", debugstr_w(patch), debugstr_w(attr), buffer, buflen);
1879
1880     if (!patch || !attr)
1881         return ERROR_INVALID_PARAMETER;
1882
1883     if (strcmpW( INSTALLPROPERTY_LOCALPACKAGEW, attr ))
1884         return ERROR_UNKNOWN_PROPERTY;
1885
1886     index = 0;
1887     while (1)
1888     {
1889         r = MsiEnumProductsW( index, product );
1890         if (r != ERROR_SUCCESS)
1891             break;
1892
1893         r = MsiGetPatchInfoExW( patch, product, NULL, MSIINSTALLCONTEXT_USERMANAGED, attr, buffer, buflen );
1894         if (r == ERROR_SUCCESS || r == ERROR_MORE_DATA)
1895             return r;
1896
1897         r = MsiGetPatchInfoExW( patch, product, NULL, MSIINSTALLCONTEXT_USERUNMANAGED, attr, buffer, buflen );
1898         if (r == ERROR_SUCCESS || r == ERROR_MORE_DATA)
1899             return r;
1900
1901         r = MsiGetPatchInfoExW( patch, product, NULL, MSIINSTALLCONTEXT_MACHINE, attr, buffer, buflen );
1902         if (r == ERROR_SUCCESS || r == ERROR_MORE_DATA)
1903             return r;
1904
1905         index++;
1906     }
1907
1908     return ERROR_UNKNOWN_PRODUCT;
1909 }
1910
1911 UINT WINAPI MsiEnableLogA(DWORD dwLogMode, LPCSTR szLogFile, DWORD attributes)
1912 {
1913     LPWSTR szwLogFile = NULL;
1914     UINT r;
1915
1916     TRACE("%08x %s %08x\n", dwLogMode, debugstr_a(szLogFile), attributes);
1917
1918     if( szLogFile )
1919     {
1920         szwLogFile = strdupAtoW( szLogFile );
1921         if( !szwLogFile )
1922             return ERROR_OUTOFMEMORY;
1923     }
1924     r = MsiEnableLogW( dwLogMode, szwLogFile, attributes );
1925     msi_free( szwLogFile );
1926     return r;
1927 }
1928
1929 UINT WINAPI MsiEnableLogW(DWORD dwLogMode, LPCWSTR szLogFile, DWORD attributes)
1930 {
1931     TRACE("%08x %s %08x\n", dwLogMode, debugstr_w(szLogFile), attributes);
1932
1933     msi_free(gszLogFile);
1934     gszLogFile = NULL;
1935     if (szLogFile)
1936     {
1937         HANDLE file;
1938
1939         if (!(attributes & INSTALLLOGATTRIBUTES_APPEND))
1940             DeleteFileW(szLogFile);
1941         file = CreateFileW(szLogFile, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_ALWAYS,
1942                            FILE_ATTRIBUTE_NORMAL, NULL);
1943         if (file != INVALID_HANDLE_VALUE)
1944         {
1945             gszLogFile = strdupW(szLogFile);
1946             CloseHandle(file);
1947         }
1948         else
1949             ERR("Unable to enable log %s (%u)\n", debugstr_w(szLogFile), GetLastError());
1950     }
1951
1952     return ERROR_SUCCESS;
1953 }
1954
1955 UINT WINAPI MsiEnumComponentCostsA( MSIHANDLE handle, LPCSTR component, DWORD index,
1956                                     INSTALLSTATE state, LPSTR drive, DWORD *buflen,
1957                                     int *cost, int *temp )
1958 {
1959     UINT r;
1960     DWORD len;
1961     WCHAR *driveW, *componentW = NULL;
1962
1963     TRACE("%d, %s, %u, %d, %p, %p, %p %p\n", handle, debugstr_a(component), index,
1964           state, drive, buflen, cost, temp);
1965
1966     if (!drive || !buflen) return ERROR_INVALID_PARAMETER;
1967     if (component && !(componentW = strdupAtoW( component ))) return ERROR_OUTOFMEMORY;
1968
1969     len = *buflen;
1970     if (!(driveW = msi_alloc( len * sizeof(WCHAR) )))
1971     {
1972         msi_free( componentW );
1973         return ERROR_OUTOFMEMORY;
1974     }
1975     r = MsiEnumComponentCostsW( handle, componentW, index, state, driveW, buflen, cost, temp );
1976     if (!r)
1977     {
1978         WideCharToMultiByte( CP_ACP, 0, driveW, -1, drive, len, NULL, NULL );
1979     }
1980     msi_free( componentW );
1981     msi_free( driveW );
1982     return r;
1983 }
1984
1985 static UINT set_drive( WCHAR *buffer, WCHAR letter )
1986 {
1987     buffer[0] = letter;
1988     buffer[1] = ':';
1989     buffer[2] = 0;
1990     return 2;
1991 }
1992
1993 UINT WINAPI MsiEnumComponentCostsW( MSIHANDLE handle, LPCWSTR component, DWORD index,
1994                                     INSTALLSTATE state, LPWSTR drive, DWORD *buflen,
1995                                     int *cost, int *temp )
1996 {
1997     UINT r = ERROR_NO_MORE_ITEMS;
1998     MSICOMPONENT *comp = NULL;
1999     MSIPACKAGE *package;
2000     MSIFILE *file;
2001     STATSTG stat = {0};
2002     WCHAR path[MAX_PATH];
2003
2004     TRACE("%d, %s, %u, %d, %p, %p, %p %p\n", handle, debugstr_w(component), index,
2005           state, drive, buflen, cost, temp);
2006
2007     if (!drive || !buflen || !cost || !temp) return ERROR_INVALID_PARAMETER;
2008     if (!(package = msihandle2msiinfo( handle, MSIHANDLETYPE_PACKAGE )))
2009     {
2010         HRESULT hr;
2011         IWineMsiRemotePackage *remote_package;
2012         BSTR bname = NULL;
2013
2014         if (!(remote_package = (IWineMsiRemotePackage *)msi_get_remote( handle )))
2015             return ERROR_INVALID_HANDLE;
2016
2017         if (component && !(bname = SysAllocString( component )))
2018         {
2019             IWineMsiRemotePackage_Release( remote_package );
2020             return ERROR_OUTOFMEMORY;
2021         }
2022         hr = IWineMsiRemotePackage_EnumComponentCosts( remote_package, bname, index, state, drive, buflen, cost, temp );
2023         IWineMsiRemotePackage_Release( remote_package );
2024         SysFreeString( bname );
2025         if (FAILED(hr))
2026         {
2027             if (HRESULT_FACILITY(hr) == FACILITY_WIN32) return HRESULT_CODE(hr);
2028             return ERROR_FUNCTION_FAILED;
2029         }
2030         return ERROR_SUCCESS;
2031     }
2032
2033     if (!msi_get_property_int( package->db, szCostingComplete, 0 ))
2034     {
2035         msiobj_release( &package->hdr );
2036         return ERROR_FUNCTION_NOT_CALLED;
2037     }
2038     if (component && component[0] && !(comp = msi_get_loaded_component( package, component )))
2039     {
2040         msiobj_release( &package->hdr );
2041         return ERROR_UNKNOWN_COMPONENT;
2042     }
2043     if (*buflen < 3)
2044     {
2045         *buflen = 2;
2046         msiobj_release( &package->hdr );
2047         return ERROR_MORE_DATA;
2048     }
2049     if (index)
2050     {
2051         msiobj_release( &package->hdr );
2052         return ERROR_NO_MORE_ITEMS;
2053     }
2054
2055     drive[0] = 0;
2056     *cost = *temp = 0;
2057     GetWindowsDirectoryW( path, MAX_PATH );
2058     if (component && component[0])
2059     {
2060         if (comp->assembly && !comp->assembly->application) *temp = comp->Cost;
2061         if (!comp->Enabled || !comp->KeyPath)
2062         {
2063             *cost = 0;
2064             *buflen = set_drive( drive, path[0] );
2065             r = ERROR_SUCCESS;
2066         }
2067         else if ((file = msi_get_loaded_file( package, comp->KeyPath )))
2068         {
2069             *cost = max( 8, comp->Cost / 512 );
2070             *buflen = set_drive( drive, file->TargetPath[0] );
2071             r = ERROR_SUCCESS;
2072         }
2073     }
2074     else if (IStorage_Stat( package->db->storage, &stat, STATFLAG_NONAME ) == S_OK)
2075     {
2076         *temp = max( 8, stat.cbSize.QuadPart / 512 );
2077         *buflen = set_drive( drive, path[0] );
2078         r = ERROR_SUCCESS;
2079     }
2080     msiobj_release( &package->hdr );
2081     return r;
2082 }
2083
2084 UINT WINAPI MsiQueryComponentStateA(LPCSTR szProductCode,
2085                                     LPCSTR szUserSid, MSIINSTALLCONTEXT dwContext,
2086                                     LPCSTR szComponent, INSTALLSTATE *pdwState)
2087 {
2088     LPWSTR prodcode = NULL, usersid = NULL, comp = NULL;
2089     UINT r;
2090
2091     TRACE("(%s, %s, %d, %s, %p)\n", debugstr_a(szProductCode),
2092           debugstr_a(szUserSid), dwContext, debugstr_a(szComponent), pdwState);
2093
2094     if (szProductCode && !(prodcode = strdupAtoW(szProductCode)))
2095         return ERROR_OUTOFMEMORY;
2096
2097     if (szUserSid && !(usersid = strdupAtoW(szUserSid)))
2098             return ERROR_OUTOFMEMORY;
2099
2100     if (szComponent && !(comp = strdupAtoW(szComponent)))
2101             return ERROR_OUTOFMEMORY;
2102
2103     r = MsiQueryComponentStateW(prodcode, usersid, dwContext, comp, pdwState);
2104
2105     msi_free(prodcode);
2106     msi_free(usersid);
2107     msi_free(comp);
2108
2109     return r;
2110 }
2111
2112 static BOOL msi_comp_find_prod_key(LPCWSTR prodcode, MSIINSTALLCONTEXT context)
2113 {
2114     UINT r;
2115     HKEY hkey = NULL;
2116
2117     r = MSIREG_OpenProductKey(prodcode, NULL, context, &hkey, FALSE);
2118     RegCloseKey(hkey);
2119     return (r == ERROR_SUCCESS);
2120 }
2121
2122 static BOOL msi_comp_find_package(LPCWSTR prodcode, MSIINSTALLCONTEXT context)
2123 {
2124     LPCWSTR package;
2125     HKEY hkey;
2126     DWORD sz;
2127     LONG res;
2128     UINT r;
2129
2130     static const WCHAR local_package[] = {'L','o','c','a','l','P','a','c','k','a','g','e',0};
2131     static const WCHAR managed_local_package[] = {
2132         'M','a','n','a','g','e','d','L','o','c','a','l','P','a','c','k','a','g','e',0
2133     };
2134
2135     r = MSIREG_OpenInstallProps(prodcode, context, NULL, &hkey, FALSE);
2136     if (r != ERROR_SUCCESS)
2137         return FALSE;
2138
2139     if (context == MSIINSTALLCONTEXT_USERMANAGED)
2140         package = managed_local_package;
2141     else
2142         package = local_package;
2143
2144     sz = 0;
2145     res = RegQueryValueExW(hkey, package, NULL, NULL, NULL, &sz);
2146     RegCloseKey(hkey);
2147
2148     return (res == ERROR_SUCCESS);
2149 }
2150
2151 static UINT msi_comp_find_prodcode(LPWSTR squished_pc,
2152                                    MSIINSTALLCONTEXT context,
2153                                    LPCWSTR comp, LPWSTR val, DWORD *sz)
2154 {
2155     HKEY hkey;
2156     LONG res;
2157     UINT r;
2158
2159     if (context == MSIINSTALLCONTEXT_MACHINE)
2160         r = MSIREG_OpenUserDataComponentKey(comp, szLocalSid, &hkey, FALSE);
2161     else
2162         r = MSIREG_OpenUserDataComponentKey(comp, NULL, &hkey, FALSE);
2163
2164     if (r != ERROR_SUCCESS)
2165         return r;
2166
2167     res = RegQueryValueExW(hkey, squished_pc, NULL, NULL, (BYTE *)val, sz);
2168     if (res != ERROR_SUCCESS)
2169         return res;
2170
2171     RegCloseKey(hkey);
2172     return res;
2173 }
2174
2175 UINT WINAPI MsiQueryComponentStateW(LPCWSTR szProductCode,
2176                                     LPCWSTR szUserSid, MSIINSTALLCONTEXT dwContext,
2177                                     LPCWSTR szComponent, INSTALLSTATE *pdwState)
2178 {
2179     WCHAR squished_pc[GUID_SIZE];
2180     BOOL found;
2181     DWORD sz;
2182
2183     TRACE("(%s, %s, %d, %s, %p)\n", debugstr_w(szProductCode),
2184           debugstr_w(szUserSid), dwContext, debugstr_w(szComponent), pdwState);
2185
2186     if (!pdwState || !szComponent)
2187         return ERROR_INVALID_PARAMETER;
2188
2189     if (!szProductCode || !*szProductCode || lstrlenW(szProductCode) != GUID_SIZE - 1)
2190         return ERROR_INVALID_PARAMETER;
2191
2192     if (!squash_guid(szProductCode, squished_pc))
2193         return ERROR_INVALID_PARAMETER;
2194
2195     found = msi_comp_find_prod_key(szProductCode, dwContext);
2196
2197     if (!msi_comp_find_package(szProductCode, dwContext))
2198     {
2199         if (found)
2200         {
2201             *pdwState = INSTALLSTATE_UNKNOWN;
2202             return ERROR_UNKNOWN_COMPONENT;
2203         }
2204
2205         return ERROR_UNKNOWN_PRODUCT;
2206     }
2207
2208     *pdwState = INSTALLSTATE_UNKNOWN;
2209
2210     sz = 0;
2211     if (msi_comp_find_prodcode(squished_pc, dwContext, szComponent, NULL, &sz))
2212         return ERROR_UNKNOWN_COMPONENT;
2213
2214     if (sz == 0)
2215         *pdwState = INSTALLSTATE_NOTUSED;
2216     else
2217     {
2218         WCHAR *val;
2219         UINT r;
2220
2221         if (!(val = msi_alloc( sz ))) return ERROR_OUTOFMEMORY;
2222         if ((r = msi_comp_find_prodcode(squished_pc, dwContext, szComponent, val, &sz)))
2223             return r;
2224
2225         if (lstrlenW(val) > 2 &&
2226             val[0] >= '0' && val[0] <= '9' && val[1] >= '0' && val[1] <= '9' && val[2] != ':')
2227         {
2228             *pdwState = INSTALLSTATE_SOURCE;
2229         }
2230         else
2231             *pdwState = INSTALLSTATE_LOCAL;
2232         msi_free( val );
2233     }
2234
2235     TRACE("-> %d\n", *pdwState);
2236     return ERROR_SUCCESS;
2237 }
2238
2239 INSTALLSTATE WINAPI MsiQueryProductStateA(LPCSTR szProduct)
2240 {
2241     LPWSTR szwProduct = NULL;
2242     INSTALLSTATE r;
2243
2244     if( szProduct )
2245     {
2246          szwProduct = strdupAtoW( szProduct );
2247          if( !szwProduct )
2248              return ERROR_OUTOFMEMORY;
2249     }
2250     r = MsiQueryProductStateW( szwProduct );
2251     msi_free( szwProduct );
2252     return r;
2253 }
2254
2255 INSTALLSTATE WINAPI MsiQueryProductStateW(LPCWSTR szProduct)
2256 {
2257     MSIINSTALLCONTEXT context = MSIINSTALLCONTEXT_USERUNMANAGED;
2258     INSTALLSTATE state = INSTALLSTATE_ADVERTISED;
2259     HKEY prodkey = 0, userdata = 0;
2260     DWORD val;
2261     UINT r;
2262
2263     TRACE("%s\n", debugstr_w(szProduct));
2264
2265     if (!szProduct || !*szProduct)
2266         return INSTALLSTATE_INVALIDARG;
2267
2268     if (lstrlenW(szProduct) != GUID_SIZE - 1)
2269         return INSTALLSTATE_INVALIDARG;
2270
2271     if (szProduct[0] != '{' || szProduct[37] != '}')
2272         return INSTALLSTATE_UNKNOWN;
2273
2274     SetLastError( ERROR_SUCCESS );
2275
2276     if (MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERMANAGED,
2277                               &prodkey, FALSE) != ERROR_SUCCESS &&
2278         MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERUNMANAGED,
2279                               &prodkey, FALSE) != ERROR_SUCCESS &&
2280         MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_MACHINE,
2281                               &prodkey, FALSE) == ERROR_SUCCESS)
2282     {
2283         context = MSIINSTALLCONTEXT_MACHINE;
2284     }
2285
2286     r = MSIREG_OpenInstallProps(szProduct, context, NULL, &userdata, FALSE);
2287     if (r != ERROR_SUCCESS)
2288         goto done;
2289
2290     if (!msi_reg_get_val_dword(userdata, szWindowsInstaller, &val))
2291         goto done;
2292
2293     if (val)
2294         state = INSTALLSTATE_DEFAULT;
2295     else
2296         state = INSTALLSTATE_UNKNOWN;
2297
2298 done:
2299     if (!prodkey)
2300     {
2301         state = INSTALLSTATE_UNKNOWN;
2302
2303         if (userdata)
2304             state = INSTALLSTATE_ABSENT;
2305     }
2306
2307     RegCloseKey(prodkey);
2308     RegCloseKey(userdata);
2309     TRACE("-> %d\n", state);
2310     return state;
2311 }
2312
2313 INSTALLUILEVEL WINAPI MsiSetInternalUI(INSTALLUILEVEL dwUILevel, HWND *phWnd)
2314 {
2315     INSTALLUILEVEL old = gUILevel;
2316     HWND oldwnd = gUIhwnd;
2317
2318     TRACE("%08x %p\n", dwUILevel, phWnd);
2319
2320     gUILevel = dwUILevel;
2321     if (phWnd)
2322     {
2323         gUIhwnd = *phWnd;
2324         *phWnd = oldwnd;
2325     }
2326     return old;
2327 }
2328
2329 INSTALLUI_HANDLERA WINAPI MsiSetExternalUIA(INSTALLUI_HANDLERA puiHandler,
2330                                   DWORD dwMessageFilter, LPVOID pvContext)
2331 {
2332     INSTALLUI_HANDLERA prev = gUIHandlerA;
2333
2334     TRACE("%p %08x %p\n", puiHandler, dwMessageFilter, pvContext);
2335
2336     gUIHandlerA = puiHandler;
2337     gUIHandlerW = NULL;
2338     gUIFilter   = dwMessageFilter;
2339     gUIContext  = pvContext;
2340
2341     return prev;
2342 }
2343
2344 INSTALLUI_HANDLERW WINAPI MsiSetExternalUIW(INSTALLUI_HANDLERW puiHandler,
2345                                   DWORD dwMessageFilter, LPVOID pvContext)
2346 {
2347     INSTALLUI_HANDLERW prev = gUIHandlerW;
2348
2349     TRACE("%p %08x %p\n", puiHandler, dwMessageFilter, pvContext);
2350
2351     gUIHandlerA = NULL;
2352     gUIHandlerW = puiHandler;
2353     gUIFilter   = dwMessageFilter;
2354     gUIContext  = pvContext;
2355
2356     return prev;
2357 }
2358
2359 /******************************************************************
2360  *  MsiLoadStringW            [MSI.@]
2361  *
2362  * Loads a string from MSI's string resources.
2363  *
2364  * PARAMS
2365  *
2366  *   handle        [I]  only -1 is handled currently
2367  *   id            [I]  id of the string to be loaded
2368  *   lpBuffer      [O]  buffer for the string to be written to
2369  *   nBufferMax    [I]  maximum size of the buffer in characters
2370  *   lang          [I]  the preferred language for the string
2371  *
2372  * RETURNS
2373  *
2374  *   If successful, this function returns the language id of the string loaded
2375  *   If the function fails, the function returns zero.
2376  *
2377  * NOTES
2378  *
2379  *   The type of the first parameter is unknown.  LoadString's prototype
2380  *  suggests that it might be a module handle.  I have made it an MSI handle
2381  *  for starters, as -1 is an invalid MSI handle, but not an invalid module
2382  *  handle.  Maybe strings can be stored in an MSI database somehow.
2383  */
2384 LANGID WINAPI MsiLoadStringW( MSIHANDLE handle, UINT id, LPWSTR lpBuffer,
2385                 int nBufferMax, LANGID lang )
2386 {
2387     HRSRC hres;
2388     HGLOBAL hResData;
2389     LPWSTR p;
2390     DWORD i, len;
2391
2392     TRACE("%d %u %p %d %d\n", handle, id, lpBuffer, nBufferMax, lang);
2393
2394     if( handle != -1 )
2395         FIXME("don't know how to deal with handle = %08x\n", handle);
2396
2397     if( !lang )
2398         lang = GetUserDefaultLangID();
2399
2400     hres = FindResourceExW( msi_hInstance, (LPCWSTR) RT_STRING,
2401                             (LPWSTR)1, lang );
2402     if( !hres )
2403         return 0;
2404     hResData = LoadResource( msi_hInstance, hres );
2405     if( !hResData )
2406         return 0;
2407     p = LockResource( hResData );
2408     if( !p )
2409         return 0;
2410
2411     for (i = 0; i < (id & 0xf); i++) p += *p + 1;
2412     len = *p;
2413
2414     if( nBufferMax <= len )
2415         return 0;
2416
2417     memcpy( lpBuffer, p+1, len * sizeof(WCHAR));
2418     lpBuffer[ len ] = 0;
2419
2420     TRACE("found -> %s\n", debugstr_w(lpBuffer));
2421     return lang;
2422 }
2423
2424 LANGID WINAPI MsiLoadStringA( MSIHANDLE handle, UINT id, LPSTR lpBuffer,
2425                 int nBufferMax, LANGID lang )
2426 {
2427     LPWSTR bufW;
2428     LANGID r;
2429     INT len;
2430
2431     bufW = msi_alloc(nBufferMax*sizeof(WCHAR));
2432     r = MsiLoadStringW(handle, id, bufW, nBufferMax, lang);
2433     if( r )
2434     {
2435         len = WideCharToMultiByte(CP_ACP, 0, bufW, -1, NULL, 0, NULL, NULL );
2436         if( len <= nBufferMax )
2437             WideCharToMultiByte( CP_ACP, 0, bufW, -1,
2438                                  lpBuffer, nBufferMax, NULL, NULL );
2439         else
2440             r = 0;
2441     }
2442     msi_free(bufW);
2443     return r;
2444 }
2445
2446 INSTALLSTATE WINAPI MsiLocateComponentA(LPCSTR szComponent, LPSTR lpPathBuf,
2447                 LPDWORD pcchBuf)
2448 {
2449     char szProduct[GUID_SIZE];
2450
2451     TRACE("%s %p %p\n", debugstr_a(szComponent), lpPathBuf, pcchBuf);
2452
2453     if (!szComponent || !pcchBuf)
2454         return INSTALLSTATE_INVALIDARG;
2455
2456     if (MsiGetProductCodeA( szComponent, szProduct ) != ERROR_SUCCESS)
2457         return INSTALLSTATE_UNKNOWN;
2458
2459     return MsiGetComponentPathA( szProduct, szComponent, lpPathBuf, pcchBuf );
2460 }
2461
2462 INSTALLSTATE WINAPI MsiLocateComponentW(LPCWSTR szComponent, LPWSTR lpPathBuf,
2463                 LPDWORD pcchBuf)
2464 {
2465     WCHAR szProduct[GUID_SIZE];
2466
2467     TRACE("%s %p %p\n", debugstr_w(szComponent), lpPathBuf, pcchBuf);
2468
2469     if (!szComponent || !pcchBuf)
2470         return INSTALLSTATE_INVALIDARG;
2471
2472     if (MsiGetProductCodeW( szComponent, szProduct ) != ERROR_SUCCESS)
2473         return INSTALLSTATE_UNKNOWN;
2474
2475     return MsiGetComponentPathW( szProduct, szComponent, lpPathBuf, pcchBuf );
2476 }
2477
2478 UINT WINAPI MsiMessageBoxA(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType,
2479                 WORD wLanguageId, DWORD f)
2480 {
2481     FIXME("%p %s %s %u %08x %08x\n", hWnd, debugstr_a(lpText), debugstr_a(lpCaption),
2482           uType, wLanguageId, f);
2483     return MessageBoxExA(hWnd,lpText,lpCaption,uType,wLanguageId); 
2484 }
2485
2486 UINT WINAPI MsiMessageBoxW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType,
2487                 WORD wLanguageId, DWORD f)
2488 {
2489     FIXME("%p %s %s %u %08x %08x\n", hWnd, debugstr_w(lpText), debugstr_w(lpCaption),
2490           uType, wLanguageId, f);
2491     return MessageBoxExW(hWnd,lpText,lpCaption,uType,wLanguageId); 
2492 }
2493
2494 UINT WINAPI MsiMessageBoxExA(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType,
2495                 DWORD unknown, WORD wLanguageId, DWORD f)
2496 {
2497     FIXME("(%p, %s, %s, %u, 0x%08x, 0x%08x, 0x%08x): semi-stub\n", hWnd, debugstr_a(lpText),
2498             debugstr_a(lpCaption), uType, unknown, wLanguageId, f);
2499     return MessageBoxExA(hWnd, lpText, lpCaption, uType, wLanguageId);
2500 }
2501
2502 UINT WINAPI MsiMessageBoxExW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType,
2503                 DWORD unknown, WORD wLanguageId, DWORD f)
2504 {
2505     FIXME("(%p, %s, %s, %u, 0x%08x, 0x%08x, 0x%08x): semi-stub\n", hWnd, debugstr_w(lpText),
2506             debugstr_w(lpCaption), uType, unknown, wLanguageId, f);
2507     return MessageBoxExW(hWnd, lpText, lpCaption, uType, wLanguageId);
2508 }
2509
2510 UINT WINAPI MsiProvideAssemblyA( LPCSTR szAssemblyName, LPCSTR szAppContext,
2511                 DWORD dwInstallMode, DWORD dwAssemblyInfo, LPSTR lpPathBuf,
2512                 LPDWORD pcchPathBuf )
2513 {
2514     FIXME("%s %s %08x %08x %p %p\n", debugstr_a(szAssemblyName),
2515           debugstr_a(szAppContext), dwInstallMode, dwAssemblyInfo, lpPathBuf,
2516           pcchPathBuf);
2517     return ERROR_CALL_NOT_IMPLEMENTED;
2518 }
2519
2520 UINT WINAPI MsiProvideAssemblyW( LPCWSTR szAssemblyName, LPCWSTR szAppContext,
2521                 DWORD dwInstallMode, DWORD dwAssemblyInfo, LPWSTR lpPathBuf,
2522                 LPDWORD pcchPathBuf )
2523 {
2524     FIXME("%s %s %08x %08x %p %p\n", debugstr_w(szAssemblyName),
2525           debugstr_w(szAppContext), dwInstallMode, dwAssemblyInfo, lpPathBuf,
2526           pcchPathBuf);
2527     return ERROR_CALL_NOT_IMPLEMENTED;
2528 }
2529
2530 UINT WINAPI MsiProvideComponentFromDescriptorA( LPCSTR szDescriptor,
2531                 LPSTR szPath, LPDWORD pcchPath, LPDWORD pcchArgs )
2532 {
2533     FIXME("%s %p %p %p\n", debugstr_a(szDescriptor), szPath, pcchPath, pcchArgs );
2534     return ERROR_CALL_NOT_IMPLEMENTED;
2535 }
2536
2537 UINT WINAPI MsiProvideComponentFromDescriptorW( LPCWSTR szDescriptor,
2538                 LPWSTR szPath, LPDWORD pcchPath, LPDWORD pcchArgs )
2539 {
2540     FIXME("%s %p %p %p\n", debugstr_w(szDescriptor), szPath, pcchPath, pcchArgs );
2541     return ERROR_CALL_NOT_IMPLEMENTED;
2542 }
2543
2544 HRESULT WINAPI MsiGetFileSignatureInformationA( LPCSTR path, DWORD flags, PCCERT_CONTEXT *cert,
2545                                                 LPBYTE hash, LPDWORD hashlen )
2546 {
2547     UINT r;
2548     WCHAR *pathW = NULL;
2549
2550     TRACE("%s %08x %p %p %p\n", debugstr_a(path), flags, cert, hash, hashlen);
2551
2552     if (path && !(pathW = strdupAtoW( path ))) return ERROR_OUTOFMEMORY;
2553     r = MsiGetFileSignatureInformationW( pathW, flags, cert, hash, hashlen );
2554     msi_free( pathW );
2555     return r;
2556 }
2557
2558 HRESULT WINAPI MsiGetFileSignatureInformationW( LPCWSTR path, DWORD flags, PCCERT_CONTEXT *cert,
2559                                                 LPBYTE hash, LPDWORD hashlen )
2560 {
2561     static GUID generic_verify_v2 = WINTRUST_ACTION_GENERIC_VERIFY_V2;
2562     HRESULT hr;
2563     WINTRUST_DATA data;
2564     WINTRUST_FILE_INFO info;
2565     CRYPT_PROVIDER_SGNR *signer;
2566     CRYPT_PROVIDER_CERT *provider;
2567
2568     TRACE("%s %08x %p %p %p\n", debugstr_w(path), flags, cert, hash, hashlen);
2569
2570     if (!path || !cert) return E_INVALIDARG;
2571
2572     info.cbStruct       = sizeof(info);
2573     info.pcwszFilePath  = path;
2574     info.hFile          = NULL;
2575     info.pgKnownSubject = NULL;
2576
2577     data.cbStruct            = sizeof(data);
2578     data.pPolicyCallbackData = NULL;
2579     data.pSIPClientData      = NULL;
2580     data.dwUIChoice          = WTD_UI_NONE;
2581     data.fdwRevocationChecks = WTD_REVOKE_WHOLECHAIN;
2582     data.dwUnionChoice       = WTD_CHOICE_FILE;
2583     data.u.pFile             = &info;
2584     data.dwStateAction       = WTD_STATEACTION_VERIFY;
2585     data.hWVTStateData       = NULL;
2586     data.pwszURLReference    = NULL;
2587     data.dwProvFlags         = 0;
2588     data.dwUIContext         = WTD_UICONTEXT_INSTALL;
2589     hr = WinVerifyTrustEx( INVALID_HANDLE_VALUE, &generic_verify_v2, &data );
2590     *cert = NULL;
2591     if (FAILED(hr)) goto done;
2592
2593     if (!(signer = WTHelperGetProvSignerFromChain( data.hWVTStateData, 0, FALSE, 0 )))
2594     {
2595         hr = TRUST_E_NOSIGNATURE;
2596         goto done;
2597     }
2598     if (hash)
2599     {
2600         DWORD len = signer->psSigner->EncryptedHash.cbData;
2601         if (*hashlen < len)
2602         {
2603             *hashlen = len;
2604             hr = HRESULT_FROM_WIN32(ERROR_MORE_DATA);
2605             goto done;
2606         }
2607         memcpy( hash, signer->psSigner->EncryptedHash.pbData, len );
2608         *hashlen = len;
2609     }
2610     if (!(provider = WTHelperGetProvCertFromChain( signer, 0 )))
2611     {
2612         hr = TRUST_E_PROVIDER_UNKNOWN;
2613         goto done;
2614     }
2615     *cert = CertDuplicateCertificateContext( provider->pCert );
2616
2617 done:
2618     data.dwStateAction = WTD_STATEACTION_CLOSE;
2619     WinVerifyTrustEx( INVALID_HANDLE_VALUE, &generic_verify_v2, &data );
2620     return hr;
2621 }
2622
2623 /******************************************************************
2624  * MsiGetProductPropertyA      [MSI.@]
2625  */
2626 UINT WINAPI MsiGetProductPropertyA(MSIHANDLE hProduct, LPCSTR szProperty,
2627                                    LPSTR szValue, LPDWORD pccbValue)
2628 {
2629     LPWSTR prop = NULL, val = NULL;
2630     DWORD len;
2631     UINT r;
2632
2633     TRACE("(%d, %s, %p, %p)\n", hProduct, debugstr_a(szProperty),
2634           szValue, pccbValue);
2635
2636     if (szValue && !pccbValue)
2637         return ERROR_INVALID_PARAMETER;
2638
2639     if (szProperty) prop = strdupAtoW(szProperty);
2640
2641     len = 0;
2642     r = MsiGetProductPropertyW(hProduct, prop, NULL, &len);
2643     if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
2644         goto done;
2645
2646     if (r == ERROR_SUCCESS)
2647     {
2648         if (szValue) *szValue = '\0';
2649         if (pccbValue) *pccbValue = 0;
2650         goto done;
2651     }
2652
2653     val = msi_alloc(++len * sizeof(WCHAR));
2654     if (!val)
2655     {
2656         r = ERROR_OUTOFMEMORY;
2657         goto done;
2658     }
2659
2660     r = MsiGetProductPropertyW(hProduct, prop, val, &len);
2661     if (r != ERROR_SUCCESS)
2662         goto done;
2663
2664     len = WideCharToMultiByte(CP_ACP, 0, val, -1, NULL, 0, NULL, NULL);
2665
2666     if (szValue)
2667         WideCharToMultiByte(CP_ACP, 0, val, -1, szValue,
2668                             *pccbValue, NULL, NULL);
2669
2670     if (pccbValue)
2671     {
2672         if (len > *pccbValue)
2673             r = ERROR_MORE_DATA;
2674
2675         *pccbValue = len - 1;
2676     }
2677
2678 done:
2679     msi_free(prop);
2680     msi_free(val);
2681
2682     return r;
2683 }
2684
2685 /******************************************************************
2686  * MsiGetProductPropertyW      [MSI.@]
2687  */
2688 UINT WINAPI MsiGetProductPropertyW(MSIHANDLE hProduct, LPCWSTR szProperty,
2689                                    LPWSTR szValue, LPDWORD pccbValue)
2690 {
2691     MSIPACKAGE *package;
2692     MSIQUERY *view = NULL;
2693     MSIRECORD *rec = NULL;
2694     LPCWSTR val;
2695     UINT r;
2696
2697     static const WCHAR query[] = {
2698        'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2699        '`','P','r','o','p','e','r','t','y','`',' ','W','H','E','R','E',' ',
2700        '`','P','r','o','p','e','r','t','y','`','=','\'','%','s','\'',0};
2701
2702     TRACE("(%d, %s, %p, %p)\n", hProduct, debugstr_w(szProperty),
2703           szValue, pccbValue);
2704
2705     if (!szProperty)
2706         return ERROR_INVALID_PARAMETER;
2707
2708     if (szValue && !pccbValue)
2709         return ERROR_INVALID_PARAMETER;
2710
2711     package = msihandle2msiinfo(hProduct, MSIHANDLETYPE_PACKAGE);
2712     if (!package)
2713         return ERROR_INVALID_HANDLE;
2714
2715     r = MSI_OpenQuery(package->db, &view, query, szProperty);
2716     if (r != ERROR_SUCCESS)
2717         goto done;
2718
2719     r = MSI_ViewExecute(view, 0);
2720     if (r != ERROR_SUCCESS)
2721         goto done;
2722
2723     r = MSI_ViewFetch(view, &rec);
2724     if (r != ERROR_SUCCESS)
2725         goto done;
2726
2727     val = MSI_RecordGetString(rec, 2);
2728     if (!val)
2729         goto done;
2730
2731     if (lstrlenW(val) >= *pccbValue)
2732     {
2733         lstrcpynW(szValue, val, *pccbValue);
2734         *pccbValue = lstrlenW(val);
2735         r = ERROR_MORE_DATA;
2736     }
2737     else
2738     {
2739         lstrcpyW(szValue, val);
2740         *pccbValue = lstrlenW(val);
2741         r = ERROR_SUCCESS;
2742     }
2743
2744 done:
2745     if (view)
2746     {
2747         MSI_ViewClose(view);
2748         msiobj_release(&view->hdr);
2749         if (rec) msiobj_release(&rec->hdr);
2750     }
2751
2752     if (!rec)
2753     {
2754         if (szValue) *szValue = '\0';
2755         if (pccbValue) *pccbValue = 0;
2756         r = ERROR_SUCCESS;
2757     }
2758
2759     msiobj_release(&package->hdr);
2760     return r;
2761 }
2762
2763 UINT WINAPI MsiVerifyPackageA( LPCSTR szPackage )
2764 {
2765     UINT r;
2766     LPWSTR szPack = NULL;
2767
2768     TRACE("%s\n", debugstr_a(szPackage) );
2769
2770     if( szPackage )
2771     {
2772         szPack = strdupAtoW( szPackage );
2773         if( !szPack )
2774             return ERROR_OUTOFMEMORY;
2775     }
2776
2777     r = MsiVerifyPackageW( szPack );
2778
2779     msi_free( szPack );
2780
2781     return r;
2782 }
2783
2784 UINT WINAPI MsiVerifyPackageW( LPCWSTR szPackage )
2785 {
2786     MSIHANDLE handle;
2787     UINT r;
2788
2789     TRACE("%s\n", debugstr_w(szPackage) );
2790
2791     r = MsiOpenDatabaseW( szPackage, MSIDBOPEN_READONLY, &handle );
2792     MsiCloseHandle( handle );
2793
2794     return r;
2795 }
2796
2797 static INSTALLSTATE MSI_GetComponentPath(LPCWSTR szProduct, LPCWSTR szComponent,
2798                                          awstring* lpPathBuf, LPDWORD pcchBuf)
2799 {
2800     static const WCHAR wininstaller[] =
2801         {'W','i','n','d','o','w','s','I','n','s','t','a','l','l','e','r',0};
2802     WCHAR squished_pc[GUID_SIZE];
2803     WCHAR squished_comp[GUID_SIZE];
2804     HKEY hkey;
2805     LPWSTR path = NULL;
2806     INSTALLSTATE state;
2807     DWORD version;
2808
2809     if (!szProduct || !szComponent)
2810         return INSTALLSTATE_INVALIDARG;
2811
2812     if (lpPathBuf->str.w && !pcchBuf)
2813         return INSTALLSTATE_INVALIDARG;
2814
2815     if (!squash_guid(szProduct, squished_pc) ||
2816         !squash_guid(szComponent, squished_comp))
2817         return INSTALLSTATE_INVALIDARG;
2818
2819     state = INSTALLSTATE_UNKNOWN;
2820
2821     if (MSIREG_OpenUserDataComponentKey(szComponent, szLocalSid, &hkey, FALSE) == ERROR_SUCCESS ||
2822         MSIREG_OpenUserDataComponentKey(szComponent, NULL, &hkey, FALSE) == ERROR_SUCCESS)
2823     {
2824         path = msi_reg_get_val_str(hkey, squished_pc);
2825         RegCloseKey(hkey);
2826
2827         state = INSTALLSTATE_ABSENT;
2828
2829         if ((MSIREG_OpenInstallProps(szProduct, MSIINSTALLCONTEXT_MACHINE, NULL,
2830                                      &hkey, FALSE) == ERROR_SUCCESS ||
2831             MSIREG_OpenUserDataProductKey(szProduct, MSIINSTALLCONTEXT_USERUNMANAGED,
2832                                           NULL, &hkey, FALSE) == ERROR_SUCCESS) &&
2833             msi_reg_get_val_dword(hkey, wininstaller, &version) &&
2834             GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES)
2835         {
2836             RegCloseKey(hkey);
2837             state = INSTALLSTATE_LOCAL;
2838         }
2839     }
2840
2841     if (state != INSTALLSTATE_LOCAL &&
2842         (MSIREG_OpenProductKey(szProduct, NULL,
2843                                MSIINSTALLCONTEXT_USERUNMANAGED,
2844                                &hkey, FALSE) == ERROR_SUCCESS ||
2845          MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_MACHINE,
2846                                &hkey, FALSE) == ERROR_SUCCESS))
2847     {
2848         RegCloseKey(hkey);
2849
2850         if (MSIREG_OpenUserDataComponentKey(szComponent, szLocalSid, &hkey, FALSE) == ERROR_SUCCESS ||
2851             MSIREG_OpenUserDataComponentKey(szComponent, NULL, &hkey, FALSE) == ERROR_SUCCESS)
2852         {
2853             msi_free(path);
2854             path = msi_reg_get_val_str(hkey, squished_pc);
2855             RegCloseKey(hkey);
2856
2857             state = INSTALLSTATE_ABSENT;
2858
2859             if (GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES)
2860                 state = INSTALLSTATE_LOCAL;
2861         }
2862     }
2863
2864     if (!path)
2865         return INSTALLSTATE_UNKNOWN;
2866
2867     if (state == INSTALLSTATE_LOCAL && !*path)
2868         state = INSTALLSTATE_NOTUSED;
2869
2870     msi_strcpy_to_awstring(path, lpPathBuf, pcchBuf);
2871     msi_free(path);
2872     return state;
2873 }
2874
2875 /******************************************************************
2876  * MsiGetComponentPathW      [MSI.@]
2877  */
2878 INSTALLSTATE WINAPI MsiGetComponentPathW(LPCWSTR szProduct, LPCWSTR szComponent,
2879                                          LPWSTR lpPathBuf, LPDWORD pcchBuf)
2880 {
2881     awstring path;
2882
2883     TRACE("%s %s %p %p\n", debugstr_w(szProduct), debugstr_w(szComponent), lpPathBuf, pcchBuf);
2884
2885     path.unicode = TRUE;
2886     path.str.w = lpPathBuf;
2887
2888     return MSI_GetComponentPath( szProduct, szComponent, &path, pcchBuf );
2889 }
2890
2891 /******************************************************************
2892  * MsiGetComponentPathA      [MSI.@]
2893  */
2894 INSTALLSTATE WINAPI MsiGetComponentPathA(LPCSTR szProduct, LPCSTR szComponent,
2895                                          LPSTR lpPathBuf, LPDWORD pcchBuf)
2896 {
2897     LPWSTR szwProduct, szwComponent = NULL;
2898     INSTALLSTATE r = INSTALLSTATE_UNKNOWN;
2899     awstring path;
2900
2901     TRACE("%s %s %p %p\n", debugstr_a(szProduct), debugstr_a(szComponent), lpPathBuf, pcchBuf);
2902
2903     szwProduct = strdupAtoW( szProduct );
2904     if( szProduct && !szwProduct)
2905         goto end;
2906
2907     szwComponent = strdupAtoW( szComponent );
2908     if( szComponent && !szwComponent )
2909         goto end;
2910
2911     path.unicode = FALSE;
2912     path.str.a = lpPathBuf;
2913
2914     r = MSI_GetComponentPath( szwProduct, szwComponent, &path, pcchBuf );
2915
2916 end:
2917     msi_free( szwProduct );
2918     msi_free( szwComponent );
2919
2920     return r;
2921 }
2922
2923 /******************************************************************
2924  * MsiQueryFeatureStateA      [MSI.@]
2925  */
2926 INSTALLSTATE WINAPI MsiQueryFeatureStateA(LPCSTR szProduct, LPCSTR szFeature)
2927 {
2928     LPWSTR szwProduct = NULL, szwFeature= NULL;
2929     INSTALLSTATE rc = INSTALLSTATE_UNKNOWN;
2930
2931     szwProduct = strdupAtoW( szProduct );
2932     if ( szProduct && !szwProduct )
2933         goto end;
2934
2935     szwFeature = strdupAtoW( szFeature );
2936     if ( szFeature && !szwFeature )
2937         goto end;
2938
2939     rc = MsiQueryFeatureStateW(szwProduct, szwFeature);
2940
2941 end:
2942     msi_free( szwProduct);
2943     msi_free( szwFeature);
2944
2945     return rc;
2946 }
2947
2948 /******************************************************************
2949  * MsiQueryFeatureStateW      [MSI.@]
2950  *
2951  * Checks the state of a feature
2952  *
2953  * PARAMS
2954  *   szProduct     [I]  Product's GUID string
2955  *   szFeature     [I]  Feature's GUID string
2956  *
2957  * RETURNS
2958  *   INSTALLSTATE_LOCAL        Feature is installed and usable
2959  *   INSTALLSTATE_ABSENT       Feature is absent
2960  *   INSTALLSTATE_ADVERTISED   Feature should be installed on demand
2961  *   INSTALLSTATE_UNKNOWN      An error occurred
2962  *   INSTALLSTATE_INVALIDARG   One of the GUIDs was invalid
2963  *
2964  */
2965 INSTALLSTATE WINAPI MsiQueryFeatureStateW(LPCWSTR szProduct, LPCWSTR szFeature)
2966 {
2967     WCHAR squishProduct[33], comp[GUID_SIZE];
2968     GUID guid;
2969     LPWSTR components, p, parent_feature, path;
2970     UINT rc;
2971     HKEY hkey;
2972     INSTALLSTATE r;
2973     BOOL missing = FALSE;
2974     BOOL machine = FALSE;
2975     BOOL source = FALSE;
2976
2977     TRACE("%s %s\n", debugstr_w(szProduct), debugstr_w(szFeature));
2978
2979     if (!szProduct || !szFeature)
2980         return INSTALLSTATE_INVALIDARG;
2981
2982     if (!squash_guid( szProduct, squishProduct ))
2983         return INSTALLSTATE_INVALIDARG;
2984
2985     SetLastError( ERROR_SUCCESS );
2986
2987     if (MSIREG_OpenFeaturesKey(szProduct, MSIINSTALLCONTEXT_USERMANAGED,
2988                                &hkey, FALSE) != ERROR_SUCCESS &&
2989         MSIREG_OpenFeaturesKey(szProduct, MSIINSTALLCONTEXT_USERUNMANAGED,
2990                                &hkey, FALSE) != ERROR_SUCCESS)
2991     {
2992         rc = MSIREG_OpenFeaturesKey(szProduct, MSIINSTALLCONTEXT_MACHINE,
2993                                     &hkey, FALSE);
2994         if (rc != ERROR_SUCCESS)
2995             return INSTALLSTATE_UNKNOWN;
2996
2997         machine = TRUE;
2998     }
2999
3000     parent_feature = msi_reg_get_val_str( hkey, szFeature );
3001     RegCloseKey(hkey);
3002
3003     if (!parent_feature)
3004         return INSTALLSTATE_UNKNOWN;
3005
3006     r = (parent_feature[0] == 6) ? INSTALLSTATE_ABSENT : INSTALLSTATE_LOCAL;
3007     msi_free(parent_feature);
3008     if (r == INSTALLSTATE_ABSENT)
3009         return r;
3010
3011     if (machine)
3012         rc = MSIREG_OpenUserDataFeaturesKey(szProduct,
3013                                             MSIINSTALLCONTEXT_MACHINE,
3014                                             &hkey, FALSE);
3015     else
3016         rc = MSIREG_OpenUserDataFeaturesKey(szProduct,
3017                                             MSIINSTALLCONTEXT_USERUNMANAGED,
3018                                             &hkey, FALSE);
3019
3020     if (rc != ERROR_SUCCESS)
3021         return INSTALLSTATE_ADVERTISED;
3022
3023     components = msi_reg_get_val_str( hkey, szFeature );
3024     RegCloseKey(hkey);
3025
3026     TRACE("rc = %d buffer = %s\n", rc, debugstr_w(components));
3027
3028     if (!components)
3029         return INSTALLSTATE_ADVERTISED;
3030
3031     for( p = components; *p && *p != 2 ; p += 20)
3032     {
3033         if (!decode_base85_guid( p, &guid ))
3034         {
3035             if (p != components)
3036                 break;
3037
3038             msi_free(components);
3039             return INSTALLSTATE_BADCONFIG;
3040         }
3041
3042         StringFromGUID2(&guid, comp, GUID_SIZE);
3043
3044         if (machine)
3045             rc = MSIREG_OpenUserDataComponentKey(comp, szLocalSid, &hkey, FALSE);
3046         else
3047             rc = MSIREG_OpenUserDataComponentKey(comp, NULL, &hkey, FALSE);
3048
3049         if (rc != ERROR_SUCCESS)
3050         {
3051             msi_free(components);
3052             return INSTALLSTATE_ADVERTISED;
3053         }
3054
3055         path = msi_reg_get_val_str(hkey, squishProduct);
3056         if (!path)
3057             missing = TRUE;
3058         else if (lstrlenW(path) > 2 &&
3059                  path[0] >= '0' && path[0] <= '9' &&
3060                  path[1] >= '0' && path[1] <= '9')
3061         {
3062             source = TRUE;
3063         }
3064
3065         msi_free(path);
3066     }
3067     msi_free(components);
3068
3069     if (missing)
3070         r = INSTALLSTATE_ADVERTISED;
3071     else if (source)
3072         r = INSTALLSTATE_SOURCE;
3073     else
3074         r = INSTALLSTATE_LOCAL;
3075
3076     TRACE("-> %d\n", r);
3077     return r;
3078 }
3079
3080 /******************************************************************
3081  * MsiGetFileVersionA         [MSI.@]
3082  */
3083 UINT WINAPI MsiGetFileVersionA(LPCSTR szFilePath, LPSTR lpVersionBuf,
3084                 LPDWORD pcchVersionBuf, LPSTR lpLangBuf, LPDWORD pcchLangBuf)
3085 {
3086     LPWSTR szwFilePath = NULL, lpwVersionBuff = NULL, lpwLangBuff = NULL;
3087     UINT ret = ERROR_OUTOFMEMORY;
3088
3089     if ((lpVersionBuf && !pcchVersionBuf) ||
3090         (lpLangBuf && !pcchLangBuf))
3091         return ERROR_INVALID_PARAMETER;
3092
3093     if( szFilePath )
3094     {
3095         szwFilePath = strdupAtoW( szFilePath );
3096         if( !szwFilePath )
3097             goto end;
3098     }
3099
3100     if( lpVersionBuf && pcchVersionBuf && *pcchVersionBuf )
3101     {
3102         lpwVersionBuff = msi_alloc(*pcchVersionBuf*sizeof(WCHAR));
3103         if( !lpwVersionBuff )
3104             goto end;
3105     }
3106
3107     if( lpLangBuf && pcchLangBuf && *pcchLangBuf )
3108     {
3109         lpwLangBuff = msi_alloc(*pcchLangBuf*sizeof(WCHAR));
3110         if( !lpwLangBuff )
3111             goto end;
3112     }
3113
3114     ret = MsiGetFileVersionW(szwFilePath, lpwVersionBuff, pcchVersionBuf,
3115                              lpwLangBuff, pcchLangBuf);
3116
3117     if( (ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA) && lpwVersionBuff )
3118         WideCharToMultiByte(CP_ACP, 0, lpwVersionBuff, -1,
3119                             lpVersionBuf, *pcchVersionBuf + 1, NULL, NULL);
3120     if( (ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA) && lpwLangBuff )
3121         WideCharToMultiByte(CP_ACP, 0, lpwLangBuff, -1,
3122                             lpLangBuf, *pcchLangBuf + 1, NULL, NULL);
3123
3124 end:
3125     msi_free(szwFilePath);
3126     msi_free(lpwVersionBuff);
3127     msi_free(lpwLangBuff);
3128
3129     return ret;
3130 }
3131
3132 static UINT get_file_version( const WCHAR *path, WCHAR *verbuf, DWORD *verlen,
3133                               WCHAR *langbuf, DWORD *langlen )
3134 {
3135     static const WCHAR szVersionResource[] = {'\\',0};
3136     static const WCHAR szVersionFormat[] = {'%','d','.','%','d','.','%','d','.','%','d',0};
3137     static const WCHAR szLangFormat[] = {'%','d',0};
3138     UINT ret = ERROR_SUCCESS;
3139     DWORD len, error;
3140     LPVOID version;
3141     VS_FIXEDFILEINFO *ffi;
3142     USHORT *lang;
3143     WCHAR tmp[32];
3144
3145     if (!(len = GetFileVersionInfoSizeW( path, NULL )))
3146     {
3147         error = GetLastError();
3148         if (error == ERROR_BAD_PATHNAME) return ERROR_FILE_NOT_FOUND;
3149         return error;
3150     }
3151     if (!(version = msi_alloc( len ))) return ERROR_OUTOFMEMORY;
3152     if (!GetFileVersionInfoW( path, 0, len, version ))
3153     {
3154         msi_free( version );
3155         return GetLastError();
3156     }
3157     if (verlen)
3158     {
3159         if (VerQueryValueW( version, szVersionResource, (LPVOID *)&ffi, &len ) && len > 0)
3160         {
3161             sprintfW( tmp, szVersionFormat,
3162                       HIWORD(ffi->dwFileVersionMS), LOWORD(ffi->dwFileVersionMS),
3163                       HIWORD(ffi->dwFileVersionLS), LOWORD(ffi->dwFileVersionLS) );
3164             if (verbuf) lstrcpynW( verbuf, tmp, *verlen );
3165             len = strlenW( tmp );
3166             if (len >= *verlen) ret = ERROR_MORE_DATA;
3167             *verlen = len;
3168         }
3169         else
3170         {
3171             if (verbuf) *verbuf = 0;
3172             *verlen = 0;
3173         }
3174     }
3175     if (langlen)
3176     {
3177         if (VerQueryValueW( version, szLangResource, (LPVOID *)&lang, &len ) && len > 0)
3178         {
3179             sprintfW( tmp, szLangFormat, *lang );
3180             if (langbuf) lstrcpynW( langbuf, tmp, *langlen );
3181             len = strlenW( tmp );
3182             if (len >= *langlen) ret = ERROR_MORE_DATA;
3183             *langlen = len;
3184         }
3185         else
3186         {
3187             if (langbuf) *langbuf = 0;
3188             *langlen = 0;
3189         }
3190     }
3191     msi_free( version );
3192     return ret;
3193 }
3194
3195
3196 /******************************************************************
3197  * MsiGetFileVersionW         [MSI.@]
3198  */
3199 UINT WINAPI MsiGetFileVersionW( LPCWSTR path, LPWSTR verbuf, LPDWORD verlen,
3200                                 LPWSTR langbuf, LPDWORD langlen )
3201 {
3202     UINT ret;
3203
3204     TRACE("%s %p %u %p %u\n", debugstr_w(path), verbuf, verlen ? *verlen : 0,
3205           langbuf, langlen ? *langlen : 0);
3206
3207     if ((verbuf && !verlen) || (langbuf && !langlen))
3208         return ERROR_INVALID_PARAMETER;
3209
3210     ret = get_file_version( path, verbuf, verlen, langbuf, langlen );
3211     if (ret == ERROR_RESOURCE_DATA_NOT_FOUND && verlen)
3212     {
3213         int len;
3214         WCHAR *version = msi_font_version_from_file( path );
3215         if (!version) return ERROR_FILE_INVALID;
3216         len = strlenW( version );
3217         if (len >= *verlen) ret = ERROR_MORE_DATA;
3218         else if (verbuf)
3219         {
3220             strcpyW( verbuf, version );
3221             ret = ERROR_SUCCESS;
3222         }
3223         *verlen = len;
3224         msi_free( version );
3225     }
3226     return ret;
3227 }
3228
3229 /***********************************************************************
3230  * MsiGetFeatureUsageW           [MSI.@]
3231  */
3232 UINT WINAPI MsiGetFeatureUsageW( LPCWSTR szProduct, LPCWSTR szFeature,
3233                                  LPDWORD pdwUseCount, LPWORD pwDateUsed )
3234 {
3235     FIXME("%s %s %p %p\n",debugstr_w(szProduct), debugstr_w(szFeature),
3236           pdwUseCount, pwDateUsed);
3237     return ERROR_CALL_NOT_IMPLEMENTED;
3238 }
3239
3240 /***********************************************************************
3241  * MsiGetFeatureUsageA           [MSI.@]
3242  */
3243 UINT WINAPI MsiGetFeatureUsageA( LPCSTR szProduct, LPCSTR szFeature,
3244                                  LPDWORD pdwUseCount, LPWORD pwDateUsed )
3245 {
3246     LPWSTR prod = NULL, feat = NULL;
3247     UINT ret = ERROR_OUTOFMEMORY;
3248
3249     TRACE("%s %s %p %p\n", debugstr_a(szProduct), debugstr_a(szFeature),
3250           pdwUseCount, pwDateUsed);
3251
3252     prod = strdupAtoW( szProduct );
3253     if (szProduct && !prod)
3254         goto end;
3255
3256     feat = strdupAtoW( szFeature );
3257     if (szFeature && !feat)
3258         goto end;
3259
3260     ret = MsiGetFeatureUsageW( prod, feat, pdwUseCount, pwDateUsed );
3261
3262 end:
3263     msi_free( prod );
3264     msi_free( feat );
3265
3266     return ret;
3267 }
3268
3269 /***********************************************************************
3270  * MsiUseFeatureExW           [MSI.@]
3271  */
3272 INSTALLSTATE WINAPI MsiUseFeatureExW( LPCWSTR szProduct, LPCWSTR szFeature,
3273                                       DWORD dwInstallMode, DWORD dwReserved )
3274 {
3275     INSTALLSTATE state;
3276
3277     TRACE("%s %s %i %i\n", debugstr_w(szProduct), debugstr_w(szFeature),
3278           dwInstallMode, dwReserved);
3279
3280     state = MsiQueryFeatureStateW( szProduct, szFeature );
3281
3282     if (dwReserved)
3283         return INSTALLSTATE_INVALIDARG;
3284
3285     if (state == INSTALLSTATE_LOCAL && dwInstallMode != INSTALLMODE_NODETECTION)
3286     {
3287         FIXME("mark product %s feature %s as used\n",
3288               debugstr_w(szProduct), debugstr_w(szFeature) );
3289     }
3290
3291     return state;
3292 }
3293
3294 /***********************************************************************
3295  * MsiUseFeatureExA           [MSI.@]
3296  */
3297 INSTALLSTATE WINAPI MsiUseFeatureExA( LPCSTR szProduct, LPCSTR szFeature,
3298                                       DWORD dwInstallMode, DWORD dwReserved )
3299 {
3300     INSTALLSTATE ret = INSTALLSTATE_UNKNOWN;
3301     LPWSTR prod = NULL, feat = NULL;
3302
3303     TRACE("%s %s %i %i\n", debugstr_a(szProduct), debugstr_a(szFeature),
3304           dwInstallMode, dwReserved);
3305
3306     prod = strdupAtoW( szProduct );
3307     if (szProduct && !prod)
3308         goto end;
3309
3310     feat = strdupAtoW( szFeature );
3311     if (szFeature && !feat)
3312         goto end;
3313
3314     ret = MsiUseFeatureExW( prod, feat, dwInstallMode, dwReserved );
3315
3316 end:
3317     msi_free( prod );
3318     msi_free( feat );
3319
3320     return ret;
3321 }
3322
3323 /***********************************************************************
3324  * MsiUseFeatureW             [MSI.@]
3325  */
3326 INSTALLSTATE WINAPI MsiUseFeatureW( LPCWSTR szProduct, LPCWSTR szFeature )
3327 {
3328     return MsiUseFeatureExW(szProduct, szFeature, 0, 0);
3329 }
3330
3331 /***********************************************************************
3332  * MsiUseFeatureA             [MSI.@]
3333  */
3334 INSTALLSTATE WINAPI MsiUseFeatureA( LPCSTR szProduct, LPCSTR szFeature )
3335 {
3336     return MsiUseFeatureExA(szProduct, szFeature, 0, 0);
3337 }
3338
3339 /***********************************************************************
3340  * MSI_ProvideQualifiedComponentEx [internal]
3341  */
3342 static UINT MSI_ProvideQualifiedComponentEx(LPCWSTR szComponent,
3343                 LPCWSTR szQualifier, DWORD dwInstallMode, LPCWSTR szProduct,
3344                 DWORD Unused1, DWORD Unused2, awstring *lpPathBuf,
3345                 LPDWORD pcchPathBuf)
3346 {
3347     WCHAR product[MAX_FEATURE_CHARS+1], component[MAX_FEATURE_CHARS+1],
3348           feature[MAX_FEATURE_CHARS+1];
3349     LPWSTR info;
3350     HKEY hkey;
3351     DWORD sz;
3352     UINT rc;
3353
3354     rc = MSIREG_OpenUserComponentsKey(szComponent, &hkey, FALSE);
3355     if (rc != ERROR_SUCCESS)
3356         return ERROR_INDEX_ABSENT;
3357
3358     info = msi_reg_get_val_str( hkey, szQualifier );
3359     RegCloseKey(hkey);
3360
3361     if (!info)
3362         return ERROR_INDEX_ABSENT;
3363
3364     MsiDecomposeDescriptorW(info, product, feature, component, &sz);
3365
3366     if (!szProduct)
3367         rc = MSI_GetComponentPath(product, component, lpPathBuf, pcchPathBuf);
3368     else
3369         rc = MSI_GetComponentPath(szProduct, component, lpPathBuf, pcchPathBuf);
3370
3371     msi_free( info );
3372
3373     if (rc != INSTALLSTATE_LOCAL)
3374         return ERROR_FILE_NOT_FOUND;
3375
3376     return ERROR_SUCCESS;
3377 }
3378
3379 /***********************************************************************
3380  * MsiProvideQualifiedComponentExW [MSI.@]
3381  */
3382 UINT WINAPI MsiProvideQualifiedComponentExW(LPCWSTR szComponent,
3383                 LPCWSTR szQualifier, DWORD dwInstallMode, LPCWSTR szProduct,
3384                 DWORD Unused1, DWORD Unused2, LPWSTR lpPathBuf,
3385                 LPDWORD pcchPathBuf)
3386 {
3387     awstring path;
3388
3389     TRACE("%s %s %u %s %u %u %p %p\n", debugstr_w(szComponent),
3390           debugstr_w(szQualifier), dwInstallMode, debugstr_w(szProduct),
3391           Unused1, Unused2, lpPathBuf, pcchPathBuf);
3392
3393     path.unicode = TRUE;
3394     path.str.w = lpPathBuf;
3395
3396     return MSI_ProvideQualifiedComponentEx(szComponent, szQualifier,
3397             dwInstallMode, szProduct, Unused1, Unused2, &path, pcchPathBuf);
3398 }
3399
3400 /***********************************************************************
3401  * MsiProvideQualifiedComponentExA [MSI.@]
3402  */
3403 UINT WINAPI MsiProvideQualifiedComponentExA(LPCSTR szComponent,
3404                 LPCSTR szQualifier, DWORD dwInstallMode, LPCSTR szProduct,
3405                 DWORD Unused1, DWORD Unused2, LPSTR lpPathBuf,
3406                 LPDWORD pcchPathBuf)
3407 {
3408     LPWSTR szwComponent, szwQualifier = NULL, szwProduct = NULL;
3409     UINT r = ERROR_OUTOFMEMORY;
3410     awstring path;
3411
3412     TRACE("%s %s %u %s %u %u %p %p\n", debugstr_a(szComponent),
3413           debugstr_a(szQualifier), dwInstallMode, debugstr_a(szProduct),
3414           Unused1, Unused2, lpPathBuf, pcchPathBuf);
3415
3416     szwComponent = strdupAtoW( szComponent );
3417     if (szComponent && !szwComponent)
3418         goto end;
3419
3420     szwQualifier = strdupAtoW( szQualifier );
3421     if (szQualifier && !szwQualifier)
3422         goto end;
3423
3424     szwProduct = strdupAtoW( szProduct );
3425     if (szProduct && !szwProduct)
3426         goto end;
3427
3428     path.unicode = FALSE;
3429     path.str.a = lpPathBuf;
3430
3431     r = MSI_ProvideQualifiedComponentEx(szwComponent, szwQualifier,
3432                               dwInstallMode, szwProduct, Unused1,
3433                               Unused2, &path, pcchPathBuf);
3434 end:
3435     msi_free(szwProduct);
3436     msi_free(szwComponent);
3437     msi_free(szwQualifier);
3438
3439     return r;
3440 }
3441
3442 /***********************************************************************
3443  * MsiProvideQualifiedComponentW [MSI.@]
3444  */
3445 UINT WINAPI MsiProvideQualifiedComponentW( LPCWSTR szComponent,
3446                 LPCWSTR szQualifier, DWORD dwInstallMode, LPWSTR lpPathBuf,
3447                 LPDWORD pcchPathBuf)
3448 {
3449     return MsiProvideQualifiedComponentExW(szComponent, szQualifier, 
3450                     dwInstallMode, NULL, 0, 0, lpPathBuf, pcchPathBuf);
3451 }
3452
3453 /***********************************************************************
3454  * MsiProvideQualifiedComponentA [MSI.@]
3455  */
3456 UINT WINAPI MsiProvideQualifiedComponentA( LPCSTR szComponent,
3457                 LPCSTR szQualifier, DWORD dwInstallMode, LPSTR lpPathBuf,
3458                 LPDWORD pcchPathBuf)
3459 {
3460     return MsiProvideQualifiedComponentExA(szComponent, szQualifier,
3461                               dwInstallMode, NULL, 0, 0, lpPathBuf, pcchPathBuf);
3462 }
3463
3464 /***********************************************************************
3465  * MSI_GetUserInfo [internal]
3466  */
3467 static USERINFOSTATE MSI_GetUserInfo(LPCWSTR szProduct,
3468                 awstring *lpUserNameBuf, LPDWORD pcchUserNameBuf,
3469                 awstring *lpOrgNameBuf, LPDWORD pcchOrgNameBuf,
3470                 awstring *lpSerialBuf, LPDWORD pcchSerialBuf)
3471 {
3472     WCHAR squished_pc[SQUISH_GUID_SIZE];
3473     LPWSTR user, org, serial;
3474     USERINFOSTATE state;
3475     HKEY hkey, props;
3476     LPCWSTR orgptr;
3477     UINT r;
3478
3479     TRACE("%s %p %p %p %p %p %p\n", debugstr_w(szProduct), lpUserNameBuf,
3480           pcchUserNameBuf, lpOrgNameBuf, pcchOrgNameBuf, lpSerialBuf,
3481           pcchSerialBuf);
3482
3483     if (!szProduct || !squash_guid(szProduct, squished_pc))
3484         return USERINFOSTATE_INVALIDARG;
3485
3486     if (MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERMANAGED,
3487                               &hkey, FALSE) != ERROR_SUCCESS &&
3488         MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_USERUNMANAGED,
3489                               &hkey, FALSE) != ERROR_SUCCESS &&
3490         MSIREG_OpenProductKey(szProduct, NULL, MSIINSTALLCONTEXT_MACHINE,
3491                               &hkey, FALSE) != ERROR_SUCCESS)
3492     {
3493         return USERINFOSTATE_UNKNOWN;
3494     }
3495
3496     if (MSIREG_OpenInstallProps(szProduct, MSIINSTALLCONTEXT_USERUNMANAGED,
3497                                 NULL, &props, FALSE) != ERROR_SUCCESS &&
3498         MSIREG_OpenInstallProps(szProduct, MSIINSTALLCONTEXT_MACHINE,
3499                                 NULL, &props, FALSE) != ERROR_SUCCESS)
3500     {
3501         RegCloseKey(hkey);
3502         return USERINFOSTATE_ABSENT;
3503     }
3504
3505     user = msi_reg_get_val_str(props, INSTALLPROPERTY_REGOWNERW);
3506     org = msi_reg_get_val_str(props, INSTALLPROPERTY_REGCOMPANYW);
3507     serial = msi_reg_get_val_str(props, INSTALLPROPERTY_PRODUCTIDW);
3508     state = USERINFOSTATE_ABSENT;
3509
3510     RegCloseKey(hkey);
3511     RegCloseKey(props);
3512
3513     if (user && serial)
3514         state = USERINFOSTATE_PRESENT;
3515
3516     if (pcchUserNameBuf)
3517     {
3518         if (lpUserNameBuf && !user)
3519         {
3520             (*pcchUserNameBuf)--;
3521             goto done;
3522         }
3523
3524         r = msi_strcpy_to_awstring(user, lpUserNameBuf, pcchUserNameBuf);
3525         if (r == ERROR_MORE_DATA)
3526         {
3527             state = USERINFOSTATE_MOREDATA;
3528             goto done;
3529         }
3530     }
3531
3532     if (pcchOrgNameBuf)
3533     {
3534         orgptr = org;
3535         if (!orgptr) orgptr = szEmpty;
3536
3537         r = msi_strcpy_to_awstring(orgptr, lpOrgNameBuf, pcchOrgNameBuf);
3538         if (r == ERROR_MORE_DATA)
3539         {
3540             state = USERINFOSTATE_MOREDATA;
3541             goto done;
3542         }
3543     }
3544
3545     if (pcchSerialBuf)
3546     {
3547         if (!serial)
3548         {
3549             (*pcchSerialBuf)--;
3550             goto done;
3551         }
3552
3553         r = msi_strcpy_to_awstring(serial, lpSerialBuf, pcchSerialBuf);
3554         if (r == ERROR_MORE_DATA)
3555             state = USERINFOSTATE_MOREDATA;
3556     }
3557
3558 done:
3559     msi_free(user);
3560     msi_free(org);
3561     msi_free(serial);
3562
3563     return state;
3564 }
3565
3566 /***********************************************************************
3567  * MsiGetUserInfoW [MSI.@]
3568  */
3569 USERINFOSTATE WINAPI MsiGetUserInfoW(LPCWSTR szProduct,
3570                 LPWSTR lpUserNameBuf, LPDWORD pcchUserNameBuf,
3571                 LPWSTR lpOrgNameBuf, LPDWORD pcchOrgNameBuf,
3572                 LPWSTR lpSerialBuf, LPDWORD pcchSerialBuf)
3573 {
3574     awstring user, org, serial;
3575
3576     if ((lpUserNameBuf && !pcchUserNameBuf) ||
3577         (lpOrgNameBuf && !pcchOrgNameBuf) ||
3578         (lpSerialBuf && !pcchSerialBuf))
3579         return USERINFOSTATE_INVALIDARG;
3580
3581     user.unicode = TRUE;
3582     user.str.w = lpUserNameBuf;
3583     org.unicode = TRUE;
3584     org.str.w = lpOrgNameBuf;
3585     serial.unicode = TRUE;
3586     serial.str.w = lpSerialBuf;
3587
3588     return MSI_GetUserInfo( szProduct, &user, pcchUserNameBuf,
3589                             &org, pcchOrgNameBuf,
3590                             &serial, pcchSerialBuf );
3591 }
3592
3593 USERINFOSTATE WINAPI MsiGetUserInfoA(LPCSTR szProduct,
3594                 LPSTR lpUserNameBuf, LPDWORD pcchUserNameBuf,
3595                 LPSTR lpOrgNameBuf, LPDWORD pcchOrgNameBuf,
3596                 LPSTR lpSerialBuf, LPDWORD pcchSerialBuf)
3597 {
3598     awstring user, org, serial;
3599     LPWSTR prod;
3600     UINT r;
3601
3602     if ((lpUserNameBuf && !pcchUserNameBuf) ||
3603         (lpOrgNameBuf && !pcchOrgNameBuf) ||
3604         (lpSerialBuf && !pcchSerialBuf))
3605         return USERINFOSTATE_INVALIDARG;
3606
3607     prod = strdupAtoW( szProduct );
3608     if (szProduct && !prod)
3609         return ERROR_OUTOFMEMORY;
3610
3611     user.unicode = FALSE;
3612     user.str.a = lpUserNameBuf;
3613     org.unicode = FALSE;
3614     org.str.a = lpOrgNameBuf;
3615     serial.unicode = FALSE;
3616     serial.str.a = lpSerialBuf;
3617
3618     r = MSI_GetUserInfo( prod, &user, pcchUserNameBuf,
3619                          &org, pcchOrgNameBuf,
3620                          &serial, pcchSerialBuf );
3621
3622     msi_free( prod );
3623
3624     return r;
3625 }
3626
3627 UINT WINAPI MsiCollectUserInfoW(LPCWSTR szProduct)
3628 {
3629     MSIHANDLE handle;
3630     UINT rc;
3631     MSIPACKAGE *package;
3632     static const WCHAR szFirstRun[] = {'F','i','r','s','t','R','u','n',0};
3633
3634     TRACE("(%s)\n",debugstr_w(szProduct));
3635
3636     rc = MsiOpenProductW(szProduct,&handle);
3637     if (rc != ERROR_SUCCESS)
3638         return ERROR_INVALID_PARAMETER;
3639
3640     /* MsiCollectUserInfo cannot be called from a custom action. */
3641     package = msihandle2msiinfo(handle, MSIHANDLETYPE_PACKAGE);
3642     if (!package)
3643         return ERROR_CALL_NOT_IMPLEMENTED;
3644
3645     rc = ACTION_PerformUIAction(package, szFirstRun, SCRIPT_NONE);
3646     msiobj_release( &package->hdr );
3647
3648     MsiCloseHandle(handle);
3649
3650     return rc;
3651 }
3652
3653 UINT WINAPI MsiCollectUserInfoA(LPCSTR szProduct)
3654 {
3655     MSIHANDLE handle;
3656     UINT rc;
3657     MSIPACKAGE *package;
3658     static const WCHAR szFirstRun[] = {'F','i','r','s','t','R','u','n',0};
3659
3660     TRACE("(%s)\n",debugstr_a(szProduct));
3661
3662     rc = MsiOpenProductA(szProduct,&handle);
3663     if (rc != ERROR_SUCCESS)
3664         return ERROR_INVALID_PARAMETER;
3665
3666     /* MsiCollectUserInfo cannot be called from a custom action. */
3667     package = msihandle2msiinfo(handle, MSIHANDLETYPE_PACKAGE);
3668     if (!package)
3669         return ERROR_CALL_NOT_IMPLEMENTED;
3670
3671     rc = ACTION_PerformUIAction(package, szFirstRun, SCRIPT_NONE);
3672     msiobj_release( &package->hdr );
3673
3674     MsiCloseHandle(handle);
3675
3676     return rc;
3677 }
3678
3679 /***********************************************************************
3680  * MsiConfigureFeatureA            [MSI.@]
3681  */
3682 UINT WINAPI MsiConfigureFeatureA(LPCSTR szProduct, LPCSTR szFeature, INSTALLSTATE eInstallState)
3683 {
3684     LPWSTR prod, feat = NULL;
3685     UINT r = ERROR_OUTOFMEMORY;
3686
3687     TRACE("%s %s %i\n", debugstr_a(szProduct), debugstr_a(szFeature), eInstallState);
3688
3689     prod = strdupAtoW( szProduct );
3690     if (szProduct && !prod)
3691         goto end;
3692
3693     feat = strdupAtoW( szFeature );
3694     if (szFeature && !feat)
3695         goto end;
3696
3697     r = MsiConfigureFeatureW(prod, feat, eInstallState);
3698
3699 end:
3700     msi_free(feat);
3701     msi_free(prod);
3702
3703     return r;
3704 }
3705
3706 /***********************************************************************
3707  * MsiConfigureFeatureW            [MSI.@]
3708  */
3709 UINT WINAPI MsiConfigureFeatureW(LPCWSTR szProduct, LPCWSTR szFeature, INSTALLSTATE eInstallState)
3710 {
3711     MSIPACKAGE *package = NULL;
3712     UINT r;
3713     WCHAR sourcepath[MAX_PATH], filename[MAX_PATH];
3714     DWORD sz;
3715
3716     TRACE("%s %s %i\n", debugstr_w(szProduct), debugstr_w(szFeature), eInstallState);
3717
3718     if (!szProduct || !szFeature)
3719         return ERROR_INVALID_PARAMETER;
3720
3721     switch (eInstallState)
3722     {
3723     case INSTALLSTATE_DEFAULT:
3724         /* FIXME: how do we figure out the default location? */
3725         eInstallState = INSTALLSTATE_LOCAL;
3726         break;
3727     case INSTALLSTATE_LOCAL:
3728     case INSTALLSTATE_SOURCE:
3729     case INSTALLSTATE_ABSENT:
3730     case INSTALLSTATE_ADVERTISED:
3731         break;
3732     default:
3733         return ERROR_INVALID_PARAMETER;
3734     }
3735
3736     r = MSI_OpenProductW( szProduct, &package );
3737     if (r != ERROR_SUCCESS)
3738         return r;
3739
3740     sz = sizeof(sourcepath);
3741     MsiSourceListGetInfoW(szProduct, NULL, MSIINSTALLCONTEXT_USERUNMANAGED,
3742                 MSICODE_PRODUCT, INSTALLPROPERTY_LASTUSEDSOURCEW, sourcepath, &sz);
3743
3744     sz = sizeof(filename);
3745     MsiSourceListGetInfoW(szProduct, NULL, MSIINSTALLCONTEXT_USERUNMANAGED,
3746                 MSICODE_PRODUCT, INSTALLPROPERTY_PACKAGENAMEW, filename, &sz);
3747
3748     lstrcatW( sourcepath, filename );
3749
3750     MsiSetInternalUI( INSTALLUILEVEL_BASIC, NULL );
3751
3752     r = ACTION_PerformUIAction( package, szCostInitialize, SCRIPT_NONE );
3753     if (r != ERROR_SUCCESS)
3754         goto end;
3755
3756     r = MSI_SetFeatureStateW( package, szFeature, eInstallState);
3757     if (r != ERROR_SUCCESS)
3758         goto end;
3759
3760     r = MSI_InstallPackage( package, sourcepath, NULL );
3761
3762 end:
3763     msiobj_release( &package->hdr );
3764
3765     return r;
3766 }
3767
3768 /***********************************************************************
3769  * MsiCreateAndVerifyInstallerDirectory [MSI.@]
3770  *
3771  * Notes: undocumented
3772  */
3773 UINT WINAPI MsiCreateAndVerifyInstallerDirectory(DWORD dwReserved)
3774 {
3775     WCHAR path[MAX_PATH];
3776
3777     TRACE("%d\n", dwReserved);
3778
3779     if (dwReserved)
3780     {
3781         FIXME("dwReserved=%d\n", dwReserved);
3782         return ERROR_INVALID_PARAMETER;
3783     }
3784
3785     if (!GetWindowsDirectoryW(path, MAX_PATH))
3786         return ERROR_FUNCTION_FAILED;
3787
3788     lstrcatW(path, installerW);
3789
3790     if (!CreateDirectoryW(path, NULL))
3791         return ERROR_FUNCTION_FAILED;
3792
3793     return ERROR_SUCCESS;
3794 }
3795
3796 /***********************************************************************
3797  * MsiGetShortcutTargetA           [MSI.@]
3798  */
3799 UINT WINAPI MsiGetShortcutTargetA( LPCSTR szShortcutTarget,
3800                                    LPSTR szProductCode, LPSTR szFeatureId,
3801                                    LPSTR szComponentCode )
3802 {
3803     LPWSTR target;
3804     const int len = MAX_FEATURE_CHARS+1;
3805     WCHAR product[MAX_FEATURE_CHARS+1], feature[MAX_FEATURE_CHARS+1], component[MAX_FEATURE_CHARS+1];
3806     UINT r;
3807
3808     target = strdupAtoW( szShortcutTarget );
3809     if (szShortcutTarget && !target )
3810         return ERROR_OUTOFMEMORY;
3811     product[0] = 0;
3812     feature[0] = 0;
3813     component[0] = 0;
3814     r = MsiGetShortcutTargetW( target, product, feature, component );
3815     msi_free( target );
3816     if (r == ERROR_SUCCESS)
3817     {
3818         WideCharToMultiByte( CP_ACP, 0, product, -1, szProductCode, len, NULL, NULL );
3819         WideCharToMultiByte( CP_ACP, 0, feature, -1, szFeatureId, len, NULL, NULL );
3820         WideCharToMultiByte( CP_ACP, 0, component, -1, szComponentCode, len, NULL, NULL );
3821     }
3822     return r;
3823 }
3824
3825 /***********************************************************************
3826  * MsiGetShortcutTargetW           [MSI.@]
3827  */
3828 UINT WINAPI MsiGetShortcutTargetW( LPCWSTR szShortcutTarget,
3829                                    LPWSTR szProductCode, LPWSTR szFeatureId,
3830                                    LPWSTR szComponentCode )
3831 {
3832     IShellLinkDataList *dl = NULL;
3833     IPersistFile *pf = NULL;
3834     LPEXP_DARWIN_LINK darwin = NULL;
3835     HRESULT r, init;
3836
3837     TRACE("%s %p %p %p\n", debugstr_w(szShortcutTarget),
3838           szProductCode, szFeatureId, szComponentCode );
3839
3840     init = CoInitialize(NULL);
3841
3842     r = CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
3843                           &IID_IPersistFile, (LPVOID*) &pf );
3844     if( SUCCEEDED( r ) )
3845     {
3846         r = IPersistFile_Load( pf, szShortcutTarget,
3847                                STGM_READ | STGM_SHARE_DENY_WRITE );
3848         if( SUCCEEDED( r ) )
3849         {
3850             r = IPersistFile_QueryInterface( pf, &IID_IShellLinkDataList,
3851                                              (LPVOID*) &dl );
3852             if( SUCCEEDED( r ) )
3853             {
3854                 IShellLinkDataList_CopyDataBlock( dl, EXP_DARWIN_ID_SIG,
3855                                                   (LPVOID) &darwin );
3856                 IShellLinkDataList_Release( dl );
3857             }
3858         }
3859         IPersistFile_Release( pf );
3860     }
3861
3862     if (SUCCEEDED(init))
3863         CoUninitialize();
3864
3865     TRACE("darwin = %p\n", darwin);
3866
3867     if (darwin)
3868     {
3869         DWORD sz;
3870         UINT ret;
3871
3872         ret = MsiDecomposeDescriptorW( darwin->szwDarwinID,
3873                   szProductCode, szFeatureId, szComponentCode, &sz );
3874         LocalFree( darwin );
3875         return ret;
3876     }
3877
3878     return ERROR_FUNCTION_FAILED;
3879 }
3880
3881 UINT WINAPI MsiReinstallFeatureW( LPCWSTR szProduct, LPCWSTR szFeature, DWORD dwReinstallMode )
3882 {
3883     static const WCHAR fmtW[] = {'%','s','=','%','s',' ','%','s','=','%','s',0};
3884     MSIPACKAGE *package;
3885     MSIINSTALLCONTEXT context;
3886     UINT r;
3887     WCHAR sourcepath[MAX_PATH], filename[MAX_PATH], reinstallmode[11];
3888     WCHAR *ptr, *cmdline;
3889     DWORD sz;
3890
3891     TRACE("%s, %s, 0x%08x\n", debugstr_w(szProduct), debugstr_w(szFeature), dwReinstallMode);
3892
3893     r = msi_locate_product( szProduct, &context );
3894     if (r != ERROR_SUCCESS)
3895         return r;
3896
3897     ptr = reinstallmode;
3898
3899     if (dwReinstallMode & REINSTALLMODE_FILEMISSING)
3900         *ptr++ = 'p';
3901     if (dwReinstallMode & REINSTALLMODE_FILEOLDERVERSION)
3902         *ptr++ = 'o';
3903     if (dwReinstallMode & REINSTALLMODE_FILEEQUALVERSION)
3904         *ptr++ = 'w';
3905     if (dwReinstallMode & REINSTALLMODE_FILEEXACT)
3906         *ptr++ = 'd';
3907     if (dwReinstallMode & REINSTALLMODE_FILEVERIFY)
3908         *ptr++ = 'c';
3909     if (dwReinstallMode & REINSTALLMODE_FILEREPLACE)
3910         *ptr++ = 'a';
3911     if (dwReinstallMode & REINSTALLMODE_USERDATA)
3912         *ptr++ = 'u';
3913     if (dwReinstallMode & REINSTALLMODE_MACHINEDATA)
3914         *ptr++ = 'm';
3915     if (dwReinstallMode & REINSTALLMODE_SHORTCUT)
3916         *ptr++ = 's';
3917     if (dwReinstallMode & REINSTALLMODE_PACKAGE)
3918         *ptr++ = 'v';
3919     *ptr = 0;
3920     
3921     sz = sizeof(sourcepath);
3922     MsiSourceListGetInfoW( szProduct, NULL, context, MSICODE_PRODUCT,
3923                            INSTALLPROPERTY_LASTUSEDSOURCEW, sourcepath, &sz );
3924     sz = sizeof(filename);
3925     MsiSourceListGetInfoW( szProduct, NULL, context, MSICODE_PRODUCT,
3926                            INSTALLPROPERTY_PACKAGENAMEW, filename, &sz );
3927     strcatW( sourcepath, filename );
3928
3929     if (dwReinstallMode & REINSTALLMODE_PACKAGE)
3930         r = MSI_OpenPackageW( sourcepath, &package );
3931     else
3932         r = MSI_OpenProductW( szProduct, &package );
3933
3934     if (r != ERROR_SUCCESS)
3935         return r;
3936
3937     sz = (strlenW( fmtW ) + strlenW( szReinstallMode ) + strlenW( reinstallmode )) * sizeof(WCHAR);
3938     sz += (strlenW( szReinstall ) + strlenW( szFeature )) * sizeof(WCHAR);
3939     if (!(cmdline = msi_alloc( sz )))
3940     {
3941         msiobj_release( &package->hdr );
3942         return ERROR_OUTOFMEMORY;
3943     }
3944     sprintfW( cmdline, fmtW, szReinstallMode, reinstallmode, szReinstall, szFeature );
3945
3946     r = MSI_InstallPackage( package, sourcepath, cmdline );
3947     msiobj_release( &package->hdr );
3948     msi_free( cmdline );
3949
3950     return r;
3951 }
3952
3953 UINT WINAPI MsiReinstallFeatureA( LPCSTR szProduct, LPCSTR szFeature,
3954                                   DWORD dwReinstallMode )
3955 {
3956     LPWSTR wszProduct;
3957     LPWSTR wszFeature;
3958     UINT rc;
3959
3960     TRACE("%s %s %i\n", debugstr_a(szProduct), debugstr_a(szFeature),
3961                            dwReinstallMode);
3962
3963     wszProduct = strdupAtoW(szProduct);
3964     wszFeature = strdupAtoW(szFeature);
3965
3966     rc = MsiReinstallFeatureW(wszProduct, wszFeature, dwReinstallMode);
3967
3968     msi_free(wszProduct);
3969     msi_free(wszFeature);
3970     return rc;
3971 }
3972
3973 typedef struct
3974 {
3975     unsigned int i[2];
3976     unsigned int buf[4];
3977     unsigned char in[64];
3978     unsigned char digest[16];
3979 } MD5_CTX;
3980
3981 extern VOID WINAPI MD5Init( MD5_CTX *);
3982 extern VOID WINAPI MD5Update( MD5_CTX *, const unsigned char *, unsigned int );
3983 extern VOID WINAPI MD5Final( MD5_CTX *);
3984
3985 /***********************************************************************
3986  * MsiGetFileHashW            [MSI.@]
3987  */
3988 UINT WINAPI MsiGetFileHashW( LPCWSTR szFilePath, DWORD dwOptions,
3989                              PMSIFILEHASHINFO pHash )
3990 {
3991     HANDLE handle, mapping;
3992     void *p;
3993     DWORD length;
3994     UINT r = ERROR_FUNCTION_FAILED;
3995
3996     TRACE("%s %08x %p\n", debugstr_w(szFilePath), dwOptions, pHash );
3997
3998     if (!szFilePath)
3999         return ERROR_INVALID_PARAMETER;
4000
4001     if (!*szFilePath)
4002         return ERROR_PATH_NOT_FOUND;
4003
4004     if (dwOptions)
4005         return ERROR_INVALID_PARAMETER;
4006     if (!pHash)
4007         return ERROR_INVALID_PARAMETER;
4008     if (pHash->dwFileHashInfoSize < sizeof *pHash)
4009         return ERROR_INVALID_PARAMETER;
4010
4011     handle = CreateFileW( szFilePath, GENERIC_READ,
4012                           FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, NULL );
4013     if (handle == INVALID_HANDLE_VALUE)
4014     {
4015         WARN("can't open file %u\n", GetLastError());
4016         return ERROR_FILE_NOT_FOUND;
4017     }
4018     length = GetFileSize( handle, NULL );
4019
4020     if (length)
4021     {
4022         mapping = CreateFileMappingW( handle, NULL, PAGE_READONLY, 0, 0, NULL );
4023         if (mapping)
4024         {
4025             p = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, length );
4026             if (p)
4027             {
4028                 MD5_CTX ctx;
4029
4030                 MD5Init( &ctx );
4031                 MD5Update( &ctx, p, length );
4032                 MD5Final( &ctx );
4033                 UnmapViewOfFile( p );
4034
4035                 memcpy( pHash->dwData, ctx.digest, sizeof pHash->dwData );
4036                 r = ERROR_SUCCESS;
4037             }
4038             CloseHandle( mapping );
4039         }
4040     }
4041     else
4042     {
4043         /* Empty file -> set hash to 0 */
4044         memset( pHash->dwData, 0, sizeof pHash->dwData );
4045         r = ERROR_SUCCESS;
4046     }
4047
4048     CloseHandle( handle );
4049
4050     return r;
4051 }
4052
4053 /***********************************************************************
4054  * MsiGetFileHashA            [MSI.@]
4055  */
4056 UINT WINAPI MsiGetFileHashA( LPCSTR szFilePath, DWORD dwOptions,
4057                              PMSIFILEHASHINFO pHash )
4058 {
4059     LPWSTR file;
4060     UINT r;
4061
4062     TRACE("%s %08x %p\n", debugstr_a(szFilePath), dwOptions, pHash );
4063
4064     file = strdupAtoW( szFilePath );
4065     if (szFilePath && !file)
4066         return ERROR_OUTOFMEMORY;
4067
4068     r = MsiGetFileHashW( file, dwOptions, pHash );
4069     msi_free( file );
4070     return r;
4071 }
4072
4073 /***********************************************************************
4074  * MsiAdvertiseScriptW        [MSI.@]
4075  */
4076 UINT WINAPI MsiAdvertiseScriptW( LPCWSTR szScriptFile, DWORD dwFlags,
4077                                  PHKEY phRegData, BOOL fRemoveItems )
4078 {
4079     FIXME("%s %08x %p %d\n",
4080           debugstr_w( szScriptFile ), dwFlags, phRegData, fRemoveItems );
4081     return ERROR_CALL_NOT_IMPLEMENTED;
4082 }
4083
4084 /***********************************************************************
4085  * MsiAdvertiseScriptA        [MSI.@]
4086  */
4087 UINT WINAPI MsiAdvertiseScriptA( LPCSTR szScriptFile, DWORD dwFlags,
4088                                  PHKEY phRegData, BOOL fRemoveItems )
4089 {
4090     FIXME("%s %08x %p %d\n",
4091           debugstr_a( szScriptFile ), dwFlags, phRegData, fRemoveItems );
4092     return ERROR_CALL_NOT_IMPLEMENTED;
4093 }
4094
4095 /***********************************************************************
4096  * MsiIsProductElevatedW        [MSI.@]
4097  */
4098 UINT WINAPI MsiIsProductElevatedW( LPCWSTR szProduct, BOOL *pfElevated )
4099 {
4100     FIXME("%s %p - stub\n",
4101           debugstr_w( szProduct ), pfElevated );
4102     *pfElevated = TRUE;
4103     return ERROR_SUCCESS;
4104 }
4105
4106 /***********************************************************************
4107  * MsiIsProductElevatedA        [MSI.@]
4108  */
4109 UINT WINAPI MsiIsProductElevatedA( LPCSTR szProduct, BOOL *pfElevated )
4110 {
4111     FIXME("%s %p - stub\n",
4112           debugstr_a( szProduct ), pfElevated );
4113     *pfElevated = TRUE;
4114     return ERROR_SUCCESS;
4115 }
4116
4117 /***********************************************************************
4118  * MsiSetExternalUIRecord     [MSI.@]
4119  */
4120 UINT WINAPI MsiSetExternalUIRecord( INSTALLUI_HANDLER_RECORD handler,
4121                                     DWORD filter, LPVOID context,
4122                                     PINSTALLUI_HANDLER_RECORD prev )
4123 {
4124     TRACE("%p %08x %p %p\n", handler, filter, context, prev);
4125
4126     if (prev)
4127         *prev = gUIHandlerRecord;
4128
4129     gUIHandlerRecord = handler;
4130     gUIFilter        = filter;
4131     gUIContext       = context;
4132
4133     return ERROR_SUCCESS;
4134 }
4135
4136 /***********************************************************************
4137  * MsiInstallMissingComponentA     [MSI.@]
4138  */
4139 UINT WINAPI MsiInstallMissingComponentA( LPCSTR product, LPCSTR component, INSTALLSTATE state )
4140 {
4141     UINT r;
4142     WCHAR *productW = NULL, *componentW = NULL;
4143
4144     TRACE("%s, %s, %d\n", debugstr_a(product), debugstr_a(component), state);
4145
4146     if (product && !(productW = strdupAtoW( product )))
4147         return ERROR_OUTOFMEMORY;
4148
4149     if (component && !(componentW = strdupAtoW( component )))
4150     {
4151         msi_free( productW );
4152         return ERROR_OUTOFMEMORY;
4153     }
4154
4155     r = MsiInstallMissingComponentW( productW, componentW, state );
4156     msi_free( productW );
4157     msi_free( componentW );
4158     return r;
4159 }
4160
4161 /***********************************************************************
4162  * MsiInstallMissingComponentW     [MSI.@]
4163  */
4164 UINT WINAPI MsiInstallMissingComponentW(LPCWSTR szProduct, LPCWSTR szComponent, INSTALLSTATE eInstallState)
4165 {
4166     FIXME("(%s %s %d\n", debugstr_w(szProduct), debugstr_w(szComponent), eInstallState);
4167     return ERROR_SUCCESS;
4168 }
4169
4170 /***********************************************************************
4171  * MsiBeginTransactionA     [MSI.@]
4172  */
4173 UINT WINAPI MsiBeginTransactionA( LPCSTR name, DWORD attrs, MSIHANDLE *id, HANDLE *event )
4174 {
4175     WCHAR *nameW;
4176     UINT r;
4177
4178     FIXME("%s %u %p %p\n", debugstr_a(name), attrs, id, event);
4179
4180     nameW = strdupAtoW( name );
4181     if (name && !nameW)
4182         return ERROR_OUTOFMEMORY;
4183
4184     r = MsiBeginTransactionW( nameW, attrs, id, event );
4185     msi_free( nameW );
4186     return r;
4187 }
4188
4189 /***********************************************************************
4190  * MsiBeginTransactionW     [MSI.@]
4191  */
4192 UINT WINAPI MsiBeginTransactionW( LPCWSTR name, DWORD attrs, MSIHANDLE *id, HANDLE *event )
4193 {
4194     FIXME("%s %u %p %p\n", debugstr_w(name), attrs, id, event);
4195
4196     *id = (MSIHANDLE)0xdeadbeef;
4197     *event = (HANDLE)0xdeadbeef;
4198
4199     return ERROR_SUCCESS;
4200 }
4201
4202 /***********************************************************************
4203  * MsiEndTransaction     [MSI.@]
4204  */
4205 UINT WINAPI MsiEndTransaction( DWORD state )
4206 {
4207     FIXME("%u\n", state);
4208     return ERROR_SUCCESS;
4209 }
4210
4211 UINT WINAPI Migrate10CachedPackagesW(void* a, void* b, void* c, DWORD d)
4212 {
4213     FIXME("%p,%p,%p,%08x\n", a, b, c, d);
4214     return ERROR_SUCCESS;
4215 }