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