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