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