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