msi: Return ERROR_INVALID_PARAMETER if a string pointer is non-NULL and the size...
[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_USERMANAGED, 
369             MSICODE_PRODUCT, INSTALLPROPERTY_LASTUSEDSOURCEW, sourcepath,
370             &sz);
371
372     sz = sizeof(filename);
373     MsiSourceListGetInfoW(szProduct, NULL, MSIINSTALLCONTEXT_USERMANAGED, 
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 UINT WINAPI MSI_GetProductInfo(LPCWSTR szProduct, LPCWSTR szAttribute,
565                                       awstring *szValue, LPDWORD pcchValueBuf)
566 {
567     UINT r;
568     HKEY hkey;
569     LPWSTR val = NULL;
570
571     TRACE("%s %s %p %p\n", debugstr_w(szProduct),
572           debugstr_w(szAttribute), szValue, pcchValueBuf);
573
574     /*
575      * FIXME: Values seem scattered/duplicated in the registry. Is there a system?
576      */
577
578     if ((szValue->str.w && !pcchValueBuf) || !szProduct || !szProduct[0] || !szAttribute)
579         return ERROR_INVALID_PARAMETER;
580
581     /* check for special properties */
582     if (!lstrcmpW(szAttribute, INSTALLPROPERTY_PACKAGECODEW))
583     {
584         LPWSTR regval;
585         WCHAR packagecode[35];
586
587         r = MSIREG_OpenUserProductsKey(szProduct, &hkey, FALSE);
588         if (r != ERROR_SUCCESS)
589             return ERROR_UNKNOWN_PRODUCT;
590
591         regval = msi_reg_get_val_str( hkey, szAttribute );
592         if (regval)
593         {
594             if (unsquash_guid(regval, packagecode))
595                 val = strdupW(packagecode);
596             msi_free(regval);
597         }
598
599         RegCloseKey(hkey);
600     }
601     else if (!lstrcmpW(szAttribute, INSTALLPROPERTY_ASSIGNMENTTYPEW))
602     {
603         static const WCHAR one[] = { '1',0 };
604         /*
605          * FIXME: should be in the Product key (user or system?)
606          *        but isn't written yet...
607          */
608         val = strdupW( one );
609     }
610     else if (!lstrcmpW(szAttribute, INSTALLPROPERTY_LANGUAGEW) ||
611              !lstrcmpW(szAttribute, INSTALLPROPERTY_VERSIONW))
612     {
613         static const WCHAR fmt[] = { '%','u',0 };
614         WCHAR szVal[16];
615         DWORD regval;
616
617         r = MSIREG_OpenUninstallKey(szProduct, &hkey, FALSE);
618         if (r != ERROR_SUCCESS)
619             return ERROR_UNKNOWN_PRODUCT;
620
621         if (msi_reg_get_val_dword( hkey, szAttribute, &regval))
622         {
623             sprintfW(szVal, fmt, regval);
624             val = strdupW( szVal );
625         }
626
627         RegCloseKey(hkey);
628     }
629     else if (!lstrcmpW(szAttribute, INSTALLPROPERTY_PRODUCTNAMEW))
630     {
631         r = MSIREG_OpenUserProductsKey(szProduct, &hkey, FALSE);
632         if (r != ERROR_SUCCESS)
633             return ERROR_UNKNOWN_PRODUCT;
634
635         val = msi_reg_get_val_str( hkey, szAttribute );
636
637         RegCloseKey(hkey);
638     }
639     else if (!szAttribute[0])
640     {
641         return ERROR_UNKNOWN_PROPERTY;
642     }
643     else
644     {
645         static const WCHAR szDisplayVersion[] = {
646             'D','i','s','p','l','a','y','V','e','r','s','i','o','n',0 };
647
648         FIXME("%s\n", debugstr_w(szAttribute));
649         /* FIXME: some attribute values not tested... */
650
651         if (!lstrcmpW( szAttribute, INSTALLPROPERTY_VERSIONSTRINGW ))
652             szAttribute = szDisplayVersion;
653
654         r = MSIREG_OpenUninstallKey( szProduct, &hkey, FALSE );
655         if (r != ERROR_SUCCESS)
656             return ERROR_UNKNOWN_PRODUCT;
657
658         val = msi_reg_get_val_str( hkey, szAttribute );
659
660         RegCloseKey(hkey);
661     }
662
663     TRACE("returning %s\n", debugstr_w(val));
664
665     if (!val)
666         return ERROR_UNKNOWN_PROPERTY;
667
668     r = msi_strcpy_to_awstring( val, szValue, pcchValueBuf );
669
670     msi_free(val);
671
672     return r;
673 }
674
675 UINT WINAPI MsiGetProductInfoA(LPCSTR szProduct, LPCSTR szAttribute,
676                                LPSTR szBuffer, LPDWORD pcchValueBuf)
677 {
678     LPWSTR szwProduct, szwAttribute = NULL;
679     UINT r = ERROR_OUTOFMEMORY;
680     awstring buffer;
681
682     TRACE("%s %s %p %p\n", debugstr_a(szProduct), debugstr_a(szAttribute),
683           szBuffer, pcchValueBuf);
684
685     szwProduct = strdupAtoW( szProduct );
686     if( szProduct && !szwProduct )
687         goto end;
688
689     szwAttribute = strdupAtoW( szAttribute );
690     if( szAttribute && !szwAttribute )
691         goto end;
692
693     buffer.unicode = FALSE;
694     buffer.str.a = szBuffer;
695
696     r = MSI_GetProductInfo( szwProduct, szwAttribute,
697                             &buffer, pcchValueBuf );
698
699 end:
700     msi_free( szwProduct );
701     msi_free( szwAttribute );
702
703     return r;
704 }
705
706 UINT WINAPI MsiGetProductInfoW(LPCWSTR szProduct, LPCWSTR szAttribute,
707                                LPWSTR szBuffer, LPDWORD pcchValueBuf)
708 {
709     awstring buffer;
710
711     TRACE("%s %s %p %p\n", debugstr_w(szProduct), debugstr_w(szAttribute),
712           szBuffer, pcchValueBuf);
713
714     buffer.unicode = TRUE;
715     buffer.str.w = szBuffer;
716
717     return MSI_GetProductInfo( szProduct, szAttribute,
718                                &buffer, pcchValueBuf );
719 }
720
721 UINT WINAPI MsiEnableLogA(DWORD dwLogMode, LPCSTR szLogFile, DWORD attributes)
722 {
723     LPWSTR szwLogFile = NULL;
724     UINT r;
725
726     TRACE("%08x %s %08x\n", dwLogMode, debugstr_a(szLogFile), attributes);
727
728     if( szLogFile )
729     {
730         szwLogFile = strdupAtoW( szLogFile );
731         if( !szwLogFile )
732             return ERROR_OUTOFMEMORY;
733     }
734     r = MsiEnableLogW( dwLogMode, szwLogFile, attributes );
735     msi_free( szwLogFile );
736     return r;
737 }
738
739 UINT WINAPI MsiEnableLogW(DWORD dwLogMode, LPCWSTR szLogFile, DWORD attributes)
740 {
741     HANDLE file = INVALID_HANDLE_VALUE;
742
743     TRACE("%08x %s %08x\n", dwLogMode, debugstr_w(szLogFile), attributes);
744
745     if (szLogFile)
746     {
747         lstrcpyW(gszLogFile,szLogFile);
748         if (!(attributes & INSTALLLOGATTRIBUTES_APPEND))
749             DeleteFileW(szLogFile);
750         file = CreateFileW(szLogFile, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
751                                FILE_ATTRIBUTE_NORMAL, NULL);
752         if (file != INVALID_HANDLE_VALUE)
753             CloseHandle(file);
754         else
755             ERR("Unable to enable log %s\n",debugstr_w(szLogFile));
756     }
757     else
758         gszLogFile[0] = '\0';
759
760     return ERROR_SUCCESS;
761 }
762
763 UINT WINAPI MsiEnumComponentCostsW(MSIHANDLE hInstall, LPCWSTR szComponent,
764                                    DWORD dwIndex, INSTALLSTATE iState,
765                                    LPWSTR lpDriveBuf, DWORD *pcchDriveBuf,
766                                    int *piCost, int *pTempCost)
767 {
768     FIXME("(%ld, %s, %d, %d, %p, %p, %p %p): stub!\n", hInstall,
769           debugstr_w(szComponent), dwIndex, iState, lpDriveBuf,
770           pcchDriveBuf, piCost, pTempCost);
771
772     return ERROR_NO_MORE_ITEMS;
773 }
774
775 UINT WINAPI MsiQueryComponentStateA(LPCSTR szProductCode,
776                                     LPCSTR szUserSid, MSIINSTALLCONTEXT dwContext,
777                                     LPCSTR szComponent, INSTALLSTATE *pdwState)
778 {
779     LPWSTR prodcode = NULL, usersid = NULL, comp = NULL;
780     UINT r;
781
782     TRACE("(%s, %s, %d, %s, %p)\n", debugstr_a(szProductCode),
783           debugstr_a(szUserSid), dwContext, debugstr_a(szComponent), pdwState);
784
785     if (szProductCode && !(prodcode = strdupAtoW(szProductCode)))
786         return ERROR_OUTOFMEMORY;
787
788     if (szUserSid && !(usersid = strdupAtoW(szUserSid)))
789             return ERROR_OUTOFMEMORY;
790
791     if (szComponent && !(comp = strdupAtoW(szComponent)))
792             return ERROR_OUTOFMEMORY;
793
794     r = MsiQueryComponentStateW(prodcode, usersid, dwContext, comp, pdwState);
795
796     msi_free(prodcode);
797     msi_free(usersid);
798     msi_free(comp);
799
800     return r;
801 }
802
803 static BOOL msi_comp_find_prod_key(LPCWSTR prodcode, MSIINSTALLCONTEXT context)
804 {
805     UINT r;
806     HKEY hkey;
807
808     if (context == MSIINSTALLCONTEXT_MACHINE)
809         r = MSIREG_OpenLocalClassesProductKey(prodcode, &hkey, FALSE);
810     else if (context == MSIINSTALLCONTEXT_USERUNMANAGED)
811         r = MSIREG_OpenUserProductsKey(prodcode, &hkey, FALSE);
812     else
813         r = MSIREG_OpenLocalManagedProductKey(prodcode, &hkey, FALSE);
814
815     RegCloseKey(hkey);
816     return (r == ERROR_SUCCESS);
817 }
818
819 static BOOL msi_comp_find_package(LPCWSTR prodcode, MSIINSTALLCONTEXT context)
820 {
821     LPCWSTR package;
822     HKEY hkey;
823     DWORD sz;
824     LONG res;
825     UINT r;
826
827     static const WCHAR local_package[] = {'L','o','c','a','l','P','a','c','k','a','g','e',0};
828     static const WCHAR managed_local_package[] = {
829         'M','a','n','a','g','e','d','L','o','c','a','l','P','a','c','k','a','g','e',0
830     };
831
832     if (context == MSIINSTALLCONTEXT_MACHINE)
833         r = MSIREG_OpenLocalSystemProductKey(prodcode, &hkey, FALSE);
834     else
835         r = MSIREG_OpenInstallPropertiesKey(prodcode, &hkey, FALSE);
836
837     if (r != ERROR_SUCCESS)
838         return FALSE;
839
840     if (context == MSIINSTALLCONTEXT_USERMANAGED)
841         package = managed_local_package;
842     else
843         package = local_package;
844
845     sz = 0;
846     res = RegQueryValueExW(hkey, package, NULL, NULL, NULL, &sz);
847     RegCloseKey(hkey);
848
849     return (res == ERROR_SUCCESS);
850 }
851
852 static BOOL msi_comp_find_prodcode(LPCWSTR prodcode, LPWSTR squished_pc,
853                                    MSIINSTALLCONTEXT context,
854                                    LPCWSTR comp, DWORD *sz)
855 {
856     HKEY hkey;
857     LONG res;
858     UINT r;
859
860     if (context == MSIINSTALLCONTEXT_MACHINE)
861         r = MSIREG_OpenLocalSystemComponentKey(comp, &hkey, FALSE);
862     else
863         r = MSIREG_OpenUserDataComponentKey(comp, &hkey, FALSE);
864
865     if (r != ERROR_SUCCESS)
866         return FALSE;
867
868     *sz = 0;
869     res = RegQueryValueExW(hkey, squished_pc, NULL, NULL, NULL, sz);
870     if (res != ERROR_SUCCESS)
871         return FALSE;
872
873     RegCloseKey(hkey);
874     return TRUE;
875 }
876
877 UINT WINAPI MsiQueryComponentStateW(LPCWSTR szProductCode,
878                                     LPCWSTR szUserSid, MSIINSTALLCONTEXT dwContext,
879                                     LPCWSTR szComponent, INSTALLSTATE *pdwState)
880 {
881     WCHAR squished_pc[GUID_SIZE];
882     BOOL found;
883     DWORD sz;
884
885     TRACE("(%s, %s, %d, %s, %p)\n", debugstr_w(szProductCode),
886           debugstr_w(szUserSid), dwContext, debugstr_w(szComponent), pdwState);
887
888     if (!pdwState)
889         return ERROR_INVALID_PARAMETER;
890
891     if (!szProductCode || !*szProductCode || lstrlenW(szProductCode) != GUID_SIZE - 1)
892         return ERROR_INVALID_PARAMETER;
893
894     if (!squash_guid(szProductCode, squished_pc))
895         return ERROR_INVALID_PARAMETER;
896
897     found = msi_comp_find_prod_key(szProductCode, dwContext);
898
899     if (!msi_comp_find_package(szProductCode, dwContext))
900     {
901         if (found)
902         {
903             *pdwState = INSTALLSTATE_UNKNOWN;
904             return ERROR_UNKNOWN_COMPONENT;
905         }
906
907         return ERROR_UNKNOWN_PRODUCT;
908     }
909
910     *pdwState = INSTALLSTATE_UNKNOWN;
911
912     if (!msi_comp_find_prodcode(szProductCode, squished_pc, dwContext, szComponent, &sz))
913         return ERROR_UNKNOWN_COMPONENT;
914
915     if (sz == 0)
916         *pdwState = INSTALLSTATE_NOTUSED;
917     else
918         *pdwState = INSTALLSTATE_LOCAL;
919
920     return ERROR_SUCCESS;
921 }
922
923 INSTALLSTATE WINAPI MsiQueryProductStateA(LPCSTR szProduct)
924 {
925     LPWSTR szwProduct = NULL;
926     INSTALLSTATE r;
927
928     if( szProduct )
929     {
930          szwProduct = strdupAtoW( szProduct );
931          if( !szwProduct )
932              return ERROR_OUTOFMEMORY;
933     }
934     r = MsiQueryProductStateW( szwProduct );
935     msi_free( szwProduct );
936     return r;
937 }
938
939 INSTALLSTATE WINAPI MsiQueryProductStateW(LPCWSTR szProduct)
940 {
941     UINT rc;
942     INSTALLSTATE state = INSTALLSTATE_UNKNOWN;
943     HKEY hkey = 0, props = 0;
944     DWORD sz;
945     BOOL userkey_exists = FALSE;
946
947     static const int GUID_LEN = 38;
948     static const WCHAR szInstallProperties[] = {
949             'I','n','s','t','a','l','l','P','r','o','p','e','r','t','i','e','s',0
950     };
951     static const WCHAR szWindowsInstaller[] = {
952             'W','i','n','d','o','w','s','I','n','s','t','a','l','l','e','r',0
953     };
954
955     TRACE("%s\n", debugstr_w(szProduct));
956
957     if (!szProduct || !*szProduct || lstrlenW(szProduct) != GUID_LEN)
958         return INSTALLSTATE_INVALIDARG;
959
960     rc = MSIREG_OpenUserProductsKey(szProduct,&hkey,FALSE);
961     if (rc == ERROR_SUCCESS)
962     {
963         userkey_exists = TRUE;
964         state = INSTALLSTATE_ADVERTISED;
965         RegCloseKey(hkey);
966     }
967
968     rc = MSIREG_OpenUserDataProductKey(szProduct,&hkey,FALSE);
969     if (rc != ERROR_SUCCESS)
970         goto end;
971
972     rc = RegOpenKeyW(hkey, szInstallProperties, &props);
973     if (rc != ERROR_SUCCESS)
974         goto end;
975
976     sz = sizeof(state);
977     rc = RegQueryValueExW(props,szWindowsInstaller,NULL,NULL,(LPVOID)&state, &sz);
978     if (rc != ERROR_SUCCESS)
979         goto end;
980
981     if (state)
982         state = INSTALLSTATE_DEFAULT;
983     else
984         state = INSTALLSTATE_UNKNOWN;
985
986     if (state == INSTALLSTATE_DEFAULT && !userkey_exists)
987         state = INSTALLSTATE_ABSENT;
988
989 end:
990     RegCloseKey(props);
991     RegCloseKey(hkey);
992     return state;
993 }
994
995 INSTALLUILEVEL WINAPI MsiSetInternalUI(INSTALLUILEVEL dwUILevel, HWND *phWnd)
996 {
997     INSTALLUILEVEL old = gUILevel;
998     HWND oldwnd = gUIhwnd;
999
1000     TRACE("%08x %p\n", dwUILevel, phWnd);
1001
1002     gUILevel = dwUILevel;
1003     if (phWnd)
1004     {
1005         gUIhwnd = *phWnd;
1006         *phWnd = oldwnd;
1007     }
1008     return old;
1009 }
1010
1011 INSTALLUI_HANDLERA WINAPI MsiSetExternalUIA(INSTALLUI_HANDLERA puiHandler,
1012                                   DWORD dwMessageFilter, LPVOID pvContext)
1013 {
1014     INSTALLUI_HANDLERA prev = gUIHandlerA;
1015
1016     TRACE("%p %x %p\n",puiHandler, dwMessageFilter,pvContext);
1017     gUIHandlerA = puiHandler;
1018     gUIFilter = dwMessageFilter;
1019     gUIContext = pvContext;
1020
1021     return prev;
1022 }
1023
1024 INSTALLUI_HANDLERW WINAPI MsiSetExternalUIW(INSTALLUI_HANDLERW puiHandler,
1025                                   DWORD dwMessageFilter, LPVOID pvContext)
1026 {
1027     INSTALLUI_HANDLERW prev = gUIHandlerW;
1028
1029     TRACE("%p %x %p\n",puiHandler,dwMessageFilter,pvContext);
1030     gUIHandlerW = puiHandler;
1031     gUIFilter = dwMessageFilter;
1032     gUIContext = pvContext;
1033
1034     return prev;
1035 }
1036
1037 /******************************************************************
1038  *  MsiLoadStringW            [MSI.@]
1039  *
1040  * Loads a string from MSI's string resources.
1041  *
1042  * PARAMS
1043  *
1044  *   handle        [I]  only -1 is handled currently
1045  *   id            [I]  id of the string to be loaded
1046  *   lpBuffer      [O]  buffer for the string to be written to
1047  *   nBufferMax    [I]  maximum size of the buffer in characters
1048  *   lang          [I]  the preferred language for the string
1049  *
1050  * RETURNS
1051  *
1052  *   If successful, this function returns the language id of the string loaded
1053  *   If the function fails, the function returns zero.
1054  *
1055  * NOTES
1056  *
1057  *   The type of the first parameter is unknown.  LoadString's prototype
1058  *  suggests that it might be a module handle.  I have made it an MSI handle
1059  *  for starters, as -1 is an invalid MSI handle, but not an invalid module
1060  *  handle.  Maybe strings can be stored in an MSI database somehow.
1061  */
1062 LANGID WINAPI MsiLoadStringW( MSIHANDLE handle, UINT id, LPWSTR lpBuffer,
1063                 int nBufferMax, LANGID lang )
1064 {
1065     HRSRC hres;
1066     HGLOBAL hResData;
1067     LPWSTR p;
1068     DWORD i, len;
1069
1070     TRACE("%ld %u %p %d %d\n", handle, id, lpBuffer, nBufferMax, lang);
1071
1072     if( handle != -1 )
1073         FIXME("don't know how to deal with handle = %08lx\n", handle);
1074
1075     if( !lang )
1076         lang = GetUserDefaultLangID();
1077
1078     hres = FindResourceExW( msi_hInstance, (LPCWSTR) RT_STRING,
1079                             (LPWSTR)1, lang );
1080     if( !hres )
1081         return 0;
1082     hResData = LoadResource( msi_hInstance, hres );
1083     if( !hResData )
1084         return 0;
1085     p = LockResource( hResData );
1086     if( !p )
1087         return 0;
1088
1089     for (i = 0; i < (id&0xf); i++)
1090         p += *p + 1;
1091     len = *p;
1092
1093     if( nBufferMax <= len )
1094         return 0;
1095
1096     memcpy( lpBuffer, p+1, len * sizeof(WCHAR));
1097     lpBuffer[ len ] = 0;
1098
1099     TRACE("found -> %s\n", debugstr_w(lpBuffer));
1100
1101     return lang;
1102 }
1103
1104 LANGID WINAPI MsiLoadStringA( MSIHANDLE handle, UINT id, LPSTR lpBuffer,
1105                 int nBufferMax, LANGID lang )
1106 {
1107     LPWSTR bufW;
1108     LANGID r;
1109     DWORD len;
1110
1111     bufW = msi_alloc(nBufferMax*sizeof(WCHAR));
1112     r = MsiLoadStringW(handle, id, bufW, nBufferMax, lang);
1113     if( r )
1114     {
1115         len = WideCharToMultiByte(CP_ACP, 0, bufW, -1, NULL, 0, NULL, NULL );
1116         if( len <= nBufferMax )
1117             WideCharToMultiByte( CP_ACP, 0, bufW, -1,
1118                                  lpBuffer, nBufferMax, NULL, NULL );
1119         else
1120             r = 0;
1121     }
1122     msi_free(bufW);
1123     return r;
1124 }
1125
1126 INSTALLSTATE WINAPI MsiLocateComponentA(LPCSTR szComponent, LPSTR lpPathBuf,
1127                 LPDWORD pcchBuf)
1128 {
1129     char szProduct[GUID_SIZE];
1130
1131     TRACE("%s %p %p\n", debugstr_a(szComponent), lpPathBuf, pcchBuf);
1132
1133     if (MsiGetProductCodeA( szComponent, szProduct ) != ERROR_SUCCESS)
1134         return INSTALLSTATE_UNKNOWN;
1135
1136     return MsiGetComponentPathA( szProduct, szComponent, lpPathBuf, pcchBuf );
1137 }
1138
1139 INSTALLSTATE WINAPI MsiLocateComponentW(LPCWSTR szComponent, LPWSTR lpPathBuf,
1140                 LPDWORD pcchBuf)
1141 {
1142     WCHAR szProduct[GUID_SIZE];
1143
1144     TRACE("%s %p %p\n", debugstr_w(szComponent), lpPathBuf, pcchBuf);
1145
1146     if (MsiGetProductCodeW( szComponent, szProduct ) != ERROR_SUCCESS)
1147         return INSTALLSTATE_UNKNOWN;
1148
1149     return MsiGetComponentPathW( szProduct, szComponent, lpPathBuf, pcchBuf );
1150 }
1151
1152 UINT WINAPI MsiMessageBoxA(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType,
1153                 WORD wLanguageId, DWORD f)
1154 {
1155     FIXME("%p %s %s %u %08x %08x\n", hWnd, debugstr_a(lpText), debugstr_a(lpCaption),
1156           uType, wLanguageId, f);
1157     return MessageBoxExA(hWnd,lpText,lpCaption,uType,wLanguageId); 
1158 }
1159
1160 UINT WINAPI MsiMessageBoxW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType,
1161                 WORD wLanguageId, DWORD f)
1162 {
1163     FIXME("%p %s %s %u %08x %08x\n", hWnd, debugstr_w(lpText), debugstr_w(lpCaption),
1164           uType, wLanguageId, f);
1165     return MessageBoxExW(hWnd,lpText,lpCaption,uType,wLanguageId); 
1166 }
1167
1168 UINT WINAPI MsiProvideAssemblyA( LPCSTR szAssemblyName, LPCSTR szAppContext,
1169                 DWORD dwInstallMode, DWORD dwAssemblyInfo, LPSTR lpPathBuf,
1170                 LPDWORD pcchPathBuf )
1171 {
1172     FIXME("%s %s %08x %08x %p %p\n", debugstr_a(szAssemblyName),
1173           debugstr_a(szAppContext), dwInstallMode, dwAssemblyInfo, lpPathBuf,
1174           pcchPathBuf);
1175     return ERROR_CALL_NOT_IMPLEMENTED;
1176 }
1177
1178 UINT WINAPI MsiProvideAssemblyW( LPCWSTR szAssemblyName, LPCWSTR szAppContext,
1179                 DWORD dwInstallMode, DWORD dwAssemblyInfo, LPWSTR lpPathBuf,
1180                 LPDWORD pcchPathBuf )
1181 {
1182     FIXME("%s %s %08x %08x %p %p\n", debugstr_w(szAssemblyName),
1183           debugstr_w(szAppContext), dwInstallMode, dwAssemblyInfo, lpPathBuf,
1184           pcchPathBuf);
1185     return ERROR_CALL_NOT_IMPLEMENTED;
1186 }
1187
1188 UINT WINAPI MsiProvideComponentFromDescriptorA( LPCSTR szDescriptor,
1189                 LPSTR szPath, LPDWORD pcchPath, LPDWORD pcchArgs )
1190 {
1191     FIXME("%s %p %p %p\n", debugstr_a(szDescriptor), szPath, pcchPath, pcchArgs );
1192     return ERROR_CALL_NOT_IMPLEMENTED;
1193 }
1194
1195 UINT WINAPI MsiProvideComponentFromDescriptorW( LPCWSTR szDescriptor,
1196                 LPWSTR szPath, LPDWORD pcchPath, LPDWORD pcchArgs )
1197 {
1198     FIXME("%s %p %p %p\n", debugstr_w(szDescriptor), szPath, pcchPath, pcchArgs );
1199     return ERROR_CALL_NOT_IMPLEMENTED;
1200 }
1201
1202 HRESULT WINAPI MsiGetFileSignatureInformationA( LPCSTR szSignedObjectPath,
1203                 DWORD dwFlags, PCCERT_CONTEXT* ppcCertContext, LPBYTE pbHashData,
1204                 LPDWORD pcbHashData)
1205 {
1206     FIXME("%s %08x %p %p %p\n", debugstr_a(szSignedObjectPath), dwFlags,
1207           ppcCertContext, pbHashData, pcbHashData);
1208     return ERROR_CALL_NOT_IMPLEMENTED;
1209 }
1210
1211 HRESULT WINAPI MsiGetFileSignatureInformationW( LPCWSTR szSignedObjectPath,
1212                 DWORD dwFlags, PCCERT_CONTEXT* ppcCertContext, LPBYTE pbHashData,
1213                 LPDWORD pcbHashData)
1214 {
1215     FIXME("%s %08x %p %p %p\n", debugstr_w(szSignedObjectPath), dwFlags,
1216           ppcCertContext, pbHashData, pcbHashData);
1217     return ERROR_CALL_NOT_IMPLEMENTED;
1218 }
1219
1220 UINT WINAPI MsiGetProductPropertyA( MSIHANDLE hProduct, LPCSTR szProperty,
1221                                     LPSTR szValue, LPDWORD pccbValue )
1222 {
1223     FIXME("%ld %s %p %p\n", hProduct, debugstr_a(szProperty), szValue, pccbValue);
1224     return ERROR_CALL_NOT_IMPLEMENTED;
1225 }
1226
1227 UINT WINAPI MsiGetProductPropertyW( MSIHANDLE hProduct, LPCWSTR szProperty,
1228                                     LPWSTR szValue, LPDWORD pccbValue )
1229 {
1230     FIXME("%ld %s %p %p\n", hProduct, debugstr_w(szProperty), szValue, pccbValue);
1231     return ERROR_CALL_NOT_IMPLEMENTED;
1232 }
1233
1234 UINT WINAPI MsiVerifyPackageA( LPCSTR szPackage )
1235 {
1236     UINT r;
1237     LPWSTR szPack = NULL;
1238
1239     TRACE("%s\n", debugstr_a(szPackage) );
1240
1241     if( szPackage )
1242     {
1243         szPack = strdupAtoW( szPackage );
1244         if( !szPack )
1245             return ERROR_OUTOFMEMORY;
1246     }
1247
1248     r = MsiVerifyPackageW( szPack );
1249
1250     msi_free( szPack );
1251
1252     return r;
1253 }
1254
1255 UINT WINAPI MsiVerifyPackageW( LPCWSTR szPackage )
1256 {
1257     MSIHANDLE handle;
1258     UINT r;
1259
1260     TRACE("%s\n", debugstr_w(szPackage) );
1261
1262     r = MsiOpenDatabaseW( szPackage, MSIDBOPEN_READONLY, &handle );
1263     MsiCloseHandle( handle );
1264
1265     return r;
1266 }
1267
1268 static INSTALLSTATE WINAPI MSI_GetComponentPath(LPCWSTR szProduct, LPCWSTR szComponent,
1269                                                 awstring* lpPathBuf, LPDWORD pcchBuf)
1270 {
1271     WCHAR squished_pc[GUID_SIZE];
1272     WCHAR squished_comp[GUID_SIZE];
1273     HKEY hkey;
1274     LPWSTR path = NULL;
1275     INSTALLSTATE state;
1276     DWORD version;
1277
1278     static const WCHAR wininstaller[] = {
1279         'W','i','n','d','o','w','s','I','n','s','t','a','l','l','e','r',0};
1280
1281     TRACE("%s %s %p %p\n", debugstr_w(szProduct),
1282            debugstr_w(szComponent), lpPathBuf->str.w, pcchBuf);
1283
1284     if (!szProduct || !szComponent)
1285         return INSTALLSTATE_INVALIDARG;
1286
1287     if (lpPathBuf->str.w && !pcchBuf)
1288         return INSTALLSTATE_INVALIDARG;
1289
1290     if (!squash_guid(szProduct, squished_pc) ||
1291         !squash_guid(szComponent, squished_comp))
1292         return INSTALLSTATE_INVALIDARG;
1293
1294     state = INSTALLSTATE_UNKNOWN;
1295
1296     if (MSIREG_OpenLocalSystemComponentKey(szComponent, &hkey, FALSE) == ERROR_SUCCESS ||
1297         MSIREG_OpenUserDataComponentKey(szComponent, &hkey, FALSE) == ERROR_SUCCESS)
1298     {
1299         path = msi_reg_get_val_str(hkey, squished_pc);
1300         RegCloseKey(hkey);
1301
1302         state = INSTALLSTATE_ABSENT;
1303
1304         if ((MSIREG_OpenLocalSystemProductKey(szProduct, &hkey, FALSE) == ERROR_SUCCESS ||
1305             MSIREG_OpenUserDataProductKey(szProduct, &hkey, FALSE) == ERROR_SUCCESS) &&
1306             msi_reg_get_val_dword(hkey, wininstaller, &version) &&
1307             GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES)
1308         {
1309             RegCloseKey(hkey);
1310             state = INSTALLSTATE_LOCAL;
1311         }
1312     }
1313
1314     if (state != INSTALLSTATE_LOCAL &&
1315         (MSIREG_OpenUserProductsKey(szProduct, &hkey, FALSE) == ERROR_SUCCESS ||
1316          MSIREG_OpenLocalClassesProductKey(szProduct, &hkey, FALSE) == ERROR_SUCCESS))
1317     {
1318         RegCloseKey(hkey);
1319
1320         if (MSIREG_OpenLocalSystemComponentKey(szComponent, &hkey, FALSE) == ERROR_SUCCESS ||
1321             MSIREG_OpenUserDataComponentKey(szComponent, &hkey, FALSE) == ERROR_SUCCESS)
1322         {
1323             msi_free(path);
1324             path = msi_reg_get_val_str(hkey, squished_pc);
1325             RegCloseKey(hkey);
1326
1327             state = INSTALLSTATE_ABSENT;
1328
1329             if (GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES)
1330                 state = INSTALLSTATE_LOCAL;
1331         }
1332     }
1333
1334     if (!path)
1335         return INSTALLSTATE_UNKNOWN;
1336
1337     if (state == INSTALLSTATE_LOCAL && !*path)
1338         state = INSTALLSTATE_NOTUSED;
1339
1340     msi_strcpy_to_awstring(path, lpPathBuf, pcchBuf);
1341     msi_free(path);
1342     return state;
1343 }
1344
1345 /******************************************************************
1346  * MsiGetComponentPathW      [MSI.@]
1347  */
1348 INSTALLSTATE WINAPI MsiGetComponentPathW(LPCWSTR szProduct, LPCWSTR szComponent,
1349                                          LPWSTR lpPathBuf, LPDWORD pcchBuf)
1350 {
1351     awstring path;
1352
1353     path.unicode = TRUE;
1354     path.str.w = lpPathBuf;
1355
1356     return MSI_GetComponentPath( szProduct, szComponent, &path, pcchBuf );
1357 }
1358
1359 /******************************************************************
1360  * MsiGetComponentPathA      [MSI.@]
1361  */
1362 INSTALLSTATE WINAPI MsiGetComponentPathA(LPCSTR szProduct, LPCSTR szComponent,
1363                                          LPSTR lpPathBuf, LPDWORD pcchBuf)
1364 {
1365     LPWSTR szwProduct, szwComponent = NULL;
1366     INSTALLSTATE r = INSTALLSTATE_UNKNOWN;
1367     awstring path;
1368
1369     szwProduct = strdupAtoW( szProduct );
1370     if( szProduct && !szwProduct)
1371         goto end;
1372
1373     szwComponent = strdupAtoW( szComponent );
1374     if( szComponent && !szwComponent )
1375         goto end;
1376
1377     path.unicode = FALSE;
1378     path.str.a = lpPathBuf;
1379
1380     r = MSI_GetComponentPath( szwProduct, szwComponent, &path, pcchBuf );
1381
1382 end:
1383     msi_free( szwProduct );
1384     msi_free( szwComponent );
1385
1386     return r;
1387 }
1388
1389 /******************************************************************
1390  * MsiQueryFeatureStateA      [MSI.@]
1391  */
1392 INSTALLSTATE WINAPI MsiQueryFeatureStateA(LPCSTR szProduct, LPCSTR szFeature)
1393 {
1394     LPWSTR szwProduct = NULL, szwFeature= NULL;
1395     INSTALLSTATE rc = INSTALLSTATE_UNKNOWN;
1396
1397     szwProduct = strdupAtoW( szProduct );
1398     if ( szProduct && !szwProduct )
1399         goto end;
1400
1401     szwFeature = strdupAtoW( szFeature );
1402     if ( szFeature && !szwFeature )
1403         goto end;
1404
1405     rc = MsiQueryFeatureStateW(szwProduct, szwFeature);
1406
1407 end:
1408     msi_free( szwProduct);
1409     msi_free( szwFeature);
1410
1411     return rc;
1412 }
1413
1414 /******************************************************************
1415  * MsiQueryFeatureStateW      [MSI.@]
1416  *
1417  * Checks the state of a feature
1418  *
1419  * PARAMS
1420  *   szProduct     [I]  Product's GUID string
1421  *   szFeature     [I]  Feature's GUID string
1422  *
1423  * RETURNS
1424  *   INSTALLSTATE_LOCAL        Feature is installed and useable
1425  *   INSTALLSTATE_ABSENT       Feature is absent
1426  *   INSTALLSTATE_ADVERTISED   Feature should be installed on demand
1427  *   INSTALLSTATE_UNKNOWN      An error occurred
1428  *   INSTALLSTATE_INVALIDARG   One of the GUIDs was invalid
1429  *
1430  */
1431 INSTALLSTATE WINAPI MsiQueryFeatureStateW(LPCWSTR szProduct, LPCWSTR szFeature)
1432 {
1433     WCHAR squishProduct[33], comp[GUID_SIZE];
1434     GUID guid;
1435     LPWSTR components, p, parent_feature, path;
1436     UINT rc;
1437     HKEY hkey;
1438     INSTALLSTATE r;
1439     BOOL missing = FALSE;
1440
1441     TRACE("%s %s\n", debugstr_w(szProduct), debugstr_w(szFeature));
1442
1443     if (!szProduct || !szFeature)
1444         return INSTALLSTATE_INVALIDARG;
1445
1446     if (!squash_guid( szProduct, squishProduct ))
1447         return INSTALLSTATE_INVALIDARG;
1448
1449     /* check that it's installed at all */
1450     rc = MSIREG_OpenUserFeaturesKey(szProduct, &hkey, FALSE);
1451     if (rc != ERROR_SUCCESS)
1452         return INSTALLSTATE_UNKNOWN;
1453
1454     parent_feature = msi_reg_get_val_str( hkey, szFeature );
1455     RegCloseKey(hkey);
1456
1457     if (!parent_feature)
1458         return INSTALLSTATE_UNKNOWN;
1459
1460     r = (parent_feature[0] == 6) ? INSTALLSTATE_ABSENT : INSTALLSTATE_LOCAL;
1461     msi_free(parent_feature);
1462     if (r == INSTALLSTATE_ABSENT)
1463         return r;
1464
1465     /* now check if it's complete or advertised */
1466     rc = MSIREG_OpenUserDataFeaturesKey(szProduct, &hkey, FALSE);
1467     if (rc != ERROR_SUCCESS)
1468         return INSTALLSTATE_ADVERTISED;
1469
1470     components = msi_reg_get_val_str( hkey, szFeature );
1471     RegCloseKey(hkey);
1472
1473     TRACE("rc = %d buffer = %s\n", rc, debugstr_w(components));
1474
1475     if (!components)
1476         return INSTALLSTATE_ADVERTISED;
1477
1478     for( p = components; *p && *p != 2 ; p += 20)
1479     {
1480         if (!decode_base85_guid( p, &guid ))
1481         {
1482             if (p != components)
1483                 break;
1484
1485             msi_free(components);
1486             return INSTALLSTATE_BADCONFIG;
1487         }
1488
1489         StringFromGUID2(&guid, comp, GUID_SIZE);
1490         rc = MSIREG_OpenUserDataComponentKey(comp, &hkey, FALSE);
1491         if (rc != ERROR_SUCCESS)
1492         {
1493             msi_free(components);
1494             return INSTALLSTATE_ADVERTISED;
1495         }
1496
1497         path = msi_reg_get_val_str(hkey, squishProduct);
1498         if (!path)
1499             missing = TRUE;
1500
1501         msi_free(path);
1502     }
1503
1504     TRACE("%s %s -> %d\n", debugstr_w(szProduct), debugstr_w(szFeature), r);
1505     msi_free(components);
1506
1507     if (missing)
1508         return INSTALLSTATE_ADVERTISED;
1509
1510     return INSTALLSTATE_LOCAL;
1511 }
1512
1513 /******************************************************************
1514  * MsiGetFileVersionA         [MSI.@]
1515  */
1516 UINT WINAPI MsiGetFileVersionA(LPCSTR szFilePath, LPSTR lpVersionBuf,
1517                 LPDWORD pcchVersionBuf, LPSTR lpLangBuf, LPDWORD pcchLangBuf)
1518 {
1519     LPWSTR szwFilePath = NULL, lpwVersionBuff = NULL, lpwLangBuff = NULL;
1520     UINT ret = ERROR_OUTOFMEMORY;
1521
1522     if ((lpVersionBuf && !pcchVersionBuf) ||
1523         (lpLangBuf && !pcchLangBuf))
1524         return ERROR_INVALID_PARAMETER;
1525
1526     if( szFilePath )
1527     {
1528         szwFilePath = strdupAtoW( szFilePath );
1529         if( !szwFilePath )
1530             goto end;
1531     }
1532
1533     if( lpVersionBuf && pcchVersionBuf && *pcchVersionBuf )
1534     {
1535         lpwVersionBuff = msi_alloc(*pcchVersionBuf*sizeof(WCHAR));
1536         if( !lpwVersionBuff )
1537             goto end;
1538     }
1539
1540     if( lpLangBuf && pcchLangBuf && *pcchLangBuf )
1541     {
1542         lpwLangBuff = msi_alloc(*pcchLangBuf*sizeof(WCHAR));
1543         if( !lpwLangBuff )
1544             goto end;
1545     }
1546
1547     ret = MsiGetFileVersionW(szwFilePath, lpwVersionBuff, pcchVersionBuf,
1548                              lpwLangBuff, pcchLangBuf);
1549
1550     if( (ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA) && lpwVersionBuff )
1551         WideCharToMultiByte(CP_ACP, 0, lpwVersionBuff, -1,
1552                             lpVersionBuf, *pcchVersionBuf, NULL, NULL);
1553     if( (ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA) && lpwLangBuff )
1554         WideCharToMultiByte(CP_ACP, 0, lpwLangBuff, -1,
1555                             lpLangBuf, *pcchLangBuf, NULL, NULL);
1556
1557 end:
1558     msi_free(szwFilePath);
1559     msi_free(lpwVersionBuff);
1560     msi_free(lpwLangBuff);
1561
1562     return ret;
1563 }
1564
1565 /******************************************************************
1566  * MsiGetFileVersionW         [MSI.@]
1567  */
1568 UINT WINAPI MsiGetFileVersionW(LPCWSTR szFilePath, LPWSTR lpVersionBuf,
1569                 LPDWORD pcchVersionBuf, LPWSTR lpLangBuf, LPDWORD pcchLangBuf)
1570 {
1571     static const WCHAR szVersionResource[] = {'\\',0};
1572     static const WCHAR szVersionFormat[] = {
1573         '%','d','.','%','d','.','%','d','.','%','d',0};
1574     static const WCHAR szLangFormat[] = {'%','d',0};
1575     UINT ret = 0;
1576     DWORD dwVerLen, gle;
1577     LPVOID lpVer = NULL;
1578     VS_FIXEDFILEINFO *ffi;
1579     UINT puLen;
1580     WCHAR tmp[32];
1581
1582     TRACE("%s %p %d %p %d\n", debugstr_w(szFilePath),
1583           lpVersionBuf, pcchVersionBuf?*pcchVersionBuf:0,
1584           lpLangBuf, pcchLangBuf?*pcchLangBuf:0);
1585
1586     if ((lpVersionBuf && !pcchVersionBuf) ||
1587         (lpLangBuf && !pcchLangBuf))
1588         return ERROR_INVALID_PARAMETER;
1589
1590     dwVerLen = GetFileVersionInfoSizeW(szFilePath, NULL);
1591     if( !dwVerLen )
1592     {
1593         gle = GetLastError();
1594         if (gle == ERROR_BAD_PATHNAME)
1595             return ERROR_FILE_NOT_FOUND;
1596
1597         return gle;
1598     }
1599
1600     lpVer = msi_alloc(dwVerLen);
1601     if( !lpVer )
1602     {
1603         ret = ERROR_OUTOFMEMORY;
1604         goto end;
1605     }
1606
1607     if( !GetFileVersionInfoW(szFilePath, 0, dwVerLen, lpVer) )
1608     {
1609         ret = GetLastError();
1610         goto end;
1611     }
1612     if( lpVersionBuf && pcchVersionBuf && *pcchVersionBuf )
1613     {
1614         if( VerQueryValueW(lpVer, szVersionResource, (LPVOID*)&ffi, &puLen) &&
1615             (puLen > 0) )
1616         {
1617             wsprintfW(tmp, szVersionFormat,
1618                   HIWORD(ffi->dwFileVersionMS), LOWORD(ffi->dwFileVersionMS),
1619                   HIWORD(ffi->dwFileVersionLS), LOWORD(ffi->dwFileVersionLS));
1620             lstrcpynW(lpVersionBuf, tmp, *pcchVersionBuf);
1621             *pcchVersionBuf = lstrlenW(lpVersionBuf);
1622         }
1623         else
1624         {
1625             *lpVersionBuf = 0;
1626             *pcchVersionBuf = 0;
1627         }
1628     }
1629
1630     if( lpLangBuf && pcchLangBuf && *pcchLangBuf )
1631     {
1632         DWORD lang = GetUserDefaultLangID();
1633
1634         FIXME("Retrieve language from file\n");
1635         wsprintfW(tmp, szLangFormat, lang);
1636         lstrcpynW(lpLangBuf, tmp, *pcchLangBuf);
1637         *pcchLangBuf = lstrlenW(lpLangBuf);
1638     }
1639
1640 end:
1641     msi_free(lpVer);
1642     return ret;
1643 }
1644
1645 /***********************************************************************
1646  * MsiGetFeatureUsageW           [MSI.@]
1647  */
1648 UINT WINAPI MsiGetFeatureUsageW( LPCWSTR szProduct, LPCWSTR szFeature,
1649                                  LPDWORD pdwUseCount, LPWORD pwDateUsed )
1650 {
1651     FIXME("%s %s %p %p\n",debugstr_w(szProduct), debugstr_w(szFeature),
1652           pdwUseCount, pwDateUsed);
1653     return ERROR_CALL_NOT_IMPLEMENTED;
1654 }
1655
1656 /***********************************************************************
1657  * MsiGetFeatureUsageA           [MSI.@]
1658  */
1659 UINT WINAPI MsiGetFeatureUsageA( LPCSTR szProduct, LPCSTR szFeature,
1660                                  LPDWORD pdwUseCount, LPWORD pwDateUsed )
1661 {
1662     LPWSTR prod = NULL, feat = NULL;
1663     UINT ret = ERROR_OUTOFMEMORY;
1664
1665     TRACE("%s %s %p %p\n", debugstr_a(szProduct), debugstr_a(szFeature),
1666           pdwUseCount, pwDateUsed);
1667
1668     prod = strdupAtoW( szProduct );
1669     if (szProduct && !prod)
1670         goto end;
1671
1672     feat = strdupAtoW( szFeature );
1673     if (szFeature && !feat)
1674         goto end;
1675
1676     ret = MsiGetFeatureUsageW( prod, feat, pdwUseCount, pwDateUsed );
1677
1678 end:
1679     msi_free( prod );
1680     msi_free( feat );
1681
1682     return ret;
1683 }
1684
1685 /***********************************************************************
1686  * MsiUseFeatureExW           [MSI.@]
1687  */
1688 INSTALLSTATE WINAPI MsiUseFeatureExW( LPCWSTR szProduct, LPCWSTR szFeature,
1689                                       DWORD dwInstallMode, DWORD dwReserved )
1690 {
1691     INSTALLSTATE state;
1692
1693     TRACE("%s %s %i %i\n", debugstr_w(szProduct), debugstr_w(szFeature),
1694           dwInstallMode, dwReserved);
1695
1696     state = MsiQueryFeatureStateW( szProduct, szFeature );
1697
1698     if (dwReserved)
1699         return INSTALLSTATE_INVALIDARG;
1700
1701     if (state == INSTALLSTATE_LOCAL && dwInstallMode != INSTALLMODE_NODETECTION)
1702     {
1703         FIXME("mark product %s feature %s as used\n",
1704               debugstr_w(szProduct), debugstr_w(szFeature) );
1705     }
1706
1707     return state;
1708 }
1709
1710 /***********************************************************************
1711  * MsiUseFeatureExA           [MSI.@]
1712  */
1713 INSTALLSTATE WINAPI MsiUseFeatureExA( LPCSTR szProduct, LPCSTR szFeature,
1714                                       DWORD dwInstallMode, DWORD dwReserved )
1715 {
1716     INSTALLSTATE ret = INSTALLSTATE_UNKNOWN;
1717     LPWSTR prod = NULL, feat = NULL;
1718
1719     TRACE("%s %s %i %i\n", debugstr_a(szProduct), debugstr_a(szFeature),
1720           dwInstallMode, dwReserved);
1721
1722     prod = strdupAtoW( szProduct );
1723     if (szProduct && !prod)
1724         goto end;
1725
1726     feat = strdupAtoW( szFeature );
1727     if (szFeature && !feat)
1728         goto end;
1729
1730     ret = MsiUseFeatureExW( prod, feat, dwInstallMode, dwReserved );
1731
1732 end:
1733     msi_free( prod );
1734     msi_free( feat );
1735
1736     return ret;
1737 }
1738
1739 /***********************************************************************
1740  * MsiUseFeatureW             [MSI.@]
1741  */
1742 INSTALLSTATE WINAPI MsiUseFeatureW( LPCWSTR szProduct, LPCWSTR szFeature )
1743 {
1744     return MsiUseFeatureExW(szProduct, szFeature, 0, 0);
1745 }
1746
1747 /***********************************************************************
1748  * MsiUseFeatureA             [MSI.@]
1749  */
1750 INSTALLSTATE WINAPI MsiUseFeatureA( LPCSTR szProduct, LPCSTR szFeature )
1751 {
1752     return MsiUseFeatureExA(szProduct, szFeature, 0, 0);
1753 }
1754
1755 /***********************************************************************
1756  * MSI_ProvideQualifiedComponentEx [internal]
1757  */
1758 static UINT WINAPI MSI_ProvideQualifiedComponentEx(LPCWSTR szComponent,
1759                 LPCWSTR szQualifier, DWORD dwInstallMode, LPCWSTR szProduct,
1760                 DWORD Unused1, DWORD Unused2, awstring *lpPathBuf,
1761                 LPDWORD pcchPathBuf)
1762 {
1763     WCHAR product[MAX_FEATURE_CHARS+1], component[MAX_FEATURE_CHARS+1],
1764           feature[MAX_FEATURE_CHARS+1];
1765     LPWSTR info;
1766     HKEY hkey;
1767     DWORD sz;
1768     UINT rc;
1769
1770     TRACE("%s %s %i %s %i %i %p %p\n", debugstr_w(szComponent),
1771           debugstr_w(szQualifier), dwInstallMode, debugstr_w(szProduct),
1772           Unused1, Unused2, lpPathBuf, pcchPathBuf);
1773
1774     rc = MSIREG_OpenUserComponentsKey(szComponent, &hkey, FALSE);
1775     if (rc != ERROR_SUCCESS)
1776         return ERROR_INDEX_ABSENT;
1777
1778     info = msi_reg_get_val_str( hkey, szQualifier );
1779     RegCloseKey(hkey);
1780
1781     if (!info)
1782         return ERROR_INDEX_ABSENT;
1783
1784     MsiDecomposeDescriptorW(info, product, feature, component, &sz);
1785
1786     if (!szProduct)
1787         rc = MSI_GetComponentPath(product, component, lpPathBuf, pcchPathBuf);
1788     else
1789         rc = MSI_GetComponentPath(szProduct, component, lpPathBuf, pcchPathBuf);
1790
1791     msi_free( info );
1792
1793     if (rc != INSTALLSTATE_LOCAL)
1794         return ERROR_FILE_NOT_FOUND;
1795
1796     return ERROR_SUCCESS;
1797 }
1798
1799 /***********************************************************************
1800  * MsiProvideQualifiedComponentExW [MSI.@]
1801  */
1802 UINT WINAPI MsiProvideQualifiedComponentExW(LPCWSTR szComponent,
1803                 LPCWSTR szQualifier, DWORD dwInstallMode, LPCWSTR szProduct,
1804                 DWORD Unused1, DWORD Unused2, LPWSTR lpPathBuf,
1805                 LPDWORD pcchPathBuf)
1806 {
1807     awstring path;
1808
1809     path.unicode = TRUE;
1810     path.str.w = lpPathBuf;
1811
1812     return MSI_ProvideQualifiedComponentEx(szComponent, szQualifier,
1813             dwInstallMode, szProduct, Unused1, Unused2, &path, pcchPathBuf);
1814 }
1815
1816 /***********************************************************************
1817  * MsiProvideQualifiedComponentExA [MSI.@]
1818  */
1819 UINT WINAPI MsiProvideQualifiedComponentExA(LPCSTR szComponent,
1820                 LPCSTR szQualifier, DWORD dwInstallMode, LPCSTR szProduct,
1821                 DWORD Unused1, DWORD Unused2, LPSTR lpPathBuf,
1822                 LPDWORD pcchPathBuf)
1823 {
1824     LPWSTR szwComponent, szwQualifier = NULL, szwProduct = NULL;
1825     UINT r = ERROR_OUTOFMEMORY;
1826     awstring path;
1827
1828     TRACE("%s %s %u %s %u %u %p %p\n", debugstr_a(szComponent),
1829           debugstr_a(szQualifier), dwInstallMode, debugstr_a(szProduct),
1830           Unused1, Unused2, lpPathBuf, pcchPathBuf);
1831
1832     szwComponent = strdupAtoW( szComponent );
1833     if (szComponent && !szwComponent)
1834         goto end;
1835
1836     szwQualifier = strdupAtoW( szQualifier );
1837     if (szQualifier && !szwQualifier)
1838         goto end;
1839
1840     szwProduct = strdupAtoW( szProduct );
1841     if (szProduct && !szwProduct)
1842         goto end;
1843
1844     path.unicode = FALSE;
1845     path.str.a = lpPathBuf;
1846
1847     r = MSI_ProvideQualifiedComponentEx(szwComponent, szwQualifier,
1848                               dwInstallMode, szwProduct, Unused1,
1849                               Unused2, &path, pcchPathBuf);
1850 end:
1851     msi_free(szwProduct);
1852     msi_free(szwComponent);
1853     msi_free(szwQualifier);
1854
1855     return r;
1856 }
1857
1858 /***********************************************************************
1859  * MsiProvideQualifiedComponentW [MSI.@]
1860  */
1861 UINT WINAPI MsiProvideQualifiedComponentW( LPCWSTR szComponent,
1862                 LPCWSTR szQualifier, DWORD dwInstallMode, LPWSTR lpPathBuf,
1863                 LPDWORD pcchPathBuf)
1864 {
1865     return MsiProvideQualifiedComponentExW(szComponent, szQualifier, 
1866                     dwInstallMode, NULL, 0, 0, lpPathBuf, pcchPathBuf);
1867 }
1868
1869 /***********************************************************************
1870  * MsiProvideQualifiedComponentA [MSI.@]
1871  */
1872 UINT WINAPI MsiProvideQualifiedComponentA( LPCSTR szComponent,
1873                 LPCSTR szQualifier, DWORD dwInstallMode, LPSTR lpPathBuf,
1874                 LPDWORD pcchPathBuf)
1875 {
1876     return MsiProvideQualifiedComponentExA(szComponent, szQualifier,
1877                               dwInstallMode, NULL, 0, 0, lpPathBuf, pcchPathBuf);
1878 }
1879
1880 /***********************************************************************
1881  * MSI_GetUserInfo [internal]
1882  */
1883 static USERINFOSTATE WINAPI MSI_GetUserInfo(LPCWSTR szProduct,
1884                 awstring *lpUserNameBuf, LPDWORD pcchUserNameBuf,
1885                 awstring *lpOrgNameBuf, LPDWORD pcchOrgNameBuf,
1886                 awstring *lpSerialBuf, LPDWORD pcchSerialBuf)
1887 {
1888     HKEY hkey;
1889     LPWSTR user, org, serial;
1890     UINT r;
1891     USERINFOSTATE state;
1892
1893     TRACE("%s %p %p %p %p %p %p\n",debugstr_w(szProduct), lpUserNameBuf,
1894           pcchUserNameBuf, lpOrgNameBuf, pcchOrgNameBuf, lpSerialBuf,
1895           pcchSerialBuf);
1896
1897     if (!szProduct)
1898         return USERINFOSTATE_INVALIDARG;
1899
1900     r = MSIREG_OpenUninstallKey(szProduct, &hkey, FALSE);
1901     if (r != ERROR_SUCCESS)
1902         return USERINFOSTATE_UNKNOWN;
1903
1904     user = msi_reg_get_val_str( hkey, INSTALLPROPERTY_REGOWNERW );
1905     org = msi_reg_get_val_str( hkey, INSTALLPROPERTY_REGCOMPANYW );
1906     serial = msi_reg_get_val_str( hkey, INSTALLPROPERTY_PRODUCTIDW );
1907
1908     RegCloseKey(hkey);
1909
1910     state = USERINFOSTATE_PRESENT;
1911
1912     if (user)
1913     {
1914         r = msi_strcpy_to_awstring( user, lpUserNameBuf, pcchUserNameBuf );
1915         if (r == ERROR_MORE_DATA)
1916             state = USERINFOSTATE_MOREDATA;
1917     }
1918     else
1919         state = USERINFOSTATE_ABSENT;
1920     if (org)
1921     {
1922         r = msi_strcpy_to_awstring( org, lpOrgNameBuf, pcchOrgNameBuf );
1923         if (r == ERROR_MORE_DATA && state == USERINFOSTATE_PRESENT)
1924             state = USERINFOSTATE_MOREDATA;
1925     }
1926     /* msdn states: The user information is considered to be present even in the absence of a company name. */
1927     if (serial)
1928     {
1929         r = msi_strcpy_to_awstring( serial, lpSerialBuf, pcchSerialBuf );
1930         if (r == ERROR_MORE_DATA && state == USERINFOSTATE_PRESENT)
1931             state = USERINFOSTATE_MOREDATA;
1932     }
1933     else
1934         state = USERINFOSTATE_ABSENT;
1935
1936     msi_free( user );
1937     msi_free( org );
1938     msi_free( serial );
1939
1940     return state;
1941 }
1942
1943 /***********************************************************************
1944  * MsiGetUserInfoW [MSI.@]
1945  */
1946 USERINFOSTATE WINAPI MsiGetUserInfoW(LPCWSTR szProduct,
1947                 LPWSTR lpUserNameBuf, LPDWORD pcchUserNameBuf,
1948                 LPWSTR lpOrgNameBuf, LPDWORD pcchOrgNameBuf,
1949                 LPWSTR lpSerialBuf, LPDWORD pcchSerialBuf)
1950 {
1951     awstring user, org, serial;
1952
1953     user.unicode = TRUE;
1954     user.str.w = lpUserNameBuf;
1955     org.unicode = TRUE;
1956     org.str.w = lpOrgNameBuf;
1957     serial.unicode = TRUE;
1958     serial.str.w = lpSerialBuf;
1959
1960     return MSI_GetUserInfo( szProduct, &user, pcchUserNameBuf,
1961                             &org, pcchOrgNameBuf,
1962                             &serial, pcchSerialBuf );
1963 }
1964
1965 USERINFOSTATE WINAPI MsiGetUserInfoA(LPCSTR szProduct,
1966                 LPSTR lpUserNameBuf, LPDWORD pcchUserNameBuf,
1967                 LPSTR lpOrgNameBuf, LPDWORD pcchOrgNameBuf,
1968                 LPSTR lpSerialBuf, LPDWORD pcchSerialBuf)
1969 {
1970     awstring user, org, serial;
1971     LPWSTR prod;
1972     UINT r;
1973
1974     prod = strdupAtoW( szProduct );
1975     if (szProduct && !prod)
1976         return ERROR_OUTOFMEMORY;
1977
1978     user.unicode = FALSE;
1979     user.str.a = lpUserNameBuf;
1980     org.unicode = FALSE;
1981     org.str.a = lpOrgNameBuf;
1982     serial.unicode = FALSE;
1983     serial.str.a = lpSerialBuf;
1984
1985     r = MSI_GetUserInfo( prod, &user, pcchUserNameBuf,
1986                          &org, pcchOrgNameBuf,
1987                          &serial, pcchSerialBuf );
1988
1989     msi_free( prod );
1990
1991     return r;
1992 }
1993
1994 UINT WINAPI MsiCollectUserInfoW(LPCWSTR szProduct)
1995 {
1996     MSIHANDLE handle;
1997     UINT rc;
1998     MSIPACKAGE *package;
1999     static const WCHAR szFirstRun[] = {'F','i','r','s','t','R','u','n',0};
2000
2001     TRACE("(%s)\n",debugstr_w(szProduct));
2002
2003     rc = MsiOpenProductW(szProduct,&handle);
2004     if (rc != ERROR_SUCCESS)
2005         return ERROR_INVALID_PARAMETER;
2006
2007     package = msihandle2msiinfo(handle, MSIHANDLETYPE_PACKAGE);
2008     rc = ACTION_PerformUIAction(package, szFirstRun, -1);
2009     msiobj_release( &package->hdr );
2010
2011     MsiCloseHandle(handle);
2012
2013     return rc;
2014 }
2015
2016 UINT WINAPI MsiCollectUserInfoA(LPCSTR szProduct)
2017 {
2018     MSIHANDLE handle;
2019     UINT rc;
2020     MSIPACKAGE *package;
2021     static const WCHAR szFirstRun[] = {'F','i','r','s','t','R','u','n',0};
2022
2023     TRACE("(%s)\n",debugstr_a(szProduct));
2024
2025     rc = MsiOpenProductA(szProduct,&handle);
2026     if (rc != ERROR_SUCCESS)
2027         return ERROR_INVALID_PARAMETER;
2028
2029     package = msihandle2msiinfo(handle, MSIHANDLETYPE_PACKAGE);
2030     rc = ACTION_PerformUIAction(package, szFirstRun, -1);
2031     msiobj_release( &package->hdr );
2032
2033     MsiCloseHandle(handle);
2034
2035     return rc;
2036 }
2037
2038 /***********************************************************************
2039  * MsiConfigureFeatureA            [MSI.@]
2040  */
2041 UINT WINAPI MsiConfigureFeatureA(LPCSTR szProduct, LPCSTR szFeature, INSTALLSTATE eInstallState)
2042 {
2043     LPWSTR prod, feat = NULL;
2044     UINT r = ERROR_OUTOFMEMORY;
2045
2046     TRACE("%s %s %i\n", debugstr_a(szProduct), debugstr_a(szFeature), eInstallState);
2047
2048     prod = strdupAtoW( szProduct );
2049     if (szProduct && !prod)
2050         goto end;
2051
2052     feat = strdupAtoW( szFeature );
2053     if (szFeature && !feat)
2054         goto end;
2055
2056     r = MsiConfigureFeatureW(prod, feat, eInstallState);
2057
2058 end:
2059     msi_free(feat);
2060     msi_free(prod);
2061
2062     return r;
2063 }
2064
2065 /***********************************************************************
2066  * MsiConfigureFeatureW            [MSI.@]
2067  */
2068 UINT WINAPI MsiConfigureFeatureW(LPCWSTR szProduct, LPCWSTR szFeature, INSTALLSTATE eInstallState)
2069 {
2070     static const WCHAR szCostInit[] = { 'C','o','s','t','I','n','i','t','i','a','l','i','z','e',0 };
2071     MSIPACKAGE *package = NULL;
2072     UINT r;
2073     WCHAR sourcepath[MAX_PATH], filename[MAX_PATH];
2074     DWORD sz;
2075
2076     TRACE("%s %s %i\n", debugstr_w(szProduct), debugstr_w(szFeature), eInstallState);
2077
2078     if (!szProduct || !szFeature)
2079         return ERROR_INVALID_PARAMETER;
2080
2081     switch (eInstallState)
2082     {
2083     case INSTALLSTATE_DEFAULT:
2084         /* FIXME: how do we figure out the default location? */
2085         eInstallState = INSTALLSTATE_LOCAL;
2086         break;
2087     case INSTALLSTATE_LOCAL:
2088     case INSTALLSTATE_SOURCE:
2089     case INSTALLSTATE_ABSENT:
2090     case INSTALLSTATE_ADVERTISED:
2091         break;
2092     default:
2093         return ERROR_INVALID_PARAMETER;
2094     }
2095
2096     r = MSI_OpenProductW( szProduct, &package );
2097     if (r != ERROR_SUCCESS)
2098         return r;
2099
2100     sz = sizeof(sourcepath);
2101     MsiSourceListGetInfoW(szProduct, NULL, MSIINSTALLCONTEXT_USERMANAGED,
2102                 MSICODE_PRODUCT, INSTALLPROPERTY_LASTUSEDSOURCEW, sourcepath, &sz);
2103
2104     sz = sizeof(filename);
2105     MsiSourceListGetInfoW(szProduct, NULL, MSIINSTALLCONTEXT_USERMANAGED,
2106                 MSICODE_PRODUCT, INSTALLPROPERTY_PACKAGENAMEW, filename, &sz);
2107
2108     lstrcatW( sourcepath, filename );
2109
2110     MsiSetInternalUI( INSTALLUILEVEL_BASIC, NULL );
2111
2112     r = ACTION_PerformUIAction( package, szCostInit, -1 );
2113     if (r != ERROR_SUCCESS)
2114         goto end;
2115
2116     r = MSI_SetFeatureStateW( package, szFeature, eInstallState);
2117     if (r != ERROR_SUCCESS)
2118         goto end;
2119
2120     r = MSI_InstallPackage( package, sourcepath, NULL );
2121
2122 end:
2123     msiobj_release( &package->hdr );
2124
2125     return r;
2126 }
2127
2128 /***********************************************************************
2129  * MsiCreateAndVerifyInstallerDirectory [MSI.@]
2130  *
2131  * Notes: undocumented
2132  */
2133 UINT WINAPI MsiCreateAndVerifyInstallerDirectory(DWORD dwReserved)
2134 {
2135     WCHAR path[MAX_PATH];
2136
2137     TRACE("%d\n", dwReserved);
2138
2139     if (dwReserved)
2140     {
2141         FIXME("dwReserved=%d\n", dwReserved);
2142         return ERROR_INVALID_PARAMETER;
2143     }
2144
2145     if (!GetWindowsDirectoryW(path, MAX_PATH))
2146         return ERROR_FUNCTION_FAILED;
2147
2148     lstrcatW(path, installerW);
2149
2150     if (!CreateDirectoryW(path, NULL))
2151         return ERROR_FUNCTION_FAILED;
2152
2153     return ERROR_SUCCESS;
2154 }
2155
2156 /***********************************************************************
2157  * MsiGetShortcutTargetA           [MSI.@]
2158  */
2159 UINT WINAPI MsiGetShortcutTargetA( LPCSTR szShortcutTarget,
2160                                    LPSTR szProductCode, LPSTR szFeatureId,
2161                                    LPSTR szComponentCode )
2162 {
2163     LPWSTR target;
2164     const int len = MAX_FEATURE_CHARS+1;
2165     WCHAR product[MAX_FEATURE_CHARS+1], feature[MAX_FEATURE_CHARS+1], component[MAX_FEATURE_CHARS+1];
2166     UINT r;
2167
2168     target = strdupAtoW( szShortcutTarget );
2169     if (szShortcutTarget && !target )
2170         return ERROR_OUTOFMEMORY;
2171     product[0] = 0;
2172     feature[0] = 0;
2173     component[0] = 0;
2174     r = MsiGetShortcutTargetW( target, product, feature, component );
2175     msi_free( target );
2176     if (r == ERROR_SUCCESS)
2177     {
2178         WideCharToMultiByte( CP_ACP, 0, product, -1, szProductCode, len, NULL, NULL );
2179         WideCharToMultiByte( CP_ACP, 0, feature, -1, szFeatureId, len, NULL, NULL );
2180         WideCharToMultiByte( CP_ACP, 0, component, -1, szComponentCode, len, NULL, NULL );
2181     }
2182     return r;
2183 }
2184
2185 /***********************************************************************
2186  * MsiGetShortcutTargetW           [MSI.@]
2187  */
2188 UINT WINAPI MsiGetShortcutTargetW( LPCWSTR szShortcutTarget,
2189                                    LPWSTR szProductCode, LPWSTR szFeatureId,
2190                                    LPWSTR szComponentCode )
2191 {
2192     IShellLinkDataList *dl = NULL;
2193     IPersistFile *pf = NULL;
2194     LPEXP_DARWIN_LINK darwin = NULL;
2195     HRESULT r, init;
2196
2197     TRACE("%s %p %p %p\n", debugstr_w(szShortcutTarget),
2198           szProductCode, szFeatureId, szComponentCode );
2199
2200     init = CoInitialize(NULL);
2201
2202     r = CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
2203                           &IID_IPersistFile, (LPVOID*) &pf );
2204     if( SUCCEEDED( r ) )
2205     {
2206         r = IPersistFile_Load( pf, szShortcutTarget,
2207                                STGM_READ | STGM_SHARE_DENY_WRITE );
2208         if( SUCCEEDED( r ) )
2209         {
2210             r = IPersistFile_QueryInterface( pf, &IID_IShellLinkDataList,
2211                                              (LPVOID*) &dl );
2212             if( SUCCEEDED( r ) )
2213             {
2214                 IShellLinkDataList_CopyDataBlock( dl, EXP_DARWIN_ID_SIG,
2215                                                   (LPVOID) &darwin );
2216                 IShellLinkDataList_Release( dl );
2217             }
2218         }
2219         IPersistFile_Release( pf );
2220     }
2221
2222     if (SUCCEEDED(init))
2223         CoUninitialize();
2224
2225     TRACE("darwin = %p\n", darwin);
2226
2227     if (darwin)
2228     {
2229         DWORD sz;
2230         UINT ret;
2231
2232         ret = MsiDecomposeDescriptorW( darwin->szwDarwinID,
2233                   szProductCode, szFeatureId, szComponentCode, &sz );
2234         LocalFree( darwin );
2235         return ret;
2236     }
2237
2238     return ERROR_FUNCTION_FAILED;
2239 }
2240
2241 UINT WINAPI MsiReinstallFeatureW( LPCWSTR szProduct, LPCWSTR szFeature,
2242                                   DWORD dwReinstallMode )
2243 {
2244     MSIPACKAGE* package = NULL;
2245     UINT r;
2246     WCHAR sourcepath[MAX_PATH];
2247     WCHAR filename[MAX_PATH];
2248     static const WCHAR szLogVerbose[] = {
2249         ' ','L','O','G','V','E','R','B','O','S','E',0 };
2250     static const WCHAR szInstalled[] = { 'I','n','s','t','a','l','l','e','d',0};
2251     static const WCHAR szReinstall[] = {'R','E','I','N','S','T','A','L','L',0};
2252     static const WCHAR szReinstallMode[] = {'R','E','I','N','S','T','A','L','L','M','O','D','E',0};
2253     static const WCHAR szOne[] = {'1',0};
2254     WCHAR reinstallmode[11];
2255     LPWSTR ptr;
2256     DWORD sz;
2257
2258     FIXME("%s %s %i\n", debugstr_w(szProduct), debugstr_w(szFeature),
2259                            dwReinstallMode);
2260
2261     ptr = reinstallmode;
2262
2263     if (dwReinstallMode & REINSTALLMODE_FILEMISSING)
2264         *ptr++ = 'p';
2265     if (dwReinstallMode & REINSTALLMODE_FILEOLDERVERSION)
2266         *ptr++ = 'o';
2267     if (dwReinstallMode & REINSTALLMODE_FILEEQUALVERSION)
2268         *ptr++ = 'w';
2269     if (dwReinstallMode & REINSTALLMODE_FILEEXACT)
2270         *ptr++ = 'd';
2271     if (dwReinstallMode & REINSTALLMODE_FILEVERIFY)
2272         *ptr++ = 'c';
2273     if (dwReinstallMode & REINSTALLMODE_FILEREPLACE)
2274         *ptr++ = 'a';
2275     if (dwReinstallMode & REINSTALLMODE_USERDATA)
2276         *ptr++ = 'u';
2277     if (dwReinstallMode & REINSTALLMODE_MACHINEDATA)
2278         *ptr++ = 'm';
2279     if (dwReinstallMode & REINSTALLMODE_SHORTCUT)
2280         *ptr++ = 's';
2281     if (dwReinstallMode & REINSTALLMODE_PACKAGE)
2282         *ptr++ = 'v';
2283     *ptr = 0;
2284     
2285     sz = sizeof(sourcepath);
2286     MsiSourceListGetInfoW(szProduct, NULL, MSIINSTALLCONTEXT_USERMANAGED, 
2287             MSICODE_PRODUCT, INSTALLPROPERTY_LASTUSEDSOURCEW, sourcepath, &sz);
2288
2289     sz = sizeof(filename);
2290     MsiSourceListGetInfoW(szProduct, NULL, MSIINSTALLCONTEXT_USERMANAGED, 
2291             MSICODE_PRODUCT, INSTALLPROPERTY_PACKAGENAMEW, filename, &sz);
2292
2293     lstrcatW( sourcepath, filename );
2294
2295     if (dwReinstallMode & REINSTALLMODE_PACKAGE)
2296         r = MSI_OpenPackageW( sourcepath, &package );
2297     else
2298         r = MSI_OpenProductW( szProduct, &package );
2299
2300     if (r != ERROR_SUCCESS)
2301         return r;
2302
2303     MSI_SetPropertyW( package, szReinstallMode, reinstallmode );
2304     MSI_SetPropertyW( package, szInstalled, szOne );
2305     MSI_SetPropertyW( package, szLogVerbose, szOne );
2306     MSI_SetPropertyW( package, szReinstall, szFeature );
2307
2308     r = MSI_InstallPackage( package, sourcepath, NULL );
2309
2310     msiobj_release( &package->hdr );
2311
2312     return r;
2313 }
2314
2315 UINT WINAPI MsiReinstallFeatureA( LPCSTR szProduct, LPCSTR szFeature,
2316                                   DWORD dwReinstallMode )
2317 {
2318     LPWSTR wszProduct;
2319     LPWSTR wszFeature;
2320     UINT rc;
2321
2322     TRACE("%s %s %i\n", debugstr_a(szProduct), debugstr_a(szFeature),
2323                            dwReinstallMode);
2324
2325     wszProduct = strdupAtoW(szProduct);
2326     wszFeature = strdupAtoW(szFeature);
2327
2328     rc = MsiReinstallFeatureW(wszProduct, wszFeature, dwReinstallMode);
2329
2330     msi_free(wszProduct);
2331     msi_free(wszFeature);
2332     return rc;
2333 }
2334
2335 typedef struct
2336 {
2337     unsigned int i[2];
2338     unsigned int buf[4];
2339     unsigned char in[64];
2340     unsigned char digest[16];
2341 } MD5_CTX;
2342
2343 extern VOID WINAPI MD5Init( MD5_CTX *);
2344 extern VOID WINAPI MD5Update( MD5_CTX *, const unsigned char *, unsigned int );
2345 extern VOID WINAPI MD5Final( MD5_CTX *);
2346
2347 /***********************************************************************
2348  * MsiGetFileHashW            [MSI.@]
2349  */
2350 UINT WINAPI MsiGetFileHashW( LPCWSTR szFilePath, DWORD dwOptions,
2351                              PMSIFILEHASHINFO pHash )
2352 {
2353     HANDLE handle, mapping;
2354     void *p;
2355     DWORD length;
2356     UINT r = ERROR_FUNCTION_FAILED;
2357
2358     TRACE("%s %08x %p\n", debugstr_w(szFilePath), dwOptions, pHash );
2359
2360     if (!szFilePath)
2361         return ERROR_INVALID_PARAMETER;
2362
2363     if (!*szFilePath)
2364         return ERROR_PATH_NOT_FOUND;
2365
2366     if (dwOptions)
2367         return ERROR_INVALID_PARAMETER;
2368     if (!pHash)
2369         return ERROR_INVALID_PARAMETER;
2370     if (pHash->dwFileHashInfoSize < sizeof *pHash)
2371         return ERROR_INVALID_PARAMETER;
2372
2373     handle = CreateFileW( szFilePath, GENERIC_READ,
2374                           FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, NULL );
2375     if (handle == INVALID_HANDLE_VALUE)
2376         return ERROR_FILE_NOT_FOUND;
2377
2378     length = GetFileSize( handle, NULL );
2379
2380     mapping = CreateFileMappingW( handle, NULL, PAGE_READONLY, 0, 0, NULL );
2381     if (mapping)
2382     {
2383         p = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, length );
2384         if (p)
2385         {
2386             MD5_CTX ctx;
2387
2388             MD5Init( &ctx );
2389             MD5Update( &ctx, p, length );
2390             MD5Final( &ctx );
2391             UnmapViewOfFile( p );
2392
2393             memcpy( pHash->dwData, &ctx.digest, sizeof pHash->dwData );
2394             r = ERROR_SUCCESS;
2395         }
2396         CloseHandle( mapping );
2397     }
2398     CloseHandle( handle );
2399
2400     return r;
2401 }
2402
2403 /***********************************************************************
2404  * MsiGetFileHashA            [MSI.@]
2405  */
2406 UINT WINAPI MsiGetFileHashA( LPCSTR szFilePath, DWORD dwOptions,
2407                              PMSIFILEHASHINFO pHash )
2408 {
2409     LPWSTR file;
2410     UINT r;
2411
2412     TRACE("%s %08x %p\n", debugstr_a(szFilePath), dwOptions, pHash );
2413
2414     file = strdupAtoW( szFilePath );
2415     if (szFilePath && !file)
2416         return ERROR_OUTOFMEMORY;
2417
2418     r = MsiGetFileHashW( file, dwOptions, pHash );
2419     msi_free( file );
2420     return r;
2421 }
2422
2423 /***********************************************************************
2424  * MsiAdvertiseScriptW        [MSI.@]
2425  */
2426 UINT WINAPI MsiAdvertiseScriptW( LPCWSTR szScriptFile, DWORD dwFlags,
2427                                  PHKEY phRegData, BOOL fRemoveItems )
2428 {
2429     FIXME("%s %08x %p %d\n",
2430           debugstr_w( szScriptFile ), dwFlags, phRegData, fRemoveItems );
2431     return ERROR_CALL_NOT_IMPLEMENTED;
2432 }
2433
2434 /***********************************************************************
2435  * MsiAdvertiseScriptA        [MSI.@]
2436  */
2437 UINT WINAPI MsiAdvertiseScriptA( LPCSTR szScriptFile, DWORD dwFlags,
2438                                  PHKEY phRegData, BOOL fRemoveItems )
2439 {
2440     FIXME("%s %08x %p %d\n",
2441           debugstr_a( szScriptFile ), dwFlags, phRegData, fRemoveItems );
2442     return ERROR_CALL_NOT_IMPLEMENTED;
2443 }