msi: Passing NULL for szBuffer and NULL for pcchValueBuf into MsiGetProductInfo{A...
[wine] / dlls / msi / msi.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2002,2003,2004,2005 Mike McCormack for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24 #define NONAMELESSUNION
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winnls.h"
30 #include "shlwapi.h"
31 #include "wine/debug.h"
32 #include "msi.h"
33 #include "msidefs.h"
34 #include "msiquery.h"
35 #include "msipriv.h"
36 #include "wincrypt.h"
37 #include "winver.h"
38 #include "winuser.h"
39 #include "shlobj.h"
40 #include "shobjidl.h"
41 #include "objidl.h"
42 #include "wine/unicode.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(msi);
45
46 static const WCHAR installerW[] = {'\\','I','n','s','t','a','l','l','e','r',0};
47
48 UINT WINAPI MsiOpenProductA(LPCSTR szProduct, MSIHANDLE *phProduct)
49 {
50     UINT r;
51     LPWSTR szwProd = NULL;
52
53     TRACE("%s %p\n",debugstr_a(szProduct), phProduct);
54
55     if( szProduct )
56     {
57         szwProd = strdupAtoW( szProduct );
58         if( !szwProd )
59             return ERROR_OUTOFMEMORY;
60     }
61
62     r = MsiOpenProductW( szwProd, phProduct );
63
64     msi_free( szwProd );
65
66     return r;
67 }
68
69 static UINT MSI_OpenProductW( LPCWSTR szProduct, MSIPACKAGE **ppackage )
70 {
71     LPWSTR path = NULL;
72     UINT r;
73     HKEY hKeyProduct = NULL;
74     DWORD count, type;
75
76     TRACE("%s %p\n", debugstr_w(szProduct), ppackage );
77
78     r = MSIREG_OpenUninstallKey(szProduct,&hKeyProduct,FALSE);
79     if( r != ERROR_SUCCESS )
80     {
81         r = ERROR_UNKNOWN_PRODUCT;
82         goto end;
83     }
84
85     /* find the size of the path */
86     type = count = 0;
87     r = RegQueryValueExW( hKeyProduct, INSTALLPROPERTY_LOCALPACKAGEW,
88                           NULL, &type, NULL, &count );
89     if( r != ERROR_SUCCESS )
90     {
91         r = ERROR_UNKNOWN_PRODUCT;
92         goto end;
93     }
94
95     /* now alloc and fetch the path of the database to open */
96     path = msi_alloc( count );
97     if( !path )
98         goto end;
99
100     r = RegQueryValueExW( hKeyProduct, INSTALLPROPERTY_LOCALPACKAGEW,
101                           NULL, &type, (LPBYTE) path, &count );
102     if( r != ERROR_SUCCESS )
103     {
104         r = ERROR_UNKNOWN_PRODUCT;
105         goto end;
106     }
107
108     r = MSI_OpenPackageW( path, ppackage );
109
110 end:
111     msi_free( path );
112     if( hKeyProduct )
113         RegCloseKey( hKeyProduct );
114
115     return r;
116 }
117
118 UINT WINAPI MsiOpenProductW( LPCWSTR szProduct, MSIHANDLE *phProduct )
119 {
120    MSIPACKAGE *package = NULL;
121    UINT r;
122
123    r = MSI_OpenProductW( szProduct, &package );
124    if( r == ERROR_SUCCESS )
125    {
126        *phProduct = alloc_msihandle( &package->hdr );
127        if (! *phProduct)
128            r = ERROR_NOT_ENOUGH_MEMORY;
129        msiobj_release( &package->hdr );
130    }
131    return r;
132 }
133
134 UINT WINAPI MsiAdvertiseProductA(LPCSTR szPackagePath, LPCSTR szScriptfilePath,
135                 LPCSTR szTransforms, LANGID lgidLanguage)
136 {
137     FIXME("%s %s %s %08x\n",debugstr_a(szPackagePath),
138           debugstr_a(szScriptfilePath), debugstr_a(szTransforms), lgidLanguage);
139     return ERROR_CALL_NOT_IMPLEMENTED;
140 }
141
142 UINT WINAPI MsiAdvertiseProductW(LPCWSTR szPackagePath, LPCWSTR szScriptfilePath,
143                 LPCWSTR szTransforms, LANGID lgidLanguage)
144 {
145     FIXME("%s %s %s %08x\n",debugstr_w(szPackagePath),
146           debugstr_w(szScriptfilePath), debugstr_w(szTransforms), lgidLanguage);
147     return ERROR_CALL_NOT_IMPLEMENTED;
148 }
149
150 UINT WINAPI MsiAdvertiseProductExA(LPCSTR szPackagePath, LPCSTR szScriptfilePath,
151       LPCSTR szTransforms, LANGID lgidLanguage, DWORD dwPlatform, DWORD dwOptions)
152 {
153     FIXME("%s %s %s %08x %08x %08x\n", debugstr_a(szPackagePath),
154           debugstr_a(szScriptfilePath), debugstr_a(szTransforms),
155           lgidLanguage, dwPlatform, dwOptions);
156     return ERROR_CALL_NOT_IMPLEMENTED;
157 }
158
159 UINT WINAPI MsiAdvertiseProductExW( LPCWSTR szPackagePath, LPCWSTR szScriptfilePath,
160       LPCWSTR szTransforms, LANGID lgidLanguage, DWORD dwPlatform, DWORD dwOptions)
161 {
162     FIXME("%s %s %s %08x %08x %08x\n", debugstr_w(szPackagePath),
163           debugstr_w(szScriptfilePath), debugstr_w(szTransforms),
164           lgidLanguage, dwPlatform, dwOptions);
165     return ERROR_CALL_NOT_IMPLEMENTED;
166 }
167
168 UINT WINAPI MsiInstallProductA(LPCSTR szPackagePath, LPCSTR szCommandLine)
169 {
170     LPWSTR szwPath = NULL, szwCommand = NULL;
171     UINT r = ERROR_OUTOFMEMORY;
172
173     TRACE("%s %s\n",debugstr_a(szPackagePath), debugstr_a(szCommandLine));
174
175     if( szPackagePath )
176     {
177         szwPath = strdupAtoW( szPackagePath );
178         if( !szwPath )
179             goto end;
180     }
181
182     if( szCommandLine )
183     {
184         szwCommand = strdupAtoW( szCommandLine );
185         if( !szwCommand )
186             goto end;
187     }
188
189     r = MsiInstallProductW( szwPath, szwCommand );
190
191 end:
192     msi_free( szwPath );
193     msi_free( szwCommand );
194
195     return r;
196 }
197
198 UINT WINAPI MsiInstallProductW(LPCWSTR szPackagePath, LPCWSTR szCommandLine)
199 {
200     MSIPACKAGE *package = NULL;
201     UINT r;
202
203     TRACE("%s %s\n",debugstr_w(szPackagePath), debugstr_w(szCommandLine));
204
205     r = MSI_OpenPackageW( szPackagePath, &package );
206     if (r == ERROR_SUCCESS)
207     {
208         r = MSI_InstallPackage( package, szPackagePath, szCommandLine );
209         msiobj_release( &package->hdr );
210     }
211
212     return r;
213 }
214
215 UINT WINAPI MsiReinstallProductA(LPCSTR szProduct, DWORD dwReinstallMode)
216 {
217     FIXME("%s %08x\n", debugstr_a(szProduct), dwReinstallMode);
218     return ERROR_CALL_NOT_IMPLEMENTED;
219 }
220
221 UINT WINAPI MsiReinstallProductW(LPCWSTR szProduct, DWORD dwReinstallMode)
222 {
223     FIXME("%s %08x\n", debugstr_w(szProduct), dwReinstallMode);
224     return ERROR_CALL_NOT_IMPLEMENTED;
225 }
226
227 UINT WINAPI MsiApplyPatchA(LPCSTR szPatchPackage, LPCSTR szInstallPackage,
228         INSTALLTYPE eInstallType, LPCSTR szCommandLine)
229 {
230     LPWSTR patch_package = NULL;
231     LPWSTR install_package = NULL;
232     LPWSTR command_line = NULL;
233     UINT r = ERROR_OUTOFMEMORY;
234
235     TRACE("%s %s %d %s\n", debugstr_a(szPatchPackage), debugstr_a(szInstallPackage),
236           eInstallType, debugstr_a(szCommandLine));
237
238     if (szPatchPackage && !(patch_package = strdupAtoW(szPatchPackage)))
239         goto done;
240
241     if (szInstallPackage && !(install_package = strdupAtoW(szInstallPackage)))
242         goto done;
243
244     if (szCommandLine && !(command_line = strdupAtoW(szCommandLine)))
245         goto done;
246
247     r = MsiApplyPatchW(patch_package, install_package, eInstallType, command_line);
248
249 done:
250     msi_free(patch_package);
251     msi_free(install_package);
252     msi_free(command_line);
253
254     return r;
255 }
256
257 UINT WINAPI MsiApplyPatchW(LPCWSTR szPatchPackage, LPCWSTR szInstallPackage,
258          INSTALLTYPE eInstallType, LPCWSTR szCommandLine)
259 {
260     MSIHANDLE patch, info;
261     UINT r, type;
262     DWORD size = 0;
263     LPCWSTR cmd_ptr = szCommandLine;
264     LPWSTR beg, end;
265     LPWSTR cmd = NULL, codes = NULL;
266
267     static const WCHAR space[] = {' ',0};
268     static const WCHAR patcheq[] = {'P','A','T','C','H','=',0};
269     static WCHAR empty[] = {0};
270
271     TRACE("%s %s %d %s\n", debugstr_w(szPatchPackage), debugstr_w(szInstallPackage),
272           eInstallType, debugstr_w(szCommandLine));
273
274     if (szInstallPackage || eInstallType == INSTALLTYPE_NETWORK_IMAGE ||
275         eInstallType == INSTALLTYPE_SINGLE_INSTANCE)
276     {
277         FIXME("Only reading target products from patch\n");
278         return ERROR_CALL_NOT_IMPLEMENTED;
279     }
280
281     r = MsiOpenDatabaseW(szPatchPackage, MSIDBOPEN_READONLY, &patch);
282     if (r != ERROR_SUCCESS)
283         return r;
284
285     r = MsiGetSummaryInformationW(patch, NULL, 0, &info);
286     if (r != ERROR_SUCCESS)
287         goto done;
288
289     r = MsiSummaryInfoGetPropertyW(info, PID_TEMPLATE, &type, NULL, NULL, empty, &size);
290     if (r != ERROR_MORE_DATA || !size || type != VT_LPSTR)
291     {
292         ERR("Failed to read product codes from patch\n");
293         goto done;
294     }
295
296     codes = msi_alloc(++size * sizeof(WCHAR));
297     if (!codes)
298     {
299         r = ERROR_OUTOFMEMORY;
300         goto done;
301     }
302
303     r = MsiSummaryInfoGetPropertyW(info, PID_TEMPLATE, &type, NULL, NULL, codes, &size);
304     if (r != ERROR_SUCCESS)
305         goto done;
306
307     if (!szCommandLine)
308         cmd_ptr = empty;
309
310     size = lstrlenW(cmd_ptr) + lstrlenW(patcheq) + lstrlenW(szPatchPackage) + 1;
311     cmd = msi_alloc(size * sizeof(WCHAR));
312     if (!cmd)
313     {
314         r = ERROR_OUTOFMEMORY;
315         goto done;
316     }
317
318     lstrcpyW(cmd, cmd_ptr);
319     if (szCommandLine) lstrcatW(cmd, space);
320     lstrcatW(cmd, patcheq);
321     lstrcatW(cmd, szPatchPackage);
322
323     beg = codes;
324     while ((end = strchrW(beg, '}')))
325     {
326         *(end + 1) = '\0';
327
328         r = MsiConfigureProductExW(beg, INSTALLLEVEL_DEFAULT, INSTALLSTATE_DEFAULT, cmd);
329         if (r != ERROR_SUCCESS)
330             goto done;
331
332         beg = end + 2;
333     }
334
335 done:
336     msi_free(cmd);
337     msi_free(codes);
338
339     MsiCloseHandle(info);
340     MsiCloseHandle(patch);
341
342     return r;
343 }
344
345 UINT WINAPI MsiConfigureProductExW(LPCWSTR szProduct, int iInstallLevel,
346                         INSTALLSTATE eInstallState, LPCWSTR szCommandLine)
347 {
348     MSIPACKAGE* package = NULL;
349     UINT r;
350     DWORD sz;
351     WCHAR sourcepath[MAX_PATH];
352     WCHAR filename[MAX_PATH];
353     static const WCHAR szInstalled[] = {
354         ' ','I','n','s','t','a','l','l','e','d','=','1',0};
355     LPWSTR commandline;
356
357     TRACE("%s %d %d %s\n",debugstr_w(szProduct), iInstallLevel, eInstallState,
358           debugstr_w(szCommandLine));
359
360     if (eInstallState != INSTALLSTATE_LOCAL &&
361         eInstallState != INSTALLSTATE_DEFAULT)
362     {
363         FIXME("Not implemented for anything other than local installs\n");
364         return ERROR_CALL_NOT_IMPLEMENTED;
365     }
366
367     sz = sizeof(sourcepath);
368     MsiSourceListGetInfoW(szProduct, NULL, MSIINSTALLCONTEXT_USERUNMANAGED,
369             MSICODE_PRODUCT, INSTALLPROPERTY_LASTUSEDSOURCEW, sourcepath,
370             &sz);
371
372     sz = sizeof(filename);
373     MsiSourceListGetInfoW(szProduct, NULL, MSIINSTALLCONTEXT_USERUNMANAGED,
374             MSICODE_PRODUCT, INSTALLPROPERTY_PACKAGENAMEW, filename, &sz);
375
376     lstrcatW(sourcepath,filename);
377
378     /*
379      * ok 1, we need to find the msi file for this product.
380      *    2, find the source dir for the files
381      *    3, do the configure/install.
382      *    4, cleanupany runonce entry.
383      */
384
385     r = MSI_OpenProductW( szProduct, &package );
386     if (r != ERROR_SUCCESS)
387         return r;
388
389     sz = lstrlenW(szInstalled) + 1;
390
391     if (szCommandLine)
392         sz += lstrlenW(szCommandLine);
393
394     commandline = msi_alloc(sz * sizeof(WCHAR));
395     if (!commandline )
396     {
397         r = ERROR_OUTOFMEMORY;
398         goto end;
399     }
400
401     commandline[0] = 0;
402     if (szCommandLine)
403         lstrcpyW(commandline,szCommandLine);
404
405     if (MsiQueryProductStateW(szProduct) != INSTALLSTATE_UNKNOWN)
406         lstrcatW(commandline,szInstalled);
407
408     r = MSI_InstallPackage( package, sourcepath, commandline );
409
410     msi_free(commandline);
411
412 end:
413     msiobj_release( &package->hdr );
414
415     return r;
416 }
417
418 UINT WINAPI MsiConfigureProductExA(LPCSTR szProduct, int iInstallLevel,
419                         INSTALLSTATE eInstallState, LPCSTR szCommandLine)
420 {
421     LPWSTR szwProduct = NULL;
422     LPWSTR szwCommandLine = NULL;
423     UINT r = ERROR_OUTOFMEMORY;
424
425     if( szProduct )
426     {
427         szwProduct = strdupAtoW( szProduct );
428         if( !szwProduct )
429             goto end;
430     }
431
432     if( szCommandLine)
433     {
434         szwCommandLine = strdupAtoW( szCommandLine );
435         if( !szwCommandLine)
436             goto end;
437     }
438
439     r = MsiConfigureProductExW( szwProduct, iInstallLevel, eInstallState,
440                                 szwCommandLine );
441 end:
442     msi_free( szwProduct );
443     msi_free( szwCommandLine);
444
445     return r;
446 }
447
448 UINT WINAPI MsiConfigureProductA(LPCSTR szProduct, int iInstallLevel,
449                                  INSTALLSTATE eInstallState)
450 {
451     LPWSTR szwProduct = NULL;
452     UINT r;
453
454     TRACE("%s %d %d\n",debugstr_a(szProduct), iInstallLevel, eInstallState);
455
456     if( szProduct )
457     {
458         szwProduct = strdupAtoW( szProduct );
459         if( !szwProduct )
460             return ERROR_OUTOFMEMORY;
461     }
462
463     r = MsiConfigureProductW( szwProduct, iInstallLevel, eInstallState );
464     msi_free( szwProduct );
465
466     return r;
467 }
468
469 UINT WINAPI MsiConfigureProductW(LPCWSTR szProduct, int iInstallLevel,
470                                  INSTALLSTATE eInstallState)
471 {
472     return MsiConfigureProductExW(szProduct, iInstallLevel, eInstallState, NULL);
473 }
474
475 UINT WINAPI MsiGetProductCodeA(LPCSTR szComponent, LPSTR szBuffer)
476 {
477     LPWSTR szwComponent = NULL;
478     UINT r;
479     WCHAR szwBuffer[GUID_SIZE];
480
481     TRACE("%s %s\n",debugstr_a(szComponent), debugstr_a(szBuffer));
482
483     if( szComponent )
484     {
485         szwComponent = strdupAtoW( szComponent );
486         if( !szwComponent )
487             return ERROR_OUTOFMEMORY;
488     }
489
490     *szwBuffer = '\0';
491     r = MsiGetProductCodeW( szwComponent, szwBuffer );
492
493     if(*szwBuffer)
494         WideCharToMultiByte(CP_ACP, 0, szwBuffer, -1, szBuffer, GUID_SIZE, NULL, NULL);
495
496     msi_free( szwComponent );
497
498     return r;
499 }
500
501 UINT WINAPI MsiGetProductCodeW(LPCWSTR szComponent, LPWSTR szBuffer)
502 {
503     UINT rc, index;
504     HKEY compkey, prodkey;
505     WCHAR squished_comp[GUID_SIZE];
506     WCHAR squished_prod[GUID_SIZE];
507     DWORD sz = GUID_SIZE;
508
509     TRACE("%s %p\n", debugstr_w(szComponent), szBuffer);
510
511     if (!szComponent || !*szComponent)
512         return ERROR_INVALID_PARAMETER;
513
514     if (!squash_guid(szComponent, squished_comp))
515         return ERROR_INVALID_PARAMETER;
516
517     if (MSIREG_OpenUserDataComponentKey(szComponent, &compkey, FALSE) != ERROR_SUCCESS &&
518         MSIREG_OpenLocalSystemComponentKey(szComponent, &compkey, FALSE) != ERROR_SUCCESS)
519     {
520         return ERROR_UNKNOWN_COMPONENT;
521     }
522
523     rc = RegEnumValueW(compkey, 0, squished_prod, &sz, NULL, NULL, NULL, NULL);
524     if (rc != ERROR_SUCCESS)
525     {
526         RegCloseKey(compkey);
527         return ERROR_UNKNOWN_COMPONENT;
528     }
529
530     /* check simple case, only one product */
531     rc = RegEnumValueW(compkey, 1, squished_prod, &sz, NULL, NULL, NULL, NULL);
532     if (rc == ERROR_NO_MORE_ITEMS)
533     {
534         rc = ERROR_SUCCESS;
535         goto done;
536     }
537
538     index = 0;
539     while ((rc = RegEnumValueW(compkey, index, squished_prod, &sz,
540            NULL, NULL, NULL, NULL)) != ERROR_NO_MORE_ITEMS)
541     {
542         index++;
543         sz = GUID_SIZE;
544         unsquash_guid(squished_prod, szBuffer);
545
546         if (MSIREG_OpenLocalManagedProductKey(szBuffer, &prodkey, FALSE) == ERROR_SUCCESS ||
547             MSIREG_OpenUserProductsKey(szBuffer, &prodkey, FALSE) == ERROR_SUCCESS ||
548             MSIREG_OpenLocalClassesProductKey(szBuffer, &prodkey, FALSE) == ERROR_SUCCESS)
549         {
550             RegCloseKey(prodkey);
551             rc = ERROR_SUCCESS;
552             goto done;
553         }
554     }
555
556     rc = ERROR_INSTALL_FAILURE;
557
558 done:
559     RegCloseKey(compkey);
560     unsquash_guid(squished_prod, szBuffer);
561     return rc;
562 }
563
564 static LPWSTR msi_reg_get_value(HKEY hkey, LPCWSTR name, DWORD *type)
565 {
566     DWORD dval;
567     LONG res;
568     WCHAR temp[20];
569
570     static const WCHAR format[] = {'%','d',0};
571
572     res = RegQueryValueExW(hkey, name, NULL, type, NULL, NULL);
573     if (res != ERROR_SUCCESS)
574         return NULL;
575
576     if (*type == REG_SZ)
577         return msi_reg_get_val_str(hkey, name);
578
579     if (!msi_reg_get_val_dword(hkey, name, &dval))
580         return NULL;
581
582     sprintfW(temp, format, dval);
583     return strdupW(temp);
584 }
585
586 static UINT WINAPI MSI_GetProductInfo(LPCWSTR szProduct, LPCWSTR szAttribute,
587                                       awstring *szValue, LPDWORD pcchValueBuf)
588 {
589     UINT r = ERROR_UNKNOWN_PROPERTY;
590     HKEY prodkey, userdata, source;
591     LPWSTR val = NULL;
592     WCHAR squished_pc[GUID_SIZE];
593     WCHAR packagecode[GUID_SIZE];
594     BOOL classes = FALSE;
595     BOOL badconfig = FALSE;
596     LONG res;
597     DWORD save, type = REG_NONE;
598
599     static WCHAR empty[] = {0};
600     static const WCHAR sourcelist[] = {
601         'S','o','u','r','c','e','L','i','s','t',0};
602     static const WCHAR display_name[] = {
603         'D','i','s','p','l','a','y','N','a','m','e',0};
604     static const WCHAR display_version[] = {
605         'D','i','s','p','l','a','y','V','e','r','s','i','o','n',0};
606     static const WCHAR assignment[] = {
607         'A','s','s','i','g','n','m','e','n','t',0};
608
609     TRACE("%s %s %p %p\n", debugstr_w(szProduct),
610           debugstr_w(szAttribute), szValue, pcchValueBuf);
611
612     if ((szValue->str.w && !pcchValueBuf) || !szProduct || !szAttribute)
613         return ERROR_INVALID_PARAMETER;
614
615     if (!squash_guid(szProduct, squished_pc))
616         return ERROR_INVALID_PARAMETER;
617
618     r = MSIREG_OpenLocalManagedProductKey(szProduct, &prodkey, FALSE);
619     if (r != ERROR_SUCCESS)
620     {
621         r = MSIREG_OpenUserProductsKey(szProduct, &prodkey, FALSE);
622         if (r != ERROR_SUCCESS)
623         {
624             r = MSIREG_OpenLocalClassesProductKey(szProduct, &prodkey, FALSE);
625             if (r == ERROR_SUCCESS)
626                 classes = TRUE;
627         }
628     }
629
630     if (classes)
631         MSIREG_OpenLocalSystemProductKey(szProduct, &userdata, FALSE);
632     else
633         MSIREG_OpenInstallPropertiesKey(szProduct, &userdata, FALSE);
634
635     if (!lstrcmpW(szAttribute, INSTALLPROPERTY_HELPLINKW) ||
636         !lstrcmpW(szAttribute, INSTALLPROPERTY_HELPTELEPHONEW) ||
637         !lstrcmpW(szAttribute, INSTALLPROPERTY_INSTALLDATEW) ||
638         !lstrcmpW(szAttribute, INSTALLPROPERTY_INSTALLEDPRODUCTNAMEW) ||
639         !lstrcmpW(szAttribute, INSTALLPROPERTY_INSTALLLOCATIONW) ||
640         !lstrcmpW(szAttribute, INSTALLPROPERTY_INSTALLSOURCEW) ||
641         !lstrcmpW(szAttribute, INSTALLPROPERTY_LOCALPACKAGEW) ||
642         !lstrcmpW(szAttribute, INSTALLPROPERTY_PUBLISHERW) ||
643         !lstrcmpW(szAttribute, INSTALLPROPERTY_URLINFOABOUTW) ||
644         !lstrcmpW(szAttribute, INSTALLPROPERTY_URLUPDATEINFOW) ||
645         !lstrcmpW(szAttribute, INSTALLPROPERTY_VERSIONMINORW) ||
646         !lstrcmpW(szAttribute, INSTALLPROPERTY_VERSIONMAJORW) ||
647         !lstrcmpW(szAttribute, INSTALLPROPERTY_VERSIONSTRINGW) ||
648         !lstrcmpW(szAttribute, INSTALLPROPERTY_PRODUCTIDW) ||
649         !lstrcmpW(szAttribute, INSTALLPROPERTY_REGCOMPANYW) ||
650         !lstrcmpW(szAttribute, INSTALLPROPERTY_REGOWNERW))
651     {
652         if (!prodkey)
653         {
654             r = ERROR_UNKNOWN_PRODUCT;
655             goto done;
656         }
657
658         if (!userdata)
659             return ERROR_UNKNOWN_PROPERTY;
660
661         if (!lstrcmpW(szAttribute, INSTALLPROPERTY_INSTALLEDPRODUCTNAMEW))
662             szAttribute = display_name;
663         else if (!lstrcmpW(szAttribute, INSTALLPROPERTY_VERSIONSTRINGW))
664             szAttribute = display_version;
665
666         val = msi_reg_get_value(userdata, szAttribute, &type);
667         if (!val)
668             val = empty;
669     }
670     else if (!lstrcmpW(szAttribute, INSTALLPROPERTY_INSTANCETYPEW) ||
671              !lstrcmpW(szAttribute, INSTALLPROPERTY_TRANSFORMSW) ||
672              !lstrcmpW(szAttribute, INSTALLPROPERTY_LANGUAGEW) ||
673              !lstrcmpW(szAttribute, INSTALLPROPERTY_PRODUCTNAMEW) ||
674              !lstrcmpW(szAttribute, INSTALLPROPERTY_ASSIGNMENTTYPEW) ||
675              !lstrcmpW(szAttribute, INSTALLPROPERTY_PACKAGECODEW) ||
676              !lstrcmpW(szAttribute, INSTALLPROPERTY_VERSIONW) ||
677              !lstrcmpW(szAttribute, INSTALLPROPERTY_PRODUCTICONW) ||
678              !lstrcmpW(szAttribute, INSTALLPROPERTY_PACKAGENAMEW) ||
679              !lstrcmpW(szAttribute, INSTALLPROPERTY_AUTHORIZED_LUA_APPW))
680     {
681         if (!prodkey)
682         {
683             r = ERROR_UNKNOWN_PRODUCT;
684             goto done;
685         }
686
687         if (!lstrcmpW(szAttribute, INSTALLPROPERTY_ASSIGNMENTTYPEW))
688             szAttribute = assignment;
689
690         if (!lstrcmpW(szAttribute, INSTALLPROPERTY_PACKAGENAMEW))
691         {
692             res = RegOpenKeyW(prodkey, sourcelist, &source);
693             if (res == ERROR_SUCCESS)
694                 val = msi_reg_get_value(source, szAttribute, &type);
695
696             RegCloseKey(source);
697         }
698         else
699         {
700             val = msi_reg_get_value(prodkey, szAttribute, &type);
701             if (!val)
702                 val = empty;
703         }
704
705         if (val != empty && type != REG_DWORD &&
706             !lstrcmpW(szAttribute, INSTALLPROPERTY_PACKAGECODEW))
707         {
708             if (lstrlenW(val) != SQUISH_GUID_SIZE - 1)
709                 badconfig = TRUE;
710             else
711             {
712                 unsquash_guid(val, packagecode);
713                 msi_free(val);
714                 val = strdupW(packagecode);
715             }
716         }
717     }
718
719     if (!val)
720     {
721         r = ERROR_UNKNOWN_PROPERTY;
722         goto done;
723     }
724
725     if (pcchValueBuf)
726     {
727         save = *pcchValueBuf;
728
729         if (lstrlenW(val) < *pcchValueBuf)
730             r = msi_strcpy_to_awstring(val, szValue, pcchValueBuf);
731         else if (szValue->str.a || szValue->str.w)
732             r = ERROR_MORE_DATA;
733
734         if (!badconfig)
735             *pcchValueBuf = lstrlenW(val);
736         else if (r == ERROR_SUCCESS)
737         {
738             *pcchValueBuf = save;
739             r = ERROR_BAD_CONFIGURATION;
740         }
741     }
742     else if (badconfig)
743         r = ERROR_BAD_CONFIGURATION;
744
745     if (val != empty)
746         msi_free(val);
747
748 done:
749     RegCloseKey(prodkey);
750     RegCloseKey(userdata);
751     return r;
752 }
753
754 UINT WINAPI MsiGetProductInfoA(LPCSTR szProduct, LPCSTR szAttribute,
755                                LPSTR szBuffer, LPDWORD pcchValueBuf)
756 {
757     LPWSTR szwProduct, szwAttribute = NULL;
758     UINT r = ERROR_OUTOFMEMORY;
759     awstring buffer;
760
761     TRACE("%s %s %p %p\n", debugstr_a(szProduct), debugstr_a(szAttribute),
762           szBuffer, pcchValueBuf);
763
764     szwProduct = strdupAtoW( szProduct );
765     if( szProduct && !szwProduct )
766         goto end;
767
768     szwAttribute = strdupAtoW( szAttribute );
769     if( szAttribute && !szwAttribute )
770         goto end;
771
772     buffer.unicode = FALSE;
773     buffer.str.a = szBuffer;
774
775     r = MSI_GetProductInfo( szwProduct, szwAttribute,
776                             &buffer, pcchValueBuf );
777
778 end:
779     msi_free( szwProduct );
780     msi_free( szwAttribute );
781
782     return r;
783 }
784
785 UINT WINAPI MsiGetProductInfoW(LPCWSTR szProduct, LPCWSTR szAttribute,
786                                LPWSTR szBuffer, LPDWORD pcchValueBuf)
787 {
788     awstring buffer;
789
790     TRACE("%s %s %p %p\n", debugstr_w(szProduct), debugstr_w(szAttribute),
791           szBuffer, pcchValueBuf);
792
793     buffer.unicode = TRUE;
794     buffer.str.w = szBuffer;
795
796     return MSI_GetProductInfo( szProduct, szAttribute,
797                                &buffer, pcchValueBuf );
798 }
799
800 UINT WINAPI MsiGetProductInfoExA(LPCSTR szProductCode, LPCSTR szUserSid,
801                                  MSIINSTALLCONTEXT dwContext, LPCSTR szProperty,
802                                  LPSTR szValue, LPDWORD pcchValue)
803 {
804     LPWSTR product = NULL;
805     LPWSTR usersid = NULL;
806     LPWSTR property = NULL;
807     LPWSTR value = NULL;
808     DWORD len = 0;
809     UINT r;
810
811     TRACE("(%s, %s, %d, %s, %p, %p)\n", debugstr_a(szProductCode),
812           debugstr_a(szUserSid), dwContext, debugstr_a(szProperty),
813            szValue, pcchValue);
814
815     if (szValue && !pcchValue)
816         return ERROR_INVALID_PARAMETER;
817
818     if (szProductCode) product = strdupAtoW(szProductCode);
819     if (szUserSid) usersid = strdupAtoW(szUserSid);
820     if (szProperty) property = strdupAtoW(szProperty);
821
822     r = MsiGetProductInfoExW(product, usersid, dwContext, property,
823                              NULL, &len);
824     if (r != ERROR_SUCCESS)
825         goto done;
826
827     value = msi_alloc(++len * sizeof(WCHAR));
828     if (!value)
829     {
830         r = ERROR_OUTOFMEMORY;
831         goto done;
832     }
833
834     r = MsiGetProductInfoExW(product, usersid, dwContext, property,
835                              value, &len);
836     if (r != ERROR_SUCCESS)
837         goto done;
838
839     if (!pcchValue)
840         goto done;
841
842     len = WideCharToMultiByte(CP_ACP, 0, value, -1, NULL, 0, NULL, NULL);
843     if (*pcchValue >= len)
844         WideCharToMultiByte(CP_ACP, 0, value, -1, szValue, len, NULL, NULL);
845     else if (szValue)
846     {
847         r = ERROR_MORE_DATA;
848         if (*pcchValue > 0)
849             *szValue = '\0';
850     }
851
852     if (*pcchValue <= len || !szValue)
853         len = len * sizeof(WCHAR) - 1;
854
855     *pcchValue = len - 1;
856
857 done:
858     msi_free(product);
859     msi_free(usersid);
860     msi_free(property);
861     msi_free(value);
862
863     return r;
864 }
865
866 static UINT msi_copy_outval(LPWSTR val, LPWSTR out, LPDWORD size)
867 {
868     UINT r;
869
870     if (!val)
871         return ERROR_UNKNOWN_PROPERTY;
872
873     if (out)
874     {
875         if (lstrlenW(val) >= *size)
876         {
877             r = ERROR_MORE_DATA;
878             if (*size > 0)
879                 *out = '\0';
880         }
881         else
882             lstrcpyW(out, val);
883     }
884
885     if (size)
886         *size = lstrlenW(val);
887
888     return ERROR_SUCCESS;
889 }
890
891 UINT WINAPI MsiGetProductInfoExW(LPCWSTR szProductCode, LPCWSTR szUserSid,
892                                  MSIINSTALLCONTEXT dwContext, LPCWSTR szProperty,
893                                  LPWSTR szValue, LPDWORD pcchValue)
894 {
895     WCHAR squished_pc[GUID_SIZE];
896     LPWSTR val = NULL;
897     LPCWSTR package = NULL;
898     HKEY props = NULL, prod;
899     HKEY classes = NULL, managed;
900     HKEY hkey = NULL;
901     DWORD type;
902     UINT r = ERROR_UNKNOWN_PRODUCT;
903
904     static const WCHAR one[] = {'1',0};
905     static const WCHAR five[] = {'5',0};
906     static const WCHAR empty[] = {0};
907     static const WCHAR displayname[] = {
908         'D','i','s','p','l','a','y','N','a','m','e',0};
909     static const WCHAR displayversion[] = {
910         'D','i','s','p','l','a','y','V','e','r','s','i','o','n',0};
911     static const WCHAR managed_local_package[] = {
912         'M','a','n','a','g','e','d','L','o','c','a','l',
913         'P','a','c','k','a','g','e',0};
914
915     TRACE("(%s, %s, %d, %s, %p, %p)\n", debugstr_w(szProductCode),
916           debugstr_w(szUserSid), dwContext, debugstr_w(szProperty),
917            szValue, pcchValue);
918
919     if (!szProductCode || !squash_guid(szProductCode, squished_pc))
920         return ERROR_INVALID_PARAMETER;
921
922     if (szValue && !pcchValue)
923         return ERROR_INVALID_PARAMETER;
924
925     if (dwContext != MSIINSTALLCONTEXT_USERUNMANAGED &&
926         dwContext != MSIINSTALLCONTEXT_USERMANAGED &&
927         dwContext != MSIINSTALLCONTEXT_MACHINE)
928         return ERROR_INVALID_PARAMETER;
929
930     if (!szProperty || !*szProperty)
931         return ERROR_INVALID_PARAMETER;
932
933     if (dwContext == MSIINSTALLCONTEXT_MACHINE && szUserSid)
934         return ERROR_INVALID_PARAMETER;
935
936     MSIREG_OpenLocalManagedProductKey(szProductCode, &managed, FALSE);
937     MSIREG_OpenUserProductsKey(szProductCode, &prod, FALSE);
938
939     if (dwContext == MSIINSTALLCONTEXT_USERUNMANAGED)
940     {
941         package = INSTALLPROPERTY_LOCALPACKAGEW;
942         MSIREG_OpenInstallPropertiesKey(szProductCode, &props, FALSE);
943
944         if (!props && !prod)
945             goto done;
946     }
947     else if (dwContext == MSIINSTALLCONTEXT_USERMANAGED)
948     {
949         package = managed_local_package;
950         MSIREG_OpenInstallPropertiesKey(szProductCode, &props, FALSE);
951
952         if (!props && !managed)
953             goto done;
954     }
955     else if (dwContext == MSIINSTALLCONTEXT_MACHINE)
956     {
957         package = INSTALLPROPERTY_LOCALPACKAGEW;
958         MSIREG_OpenLocalSystemProductKey(szProductCode, &props, FALSE);
959         MSIREG_OpenLocalClassesProductKey(szProductCode, &classes, FALSE);
960
961         if (!props && !classes)
962             goto done;
963     }
964
965     if (!lstrcmpW(szProperty, INSTALLPROPERTY_HELPLINKW) ||
966         !lstrcmpW(szProperty, INSTALLPROPERTY_HELPTELEPHONEW) ||
967         !lstrcmpW(szProperty, INSTALLPROPERTY_INSTALLDATEW) ||
968         !lstrcmpW(szProperty, INSTALLPROPERTY_INSTALLEDPRODUCTNAMEW) ||
969         !lstrcmpW(szProperty, INSTALLPROPERTY_INSTALLLOCATIONW) ||
970         !lstrcmpW(szProperty, INSTALLPROPERTY_INSTALLSOURCEW) ||
971         !lstrcmpW(szProperty, INSTALLPROPERTY_LOCALPACKAGEW) ||
972         !lstrcmpW(szProperty, INSTALLPROPERTY_PUBLISHERW) ||
973         !lstrcmpW(szProperty, INSTALLPROPERTY_URLINFOABOUTW) ||
974         !lstrcmpW(szProperty, INSTALLPROPERTY_URLUPDATEINFOW) ||
975         !lstrcmpW(szProperty, INSTALLPROPERTY_VERSIONMINORW) ||
976         !lstrcmpW(szProperty, INSTALLPROPERTY_VERSIONMAJORW) ||
977         !lstrcmpW(szProperty, INSTALLPROPERTY_VERSIONSTRINGW) ||
978         !lstrcmpW(szProperty, INSTALLPROPERTY_PRODUCTIDW) ||
979         !lstrcmpW(szProperty, INSTALLPROPERTY_REGCOMPANYW) ||
980         !lstrcmpW(szProperty, INSTALLPROPERTY_REGOWNERW) ||
981         !lstrcmpW(szProperty, INSTALLPROPERTY_INSTANCETYPEW))
982     {
983         val = msi_reg_get_value(props, package, &type);
984         if (!val)
985         {
986             if (prod || classes)
987                 r = ERROR_UNKNOWN_PROPERTY;
988
989             goto done;
990         }
991
992         msi_free(val);
993
994         if (!lstrcmpW(szProperty, INSTALLPROPERTY_INSTALLEDPRODUCTNAMEW))
995             szProperty = displayname;
996         else if (!lstrcmpW(szProperty, INSTALLPROPERTY_VERSIONSTRINGW))
997             szProperty = displayversion;
998
999         val = msi_reg_get_value(props, szProperty, &type);
1000         if (!val)
1001             val = strdupW(empty);
1002
1003         r = msi_copy_outval(val, szValue, pcchValue);
1004     }
1005     else if (!lstrcmpW(szProperty, INSTALLPROPERTY_TRANSFORMSW) ||
1006              !lstrcmpW(szProperty, INSTALLPROPERTY_LANGUAGEW) ||
1007              !lstrcmpW(szProperty, INSTALLPROPERTY_PRODUCTNAMEW) ||
1008              !lstrcmpW(szProperty, INSTALLPROPERTY_PACKAGECODEW) ||
1009              !lstrcmpW(szProperty, INSTALLPROPERTY_VERSIONW) ||
1010              !lstrcmpW(szProperty, INSTALLPROPERTY_PRODUCTICONW) ||
1011              !lstrcmpW(szProperty, INSTALLPROPERTY_PACKAGENAMEW) ||
1012              !lstrcmpW(szProperty, INSTALLPROPERTY_AUTHORIZED_LUA_APPW))
1013     {
1014         if (!prod && !classes)
1015             goto done;
1016
1017         if (dwContext == MSIINSTALLCONTEXT_USERUNMANAGED)
1018             hkey = prod;
1019         else if (dwContext == MSIINSTALLCONTEXT_USERMANAGED)
1020             hkey = managed;
1021         else if (dwContext == MSIINSTALLCONTEXT_MACHINE)
1022             hkey = classes;
1023
1024         val = msi_reg_get_value(hkey, szProperty, &type);
1025         if (!val)
1026             val = strdupW(empty);
1027
1028         r = msi_copy_outval(val, szValue, pcchValue);
1029     }
1030     else if (!lstrcmpW(szProperty, INSTALLPROPERTY_PRODUCTSTATEW))
1031     {
1032         if (dwContext == MSIINSTALLCONTEXT_MACHINE)
1033         {
1034             if (props)
1035             {
1036                 val = msi_reg_get_value(props, package, &type);
1037                 if (!val)
1038                     goto done;
1039
1040                 msi_free(val);
1041                 val = strdupW(five);
1042             }
1043             else
1044                 val = strdupW(one);
1045
1046             r = msi_copy_outval(val, szValue, pcchValue);
1047             goto done;
1048         }
1049         else if (props && (val = msi_reg_get_value(props, package, &type)))
1050         {
1051             msi_free(val);
1052             val = strdupW(five);
1053             r = msi_copy_outval(val, szValue, pcchValue);
1054             goto done;
1055         }
1056
1057         if (prod || managed)
1058             val = strdupW(one);
1059         else
1060             goto done;
1061
1062         r = msi_copy_outval(val, szValue, pcchValue);
1063     }
1064     else if (!lstrcmpW(szProperty, INSTALLPROPERTY_ASSIGNMENTTYPEW))
1065     {
1066         if (!prod && !classes)
1067             goto done;
1068
1069         /* FIME */
1070         val = strdupW(empty);
1071         r = msi_copy_outval(val, szValue, pcchValue);
1072     }
1073     else
1074         r = ERROR_UNKNOWN_PROPERTY;
1075
1076 done:
1077     RegCloseKey(props);
1078     RegCloseKey(prod);
1079     RegCloseKey(managed);
1080     RegCloseKey(classes);
1081     msi_free(val);
1082
1083     return r;
1084 }
1085
1086 UINT WINAPI MsiEnableLogA(DWORD dwLogMode, LPCSTR szLogFile, DWORD attributes)
1087 {
1088     LPWSTR szwLogFile = NULL;
1089     UINT r;
1090
1091     TRACE("%08x %s %08x\n", dwLogMode, debugstr_a(szLogFile), attributes);
1092
1093     if( szLogFile )
1094     {
1095         szwLogFile = strdupAtoW( szLogFile );
1096         if( !szwLogFile )
1097             return ERROR_OUTOFMEMORY;
1098     }
1099     r = MsiEnableLogW( dwLogMode, szwLogFile, attributes );
1100     msi_free( szwLogFile );
1101     return r;
1102 }
1103
1104 UINT WINAPI MsiEnableLogW(DWORD dwLogMode, LPCWSTR szLogFile, DWORD attributes)
1105 {
1106     HANDLE file = INVALID_HANDLE_VALUE;
1107
1108     TRACE("%08x %s %08x\n", dwLogMode, debugstr_w(szLogFile), attributes);
1109
1110     if (szLogFile)
1111     {
1112         lstrcpyW(gszLogFile,szLogFile);
1113         if (!(attributes & INSTALLLOGATTRIBUTES_APPEND))
1114             DeleteFileW(szLogFile);
1115         file = CreateFileW(szLogFile, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
1116                                FILE_ATTRIBUTE_NORMAL, NULL);
1117         if (file != INVALID_HANDLE_VALUE)
1118             CloseHandle(file);
1119         else
1120             ERR("Unable to enable log %s\n",debugstr_w(szLogFile));
1121     }
1122     else
1123         gszLogFile[0] = '\0';
1124
1125     return ERROR_SUCCESS;
1126 }
1127
1128 UINT WINAPI MsiEnumComponentCostsW(MSIHANDLE hInstall, LPCWSTR szComponent,
1129                                    DWORD dwIndex, INSTALLSTATE iState,
1130                                    LPWSTR lpDriveBuf, DWORD *pcchDriveBuf,
1131                                    int *piCost, int *pTempCost)
1132 {
1133     FIXME("(%ld, %s, %d, %d, %p, %p, %p %p): stub!\n", hInstall,
1134           debugstr_w(szComponent), dwIndex, iState, lpDriveBuf,
1135           pcchDriveBuf, piCost, pTempCost);
1136
1137     return ERROR_NO_MORE_ITEMS;
1138 }
1139
1140 UINT WINAPI MsiQueryComponentStateA(LPCSTR szProductCode,
1141                                     LPCSTR szUserSid, MSIINSTALLCONTEXT dwContext,
1142                                     LPCSTR szComponent, INSTALLSTATE *pdwState)
1143 {
1144     LPWSTR prodcode = NULL, usersid = NULL, comp = NULL;
1145     UINT r;
1146
1147     TRACE("(%s, %s, %d, %s, %p)\n", debugstr_a(szProductCode),
1148           debugstr_a(szUserSid), dwContext, debugstr_a(szComponent), pdwState);
1149
1150     if (szProductCode && !(prodcode = strdupAtoW(szProductCode)))
1151         return ERROR_OUTOFMEMORY;
1152
1153     if (szUserSid && !(usersid = strdupAtoW(szUserSid)))
1154             return ERROR_OUTOFMEMORY;
1155
1156     if (szComponent && !(comp = strdupAtoW(szComponent)))
1157             return ERROR_OUTOFMEMORY;
1158
1159     r = MsiQueryComponentStateW(prodcode, usersid, dwContext, comp, pdwState);
1160
1161     msi_free(prodcode);
1162     msi_free(usersid);
1163     msi_free(comp);
1164
1165     return r;
1166 }
1167
1168 static BOOL msi_comp_find_prod_key(LPCWSTR prodcode, MSIINSTALLCONTEXT context)
1169 {
1170     UINT r;
1171     HKEY hkey;
1172
1173     if (context == MSIINSTALLCONTEXT_MACHINE)
1174         r = MSIREG_OpenLocalClassesProductKey(prodcode, &hkey, FALSE);
1175     else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
1176         r = MSIREG_OpenUserProductsKey(prodcode, &hkey, FALSE);
1177     else
1178         r = MSIREG_OpenLocalManagedProductKey(prodcode, &hkey, FALSE);
1179
1180     RegCloseKey(hkey);
1181     return (r == ERROR_SUCCESS);
1182 }
1183
1184 static BOOL msi_comp_find_package(LPCWSTR prodcode, MSIINSTALLCONTEXT context)
1185 {
1186     LPCWSTR package;
1187     HKEY hkey;
1188     DWORD sz;
1189     LONG res;
1190     UINT r;
1191
1192     static const WCHAR local_package[] = {'L','o','c','a','l','P','a','c','k','a','g','e',0};
1193     static const WCHAR managed_local_package[] = {
1194         'M','a','n','a','g','e','d','L','o','c','a','l','P','a','c','k','a','g','e',0
1195     };
1196
1197     if (context == MSIINSTALLCONTEXT_MACHINE)
1198         r = MSIREG_OpenLocalSystemProductKey(prodcode, &hkey, FALSE);
1199     else
1200         r = MSIREG_OpenInstallPropertiesKey(prodcode, &hkey, FALSE);
1201
1202     if (r != ERROR_SUCCESS)
1203         return FALSE;
1204
1205     if (context == MSIINSTALLCONTEXT_USERMANAGED)
1206         package = managed_local_package;
1207     else
1208         package = local_package;
1209
1210     sz = 0;
1211     res = RegQueryValueExW(hkey, package, NULL, NULL, NULL, &sz);
1212     RegCloseKey(hkey);
1213
1214     return (res == ERROR_SUCCESS);
1215 }
1216
1217 static BOOL msi_comp_find_prodcode(LPWSTR squished_pc,
1218                                    MSIINSTALLCONTEXT context,
1219                                    LPCWSTR comp, DWORD *sz)
1220 {
1221     HKEY hkey;
1222     LONG res;
1223     UINT r;
1224
1225     if (context == MSIINSTALLCONTEXT_MACHINE)
1226         r = MSIREG_OpenLocalSystemComponentKey(comp, &hkey, FALSE);
1227     else
1228         r = MSIREG_OpenUserDataComponentKey(comp, &hkey, FALSE);
1229
1230     if (r != ERROR_SUCCESS)
1231         return FALSE;
1232
1233     *sz = 0;
1234     res = RegQueryValueExW(hkey, squished_pc, NULL, NULL, NULL, sz);
1235     if (res != ERROR_SUCCESS)
1236         return FALSE;
1237
1238     RegCloseKey(hkey);
1239     return TRUE;
1240 }
1241
1242 UINT WINAPI MsiQueryComponentStateW(LPCWSTR szProductCode,
1243                                     LPCWSTR szUserSid, MSIINSTALLCONTEXT dwContext,
1244                                     LPCWSTR szComponent, INSTALLSTATE *pdwState)
1245 {
1246     WCHAR squished_pc[GUID_SIZE];
1247     BOOL found;
1248     DWORD sz;
1249
1250     TRACE("(%s, %s, %d, %s, %p)\n", debugstr_w(szProductCode),
1251           debugstr_w(szUserSid), dwContext, debugstr_w(szComponent), pdwState);
1252
1253     if (!pdwState)
1254         return ERROR_INVALID_PARAMETER;
1255
1256     if (!szProductCode || !*szProductCode || lstrlenW(szProductCode) != GUID_SIZE - 1)
1257         return ERROR_INVALID_PARAMETER;
1258
1259     if (!squash_guid(szProductCode, squished_pc))
1260         return ERROR_INVALID_PARAMETER;
1261
1262     found = msi_comp_find_prod_key(szProductCode, dwContext);
1263
1264     if (!msi_comp_find_package(szProductCode, dwContext))
1265     {
1266         if (found)
1267         {
1268             *pdwState = INSTALLSTATE_UNKNOWN;
1269             return ERROR_UNKNOWN_COMPONENT;
1270         }
1271
1272         return ERROR_UNKNOWN_PRODUCT;
1273     }
1274
1275     *pdwState = INSTALLSTATE_UNKNOWN;
1276
1277     if (!msi_comp_find_prodcode(squished_pc, dwContext, szComponent, &sz))
1278         return ERROR_UNKNOWN_COMPONENT;
1279
1280     if (sz == 0)
1281         *pdwState = INSTALLSTATE_NOTUSED;
1282     else
1283         *pdwState = INSTALLSTATE_LOCAL;
1284
1285     return ERROR_SUCCESS;
1286 }
1287
1288 INSTALLSTATE WINAPI MsiQueryProductStateA(LPCSTR szProduct)
1289 {
1290     LPWSTR szwProduct = NULL;
1291     INSTALLSTATE r;
1292
1293     if( szProduct )
1294     {
1295          szwProduct = strdupAtoW( szProduct );
1296          if( !szwProduct )
1297              return ERROR_OUTOFMEMORY;
1298     }
1299     r = MsiQueryProductStateW( szwProduct );
1300     msi_free( szwProduct );
1301     return r;
1302 }
1303
1304 INSTALLSTATE WINAPI MsiQueryProductStateW(LPCWSTR szProduct)
1305 {
1306     UINT rc;
1307     INSTALLSTATE state = INSTALLSTATE_UNKNOWN;
1308     HKEY hkey = 0, props = 0;
1309     DWORD sz;
1310     BOOL userkey_exists = FALSE;
1311
1312     static const int GUID_LEN = 38;
1313     static const WCHAR szInstallProperties[] = {
1314             'I','n','s','t','a','l','l','P','r','o','p','e','r','t','i','e','s',0
1315     };
1316     static const WCHAR szWindowsInstaller[] = {
1317             'W','i','n','d','o','w','s','I','n','s','t','a','l','l','e','r',0
1318     };
1319
1320     TRACE("%s\n", debugstr_w(szProduct));
1321
1322     if (!szProduct || !*szProduct || lstrlenW(szProduct) != GUID_LEN)
1323         return INSTALLSTATE_INVALIDARG;
1324
1325     rc = MSIREG_OpenUserProductsKey(szProduct,&hkey,FALSE);
1326     if (rc == ERROR_SUCCESS)
1327     {
1328         userkey_exists = TRUE;
1329         state = INSTALLSTATE_ADVERTISED;
1330         RegCloseKey(hkey);
1331     }
1332
1333     rc = MSIREG_OpenUserDataProductKey(szProduct,&hkey,FALSE);
1334     if (rc != ERROR_SUCCESS)
1335         goto end;
1336
1337     rc = RegOpenKeyW(hkey, szInstallProperties, &props);
1338     if (rc != ERROR_SUCCESS)
1339         goto end;
1340
1341     sz = sizeof(state);
1342     rc = RegQueryValueExW(props,szWindowsInstaller,NULL,NULL,(LPVOID)&state, &sz);
1343     if (rc != ERROR_SUCCESS)
1344         goto end;
1345
1346     if (state)
1347         state = INSTALLSTATE_DEFAULT;
1348     else
1349         state = INSTALLSTATE_UNKNOWN;
1350
1351     if (state == INSTALLSTATE_DEFAULT && !userkey_exists)
1352         state = INSTALLSTATE_ABSENT;
1353
1354 end:
1355     RegCloseKey(props);
1356     RegCloseKey(hkey);
1357     return state;
1358 }
1359
1360 INSTALLUILEVEL WINAPI MsiSetInternalUI(INSTALLUILEVEL dwUILevel, HWND *phWnd)
1361 {
1362     INSTALLUILEVEL old = gUILevel;
1363     HWND oldwnd = gUIhwnd;
1364
1365     TRACE("%08x %p\n", dwUILevel, phWnd);
1366
1367     gUILevel = dwUILevel;
1368     if (phWnd)
1369     {
1370         gUIhwnd = *phWnd;
1371         *phWnd = oldwnd;
1372     }
1373     return old;
1374 }
1375
1376 INSTALLUI_HANDLERA WINAPI MsiSetExternalUIA(INSTALLUI_HANDLERA puiHandler,
1377                                   DWORD dwMessageFilter, LPVOID pvContext)
1378 {
1379     INSTALLUI_HANDLERA prev = gUIHandlerA;
1380
1381     TRACE("%p %x %p\n",puiHandler, dwMessageFilter,pvContext);
1382     gUIHandlerA = puiHandler;
1383     gUIFilter = dwMessageFilter;
1384     gUIContext = pvContext;
1385
1386     return prev;
1387 }
1388
1389 INSTALLUI_HANDLERW WINAPI MsiSetExternalUIW(INSTALLUI_HANDLERW puiHandler,
1390                                   DWORD dwMessageFilter, LPVOID pvContext)
1391 {
1392     INSTALLUI_HANDLERW prev = gUIHandlerW;
1393
1394     TRACE("%p %x %p\n",puiHandler,dwMessageFilter,pvContext);
1395     gUIHandlerW = puiHandler;
1396     gUIFilter = dwMessageFilter;
1397     gUIContext = pvContext;
1398
1399     return prev;
1400 }
1401
1402 /******************************************************************
1403  *  MsiLoadStringW            [MSI.@]
1404  *
1405  * Loads a string from MSI's string resources.
1406  *
1407  * PARAMS
1408  *
1409  *   handle        [I]  only -1 is handled currently
1410  *   id            [I]  id of the string to be loaded
1411  *   lpBuffer      [O]  buffer for the string to be written to
1412  *   nBufferMax    [I]  maximum size of the buffer in characters
1413  *   lang          [I]  the preferred language for the string
1414  *
1415  * RETURNS
1416  *
1417  *   If successful, this function returns the language id of the string loaded
1418  *   If the function fails, the function returns zero.
1419  *
1420  * NOTES
1421  *
1422  *   The type of the first parameter is unknown.  LoadString's prototype
1423  *  suggests that it might be a module handle.  I have made it an MSI handle
1424  *  for starters, as -1 is an invalid MSI handle, but not an invalid module
1425  *  handle.  Maybe strings can be stored in an MSI database somehow.
1426  */
1427 LANGID WINAPI MsiLoadStringW( MSIHANDLE handle, UINT id, LPWSTR lpBuffer,
1428                 int nBufferMax, LANGID lang )
1429 {
1430     HRSRC hres;
1431     HGLOBAL hResData;
1432     LPWSTR p;
1433     DWORD i, len;
1434
1435     TRACE("%ld %u %p %d %d\n", handle, id, lpBuffer, nBufferMax, lang);
1436
1437     if( handle != -1 )
1438         FIXME("don't know how to deal with handle = %08lx\n", handle);
1439
1440     if( !lang )
1441         lang = GetUserDefaultLangID();
1442
1443     hres = FindResourceExW( msi_hInstance, (LPCWSTR) RT_STRING,
1444                             (LPWSTR)1, lang );
1445     if( !hres )
1446         return 0;
1447     hResData = LoadResource( msi_hInstance, hres );
1448     if( !hResData )
1449         return 0;
1450     p = LockResource( hResData );
1451     if( !p )
1452         return 0;
1453
1454     for (i = 0; i < (id&0xf); i++)
1455         p += *p + 1;
1456     len = *p;
1457
1458     if( nBufferMax <= len )
1459         return 0;
1460
1461     memcpy( lpBuffer, p+1, len * sizeof(WCHAR));
1462     lpBuffer[ len ] = 0;
1463
1464     TRACE("found -> %s\n", debugstr_w(lpBuffer));
1465
1466     return lang;
1467 }
1468
1469 LANGID WINAPI MsiLoadStringA( MSIHANDLE handle, UINT id, LPSTR lpBuffer,
1470                 int nBufferMax, LANGID lang )
1471 {
1472     LPWSTR bufW;
1473     LANGID r;
1474     DWORD len;
1475
1476     bufW = msi_alloc(nBufferMax*sizeof(WCHAR));
1477     r = MsiLoadStringW(handle, id, bufW, nBufferMax, lang);
1478     if( r )
1479     {
1480         len = WideCharToMultiByte(CP_ACP, 0, bufW, -1, NULL, 0, NULL, NULL );
1481         if( len <= nBufferMax )
1482             WideCharToMultiByte( CP_ACP, 0, bufW, -1,
1483                                  lpBuffer, nBufferMax, NULL, NULL );
1484         else
1485             r = 0;
1486     }
1487     msi_free(bufW);
1488     return r;
1489 }
1490
1491 INSTALLSTATE WINAPI MsiLocateComponentA(LPCSTR szComponent, LPSTR lpPathBuf,
1492                 LPDWORD pcchBuf)
1493 {
1494     char szProduct[GUID_SIZE];
1495
1496     TRACE("%s %p %p\n", debugstr_a(szComponent), lpPathBuf, pcchBuf);
1497
1498     if (MsiGetProductCodeA( szComponent, szProduct ) != ERROR_SUCCESS)
1499         return INSTALLSTATE_UNKNOWN;
1500
1501     return MsiGetComponentPathA( szProduct, szComponent, lpPathBuf, pcchBuf );
1502 }
1503
1504 INSTALLSTATE WINAPI MsiLocateComponentW(LPCWSTR szComponent, LPWSTR lpPathBuf,
1505                 LPDWORD pcchBuf)
1506 {
1507     WCHAR szProduct[GUID_SIZE];
1508
1509     TRACE("%s %p %p\n", debugstr_w(szComponent), lpPathBuf, pcchBuf);
1510
1511     if (MsiGetProductCodeW( szComponent, szProduct ) != ERROR_SUCCESS)
1512         return INSTALLSTATE_UNKNOWN;
1513
1514     return MsiGetComponentPathW( szProduct, szComponent, lpPathBuf, pcchBuf );
1515 }
1516
1517 UINT WINAPI MsiMessageBoxA(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType,
1518                 WORD wLanguageId, DWORD f)
1519 {
1520     FIXME("%p %s %s %u %08x %08x\n", hWnd, debugstr_a(lpText), debugstr_a(lpCaption),
1521           uType, wLanguageId, f);
1522     return MessageBoxExA(hWnd,lpText,lpCaption,uType,wLanguageId); 
1523 }
1524
1525 UINT WINAPI MsiMessageBoxW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType,
1526                 WORD wLanguageId, DWORD f)
1527 {
1528     FIXME("%p %s %s %u %08x %08x\n", hWnd, debugstr_w(lpText), debugstr_w(lpCaption),
1529           uType, wLanguageId, f);
1530     return MessageBoxExW(hWnd,lpText,lpCaption,uType,wLanguageId); 
1531 }
1532
1533 UINT WINAPI MsiProvideAssemblyA( LPCSTR szAssemblyName, LPCSTR szAppContext,
1534                 DWORD dwInstallMode, DWORD dwAssemblyInfo, LPSTR lpPathBuf,
1535                 LPDWORD pcchPathBuf )
1536 {
1537     FIXME("%s %s %08x %08x %p %p\n", debugstr_a(szAssemblyName),
1538           debugstr_a(szAppContext), dwInstallMode, dwAssemblyInfo, lpPathBuf,
1539           pcchPathBuf);
1540     return ERROR_CALL_NOT_IMPLEMENTED;
1541 }
1542
1543 UINT WINAPI MsiProvideAssemblyW( LPCWSTR szAssemblyName, LPCWSTR szAppContext,
1544                 DWORD dwInstallMode, DWORD dwAssemblyInfo, LPWSTR lpPathBuf,
1545                 LPDWORD pcchPathBuf )
1546 {
1547     FIXME("%s %s %08x %08x %p %p\n", debugstr_w(szAssemblyName),
1548           debugstr_w(szAppContext), dwInstallMode, dwAssemblyInfo, lpPathBuf,
1549           pcchPathBuf);
1550     return ERROR_CALL_NOT_IMPLEMENTED;
1551 }
1552
1553 UINT WINAPI MsiProvideComponentFromDescriptorA( LPCSTR szDescriptor,
1554                 LPSTR szPath, LPDWORD pcchPath, LPDWORD pcchArgs )
1555 {
1556     FIXME("%s %p %p %p\n", debugstr_a(szDescriptor), szPath, pcchPath, pcchArgs );
1557     return ERROR_CALL_NOT_IMPLEMENTED;
1558 }
1559
1560 UINT WINAPI MsiProvideComponentFromDescriptorW( LPCWSTR szDescriptor,
1561                 LPWSTR szPath, LPDWORD pcchPath, LPDWORD pcchArgs )
1562 {
1563     FIXME("%s %p %p %p\n", debugstr_w(szDescriptor), szPath, pcchPath, pcchArgs );
1564     return ERROR_CALL_NOT_IMPLEMENTED;
1565 }
1566
1567 HRESULT WINAPI MsiGetFileSignatureInformationA( LPCSTR szSignedObjectPath,
1568                 DWORD dwFlags, PCCERT_CONTEXT* ppcCertContext, LPBYTE pbHashData,
1569                 LPDWORD pcbHashData)
1570 {
1571     FIXME("%s %08x %p %p %p\n", debugstr_a(szSignedObjectPath), dwFlags,
1572           ppcCertContext, pbHashData, pcbHashData);
1573     return ERROR_CALL_NOT_IMPLEMENTED;
1574 }
1575
1576 HRESULT WINAPI MsiGetFileSignatureInformationW( LPCWSTR szSignedObjectPath,
1577                 DWORD dwFlags, PCCERT_CONTEXT* ppcCertContext, LPBYTE pbHashData,
1578                 LPDWORD pcbHashData)
1579 {
1580     FIXME("%s %08x %p %p %p\n", debugstr_w(szSignedObjectPath), dwFlags,
1581           ppcCertContext, pbHashData, pcbHashData);
1582     return ERROR_CALL_NOT_IMPLEMENTED;
1583 }
1584
1585 UINT WINAPI MsiGetProductPropertyA( MSIHANDLE hProduct, LPCSTR szProperty,
1586                                     LPSTR szValue, LPDWORD pccbValue )
1587 {
1588     FIXME("%ld %s %p %p\n", hProduct, debugstr_a(szProperty), szValue, pccbValue);
1589     return ERROR_CALL_NOT_IMPLEMENTED;
1590 }
1591
1592 UINT WINAPI MsiGetProductPropertyW( MSIHANDLE hProduct, LPCWSTR szProperty,
1593                                     LPWSTR szValue, LPDWORD pccbValue )
1594 {
1595     FIXME("%ld %s %p %p\n", hProduct, debugstr_w(szProperty), szValue, pccbValue);
1596     return ERROR_CALL_NOT_IMPLEMENTED;
1597 }
1598
1599 UINT WINAPI MsiVerifyPackageA( LPCSTR szPackage )
1600 {
1601     UINT r;
1602     LPWSTR szPack = NULL;
1603
1604     TRACE("%s\n", debugstr_a(szPackage) );
1605
1606     if( szPackage )
1607     {
1608         szPack = strdupAtoW( szPackage );
1609         if( !szPack )
1610             return ERROR_OUTOFMEMORY;
1611     }
1612
1613     r = MsiVerifyPackageW( szPack );
1614
1615     msi_free( szPack );
1616
1617     return r;
1618 }
1619
1620 UINT WINAPI MsiVerifyPackageW( LPCWSTR szPackage )
1621 {
1622     MSIHANDLE handle;
1623     UINT r;
1624
1625     TRACE("%s\n", debugstr_w(szPackage) );
1626
1627     r = MsiOpenDatabaseW( szPackage, MSIDBOPEN_READONLY, &handle );
1628     MsiCloseHandle( handle );
1629
1630     return r;
1631 }
1632
1633 static INSTALLSTATE WINAPI MSI_GetComponentPath(LPCWSTR szProduct, LPCWSTR szComponent,
1634                                                 awstring* lpPathBuf, LPDWORD pcchBuf)
1635 {
1636     WCHAR squished_pc[GUID_SIZE];
1637     WCHAR squished_comp[GUID_SIZE];
1638     HKEY hkey;
1639     LPWSTR path = NULL;
1640     INSTALLSTATE state;
1641     DWORD version;
1642
1643     static const WCHAR wininstaller[] = {
1644         'W','i','n','d','o','w','s','I','n','s','t','a','l','l','e','r',0};
1645
1646     TRACE("%s %s %p %p\n", debugstr_w(szProduct),
1647            debugstr_w(szComponent), lpPathBuf->str.w, pcchBuf);
1648
1649     if (!szProduct || !szComponent)
1650         return INSTALLSTATE_INVALIDARG;
1651
1652     if (lpPathBuf->str.w && !pcchBuf)
1653         return INSTALLSTATE_INVALIDARG;
1654
1655     if (!squash_guid(szProduct, squished_pc) ||
1656         !squash_guid(szComponent, squished_comp))
1657         return INSTALLSTATE_INVALIDARG;
1658
1659     state = INSTALLSTATE_UNKNOWN;
1660
1661     if (MSIREG_OpenLocalSystemComponentKey(szComponent, &hkey, FALSE) == ERROR_SUCCESS ||
1662         MSIREG_OpenUserDataComponentKey(szComponent, &hkey, FALSE) == ERROR_SUCCESS)
1663     {
1664         path = msi_reg_get_val_str(hkey, squished_pc);
1665         RegCloseKey(hkey);
1666
1667         state = INSTALLSTATE_ABSENT;
1668
1669         if ((MSIREG_OpenLocalSystemProductKey(szProduct, &hkey, FALSE) == ERROR_SUCCESS ||
1670             MSIREG_OpenUserDataProductKey(szProduct, &hkey, FALSE) == ERROR_SUCCESS) &&
1671             msi_reg_get_val_dword(hkey, wininstaller, &version) &&
1672             GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES)
1673         {
1674             RegCloseKey(hkey);
1675             state = INSTALLSTATE_LOCAL;
1676         }
1677     }
1678
1679     if (state != INSTALLSTATE_LOCAL &&
1680         (MSIREG_OpenUserProductsKey(szProduct, &hkey, FALSE) == ERROR_SUCCESS ||
1681          MSIREG_OpenLocalClassesProductKey(szProduct, &hkey, FALSE) == ERROR_SUCCESS))
1682     {
1683         RegCloseKey(hkey);
1684
1685         if (MSIREG_OpenLocalSystemComponentKey(szComponent, &hkey, FALSE) == ERROR_SUCCESS ||
1686             MSIREG_OpenUserDataComponentKey(szComponent, &hkey, FALSE) == ERROR_SUCCESS)
1687         {
1688             msi_free(path);
1689             path = msi_reg_get_val_str(hkey, squished_pc);
1690             RegCloseKey(hkey);
1691
1692             state = INSTALLSTATE_ABSENT;
1693
1694             if (GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES)
1695                 state = INSTALLSTATE_LOCAL;
1696         }
1697     }
1698
1699     if (!path)
1700         return INSTALLSTATE_UNKNOWN;
1701
1702     if (state == INSTALLSTATE_LOCAL && !*path)
1703         state = INSTALLSTATE_NOTUSED;
1704
1705     msi_strcpy_to_awstring(path, lpPathBuf, pcchBuf);
1706     msi_free(path);
1707     return state;
1708 }
1709
1710 /******************************************************************
1711  * MsiGetComponentPathW      [MSI.@]
1712  */
1713 INSTALLSTATE WINAPI MsiGetComponentPathW(LPCWSTR szProduct, LPCWSTR szComponent,
1714                                          LPWSTR lpPathBuf, LPDWORD pcchBuf)
1715 {
1716     awstring path;
1717
1718     path.unicode = TRUE;
1719     path.str.w = lpPathBuf;
1720
1721     return MSI_GetComponentPath( szProduct, szComponent, &path, pcchBuf );
1722 }
1723
1724 /******************************************************************
1725  * MsiGetComponentPathA      [MSI.@]
1726  */
1727 INSTALLSTATE WINAPI MsiGetComponentPathA(LPCSTR szProduct, LPCSTR szComponent,
1728                                          LPSTR lpPathBuf, LPDWORD pcchBuf)
1729 {
1730     LPWSTR szwProduct, szwComponent = NULL;
1731     INSTALLSTATE r = INSTALLSTATE_UNKNOWN;
1732     awstring path;
1733
1734     szwProduct = strdupAtoW( szProduct );
1735     if( szProduct && !szwProduct)
1736         goto end;
1737
1738     szwComponent = strdupAtoW( szComponent );
1739     if( szComponent && !szwComponent )
1740         goto end;
1741
1742     path.unicode = FALSE;
1743     path.str.a = lpPathBuf;
1744
1745     r = MSI_GetComponentPath( szwProduct, szwComponent, &path, pcchBuf );
1746
1747 end:
1748     msi_free( szwProduct );
1749     msi_free( szwComponent );
1750
1751     return r;
1752 }
1753
1754 /******************************************************************
1755  * MsiQueryFeatureStateA      [MSI.@]
1756  */
1757 INSTALLSTATE WINAPI MsiQueryFeatureStateA(LPCSTR szProduct, LPCSTR szFeature)
1758 {
1759     LPWSTR szwProduct = NULL, szwFeature= NULL;
1760     INSTALLSTATE rc = INSTALLSTATE_UNKNOWN;
1761
1762     szwProduct = strdupAtoW( szProduct );
1763     if ( szProduct && !szwProduct )
1764         goto end;
1765
1766     szwFeature = strdupAtoW( szFeature );
1767     if ( szFeature && !szwFeature )
1768         goto end;
1769
1770     rc = MsiQueryFeatureStateW(szwProduct, szwFeature);
1771
1772 end:
1773     msi_free( szwProduct);
1774     msi_free( szwFeature);
1775
1776     return rc;
1777 }
1778
1779 /******************************************************************
1780  * MsiQueryFeatureStateW      [MSI.@]
1781  *
1782  * Checks the state of a feature
1783  *
1784  * PARAMS
1785  *   szProduct     [I]  Product's GUID string
1786  *   szFeature     [I]  Feature's GUID string
1787  *
1788  * RETURNS
1789  *   INSTALLSTATE_LOCAL        Feature is installed and useable
1790  *   INSTALLSTATE_ABSENT       Feature is absent
1791  *   INSTALLSTATE_ADVERTISED   Feature should be installed on demand
1792  *   INSTALLSTATE_UNKNOWN      An error occurred
1793  *   INSTALLSTATE_INVALIDARG   One of the GUIDs was invalid
1794  *
1795  */
1796 INSTALLSTATE WINAPI MsiQueryFeatureStateW(LPCWSTR szProduct, LPCWSTR szFeature)
1797 {
1798     WCHAR squishProduct[33], comp[GUID_SIZE];
1799     GUID guid;
1800     LPWSTR components, p, parent_feature, path;
1801     UINT rc;
1802     HKEY hkey;
1803     INSTALLSTATE r;
1804     BOOL missing = FALSE;
1805
1806     TRACE("%s %s\n", debugstr_w(szProduct), debugstr_w(szFeature));
1807
1808     if (!szProduct || !szFeature)
1809         return INSTALLSTATE_INVALIDARG;
1810
1811     if (!squash_guid( szProduct, squishProduct ))
1812         return INSTALLSTATE_INVALIDARG;
1813
1814     /* check that it's installed at all */
1815     rc = MSIREG_OpenUserFeaturesKey(szProduct, &hkey, FALSE);
1816     if (rc != ERROR_SUCCESS)
1817         return INSTALLSTATE_UNKNOWN;
1818
1819     parent_feature = msi_reg_get_val_str( hkey, szFeature );
1820     RegCloseKey(hkey);
1821
1822     if (!parent_feature)
1823         return INSTALLSTATE_UNKNOWN;
1824
1825     r = (parent_feature[0] == 6) ? INSTALLSTATE_ABSENT : INSTALLSTATE_LOCAL;
1826     msi_free(parent_feature);
1827     if (r == INSTALLSTATE_ABSENT)
1828         return r;
1829
1830     /* now check if it's complete or advertised */
1831     rc = MSIREG_OpenUserDataFeaturesKey(szProduct, &hkey, FALSE);
1832     if (rc != ERROR_SUCCESS)
1833         return INSTALLSTATE_ADVERTISED;
1834
1835     components = msi_reg_get_val_str( hkey, szFeature );
1836     RegCloseKey(hkey);
1837
1838     TRACE("rc = %d buffer = %s\n", rc, debugstr_w(components));
1839
1840     if (!components)
1841         return INSTALLSTATE_ADVERTISED;
1842
1843     for( p = components; *p && *p != 2 ; p += 20)
1844     {
1845         if (!decode_base85_guid( p, &guid ))
1846         {
1847             if (p != components)
1848                 break;
1849
1850             msi_free(components);
1851             return INSTALLSTATE_BADCONFIG;
1852         }
1853
1854         StringFromGUID2(&guid, comp, GUID_SIZE);
1855         rc = MSIREG_OpenUserDataComponentKey(comp, &hkey, FALSE);
1856         if (rc != ERROR_SUCCESS)
1857         {
1858             msi_free(components);
1859             return INSTALLSTATE_ADVERTISED;
1860         }
1861
1862         path = msi_reg_get_val_str(hkey, squishProduct);
1863         if (!path)
1864             missing = TRUE;
1865
1866         msi_free(path);
1867     }
1868
1869     TRACE("%s %s -> %d\n", debugstr_w(szProduct), debugstr_w(szFeature), r);
1870     msi_free(components);
1871
1872     if (missing)
1873         return INSTALLSTATE_ADVERTISED;
1874
1875     return INSTALLSTATE_LOCAL;
1876 }
1877
1878 /******************************************************************
1879  * MsiGetFileVersionA         [MSI.@]
1880  */
1881 UINT WINAPI MsiGetFileVersionA(LPCSTR szFilePath, LPSTR lpVersionBuf,
1882                 LPDWORD pcchVersionBuf, LPSTR lpLangBuf, LPDWORD pcchLangBuf)
1883 {
1884     LPWSTR szwFilePath = NULL, lpwVersionBuff = NULL, lpwLangBuff = NULL;
1885     UINT ret = ERROR_OUTOFMEMORY;
1886
1887     if ((lpVersionBuf && !pcchVersionBuf) ||
1888         (lpLangBuf && !pcchLangBuf))
1889         return ERROR_INVALID_PARAMETER;
1890
1891     if( szFilePath )
1892     {
1893         szwFilePath = strdupAtoW( szFilePath );
1894         if( !szwFilePath )
1895             goto end;
1896     }
1897
1898     if( lpVersionBuf && pcchVersionBuf && *pcchVersionBuf )
1899     {
1900         lpwVersionBuff = msi_alloc(*pcchVersionBuf*sizeof(WCHAR));
1901         if( !lpwVersionBuff )
1902             goto end;
1903     }
1904
1905     if( lpLangBuf && pcchLangBuf && *pcchLangBuf )
1906     {
1907         lpwLangBuff = msi_alloc(*pcchLangBuf*sizeof(WCHAR));
1908         if( !lpwLangBuff )
1909             goto end;
1910     }
1911
1912     ret = MsiGetFileVersionW(szwFilePath, lpwVersionBuff, pcchVersionBuf,
1913                              lpwLangBuff, pcchLangBuf);
1914
1915     if( (ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA) && lpwVersionBuff )
1916         WideCharToMultiByte(CP_ACP, 0, lpwVersionBuff, -1,
1917                             lpVersionBuf, *pcchVersionBuf + 1, NULL, NULL);
1918     if( (ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA) && lpwLangBuff )
1919         WideCharToMultiByte(CP_ACP, 0, lpwLangBuff, -1,
1920                             lpLangBuf, *pcchLangBuf + 1, NULL, NULL);
1921
1922 end:
1923     msi_free(szwFilePath);
1924     msi_free(lpwVersionBuff);
1925     msi_free(lpwLangBuff);
1926
1927     return ret;
1928 }
1929
1930 /******************************************************************
1931  * MsiGetFileVersionW         [MSI.@]
1932  */
1933 UINT WINAPI MsiGetFileVersionW(LPCWSTR szFilePath, LPWSTR lpVersionBuf,
1934                 LPDWORD pcchVersionBuf, LPWSTR lpLangBuf, LPDWORD pcchLangBuf)
1935 {
1936     static const WCHAR szVersionResource[] = {'\\',0};
1937     static const WCHAR szVersionFormat[] = {
1938         '%','d','.','%','d','.','%','d','.','%','d',0};
1939     static const WCHAR szLangResource[] = {
1940         '\\','V','a','r','F','i','l','e','I','n','f','o','\\',
1941         'T','r','a','n','s','l','a','t','i','o','n',0};
1942     static const WCHAR szLangFormat[] = {'%','d',0};
1943     UINT ret = 0;
1944     DWORD dwVerLen, gle;
1945     LPVOID lpVer = NULL;
1946     VS_FIXEDFILEINFO *ffi;
1947     USHORT *lang;
1948     UINT puLen;
1949     WCHAR tmp[32];
1950
1951     TRACE("%s %p %d %p %d\n", debugstr_w(szFilePath),
1952           lpVersionBuf, pcchVersionBuf?*pcchVersionBuf:0,
1953           lpLangBuf, pcchLangBuf?*pcchLangBuf:0);
1954
1955     if ((lpVersionBuf && !pcchVersionBuf) ||
1956         (lpLangBuf && !pcchLangBuf))
1957         return ERROR_INVALID_PARAMETER;
1958
1959     dwVerLen = GetFileVersionInfoSizeW(szFilePath, NULL);
1960     if( !dwVerLen )
1961     {
1962         gle = GetLastError();
1963         if (gle == ERROR_BAD_PATHNAME)
1964             return ERROR_FILE_NOT_FOUND;
1965         else if (gle == ERROR_RESOURCE_DATA_NOT_FOUND)
1966             return ERROR_FILE_INVALID;
1967
1968         return gle;
1969     }
1970
1971     lpVer = msi_alloc(dwVerLen);
1972     if( !lpVer )
1973     {
1974         ret = ERROR_OUTOFMEMORY;
1975         goto end;
1976     }
1977
1978     if( !GetFileVersionInfoW(szFilePath, 0, dwVerLen, lpVer) )
1979     {
1980         ret = GetLastError();
1981         goto end;
1982     }
1983
1984     if (pcchVersionBuf)
1985     {
1986         if( VerQueryValueW(lpVer, szVersionResource, (LPVOID*)&ffi, &puLen) &&
1987             (puLen > 0) )
1988         {
1989             wsprintfW(tmp, szVersionFormat,
1990                   HIWORD(ffi->dwFileVersionMS), LOWORD(ffi->dwFileVersionMS),
1991                   HIWORD(ffi->dwFileVersionLS), LOWORD(ffi->dwFileVersionLS));
1992             if (lpVersionBuf) lstrcpynW(lpVersionBuf, tmp, *pcchVersionBuf);
1993
1994             if (lstrlenW(tmp) >= *pcchVersionBuf)
1995                 ret = ERROR_MORE_DATA;
1996
1997             *pcchVersionBuf = lstrlenW(tmp);
1998         }
1999         else
2000         {
2001             if (lpVersionBuf) *lpVersionBuf = 0;
2002             *pcchVersionBuf = 0;
2003         }
2004     }
2005
2006     if (pcchLangBuf)
2007     {
2008         if (VerQueryValueW(lpVer, szLangResource, (LPVOID*)&lang, &puLen) &&
2009             (puLen > 0))
2010         {
2011             wsprintfW(tmp, szLangFormat, *lang);
2012             if (lpLangBuf) lstrcpynW(lpLangBuf, tmp, *pcchLangBuf);
2013
2014             if (lstrlenW(tmp) >= *pcchLangBuf)
2015                 ret = ERROR_MORE_DATA;
2016
2017             *pcchLangBuf = lstrlenW(tmp);
2018         }
2019         else
2020         {
2021             if (lpLangBuf) *lpLangBuf = 0;
2022             *pcchLangBuf = 0;
2023         }
2024     }
2025
2026 end:
2027     msi_free(lpVer);
2028     return ret;
2029 }
2030
2031 /***********************************************************************
2032  * MsiGetFeatureUsageW           [MSI.@]
2033  */
2034 UINT WINAPI MsiGetFeatureUsageW( LPCWSTR szProduct, LPCWSTR szFeature,
2035                                  LPDWORD pdwUseCount, LPWORD pwDateUsed )
2036 {
2037     FIXME("%s %s %p %p\n",debugstr_w(szProduct), debugstr_w(szFeature),
2038           pdwUseCount, pwDateUsed);
2039     return ERROR_CALL_NOT_IMPLEMENTED;
2040 }
2041
2042 /***********************************************************************
2043  * MsiGetFeatureUsageA           [MSI.@]
2044  */
2045 UINT WINAPI MsiGetFeatureUsageA( LPCSTR szProduct, LPCSTR szFeature,
2046                                  LPDWORD pdwUseCount, LPWORD pwDateUsed )
2047 {
2048     LPWSTR prod = NULL, feat = NULL;
2049     UINT ret = ERROR_OUTOFMEMORY;
2050
2051     TRACE("%s %s %p %p\n", debugstr_a(szProduct), debugstr_a(szFeature),
2052           pdwUseCount, pwDateUsed);
2053
2054     prod = strdupAtoW( szProduct );
2055     if (szProduct && !prod)
2056         goto end;
2057
2058     feat = strdupAtoW( szFeature );
2059     if (szFeature && !feat)
2060         goto end;
2061
2062     ret = MsiGetFeatureUsageW( prod, feat, pdwUseCount, pwDateUsed );
2063
2064 end:
2065     msi_free( prod );
2066     msi_free( feat );
2067
2068     return ret;
2069 }
2070
2071 /***********************************************************************
2072  * MsiUseFeatureExW           [MSI.@]
2073  */
2074 INSTALLSTATE WINAPI MsiUseFeatureExW( LPCWSTR szProduct, LPCWSTR szFeature,
2075                                       DWORD dwInstallMode, DWORD dwReserved )
2076 {
2077     INSTALLSTATE state;
2078
2079     TRACE("%s %s %i %i\n", debugstr_w(szProduct), debugstr_w(szFeature),
2080           dwInstallMode, dwReserved);
2081
2082     state = MsiQueryFeatureStateW( szProduct, szFeature );
2083
2084     if (dwReserved)
2085         return INSTALLSTATE_INVALIDARG;
2086
2087     if (state == INSTALLSTATE_LOCAL && dwInstallMode != INSTALLMODE_NODETECTION)
2088     {
2089         FIXME("mark product %s feature %s as used\n",
2090               debugstr_w(szProduct), debugstr_w(szFeature) );
2091     }
2092
2093     return state;
2094 }
2095
2096 /***********************************************************************
2097  * MsiUseFeatureExA           [MSI.@]
2098  */
2099 INSTALLSTATE WINAPI MsiUseFeatureExA( LPCSTR szProduct, LPCSTR szFeature,
2100                                       DWORD dwInstallMode, DWORD dwReserved )
2101 {
2102     INSTALLSTATE ret = INSTALLSTATE_UNKNOWN;
2103     LPWSTR prod = NULL, feat = NULL;
2104
2105     TRACE("%s %s %i %i\n", debugstr_a(szProduct), debugstr_a(szFeature),
2106           dwInstallMode, dwReserved);
2107
2108     prod = strdupAtoW( szProduct );
2109     if (szProduct && !prod)
2110         goto end;
2111
2112     feat = strdupAtoW( szFeature );
2113     if (szFeature && !feat)
2114         goto end;
2115
2116     ret = MsiUseFeatureExW( prod, feat, dwInstallMode, dwReserved );
2117
2118 end:
2119     msi_free( prod );
2120     msi_free( feat );
2121
2122     return ret;
2123 }
2124
2125 /***********************************************************************
2126  * MsiUseFeatureW             [MSI.@]
2127  */
2128 INSTALLSTATE WINAPI MsiUseFeatureW( LPCWSTR szProduct, LPCWSTR szFeature )
2129 {
2130     return MsiUseFeatureExW(szProduct, szFeature, 0, 0);
2131 }
2132
2133 /***********************************************************************
2134  * MsiUseFeatureA             [MSI.@]
2135  */
2136 INSTALLSTATE WINAPI MsiUseFeatureA( LPCSTR szProduct, LPCSTR szFeature )
2137 {
2138     return MsiUseFeatureExA(szProduct, szFeature, 0, 0);
2139 }
2140
2141 /***********************************************************************
2142  * MSI_ProvideQualifiedComponentEx [internal]
2143  */
2144 static UINT WINAPI MSI_ProvideQualifiedComponentEx(LPCWSTR szComponent,
2145                 LPCWSTR szQualifier, DWORD dwInstallMode, LPCWSTR szProduct,
2146                 DWORD Unused1, DWORD Unused2, awstring *lpPathBuf,
2147                 LPDWORD pcchPathBuf)
2148 {
2149     WCHAR product[MAX_FEATURE_CHARS+1], component[MAX_FEATURE_CHARS+1],
2150           feature[MAX_FEATURE_CHARS+1];
2151     LPWSTR info;
2152     HKEY hkey;
2153     DWORD sz;
2154     UINT rc;
2155
2156     TRACE("%s %s %i %s %i %i %p %p\n", debugstr_w(szComponent),
2157           debugstr_w(szQualifier), dwInstallMode, debugstr_w(szProduct),
2158           Unused1, Unused2, lpPathBuf, pcchPathBuf);
2159
2160     rc = MSIREG_OpenUserComponentsKey(szComponent, &hkey, FALSE);
2161     if (rc != ERROR_SUCCESS)
2162         return ERROR_INDEX_ABSENT;
2163
2164     info = msi_reg_get_val_str( hkey, szQualifier );
2165     RegCloseKey(hkey);
2166
2167     if (!info)
2168         return ERROR_INDEX_ABSENT;
2169
2170     MsiDecomposeDescriptorW(info, product, feature, component, &sz);
2171
2172     if (!szProduct)
2173         rc = MSI_GetComponentPath(product, component, lpPathBuf, pcchPathBuf);
2174     else
2175         rc = MSI_GetComponentPath(szProduct, component, lpPathBuf, pcchPathBuf);
2176
2177     msi_free( info );
2178
2179     if (rc != INSTALLSTATE_LOCAL)
2180         return ERROR_FILE_NOT_FOUND;
2181
2182     return ERROR_SUCCESS;
2183 }
2184
2185 /***********************************************************************
2186  * MsiProvideQualifiedComponentExW [MSI.@]
2187  */
2188 UINT WINAPI MsiProvideQualifiedComponentExW(LPCWSTR szComponent,
2189                 LPCWSTR szQualifier, DWORD dwInstallMode, LPCWSTR szProduct,
2190                 DWORD Unused1, DWORD Unused2, LPWSTR lpPathBuf,
2191                 LPDWORD pcchPathBuf)
2192 {
2193     awstring path;
2194
2195     path.unicode = TRUE;
2196     path.str.w = lpPathBuf;
2197
2198     return MSI_ProvideQualifiedComponentEx(szComponent, szQualifier,
2199             dwInstallMode, szProduct, Unused1, Unused2, &path, pcchPathBuf);
2200 }
2201
2202 /***********************************************************************
2203  * MsiProvideQualifiedComponentExA [MSI.@]
2204  */
2205 UINT WINAPI MsiProvideQualifiedComponentExA(LPCSTR szComponent,
2206                 LPCSTR szQualifier, DWORD dwInstallMode, LPCSTR szProduct,
2207                 DWORD Unused1, DWORD Unused2, LPSTR lpPathBuf,
2208                 LPDWORD pcchPathBuf)
2209 {
2210     LPWSTR szwComponent, szwQualifier = NULL, szwProduct = NULL;
2211     UINT r = ERROR_OUTOFMEMORY;
2212     awstring path;
2213
2214     TRACE("%s %s %u %s %u %u %p %p\n", debugstr_a(szComponent),
2215           debugstr_a(szQualifier), dwInstallMode, debugstr_a(szProduct),
2216           Unused1, Unused2, lpPathBuf, pcchPathBuf);
2217
2218     szwComponent = strdupAtoW( szComponent );
2219     if (szComponent && !szwComponent)
2220         goto end;
2221
2222     szwQualifier = strdupAtoW( szQualifier );
2223     if (szQualifier && !szwQualifier)
2224         goto end;
2225
2226     szwProduct = strdupAtoW( szProduct );
2227     if (szProduct && !szwProduct)
2228         goto end;
2229
2230     path.unicode = FALSE;
2231     path.str.a = lpPathBuf;
2232
2233     r = MSI_ProvideQualifiedComponentEx(szwComponent, szwQualifier,
2234                               dwInstallMode, szwProduct, Unused1,
2235                               Unused2, &path, pcchPathBuf);
2236 end:
2237     msi_free(szwProduct);
2238     msi_free(szwComponent);
2239     msi_free(szwQualifier);
2240
2241     return r;
2242 }
2243
2244 /***********************************************************************
2245  * MsiProvideQualifiedComponentW [MSI.@]
2246  */
2247 UINT WINAPI MsiProvideQualifiedComponentW( LPCWSTR szComponent,
2248                 LPCWSTR szQualifier, DWORD dwInstallMode, LPWSTR lpPathBuf,
2249                 LPDWORD pcchPathBuf)
2250 {
2251     return MsiProvideQualifiedComponentExW(szComponent, szQualifier, 
2252                     dwInstallMode, NULL, 0, 0, lpPathBuf, pcchPathBuf);
2253 }
2254
2255 /***********************************************************************
2256  * MsiProvideQualifiedComponentA [MSI.@]
2257  */
2258 UINT WINAPI MsiProvideQualifiedComponentA( LPCSTR szComponent,
2259                 LPCSTR szQualifier, DWORD dwInstallMode, LPSTR lpPathBuf,
2260                 LPDWORD pcchPathBuf)
2261 {
2262     return MsiProvideQualifiedComponentExA(szComponent, szQualifier,
2263                               dwInstallMode, NULL, 0, 0, lpPathBuf, pcchPathBuf);
2264 }
2265
2266 /***********************************************************************
2267  * MSI_GetUserInfo [internal]
2268  */
2269 static USERINFOSTATE WINAPI MSI_GetUserInfo(LPCWSTR szProduct,
2270                 awstring *lpUserNameBuf, LPDWORD pcchUserNameBuf,
2271                 awstring *lpOrgNameBuf, LPDWORD pcchOrgNameBuf,
2272                 awstring *lpSerialBuf, LPDWORD pcchSerialBuf)
2273 {
2274     HKEY hkey;
2275     LPWSTR user, org, serial;
2276     UINT r;
2277     USERINFOSTATE state;
2278
2279     TRACE("%s %p %p %p %p %p %p\n",debugstr_w(szProduct), lpUserNameBuf,
2280           pcchUserNameBuf, lpOrgNameBuf, pcchOrgNameBuf, lpSerialBuf,
2281           pcchSerialBuf);
2282
2283     if (!szProduct)
2284         return USERINFOSTATE_INVALIDARG;
2285
2286     r = MSIREG_OpenUninstallKey(szProduct, &hkey, FALSE);
2287     if (r != ERROR_SUCCESS)
2288         return USERINFOSTATE_UNKNOWN;
2289
2290     user = msi_reg_get_val_str( hkey, INSTALLPROPERTY_REGOWNERW );
2291     org = msi_reg_get_val_str( hkey, INSTALLPROPERTY_REGCOMPANYW );
2292     serial = msi_reg_get_val_str( hkey, INSTALLPROPERTY_PRODUCTIDW );
2293
2294     RegCloseKey(hkey);
2295
2296     state = USERINFOSTATE_PRESENT;
2297
2298     if (user)
2299     {
2300         r = msi_strcpy_to_awstring( user, lpUserNameBuf, pcchUserNameBuf );
2301         if (r == ERROR_MORE_DATA)
2302             state = USERINFOSTATE_MOREDATA;
2303     }
2304     else
2305         state = USERINFOSTATE_ABSENT;
2306     if (org)
2307     {
2308         r = msi_strcpy_to_awstring( org, lpOrgNameBuf, pcchOrgNameBuf );
2309         if (r == ERROR_MORE_DATA && state == USERINFOSTATE_PRESENT)
2310             state = USERINFOSTATE_MOREDATA;
2311     }
2312     /* msdn states: The user information is considered to be present even in the absence of a company name. */
2313     if (serial)
2314     {
2315         r = msi_strcpy_to_awstring( serial, lpSerialBuf, pcchSerialBuf );
2316         if (r == ERROR_MORE_DATA && state == USERINFOSTATE_PRESENT)
2317             state = USERINFOSTATE_MOREDATA;
2318     }
2319     else
2320         state = USERINFOSTATE_ABSENT;
2321
2322     msi_free( user );
2323     msi_free( org );
2324     msi_free( serial );
2325
2326     return state;
2327 }
2328
2329 /***********************************************************************
2330  * MsiGetUserInfoW [MSI.@]
2331  */
2332 USERINFOSTATE WINAPI MsiGetUserInfoW(LPCWSTR szProduct,
2333                 LPWSTR lpUserNameBuf, LPDWORD pcchUserNameBuf,
2334                 LPWSTR lpOrgNameBuf, LPDWORD pcchOrgNameBuf,
2335                 LPWSTR lpSerialBuf, LPDWORD pcchSerialBuf)
2336 {
2337     awstring user, org, serial;
2338
2339     user.unicode = TRUE;
2340     user.str.w = lpUserNameBuf;
2341     org.unicode = TRUE;
2342     org.str.w = lpOrgNameBuf;
2343     serial.unicode = TRUE;
2344     serial.str.w = lpSerialBuf;
2345
2346     return MSI_GetUserInfo( szProduct, &user, pcchUserNameBuf,
2347                             &org, pcchOrgNameBuf,
2348                             &serial, pcchSerialBuf );
2349 }
2350
2351 USERINFOSTATE WINAPI MsiGetUserInfoA(LPCSTR szProduct,
2352                 LPSTR lpUserNameBuf, LPDWORD pcchUserNameBuf,
2353                 LPSTR lpOrgNameBuf, LPDWORD pcchOrgNameBuf,
2354                 LPSTR lpSerialBuf, LPDWORD pcchSerialBuf)
2355 {
2356     awstring user, org, serial;
2357     LPWSTR prod;
2358     UINT r;
2359
2360     prod = strdupAtoW( szProduct );
2361     if (szProduct && !prod)
2362         return ERROR_OUTOFMEMORY;
2363
2364     user.unicode = FALSE;
2365     user.str.a = lpUserNameBuf;
2366     org.unicode = FALSE;
2367     org.str.a = lpOrgNameBuf;
2368     serial.unicode = FALSE;
2369     serial.str.a = lpSerialBuf;
2370
2371     r = MSI_GetUserInfo( prod, &user, pcchUserNameBuf,
2372                          &org, pcchOrgNameBuf,
2373                          &serial, pcchSerialBuf );
2374
2375     msi_free( prod );
2376
2377     return r;
2378 }
2379
2380 UINT WINAPI MsiCollectUserInfoW(LPCWSTR szProduct)
2381 {
2382     MSIHANDLE handle;
2383     UINT rc;
2384     MSIPACKAGE *package;
2385     static const WCHAR szFirstRun[] = {'F','i','r','s','t','R','u','n',0};
2386
2387     TRACE("(%s)\n",debugstr_w(szProduct));
2388
2389     rc = MsiOpenProductW(szProduct,&handle);
2390     if (rc != ERROR_SUCCESS)
2391         return ERROR_INVALID_PARAMETER;
2392
2393     package = msihandle2msiinfo(handle, MSIHANDLETYPE_PACKAGE);
2394     rc = ACTION_PerformUIAction(package, szFirstRun, -1);
2395     msiobj_release( &package->hdr );
2396
2397     MsiCloseHandle(handle);
2398
2399     return rc;
2400 }
2401
2402 UINT WINAPI MsiCollectUserInfoA(LPCSTR szProduct)
2403 {
2404     MSIHANDLE handle;
2405     UINT rc;
2406     MSIPACKAGE *package;
2407     static const WCHAR szFirstRun[] = {'F','i','r','s','t','R','u','n',0};
2408
2409     TRACE("(%s)\n",debugstr_a(szProduct));
2410
2411     rc = MsiOpenProductA(szProduct,&handle);
2412     if (rc != ERROR_SUCCESS)
2413         return ERROR_INVALID_PARAMETER;
2414
2415     package = msihandle2msiinfo(handle, MSIHANDLETYPE_PACKAGE);
2416     rc = ACTION_PerformUIAction(package, szFirstRun, -1);
2417     msiobj_release( &package->hdr );
2418
2419     MsiCloseHandle(handle);
2420
2421     return rc;
2422 }
2423
2424 /***********************************************************************
2425  * MsiConfigureFeatureA            [MSI.@]
2426  */
2427 UINT WINAPI MsiConfigureFeatureA(LPCSTR szProduct, LPCSTR szFeature, INSTALLSTATE eInstallState)
2428 {
2429     LPWSTR prod, feat = NULL;
2430     UINT r = ERROR_OUTOFMEMORY;
2431
2432     TRACE("%s %s %i\n", debugstr_a(szProduct), debugstr_a(szFeature), eInstallState);
2433
2434     prod = strdupAtoW( szProduct );
2435     if (szProduct && !prod)
2436         goto end;
2437
2438     feat = strdupAtoW( szFeature );
2439     if (szFeature && !feat)
2440         goto end;
2441
2442     r = MsiConfigureFeatureW(prod, feat, eInstallState);
2443
2444 end:
2445     msi_free(feat);
2446     msi_free(prod);
2447
2448     return r;
2449 }
2450
2451 /***********************************************************************
2452  * MsiConfigureFeatureW            [MSI.@]
2453  */
2454 UINT WINAPI MsiConfigureFeatureW(LPCWSTR szProduct, LPCWSTR szFeature, INSTALLSTATE eInstallState)
2455 {
2456     static const WCHAR szCostInit[] = { 'C','o','s','t','I','n','i','t','i','a','l','i','z','e',0 };
2457     MSIPACKAGE *package = NULL;
2458     UINT r;
2459     WCHAR sourcepath[MAX_PATH], filename[MAX_PATH];
2460     DWORD sz;
2461
2462     TRACE("%s %s %i\n", debugstr_w(szProduct), debugstr_w(szFeature), eInstallState);
2463
2464     if (!szProduct || !szFeature)
2465         return ERROR_INVALID_PARAMETER;
2466
2467     switch (eInstallState)
2468     {
2469     case INSTALLSTATE_DEFAULT:
2470         /* FIXME: how do we figure out the default location? */
2471         eInstallState = INSTALLSTATE_LOCAL;
2472         break;
2473     case INSTALLSTATE_LOCAL:
2474     case INSTALLSTATE_SOURCE:
2475     case INSTALLSTATE_ABSENT:
2476     case INSTALLSTATE_ADVERTISED:
2477         break;
2478     default:
2479         return ERROR_INVALID_PARAMETER;
2480     }
2481
2482     r = MSI_OpenProductW( szProduct, &package );
2483     if (r != ERROR_SUCCESS)
2484         return r;
2485
2486     sz = sizeof(sourcepath);
2487     MsiSourceListGetInfoW(szProduct, NULL, MSIINSTALLCONTEXT_USERUNMANAGED,
2488                 MSICODE_PRODUCT, INSTALLPROPERTY_LASTUSEDSOURCEW, sourcepath, &sz);
2489
2490     sz = sizeof(filename);
2491     MsiSourceListGetInfoW(szProduct, NULL, MSIINSTALLCONTEXT_USERUNMANAGED,
2492                 MSICODE_PRODUCT, INSTALLPROPERTY_PACKAGENAMEW, filename, &sz);
2493
2494     lstrcatW( sourcepath, filename );
2495
2496     MsiSetInternalUI( INSTALLUILEVEL_BASIC, NULL );
2497
2498     r = ACTION_PerformUIAction( package, szCostInit, -1 );
2499     if (r != ERROR_SUCCESS)
2500         goto end;
2501
2502     r = MSI_SetFeatureStateW( package, szFeature, eInstallState);
2503     if (r != ERROR_SUCCESS)
2504         goto end;
2505
2506     r = MSI_InstallPackage( package, sourcepath, NULL );
2507
2508 end:
2509     msiobj_release( &package->hdr );
2510
2511     return r;
2512 }
2513
2514 /***********************************************************************
2515  * MsiCreateAndVerifyInstallerDirectory [MSI.@]
2516  *
2517  * Notes: undocumented
2518  */
2519 UINT WINAPI MsiCreateAndVerifyInstallerDirectory(DWORD dwReserved)
2520 {
2521     WCHAR path[MAX_PATH];
2522
2523     TRACE("%d\n", dwReserved);
2524
2525     if (dwReserved)
2526     {
2527         FIXME("dwReserved=%d\n", dwReserved);
2528         return ERROR_INVALID_PARAMETER;
2529     }
2530
2531     if (!GetWindowsDirectoryW(path, MAX_PATH))
2532         return ERROR_FUNCTION_FAILED;
2533
2534     lstrcatW(path, installerW);
2535
2536     if (!CreateDirectoryW(path, NULL))
2537         return ERROR_FUNCTION_FAILED;
2538
2539     return ERROR_SUCCESS;
2540 }
2541
2542 /***********************************************************************
2543  * MsiGetShortcutTargetA           [MSI.@]
2544  */
2545 UINT WINAPI MsiGetShortcutTargetA( LPCSTR szShortcutTarget,
2546                                    LPSTR szProductCode, LPSTR szFeatureId,
2547                                    LPSTR szComponentCode )
2548 {
2549     LPWSTR target;
2550     const int len = MAX_FEATURE_CHARS+1;
2551     WCHAR product[MAX_FEATURE_CHARS+1], feature[MAX_FEATURE_CHARS+1], component[MAX_FEATURE_CHARS+1];
2552     UINT r;
2553
2554     target = strdupAtoW( szShortcutTarget );
2555     if (szShortcutTarget && !target )
2556         return ERROR_OUTOFMEMORY;
2557     product[0] = 0;
2558     feature[0] = 0;
2559     component[0] = 0;
2560     r = MsiGetShortcutTargetW( target, product, feature, component );
2561     msi_free( target );
2562     if (r == ERROR_SUCCESS)
2563     {
2564         WideCharToMultiByte( CP_ACP, 0, product, -1, szProductCode, len, NULL, NULL );
2565         WideCharToMultiByte( CP_ACP, 0, feature, -1, szFeatureId, len, NULL, NULL );
2566         WideCharToMultiByte( CP_ACP, 0, component, -1, szComponentCode, len, NULL, NULL );
2567     }
2568     return r;
2569 }
2570
2571 /***********************************************************************
2572  * MsiGetShortcutTargetW           [MSI.@]
2573  */
2574 UINT WINAPI MsiGetShortcutTargetW( LPCWSTR szShortcutTarget,
2575                                    LPWSTR szProductCode, LPWSTR szFeatureId,
2576                                    LPWSTR szComponentCode )
2577 {
2578     IShellLinkDataList *dl = NULL;
2579     IPersistFile *pf = NULL;
2580     LPEXP_DARWIN_LINK darwin = NULL;
2581     HRESULT r, init;
2582
2583     TRACE("%s %p %p %p\n", debugstr_w(szShortcutTarget),
2584           szProductCode, szFeatureId, szComponentCode );
2585
2586     init = CoInitialize(NULL);
2587
2588     r = CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
2589                           &IID_IPersistFile, (LPVOID*) &pf );
2590     if( SUCCEEDED( r ) )
2591     {
2592         r = IPersistFile_Load( pf, szShortcutTarget,
2593                                STGM_READ | STGM_SHARE_DENY_WRITE );
2594         if( SUCCEEDED( r ) )
2595         {
2596             r = IPersistFile_QueryInterface( pf, &IID_IShellLinkDataList,
2597                                              (LPVOID*) &dl );
2598             if( SUCCEEDED( r ) )
2599             {
2600                 IShellLinkDataList_CopyDataBlock( dl, EXP_DARWIN_ID_SIG,
2601                                                   (LPVOID) &darwin );
2602                 IShellLinkDataList_Release( dl );
2603             }
2604         }
2605         IPersistFile_Release( pf );
2606     }
2607
2608     if (SUCCEEDED(init))
2609         CoUninitialize();
2610
2611     TRACE("darwin = %p\n", darwin);
2612
2613     if (darwin)
2614     {
2615         DWORD sz;
2616         UINT ret;
2617
2618         ret = MsiDecomposeDescriptorW( darwin->szwDarwinID,
2619                   szProductCode, szFeatureId, szComponentCode, &sz );
2620         LocalFree( darwin );
2621         return ret;
2622     }
2623
2624     return ERROR_FUNCTION_FAILED;
2625 }
2626
2627 UINT WINAPI MsiReinstallFeatureW( LPCWSTR szProduct, LPCWSTR szFeature,
2628                                   DWORD dwReinstallMode )
2629 {
2630     MSIPACKAGE* package = NULL;
2631     UINT r;
2632     WCHAR sourcepath[MAX_PATH];
2633     WCHAR filename[MAX_PATH];
2634     static const WCHAR szLogVerbose[] = {
2635         ' ','L','O','G','V','E','R','B','O','S','E',0 };
2636     static const WCHAR szInstalled[] = { 'I','n','s','t','a','l','l','e','d',0};
2637     static const WCHAR szReinstall[] = {'R','E','I','N','S','T','A','L','L',0};
2638     static const WCHAR szReinstallMode[] = {'R','E','I','N','S','T','A','L','L','M','O','D','E',0};
2639     static const WCHAR szOne[] = {'1',0};
2640     WCHAR reinstallmode[11];
2641     LPWSTR ptr;
2642     DWORD sz;
2643
2644     FIXME("%s %s %i\n", debugstr_w(szProduct), debugstr_w(szFeature),
2645                            dwReinstallMode);
2646
2647     ptr = reinstallmode;
2648
2649     if (dwReinstallMode & REINSTALLMODE_FILEMISSING)
2650         *ptr++ = 'p';
2651     if (dwReinstallMode & REINSTALLMODE_FILEOLDERVERSION)
2652         *ptr++ = 'o';
2653     if (dwReinstallMode & REINSTALLMODE_FILEEQUALVERSION)
2654         *ptr++ = 'w';
2655     if (dwReinstallMode & REINSTALLMODE_FILEEXACT)
2656         *ptr++ = 'd';
2657     if (dwReinstallMode & REINSTALLMODE_FILEVERIFY)
2658         *ptr++ = 'c';
2659     if (dwReinstallMode & REINSTALLMODE_FILEREPLACE)
2660         *ptr++ = 'a';
2661     if (dwReinstallMode & REINSTALLMODE_USERDATA)
2662         *ptr++ = 'u';
2663     if (dwReinstallMode & REINSTALLMODE_MACHINEDATA)
2664         *ptr++ = 'm';
2665     if (dwReinstallMode & REINSTALLMODE_SHORTCUT)
2666         *ptr++ = 's';
2667     if (dwReinstallMode & REINSTALLMODE_PACKAGE)
2668         *ptr++ = 'v';
2669     *ptr = 0;
2670     
2671     sz = sizeof(sourcepath);
2672     MsiSourceListGetInfoW(szProduct, NULL, MSIINSTALLCONTEXT_USERUNMANAGED,
2673             MSICODE_PRODUCT, INSTALLPROPERTY_LASTUSEDSOURCEW, sourcepath, &sz);
2674
2675     sz = sizeof(filename);
2676     MsiSourceListGetInfoW(szProduct, NULL, MSIINSTALLCONTEXT_USERUNMANAGED,
2677             MSICODE_PRODUCT, INSTALLPROPERTY_PACKAGENAMEW, filename, &sz);
2678
2679     lstrcatW( sourcepath, filename );
2680
2681     if (dwReinstallMode & REINSTALLMODE_PACKAGE)
2682         r = MSI_OpenPackageW( sourcepath, &package );
2683     else
2684         r = MSI_OpenProductW( szProduct, &package );
2685
2686     if (r != ERROR_SUCCESS)
2687         return r;
2688
2689     MSI_SetPropertyW( package, szReinstallMode, reinstallmode );
2690     MSI_SetPropertyW( package, szInstalled, szOne );
2691     MSI_SetPropertyW( package, szLogVerbose, szOne );
2692     MSI_SetPropertyW( package, szReinstall, szFeature );
2693
2694     r = MSI_InstallPackage( package, sourcepath, NULL );
2695
2696     msiobj_release( &package->hdr );
2697
2698     return r;
2699 }
2700
2701 UINT WINAPI MsiReinstallFeatureA( LPCSTR szProduct, LPCSTR szFeature,
2702                                   DWORD dwReinstallMode )
2703 {
2704     LPWSTR wszProduct;
2705     LPWSTR wszFeature;
2706     UINT rc;
2707
2708     TRACE("%s %s %i\n", debugstr_a(szProduct), debugstr_a(szFeature),
2709                            dwReinstallMode);
2710
2711     wszProduct = strdupAtoW(szProduct);
2712     wszFeature = strdupAtoW(szFeature);
2713
2714     rc = MsiReinstallFeatureW(wszProduct, wszFeature, dwReinstallMode);
2715
2716     msi_free(wszProduct);
2717     msi_free(wszFeature);
2718     return rc;
2719 }
2720
2721 typedef struct
2722 {
2723     unsigned int i[2];
2724     unsigned int buf[4];
2725     unsigned char in[64];
2726     unsigned char digest[16];
2727 } MD5_CTX;
2728
2729 extern VOID WINAPI MD5Init( MD5_CTX *);
2730 extern VOID WINAPI MD5Update( MD5_CTX *, const unsigned char *, unsigned int );
2731 extern VOID WINAPI MD5Final( MD5_CTX *);
2732
2733 /***********************************************************************
2734  * MsiGetFileHashW            [MSI.@]
2735  */
2736 UINT WINAPI MsiGetFileHashW( LPCWSTR szFilePath, DWORD dwOptions,
2737                              PMSIFILEHASHINFO pHash )
2738 {
2739     HANDLE handle, mapping;
2740     void *p;
2741     DWORD length;
2742     UINT r = ERROR_FUNCTION_FAILED;
2743
2744     TRACE("%s %08x %p\n", debugstr_w(szFilePath), dwOptions, pHash );
2745
2746     if (!szFilePath)
2747         return ERROR_INVALID_PARAMETER;
2748
2749     if (!*szFilePath)
2750         return ERROR_PATH_NOT_FOUND;
2751
2752     if (dwOptions)
2753         return ERROR_INVALID_PARAMETER;
2754     if (!pHash)
2755         return ERROR_INVALID_PARAMETER;
2756     if (pHash->dwFileHashInfoSize < sizeof *pHash)
2757         return ERROR_INVALID_PARAMETER;
2758
2759     handle = CreateFileW( szFilePath, GENERIC_READ,
2760                           FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, NULL );
2761     if (handle == INVALID_HANDLE_VALUE)
2762         return ERROR_FILE_NOT_FOUND;
2763
2764     length = GetFileSize( handle, NULL );
2765
2766     mapping = CreateFileMappingW( handle, NULL, PAGE_READONLY, 0, 0, NULL );
2767     if (mapping)
2768     {
2769         p = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, length );
2770         if (p)
2771         {
2772             MD5_CTX ctx;
2773
2774             MD5Init( &ctx );
2775             MD5Update( &ctx, p, length );
2776             MD5Final( &ctx );
2777             UnmapViewOfFile( p );
2778
2779             memcpy( pHash->dwData, &ctx.digest, sizeof pHash->dwData );
2780             r = ERROR_SUCCESS;
2781         }
2782         CloseHandle( mapping );
2783     }
2784     CloseHandle( handle );
2785
2786     return r;
2787 }
2788
2789 /***********************************************************************
2790  * MsiGetFileHashA            [MSI.@]
2791  */
2792 UINT WINAPI MsiGetFileHashA( LPCSTR szFilePath, DWORD dwOptions,
2793                              PMSIFILEHASHINFO pHash )
2794 {
2795     LPWSTR file;
2796     UINT r;
2797
2798     TRACE("%s %08x %p\n", debugstr_a(szFilePath), dwOptions, pHash );
2799
2800     file = strdupAtoW( szFilePath );
2801     if (szFilePath && !file)
2802         return ERROR_OUTOFMEMORY;
2803
2804     r = MsiGetFileHashW( file, dwOptions, pHash );
2805     msi_free( file );
2806     return r;
2807 }
2808
2809 /***********************************************************************
2810  * MsiAdvertiseScriptW        [MSI.@]
2811  */
2812 UINT WINAPI MsiAdvertiseScriptW( LPCWSTR szScriptFile, DWORD dwFlags,
2813                                  PHKEY phRegData, BOOL fRemoveItems )
2814 {
2815     FIXME("%s %08x %p %d\n",
2816           debugstr_w( szScriptFile ), dwFlags, phRegData, fRemoveItems );
2817     return ERROR_CALL_NOT_IMPLEMENTED;
2818 }
2819
2820 /***********************************************************************
2821  * MsiAdvertiseScriptA        [MSI.@]
2822  */
2823 UINT WINAPI MsiAdvertiseScriptA( LPCSTR szScriptFile, DWORD dwFlags,
2824                                  PHKEY phRegData, BOOL fRemoveItems )
2825 {
2826     FIXME("%s %08x %p %d\n",
2827           debugstr_a( szScriptFile ), dwFlags, phRegData, fRemoveItems );
2828     return ERROR_CALL_NOT_IMPLEMENTED;
2829 }
2830
2831 /***********************************************************************
2832  * MsiIsProductElevatedW        [MSI.@]
2833  */
2834 UINT WINAPI MsiIsProductElevatedW( LPCWSTR szProduct, BOOL *pfElevated )
2835 {
2836     FIXME("%s %p - stub\n",
2837           debugstr_w( szProduct ), pfElevated );
2838     *pfElevated = TRUE;
2839     return ERROR_SUCCESS;
2840 }
2841
2842 /***********************************************************************
2843  * MsiIsProductElevatedA        [MSI.@]
2844  */
2845 UINT WINAPI MsiIsProductElevatedA( LPCSTR szProduct, BOOL *pfElevated )
2846 {
2847     FIXME("%s %p - stub\n",
2848           debugstr_a( szProduct ), pfElevated );
2849     *pfElevated = TRUE;
2850     return ERROR_SUCCESS;
2851 }