Reverse the order for deleting the items in resetcontent to correctly
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 "msiquery.h"
34 #include "msipriv.h"
35 #include "objidl.h"
36 #include "wincrypt.h"
37 #include "wine/unicode.h"
38 #include "objbase.h"
39 #include "winver.h"
40 #include "winuser.h"
41
42 #include "initguid.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(msi);
45
46 /*
47  * The MSVC headers define the MSIDBOPEN_* macros cast to LPCTSTR,
48  *  which is a problem because LPCTSTR isn't defined when compiling wine.
49  * To work around this problem, we need to define LPCTSTR as LPCWSTR here,
50  *  and make sure to only use it in W functions.
51  */
52 #define LPCTSTR LPCWSTR
53
54 DEFINE_GUID( CLSID_MsiDatabase, 0x000c1084, 0x0000, 0x0000,
55              0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
56
57 /* the UI level */
58 INSTALLUILEVEL gUILevel = INSTALLUILEVEL_BASIC;
59 HWND           gUIhwnd = 0;
60 INSTALLUI_HANDLERA gUIHandlerA = NULL;
61 INSTALLUI_HANDLERW gUIHandlerW = NULL;
62 DWORD gUIFilter = 0;
63 LPVOID gUIContext = NULL;
64 WCHAR gszLogFile[MAX_PATH];
65 HINSTANCE msi_hInstance;
66
67 /*
68  *  .MSI  file format
69  *
70  *  A .msi file is a structured storage file.
71  *  It should contain a number of streams.
72  */
73
74 VOID MSI_CloseDatabase( MSIOBJECTHDR *arg )
75 {
76     MSIDATABASE *db = (MSIDATABASE *) arg;
77     DWORD r;
78
79     free_cached_tables( db );
80     r = IStorage_Release( db->storage );
81     if( r )
82         ERR("database reference count was not zero (%ld)\n", r);
83 }
84
85 UINT MSI_OpenDatabaseW(LPCWSTR szDBPath, LPCWSTR szPersist, MSIDATABASE **pdb)
86 {
87     IStorage *stg = NULL;
88     HRESULT r;
89     MSIDATABASE *db = NULL;
90     UINT ret = ERROR_FUNCTION_FAILED;
91     LPWSTR szMode;
92     STATSTG stat;
93
94     TRACE("%s %s\n",debugstr_w(szDBPath),debugstr_w(szPersist) );
95
96     if( !pdb )
97         return ERROR_INVALID_PARAMETER;
98
99     szMode = (LPWSTR) szPersist;
100     if( HIWORD( szPersist ) )
101     {
102         /* UINT len = lstrlenW( szPerist ) + 1; */
103         FIXME("don't support persist files yet\b");
104         return ERROR_INVALID_PARAMETER;
105         /* szMode = HeapAlloc( GetProcessHeap(), 0, len * sizeof (DWORD) ); */
106     }
107     else if( szPersist == MSIDBOPEN_READONLY )
108     {
109         r = StgOpenStorage( szDBPath, NULL,
110               STGM_DIRECT|STGM_READ|STGM_SHARE_DENY_WRITE, NULL, 0, &stg);
111     }
112     else if( szPersist == MSIDBOPEN_CREATE )
113     {
114         r = StgCreateDocfile( szDBPath, 
115               STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, 0, &stg);
116         if( r == ERROR_SUCCESS )
117         {
118             IStorage_SetClass( stg, &CLSID_MsiDatabase );
119             r = init_string_table( stg );
120         }
121     }
122     else if( szPersist == MSIDBOPEN_TRANSACT )
123     {
124         r = StgOpenStorage( szDBPath, NULL,
125               STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
126     }
127     else
128     {
129         ERR("unknown flag %p\n",szPersist);
130         return ERROR_INVALID_PARAMETER;
131     }
132
133     if( FAILED( r ) )
134     {
135         FIXME("open failed r = %08lx!\n",r);
136         return ERROR_FUNCTION_FAILED;
137     }
138
139     r = IStorage_Stat( stg, &stat, STATFLAG_NONAME );
140     if( FAILED( r ) )
141     {
142         FIXME("Failed to stat storage\n");
143         goto end;
144     }
145
146     if( memcmp( &stat.clsid, &CLSID_MsiDatabase, sizeof (GUID) ) )
147     {
148         ERR("storage GUID is not a MSI database GUID %s\n",
149              debugstr_guid(&stat.clsid) );
150         goto end;
151     }
152
153
154     db = alloc_msiobject( MSIHANDLETYPE_DATABASE, sizeof (MSIDATABASE),
155                               MSI_CloseDatabase );
156     if( !db )
157     {
158         FIXME("Failed to allocate a handle\n");
159         goto end;
160     }
161
162     if( TRACE_ON( msi ) )
163         enum_stream_names( stg );
164
165     db->storage = stg;
166     db->mode = szMode;
167
168     ret = load_string_table( db );
169     if( ret != ERROR_SUCCESS )
170         goto end;
171
172     msiobj_addref( &db->hdr );
173     IStorage_AddRef( stg );
174     *pdb = db;
175
176 end:
177     if( db )
178         msiobj_release( &db->hdr );
179     if( stg )
180         IStorage_Release( stg );
181
182     return ret;
183 }
184
185 UINT WINAPI MsiOpenDatabaseW(LPCWSTR szDBPath, LPCWSTR szPersist, MSIHANDLE *phDB)
186 {
187     MSIDATABASE *db;
188     UINT ret;
189
190     TRACE("%s %s %p\n",debugstr_w(szDBPath),debugstr_w(szPersist), phDB);
191
192     ret = MSI_OpenDatabaseW( szDBPath, szPersist, &db );
193     if( ret == ERROR_SUCCESS )
194     {
195         *phDB = alloc_msihandle( &db->hdr );
196         msiobj_release( &db->hdr );
197     }
198
199     return ret;
200 }
201
202 UINT WINAPI MsiOpenDatabaseA(LPCSTR szDBPath, LPCSTR szPersist, MSIHANDLE *phDB)
203 {
204     HRESULT r = ERROR_FUNCTION_FAILED;
205     LPWSTR szwDBPath = NULL, szwPersist = NULL;
206     UINT len;
207
208     TRACE("%s %s %p\n", debugstr_a(szDBPath), debugstr_a(szPersist), phDB);
209
210     if( szDBPath )
211     {
212         len = MultiByteToWideChar( CP_ACP, 0, szDBPath, -1, NULL, 0 );
213         szwDBPath = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
214         if( !szwDBPath )
215             goto end;
216         MultiByteToWideChar( CP_ACP, 0, szDBPath, -1, szwDBPath, len );
217     }
218
219     if( HIWORD(szPersist) )
220     {
221         len = MultiByteToWideChar( CP_ACP, 0, szPersist, -1, NULL, 0 );
222         szwPersist = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
223         if( !szwPersist )
224             goto end;
225         MultiByteToWideChar( CP_ACP, 0, szPersist, -1, szwPersist, len );
226     }
227     else
228         szwPersist = (LPWSTR) szPersist;
229
230     r = MsiOpenDatabaseW( szwDBPath, szwPersist, phDB );
231
232 end:
233     HeapFree( GetProcessHeap(), 0, szwPersist );
234     HeapFree( GetProcessHeap(), 0, szwDBPath );
235
236     return r;
237 }
238
239 UINT WINAPI MsiOpenProductA(LPCSTR szProduct, MSIHANDLE *phProduct)
240 {
241     UINT len, ret;
242     LPWSTR szwProd = NULL;
243
244     TRACE("%s %p\n",debugstr_a(szProduct), phProduct);
245
246     if( szProduct )
247     {
248         len = MultiByteToWideChar( CP_ACP, 0, szProduct, -1, NULL, 0 );
249         szwProd = HeapAlloc( GetProcessHeap(), 0, len * sizeof (WCHAR) );
250         if( szwProd )
251             MultiByteToWideChar( CP_ACP, 0, szProduct, -1, szwProd, len );
252     }
253
254     ret = MsiOpenProductW( szwProd, phProduct );
255
256     HeapFree( GetProcessHeap(), 0, szwProd );
257
258     return ret;
259 }
260
261 UINT WINAPI MsiOpenProductW(LPCWSTR szProduct, MSIHANDLE *phProduct)
262 {
263     static const WCHAR szLocalPackage[] = {
264         'L','o','c','a','l','P','a','c','k','a','g','e', 0
265     };
266     LPWSTR path = NULL;
267     UINT r;
268     HKEY hKeyProduct = NULL;
269     DWORD count, type;
270
271     TRACE("%s %p\n",debugstr_w(szProduct), phProduct);
272
273     r = MSIREG_OpenUninstallKey(szProduct,&hKeyProduct,FALSE);
274     if( r != ERROR_SUCCESS )
275     {
276         r = ERROR_UNKNOWN_PRODUCT;
277         goto end;
278     }
279
280     /* find the size of the path */
281     type = count = 0;
282     r = RegQueryValueExW( hKeyProduct, szLocalPackage,
283                           NULL, &type, NULL, &count );
284     if( r != ERROR_SUCCESS )
285     {
286         r = ERROR_UNKNOWN_PRODUCT;
287         goto end;
288     }
289
290     /* now alloc and fetch the path of the database to open */
291     path = HeapAlloc( GetProcessHeap(), 0, count );
292     if( !path )
293         goto end;
294
295     r = RegQueryValueExW( hKeyProduct, szLocalPackage,
296                           NULL, &type, (LPBYTE) path, &count );
297     if( r != ERROR_SUCCESS )
298     {
299         r = ERROR_UNKNOWN_PRODUCT;
300         goto end;
301     }
302
303     r = MsiOpenPackageW( path, phProduct );
304
305 end:
306     HeapFree( GetProcessHeap(), 0, path );
307     if( hKeyProduct )
308         RegCloseKey( hKeyProduct );
309
310     return r;
311 }
312
313 UINT WINAPI MsiAdvertiseProductA(LPCSTR szPackagePath, LPCSTR szScriptfilePath,
314                 LPCSTR szTransforms, LANGID lgidLanguage)
315 {
316     FIXME("%s %s %s %08x\n",debugstr_a(szPackagePath),
317           debugstr_a(szScriptfilePath), debugstr_a(szTransforms), lgidLanguage);
318     return ERROR_CALL_NOT_IMPLEMENTED;
319 }
320
321 UINT WINAPI MsiAdvertiseProductW(LPCWSTR szPackagePath, LPCWSTR szScriptfilePath,
322                 LPCWSTR szTransforms, LANGID lgidLanguage)
323 {
324     FIXME("%s %s %s %08x\n",debugstr_w(szPackagePath),
325           debugstr_w(szScriptfilePath), debugstr_w(szTransforms), lgidLanguage);
326     return ERROR_CALL_NOT_IMPLEMENTED;
327 }
328
329 UINT WINAPI MsiAdvertiseProductExA(LPCSTR szPackagePath, LPCSTR szScriptfilePath,
330       LPCSTR szTransforms, LANGID lgidLanguage, DWORD dwPlatform, DWORD dwOptions)
331 {
332     FIXME("%s %s %s %08x %08lx %08lx\n", debugstr_a(szPackagePath),
333           debugstr_a(szScriptfilePath), debugstr_a(szTransforms),
334           lgidLanguage, dwPlatform, dwOptions);
335     return ERROR_CALL_NOT_IMPLEMENTED;
336 }
337
338 UINT WINAPI MsiAdvertiseProductExW( LPCWSTR szPackagePath, LPCWSTR szScriptfilePath,
339       LPCWSTR szTransforms, LANGID lgidLanguage, DWORD dwPlatform, DWORD dwOptions)
340 {
341     FIXME("%s %s %s %08x %08lx %08lx\n", debugstr_w(szPackagePath),
342           debugstr_w(szScriptfilePath), debugstr_w(szTransforms),
343           lgidLanguage, dwPlatform, dwOptions);
344     return ERROR_CALL_NOT_IMPLEMENTED;
345 }
346
347 UINT WINAPI MsiInstallProductA(LPCSTR szPackagePath, LPCSTR szCommandLine)
348 {
349     LPWSTR szwPath = NULL, szwCommand = NULL;
350     UINT r = ERROR_FUNCTION_FAILED; /* FIXME: check return code */
351
352     TRACE("%s %s\n",debugstr_a(szPackagePath), debugstr_a(szCommandLine));
353
354     if( szPackagePath )
355     {
356         UINT len = MultiByteToWideChar( CP_ACP, 0, szPackagePath, -1, NULL, 0 );
357         szwPath = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
358         if( !szwPath )
359             goto end;
360         MultiByteToWideChar( CP_ACP, 0, szPackagePath, -1, szwPath, len );
361     }
362
363     if( szCommandLine )
364     {
365         UINT len = MultiByteToWideChar( CP_ACP, 0, szCommandLine, -1, NULL, 0 );
366         szwCommand = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
367         if( !szwCommand )
368             goto end;
369         MultiByteToWideChar( CP_ACP, 0, szCommandLine, -1, szwCommand, len );
370     }
371  
372     r = MsiInstallProductW( szwPath, szwCommand );
373
374 end:
375     HeapFree( GetProcessHeap(), 0, szwPath );
376     HeapFree( GetProcessHeap(), 0, szwCommand );
377
378     return r;
379 }
380
381 UINT WINAPI MsiInstallProductW(LPCWSTR szPackagePath, LPCWSTR szCommandLine)
382 {
383     MSIPACKAGE *package = NULL;
384     UINT rc = ERROR_SUCCESS; 
385     MSIHANDLE handle;
386
387     FIXME("%s %s\n",debugstr_w(szPackagePath), debugstr_w(szCommandLine));
388
389     rc = MsiVerifyPackageW(szPackagePath);
390     if (rc != ERROR_SUCCESS)
391         return rc;
392
393     rc = MSI_OpenPackageW(szPackagePath,&package);
394     if (rc != ERROR_SUCCESS)
395         return rc;
396
397     handle = alloc_msihandle( &package->hdr );
398
399     rc = ACTION_DoTopLevelINSTALL(package, szPackagePath, szCommandLine);
400
401     MsiCloseHandle(handle);
402     msiobj_release( &package->hdr );
403     return rc;
404 }
405
406 UINT WINAPI MsiReinstallProductA(LPCSTR szProduct, DWORD dwReinstallMode)
407 {
408     FIXME("%s %08lx\n", debugstr_a(szProduct), dwReinstallMode);
409     return ERROR_CALL_NOT_IMPLEMENTED;
410 }
411
412 UINT WINAPI MsiReinstallProductW(LPCWSTR szProduct, DWORD dwReinstallMode)
413 {
414     FIXME("%s %08lx\n", debugstr_w(szProduct), dwReinstallMode);
415     return ERROR_CALL_NOT_IMPLEMENTED;
416 }
417
418 UINT WINAPI MsiApplyPatchA(LPCSTR szPatchPackage, LPCSTR szInstallPackage,
419         INSTALLTYPE eInstallType, LPCSTR szCommandLine)
420 {
421     FIXME("%s %s %d %s\n", debugstr_a(szPatchPackage), debugstr_a(szInstallPackage),
422           eInstallType, debugstr_a(szCommandLine));
423     return ERROR_CALL_NOT_IMPLEMENTED;
424 }
425
426 UINT WINAPI MsiApplyPatchW(LPCWSTR szPatchPackage, LPCWSTR szInstallPackage,
427          INSTALLTYPE eInstallType, LPCWSTR szCommandLine)
428 {
429     FIXME("%s %s %d %s\n", debugstr_w(szPatchPackage), debugstr_w(szInstallPackage),
430           eInstallType, debugstr_w(szCommandLine));
431     return ERROR_CALL_NOT_IMPLEMENTED;
432 }
433
434 UINT WINAPI MsiConfigureProductExW(LPCWSTR szProduct, int iInstallLevel,
435                         INSTALLSTATE eInstallState, LPCWSTR szCommandLine)
436 {
437     MSIHANDLE handle; 
438     MSIPACKAGE* package;
439     UINT rc;
440     HKEY hkey=0,hkey1=0;
441     DWORD sz;
442     static const WCHAR szSouceList[] = {
443         'S','o','u','r','c','e','L','i','s','t',0};
444     static const WCHAR szLUS[] = {
445         'L','a','s','t','U','s','e','d','S','o','u','r','c','e',0};
446     WCHAR sourcepath[0x200];
447     static const WCHAR szInstalled[] = {
448         ' ','I','n','s','t','a','l','l','e','d','=','1',0};
449     LPWSTR commandline;
450
451     FIXME("%s %d %d %s\n",debugstr_w(szProduct), iInstallLevel, eInstallState,
452           debugstr_w(szCommandLine));
453
454     if (eInstallState != INSTALLSTATE_LOCAL && 
455         eInstallState != INSTALLSTATE_DEFAULT)
456     {
457         FIXME("Not implemented for anything other than local installs\n");
458         return ERROR_CALL_NOT_IMPLEMENTED;
459     }
460
461     rc = MSIREG_OpenUserProductsKey(szProduct,&hkey,FALSE);
462     if (rc != ERROR_SUCCESS)
463         goto end;
464
465     rc = RegOpenKeyW(hkey,szSouceList,&hkey1);
466     if (rc != ERROR_SUCCESS)
467         goto end;
468
469     sz = sizeof(sourcepath);
470     rc = RegQueryValueExW(hkey1, szLUS, NULL, NULL,(LPBYTE)sourcepath, &sz);
471     if (rc != ERROR_SUCCESS)
472         goto end;
473
474     RegCloseKey(hkey1);
475     /*
476      * ok 1, we need to find the msi file for this product.
477      *    2, find the source dir for the files
478      *    3, do the configure/install.
479      *    4, cleanupany runonce entry. 
480      */
481
482     rc = MsiOpenProductW(szProduct,&handle);
483     if (rc != ERROR_SUCCESS)
484         goto end;
485
486     package = msihandle2msiinfo(handle, MSIHANDLETYPE_PACKAGE);
487   
488     sz = strlenW(szInstalled);
489
490     if (szCommandLine)
491         sz += strlenW(szCommandLine);
492
493     commandline = HeapAlloc(GetProcessHeap(),0,sz * sizeof(WCHAR));
494
495     if (szCommandLine) 
496         strcpyW(commandline,szCommandLine);
497     else
498         commandline[0] = 0;
499
500     if (MsiQueryProductStateW(szProduct) != INSTALLSTATE_UNKNOWN)
501         strcatW(commandline,szInstalled);
502
503     rc = ACTION_DoTopLevelINSTALL(package, sourcepath, commandline);
504
505     msiobj_release( &package->hdr );
506
507     HeapFree(GetProcessHeap(),0,commandline);
508 end:
509     RegCloseKey(hkey);
510
511     return rc;
512 }
513
514 UINT WINAPI MsiConfigureProductExA(LPCSTR szProduct, int iInstallLevel,
515                         INSTALLSTATE eInstallState, LPCSTR szCommandLine)
516 {
517     LPWSTR szwProduct = NULL;
518     LPWSTR szwCommandLine = NULL;
519     UINT hr = ERROR_FUNCTION_FAILED;
520
521     if( szProduct )
522     {
523         UINT len = MultiByteToWideChar( CP_ACP, 0, szProduct, -1, NULL, 0 );
524         szwProduct = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
525         if( !szwProduct )
526             goto end;
527         MultiByteToWideChar( CP_ACP, 0, szProduct, -1, szwProduct, len );
528     }
529
530     if( szCommandLine)
531     {
532         UINT len = MultiByteToWideChar( CP_ACP, 0, szCommandLine, -1, NULL, 0 );
533         szwCommandLine= HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
534         if( !szwCommandLine)
535             goto end;
536         MultiByteToWideChar( CP_ACP, 0, szCommandLine, -1, szwCommandLine, len );
537     }
538
539     hr = MsiConfigureProductExW( szwProduct, iInstallLevel, eInstallState,
540                                 szwCommandLine );
541 end:
542     HeapFree( GetProcessHeap(), 0, szwProduct );
543     HeapFree( GetProcessHeap(), 0, szwCommandLine);
544
545     return hr;
546 }
547
548 UINT WINAPI MsiConfigureProductA(LPCSTR szProduct, int iInstallLevel, 
549                                  INSTALLSTATE eInstallState)
550 {
551     LPWSTR szwProduct = NULL;
552     UINT hr = ERROR_SUCCESS;
553
554     FIXME("%s %d %d\n",debugstr_a(szProduct), iInstallLevel, eInstallState);
555
556     if( szProduct )
557     {
558         UINT len = MultiByteToWideChar( CP_ACP, 0, szProduct, -1, NULL, 0 );
559         szwProduct = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
560         if( !szwProduct )
561             goto end;
562         MultiByteToWideChar( CP_ACP, 0, szProduct, -1, szwProduct, len );
563     }
564
565     hr = MsiConfigureProductW( szwProduct, iInstallLevel, eInstallState );
566
567 end:
568     HeapFree( GetProcessHeap(), 0, szwProduct );
569
570     return hr;
571 }
572
573 UINT WINAPI MsiConfigureProductW(LPCWSTR szProduct, int iInstallLevel, 
574                                  INSTALLSTATE eInstallState)
575 {
576     FIXME("%s %d %d\n", debugstr_w(szProduct), iInstallLevel, eInstallState);
577
578     return MsiConfigureProductExW(szProduct, iInstallLevel, eInstallState,
579                                   NULL);
580 }
581
582 UINT WINAPI MsiGetProductCodeA(LPCSTR szComponent, LPSTR szBuffer)
583 {
584     LPWSTR szwComponent = NULL;
585     UINT hr = ERROR_INSTALL_FAILURE;
586     WCHAR szwBuffer[GUID_SIZE];
587
588     FIXME("%s %s\n",debugstr_a(szComponent), debugstr_a(szBuffer));
589
590     if( szComponent )
591     {
592         UINT len = MultiByteToWideChar( CP_ACP, 0, szComponent, -1, NULL, 0 );
593         szwComponent = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
594         if( !szwComponent )
595             goto end;
596         MultiByteToWideChar( CP_ACP, 0, szComponent, -1, szwComponent, len );
597     }
598     else
599         return ERROR_INVALID_PARAMETER;
600
601     hr = MsiGetProductCodeW( szwComponent, szwBuffer );
602
603     if( ERROR_SUCCESS == hr )
604         WideCharToMultiByte(CP_ACP, 0, szwBuffer, -1, szBuffer, GUID_SIZE, NULL, NULL);
605
606 end:
607     HeapFree( GetProcessHeap(), 0, szwComponent );
608
609     return hr;
610 }
611
612 UINT WINAPI MsiGetProductCodeW(LPCWSTR szComponent, LPWSTR szBuffer)
613 {
614     FIXME("%s %s\n",debugstr_w(szComponent), debugstr_w(szBuffer));
615     if (NULL == szComponent)
616         return ERROR_INVALID_PARAMETER;
617     return ERROR_CALL_NOT_IMPLEMENTED;
618 }
619
620 UINT WINAPI MsiGetProductInfoA(LPCSTR szProduct, LPCSTR szAttribute,
621                  LPSTR szBuffer, DWORD *pcchValueBuf)
622 {
623     LPWSTR szwProduct = NULL, szwAttribute = NULL, szwBuffer = NULL;
624     UINT hr = ERROR_INSTALL_FAILURE;
625
626     FIXME("%s %s %p %p\n",debugstr_a(szProduct), debugstr_a(szAttribute),
627           szBuffer, pcchValueBuf);
628
629     if( NULL != szBuffer && NULL == pcchValueBuf )
630         return ERROR_INVALID_PARAMETER;
631     if( szProduct )
632     {
633         UINT len = MultiByteToWideChar( CP_ACP, 0, szProduct, -1, NULL, 0 );
634         szwProduct = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
635         if( !szwProduct )
636             goto end;
637         MultiByteToWideChar( CP_ACP, 0, szProduct, -1, szwProduct, len );
638     }
639     else
640         return ERROR_INVALID_PARAMETER;
641     
642     if( szAttribute )
643     {
644         UINT len = MultiByteToWideChar( CP_ACP, 0, szAttribute, -1, NULL, 0 );
645         szwAttribute = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
646         if( !szwAttribute )
647             goto end;
648         MultiByteToWideChar( CP_ACP, 0, szAttribute, -1, szwAttribute, len );
649     }
650     else
651     {
652         hr = ERROR_INVALID_PARAMETER;
653         goto end;
654     }
655
656     if( szBuffer )
657     {
658         szwBuffer = HeapAlloc( GetProcessHeap(), 0, (*pcchValueBuf) * sizeof(WCHAR) );
659         if( !szwBuffer )     
660             goto end;
661     }
662
663     hr = MsiGetProductInfoW( szwProduct, szwAttribute, szwBuffer, pcchValueBuf );
664
665     if( ERROR_SUCCESS == hr )
666         WideCharToMultiByte(CP_ACP, 0, szwBuffer, -1, szBuffer, *pcchValueBuf, NULL, NULL);
667
668 end:
669     HeapFree( GetProcessHeap(), 0, szwProduct );
670     HeapFree( GetProcessHeap(), 0, szwAttribute );
671     HeapFree( GetProcessHeap(), 0, szwBuffer );
672
673     return hr;    
674 }
675
676 UINT WINAPI MsiGetProductInfoW(LPCWSTR szProduct, LPCWSTR szAttribute,
677                 LPWSTR szBuffer, DWORD *pcchValueBuf)
678 {
679     MSIHANDLE hProduct;
680     UINT hr;
681     
682     FIXME("%s %s %p %p\n",debugstr_w(szProduct), debugstr_w(szAttribute),
683           szBuffer, pcchValueBuf);
684
685     if (NULL != szBuffer && NULL == pcchValueBuf)
686         return ERROR_INVALID_PARAMETER;
687     if (NULL == szProduct || NULL == szAttribute)
688         return ERROR_INVALID_PARAMETER;
689
690     hr = MsiOpenProductW(szProduct, &hProduct);
691     if (ERROR_SUCCESS != hr)
692         return hr;
693
694     hr = MsiGetPropertyW(hProduct, szAttribute, szBuffer, pcchValueBuf);
695     MsiCloseHandle(hProduct);
696     return hr;
697 }
698
699 UINT WINAPI MsiDatabaseImportA(LPCSTR szFolderPath, LPCSTR szFilename)
700 {
701     FIXME("%s %s\n",debugstr_a(szFolderPath), debugstr_a(szFilename));
702     return ERROR_CALL_NOT_IMPLEMENTED;
703 }
704
705 UINT WINAPI MsiDatabaseImportW(LPCWSTR szFolderPath, LPCWSTR szFilename)
706 {
707     FIXME("%s %s\n",debugstr_w(szFolderPath), debugstr_w(szFilename));
708     return ERROR_CALL_NOT_IMPLEMENTED;
709 }
710
711 UINT WINAPI MsiEnableLogA(DWORD dwLogMode, LPCSTR szLogFile, DWORD attributes)
712 {
713     LPWSTR szwLogFile = NULL;
714     UINT hr = ERROR_INSTALL_FAILURE;
715
716     FIXME("%08lx %s %08lx\n", dwLogMode, debugstr_a(szLogFile), attributes);
717
718     if( szLogFile )
719     {
720         UINT len = MultiByteToWideChar( CP_ACP, 0, szLogFile, -1, NULL, 0 );
721         szwLogFile = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
722         if( !szwLogFile )
723             goto end;
724         MultiByteToWideChar( CP_ACP, 0, szLogFile, -1, szwLogFile, len );
725     }
726     else
727         return ERROR_INVALID_PARAMETER;
728
729     hr = MsiEnableLogW( dwLogMode, szwLogFile, attributes );
730
731 end:
732     HeapFree( GetProcessHeap(), 0, szwLogFile );
733
734     return hr;
735 }
736
737 UINT WINAPI MsiEnableLogW(DWORD dwLogMode, LPCWSTR szLogFile, DWORD attributes)
738 {
739     HANDLE file = INVALID_HANDLE_VALUE;
740
741     TRACE("%08lx %s %08lx\n", dwLogMode, debugstr_w(szLogFile), attributes);
742
743     strcpyW(gszLogFile,szLogFile);
744     if (!(attributes & INSTALLLOGATTRIBUTES_APPEND))
745         DeleteFileW(szLogFile);
746     file = CreateFileW(szLogFile, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
747                            FILE_ATTRIBUTE_NORMAL, NULL);
748     if (file != INVALID_HANDLE_VALUE)
749         CloseHandle(file);
750     else
751         ERR("Unable to enable log %s\n",debugstr_w(szLogFile));
752
753     return ERROR_SUCCESS;
754 }
755
756 INSTALLSTATE WINAPI MsiQueryProductStateA(LPCSTR szProduct)
757 {
758     LPWSTR szwProduct;
759     UINT len;
760     INSTALLSTATE rc;
761
762     len = MultiByteToWideChar(CP_ACP,0,szProduct,-1,NULL,0);
763     szwProduct = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
764     MultiByteToWideChar(CP_ACP,0,szProduct,-1,szwProduct,len);
765     rc = MsiQueryProductStateW(szwProduct);
766     HeapFree(GetProcessHeap(),0,szwProduct);
767     return rc;
768 }
769
770 INSTALLSTATE WINAPI MsiQueryProductStateW(LPCWSTR szProduct)
771 {
772     UINT rc;
773     INSTALLSTATE rrc = INSTALLSTATE_UNKNOWN;
774     HKEY hkey=0;
775     static const WCHAR szWindowsInstaller[] = {
776          'W','i','n','d','o','w','s','I','n','s','t','a','l','l','e','r',0 };
777     DWORD sz;
778
779     TRACE("%s\n", debugstr_w(szProduct));
780
781     rc = MSIREG_OpenUserProductsKey(szProduct,&hkey,FALSE);
782     if (rc != ERROR_SUCCESS)
783         goto end;
784
785     RegCloseKey(hkey);
786
787     rc = MSIREG_OpenUninstallKey(szProduct,&hkey,FALSE);
788     if (rc != ERROR_SUCCESS)
789         goto end;
790
791     sz = sizeof(rrc);
792     rc = RegQueryValueExW(hkey,szWindowsInstaller,NULL,NULL,(LPVOID)&rrc, &sz);
793     if (rc != ERROR_SUCCESS)
794         goto end;
795
796     switch (rrc)
797     {
798     case 1:
799         /* default */
800         rrc = INSTALLSTATE_DEFAULT;
801         break;
802     default:
803         FIXME("Unknown install state read from registry (%i)\n",rrc);
804         rrc = INSTALLSTATE_UNKNOWN;
805         break;
806     }
807 end:
808     RegCloseKey(hkey);
809     return rrc;
810 }
811
812 INSTALLUILEVEL WINAPI MsiSetInternalUI(INSTALLUILEVEL dwUILevel, HWND *phWnd)
813 {
814     INSTALLUILEVEL old = gUILevel;
815     HWND oldwnd = gUIhwnd;
816
817     TRACE("%08x %p\n", dwUILevel, phWnd);
818
819     gUILevel = dwUILevel;
820     if (phWnd)
821     {
822         gUIhwnd = *phWnd;
823         *phWnd = oldwnd;
824     }
825     return old;
826 }
827
828 INSTALLUI_HANDLERA WINAPI MsiSetExternalUIA(INSTALLUI_HANDLERA puiHandler, 
829                                   DWORD dwMessageFilter, LPVOID pvContext)
830 {
831     INSTALLUI_HANDLERA prev = gUIHandlerA;
832
833     TRACE("%p %lx %p\n",puiHandler, dwMessageFilter,pvContext);
834     gUIHandlerA = puiHandler;
835     gUIFilter = dwMessageFilter;
836     gUIContext = pvContext;
837
838     return prev;
839 }
840
841 INSTALLUI_HANDLERW WINAPI MsiSetExternalUIW(INSTALLUI_HANDLERW puiHandler,
842                                   DWORD dwMessageFilter, LPVOID pvContext)
843 {
844     INSTALLUI_HANDLERW prev = gUIHandlerW;
845
846     TRACE("%p %lx %p\n",puiHandler,dwMessageFilter,pvContext);
847     gUIHandlerW = puiHandler;
848     gUIFilter = dwMessageFilter;
849     gUIContext = pvContext;
850
851     return prev;
852 }
853
854 /******************************************************************
855  *  MsiLoadStringW            [MSI.@]
856  *
857  * Loads a string from MSI's string resources.
858  *
859  * PARAMS
860  *
861  *   handle        [I]  only -1 is handled currently
862  *   id            [I]  id of the string to be loaded
863  *   lpBuffer      [O]  buffer for the string to be written to
864  *   nBufferMax    [I]  maximum size of the buffer in characters
865  *   lang          [I]  the prefered language for the string
866  *
867  * RETURNS
868  *
869  *   If successful, this function returns the language id of the string loaded
870  *   If the function fails, the function returns zero.
871  *
872  * NOTES
873  *
874  *   The type of the first parameter is unknown.  LoadString's prototype
875  *  suggests that it might be a module handle.  I have made it an MSI handle
876  *  for starters, as -1 is an invalid MSI handle, but not an invalid module
877  *  handle.  Maybe strings can be stored in an MSI database somehow.
878  */
879 LANGID WINAPI MsiLoadStringW( MSIHANDLE handle, UINT id, LPWSTR lpBuffer,
880                 int nBufferMax, LANGID lang )
881 {
882     HRSRC hres;
883     HGLOBAL hResData;
884     LPWSTR p;
885     DWORD i, len;
886
887     TRACE("%ld %u %p %d %d\n", handle, id, lpBuffer, nBufferMax, lang);
888
889     if( handle != -1 )
890         FIXME("don't know how to deal with handle = %08lx\n", handle);
891
892     if( !lang )
893         lang = GetUserDefaultLangID();
894
895     hres = FindResourceExW( msi_hInstance, (LPCWSTR) RT_STRING,
896                             (LPWSTR)1, lang );
897     if( !hres )
898         return 0;
899     hResData = LoadResource( msi_hInstance, hres );
900     if( !hResData )
901         return 0;
902     p = LockResource( hResData );
903     if( !p )
904         return 0;
905
906     for (i = 0; i < (id&0xf); i++)
907         p += *p + 1;
908     len = *p;
909
910     if( nBufferMax <= len )
911         return 0;
912
913     memcpy( lpBuffer, p+1, len * sizeof(WCHAR));
914     lpBuffer[ len ] = 0;
915
916     TRACE("found -> %s\n", debugstr_w(lpBuffer));
917
918     return lang;
919 }
920
921 LANGID WINAPI MsiLoadStringA( MSIHANDLE handle, UINT id, LPSTR lpBuffer,
922                 int nBufferMax, LANGID lang )
923 {
924     LPWSTR bufW;
925     LANGID r;
926     DWORD len;
927
928     bufW = HeapAlloc(GetProcessHeap(), 0, nBufferMax*sizeof(WCHAR));
929     r = MsiLoadStringW(handle, id, bufW, nBufferMax, lang);
930     if( r )
931     {
932         len = WideCharToMultiByte(CP_ACP, 0, bufW, -1, NULL, 0, NULL, NULL );
933         if( len <= nBufferMax )
934             WideCharToMultiByte( CP_ACP, 0, bufW, -1,
935                                  lpBuffer, nBufferMax, NULL, NULL );
936         else
937             r = 0;
938     }
939     HeapFree(GetProcessHeap(), 0, bufW);
940     return r;
941 }
942
943 INSTALLSTATE WINAPI MsiLocateComponentA(LPCSTR szComponent, LPSTR lpPathBuf,
944                 DWORD *pcchBuf)
945 {
946     FIXME("%s %p %08lx\n", debugstr_a(szComponent), lpPathBuf, *pcchBuf);
947     return INSTALLSTATE_UNKNOWN;
948 }
949
950 INSTALLSTATE WINAPI MsiLocateComponentW(LPCWSTR szComponent, LPSTR lpPathBuf,
951                 DWORD *pcchBuf)
952 {
953     FIXME("%s %p %08lx\n", debugstr_w(szComponent), lpPathBuf, *pcchBuf);
954     return INSTALLSTATE_UNKNOWN;
955 }
956
957 UINT WINAPI MsiMessageBoxA(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType,
958                 WORD wLanguageId, DWORD f)
959 {
960     FIXME("%p %s %s %u %08x %08lx\n",hWnd,debugstr_a(lpText),debugstr_a(lpCaption),
961           uType,wLanguageId,f);
962     return ERROR_CALL_NOT_IMPLEMENTED;
963 }
964
965 UINT WINAPI MsiMessageBoxW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType,
966                 WORD wLanguageId, DWORD f)
967 {
968     FIXME("%p %s %s %u %08x %08lx\n",hWnd,debugstr_w(lpText),debugstr_w(lpCaption),
969           uType,wLanguageId,f);
970     return ERROR_CALL_NOT_IMPLEMENTED;
971 }
972
973 UINT WINAPI MsiProvideAssemblyA( LPCSTR szAssemblyName, LPCSTR szAppContext,
974                 DWORD dwInstallMode, DWORD dwAssemblyInfo, LPSTR lpPathBuf,
975                 DWORD* pcchPathBuf ) 
976 {
977     FIXME("%s %s %08lx %08lx %p %p\n", debugstr_a(szAssemblyName),
978           debugstr_a(szAppContext), dwInstallMode, dwAssemblyInfo, lpPathBuf,
979           pcchPathBuf);
980     return ERROR_CALL_NOT_IMPLEMENTED;
981 }
982
983 UINT WINAPI MsiProvideAssemblyW( LPCWSTR szAssemblyName, LPCWSTR szAppContext,
984                 DWORD dwInstallMode, DWORD dwAssemblyInfo, LPWSTR lpPathBuf,
985                 DWORD* pcchPathBuf )
986 {
987     FIXME("%s %s %08lx %08lx %p %p\n", debugstr_w(szAssemblyName), 
988           debugstr_w(szAppContext), dwInstallMode, dwAssemblyInfo, lpPathBuf,
989           pcchPathBuf);
990     return ERROR_CALL_NOT_IMPLEMENTED;
991 }
992
993 UINT WINAPI MsiProvideComponentFromDescriptorA( LPCSTR szDescriptor,
994                 LPSTR szPath, DWORD *pcchPath, DWORD *pcchArgs )
995 {
996     FIXME("%s %p %p %p\n", debugstr_a(szDescriptor), szPath, pcchPath, pcchArgs );
997     return ERROR_CALL_NOT_IMPLEMENTED;
998 }
999
1000 UINT WINAPI MsiProvideComponentFromDescriptorW( LPCWSTR szDescriptor,
1001                 LPWSTR szPath, DWORD *pcchPath, DWORD *pcchArgs )
1002 {
1003     FIXME("%s %p %p %p\n", debugstr_w(szDescriptor), szPath, pcchPath, pcchArgs );
1004     return ERROR_CALL_NOT_IMPLEMENTED;
1005 }
1006
1007 HRESULT WINAPI MsiGetFileSignatureInformationA( LPCSTR szSignedObjectPath,
1008                 DWORD dwFlags, PCCERT_CONTEXT* ppcCertContext, BYTE* pbHashData,
1009                 DWORD* pcbHashData)
1010 {
1011     FIXME("%s %08lx %p %p %p\n", debugstr_a(szSignedObjectPath), dwFlags,
1012           ppcCertContext, pbHashData, pcbHashData);
1013     return ERROR_CALL_NOT_IMPLEMENTED;
1014 }
1015
1016 HRESULT WINAPI MsiGetFileSignatureInformationW( LPCWSTR szSignedObjectPath,
1017                 DWORD dwFlags, PCCERT_CONTEXT* ppcCertContext, BYTE* pbHashData,
1018                 DWORD* pcbHashData)
1019 {
1020     FIXME("%s %08lx %p %p %p\n", debugstr_w(szSignedObjectPath), dwFlags,
1021           ppcCertContext, pbHashData, pcbHashData);
1022     return ERROR_CALL_NOT_IMPLEMENTED;
1023 }
1024
1025 UINT WINAPI MsiGetProductPropertyA( MSIHANDLE hProduct, LPCSTR szProperty,
1026                                     LPSTR szValue, DWORD *pccbValue )
1027 {
1028     FIXME("%ld %s %p %p\n", hProduct, debugstr_a(szProperty), szValue, pccbValue);
1029     return ERROR_CALL_NOT_IMPLEMENTED;
1030 }
1031
1032 UINT WINAPI MsiGetProductPropertyW( MSIHANDLE hProduct, LPCWSTR szProperty,
1033                                     LPWSTR szValue, DWORD *pccbValue )
1034 {
1035     FIXME("%ld %s %p %p\n", hProduct, debugstr_w(szProperty), szValue, pccbValue);
1036     return ERROR_CALL_NOT_IMPLEMENTED;
1037 }
1038
1039 UINT WINAPI MsiVerifyPackageA( LPCSTR szPackage )
1040 {
1041     UINT r, len;
1042     LPWSTR szPack = NULL;
1043
1044     TRACE("%s\n", debugstr_a(szPackage) );
1045
1046     if( szPackage )
1047     {
1048         len = MultiByteToWideChar( CP_ACP, 0, szPackage, -1, NULL, 0 );
1049         szPack = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1050         if( !szPack )
1051             return ERROR_OUTOFMEMORY;
1052         MultiByteToWideChar( CP_ACP, 0, szPackage, -1, szPack, len );
1053     }
1054
1055     r = MsiVerifyPackageW( szPack );
1056
1057     HeapFree( GetProcessHeap(), 0, szPack );
1058
1059     return r;
1060 }
1061
1062 UINT WINAPI MsiVerifyPackageW( LPCWSTR szPackage )
1063 {
1064     MSIHANDLE handle;
1065     UINT r;
1066
1067     TRACE("%s\n", debugstr_w(szPackage) );
1068
1069     r = MsiOpenDatabaseW( szPackage, MSIDBOPEN_READONLY, &handle );
1070     MsiCloseHandle( handle );
1071
1072     return r;
1073 }
1074
1075 INSTALLSTATE WINAPI MsiGetComponentPathA(LPCSTR szProduct, LPCSTR szComponent,
1076                                          LPSTR lpPathBuf, DWORD* pcchBuf)
1077 {
1078     LPWSTR szwProduct = NULL, szwComponent = NULL, lpwPathBuf= NULL;
1079     INSTALLSTATE rc;
1080     UINT len, incoming_len;
1081
1082     if( szProduct )
1083     {
1084         len = MultiByteToWideChar( CP_ACP, 0, szProduct, -1, NULL, 0 );
1085         szwProduct= HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1086         if( !szwProduct)
1087             return ERROR_OUTOFMEMORY;
1088         MultiByteToWideChar( CP_ACP, 0, szProduct, -1, szwProduct, len );
1089     }
1090
1091     if( szComponent )
1092     {
1093         len = MultiByteToWideChar( CP_ACP, 0, szComponent, -1, NULL, 0 );
1094         szwComponent= HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1095         if( !szwComponent )
1096         {
1097             HeapFree( GetProcessHeap(), 0, szwProduct);
1098             return ERROR_OUTOFMEMORY;
1099         }
1100         MultiByteToWideChar( CP_ACP, 0, szComponent, -1, szwComponent, len );
1101     }
1102
1103     if( pcchBuf && *pcchBuf > 0 )
1104         lpwPathBuf = HeapAlloc( GetProcessHeap(), 0, *pcchBuf * sizeof(WCHAR));
1105     else
1106         lpwPathBuf = NULL;
1107
1108     incoming_len = *pcchBuf;
1109     rc = MsiGetComponentPathW(szwProduct, szwComponent, lpwPathBuf, pcchBuf);
1110
1111     HeapFree( GetProcessHeap(), 0, szwProduct);
1112     HeapFree( GetProcessHeap(), 0, szwComponent);
1113     if (lpwPathBuf)
1114     {
1115         if (rc != INSTALLSTATE_UNKNOWN)
1116             WideCharToMultiByte(CP_ACP, 0, lpwPathBuf, incoming_len,
1117                             lpPathBuf, incoming_len, NULL, NULL);
1118         HeapFree( GetProcessHeap(), 0, lpwPathBuf);
1119     }
1120
1121     return rc;
1122 }
1123
1124 INSTALLSTATE WINAPI MsiGetComponentPathW(LPCWSTR szProduct, LPCWSTR szComponent,
1125                                          LPWSTR lpPathBuf, DWORD* pcchBuf)
1126 {
1127     WCHAR squished_pc[GUID_SIZE];
1128     UINT rc;
1129     INSTALLSTATE rrc = INSTALLSTATE_UNKNOWN;
1130     HKEY hkey = 0;
1131     LPWSTR path = NULL;
1132     DWORD sz, type;
1133
1134     TRACE("%s %s %p %p\n", debugstr_w(szProduct),
1135            debugstr_w(szComponent), lpPathBuf, pcchBuf);
1136
1137     if( lpPathBuf && !pcchBuf )
1138         return INSTALLSTATE_INVALIDARG;
1139
1140     squash_guid(szProduct,squished_pc);
1141
1142     rc = MSIREG_OpenProductsKey( szProduct, &hkey, FALSE);
1143     if( rc != ERROR_SUCCESS )
1144         goto end;
1145
1146     RegCloseKey(hkey);
1147
1148     rc = MSIREG_OpenComponentsKey( szComponent, &hkey, FALSE);
1149     if( rc != ERROR_SUCCESS )
1150         goto end;
1151
1152     sz = 0;
1153     type = 0;
1154     rc = RegQueryValueExW( hkey, squished_pc, NULL, &type, NULL, &sz );
1155     if( rc != ERROR_SUCCESS )
1156         goto end;
1157     if( type != REG_SZ )
1158         goto end;
1159
1160     sz += sizeof(WCHAR);
1161     path = HeapAlloc( GetProcessHeap(), 0, sz );
1162     if( !path )
1163         goto end;
1164
1165     rc = RegQueryValueExW( hkey, squished_pc, NULL, NULL, (LPVOID) path, &sz );
1166     if( rc != ERROR_SUCCESS )
1167         goto end;
1168
1169     TRACE("found path of (%s:%s)(%s)\n", debugstr_w(szComponent),
1170            debugstr_w(szProduct), debugstr_w(path));
1171
1172     FIXME("Only working for installed files, not registry keys\n");
1173     if ( GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES )
1174         rrc = INSTALLSTATE_LOCAL;
1175     else
1176         rrc = INSTALLSTATE_ABSENT;
1177
1178     if( pcchBuf )
1179     {
1180         sz = sz / sizeof(WCHAR);
1181         if( *pcchBuf >= sz )
1182             strcpyW( lpPathBuf, path );
1183         *pcchBuf = sz;
1184     }
1185
1186 end:
1187     HeapFree(GetProcessHeap(), 0, path );
1188     RegCloseKey(hkey);
1189     return rrc;
1190 }
1191
1192 INSTALLSTATE WINAPI MsiQueryFeatureStateA(LPCSTR szProduct, LPCSTR szFeature)
1193 {
1194     INSTALLSTATE rc;
1195     UINT len;
1196     LPWSTR szwProduct= NULL;
1197     LPWSTR szwFeature= NULL;
1198
1199     if( szProduct )
1200     {
1201         len = MultiByteToWideChar( CP_ACP, 0, szProduct, -1, NULL, 0 );
1202         szwProduct= HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1203         if( !szwProduct)
1204             return ERROR_OUTOFMEMORY;
1205         MultiByteToWideChar( CP_ACP, 0, szProduct, -1, szwProduct, len );
1206     }
1207
1208     if( szFeature )
1209     {
1210         len = MultiByteToWideChar( CP_ACP, 0, szFeature, -1, NULL, 0 );
1211         szwFeature= HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1212         if( !szwFeature)
1213         {
1214             HeapFree( GetProcessHeap(), 0, szwProduct);
1215             return ERROR_OUTOFMEMORY;
1216         }
1217         MultiByteToWideChar( CP_ACP, 0, szFeature, -1, szwFeature, len );
1218     }
1219
1220     rc = MsiQueryFeatureStateW(szwProduct, szwFeature);
1221
1222     HeapFree( GetProcessHeap(), 0, szwProduct);
1223     HeapFree( GetProcessHeap(), 0, szwFeature);
1224
1225     return rc;
1226 }
1227
1228 INSTALLSTATE WINAPI MsiQueryFeatureStateW(LPCWSTR szProduct, LPCWSTR szFeature)
1229 {
1230     FIXME("%s %s\n", debugstr_w(szProduct), debugstr_w(szFeature));
1231     /*
1232      * Iterates all the features components and the features parents components
1233      */
1234     return INSTALLSTATE_LOCAL;
1235 }
1236
1237 UINT WINAPI MsiGetFileVersionA(LPCSTR szFilePath, LPSTR lpVersionBuf,
1238                 DWORD* pcchVersionBuf, LPSTR lpLangBuf, DWORD* pcchLangBuf)
1239 {
1240     LPWSTR szwFilePath = NULL, lpwVersionBuff = NULL, lpwLangBuff = NULL;
1241     UINT len, ret = ERROR_OUTOFMEMORY;
1242     
1243     if( szFilePath )
1244     {
1245         len = MultiByteToWideChar( CP_ACP, 0, szFilePath, -1, NULL, 0 );
1246         szwFilePath = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1247         if( !szwFilePath )
1248             goto end;
1249         MultiByteToWideChar( CP_ACP, 0, szFilePath, -1, szwFilePath, len );
1250     }
1251     
1252     if( lpVersionBuf && pcchVersionBuf && *pcchVersionBuf )
1253     {
1254         lpwVersionBuff = HeapAlloc(GetProcessHeap(), 0, *pcchVersionBuf*sizeof(WCHAR));
1255         if( !lpwVersionBuff )
1256             goto end;
1257     }
1258
1259     if( lpLangBuf && pcchLangBuf && *pcchLangBuf )
1260     {
1261         lpwLangBuff = HeapAlloc(GetProcessHeap(), 0, *pcchVersionBuf*sizeof(WCHAR));
1262         if( !lpwLangBuff )
1263             goto end;
1264     }
1265         
1266     ret = MsiGetFileVersionW(szwFilePath, lpwVersionBuff, pcchVersionBuf,
1267                              lpwLangBuff, pcchLangBuf);
1268     
1269     if( lpwVersionBuff )
1270         WideCharToMultiByte(CP_ACP, 0, lpwVersionBuff, -1,
1271                             lpVersionBuf, *pcchVersionBuf, NULL, NULL);
1272     if( lpwLangBuff )
1273         WideCharToMultiByte(CP_ACP, 0, lpwLangBuff, -1,
1274                             lpLangBuf, *pcchLangBuf, NULL, NULL);
1275     
1276 end:
1277     HeapFree(GetProcessHeap(), 0, szwFilePath);
1278     HeapFree(GetProcessHeap(), 0, lpwVersionBuff);
1279     HeapFree(GetProcessHeap(), 0, lpwLangBuff);
1280     
1281     return ret;
1282 }
1283
1284 UINT WINAPI MsiGetFileVersionW(LPCWSTR szFilePath, LPWSTR lpVersionBuf,
1285                 DWORD* pcchVersionBuf, LPWSTR lpLangBuf, DWORD* pcchLangBuf)
1286 {
1287     static const WCHAR szVersionResource[] = {'\\',0};
1288     static const WCHAR szVersionFormat[] = {
1289         '%','d','.','%','d','.','%','d','.','%','d',0};
1290     static const WCHAR szLangFormat[] = {'%','d',0};
1291     UINT ret = 0;
1292     DWORD dwVerLen;
1293     LPVOID lpVer = NULL;
1294     VS_FIXEDFILEINFO *ffi;
1295     UINT puLen;
1296     WCHAR tmp[32];
1297
1298     TRACE("%s %p %ld %p %ld\n", debugstr_w(szFilePath),
1299           lpVersionBuf, pcchVersionBuf?*pcchVersionBuf:0,
1300           lpLangBuf, pcchLangBuf?*pcchLangBuf:0);
1301
1302     dwVerLen = GetFileVersionInfoSizeW(szFilePath, NULL);
1303     if( !dwVerLen )
1304         return GetLastError();
1305
1306     lpVer = HeapAlloc(GetProcessHeap(), 0, dwVerLen);
1307     if( !lpVer )
1308     {
1309         ret = ERROR_OUTOFMEMORY;
1310         goto end;
1311     }
1312
1313     if( !GetFileVersionInfoW(szFilePath, 0, dwVerLen, lpVer) )
1314     {
1315         ret = GetLastError();
1316         goto end;
1317     }
1318     if( lpVersionBuf && pcchVersionBuf && *pcchVersionBuf )
1319     {
1320         if( VerQueryValueW(lpVer, szVersionResource, (LPVOID*)&ffi, &puLen) &&
1321             (puLen > 0) )
1322         {
1323             wsprintfW(tmp, szVersionFormat,
1324                   HIWORD(ffi->dwFileVersionMS), LOWORD(ffi->dwFileVersionMS),
1325                   HIWORD(ffi->dwFileVersionLS), LOWORD(ffi->dwFileVersionLS));
1326             lstrcpynW(lpVersionBuf, tmp, *pcchVersionBuf);
1327             *pcchVersionBuf = strlenW(lpVersionBuf);
1328         }
1329         else
1330         {
1331             *lpVersionBuf = 0;
1332             *pcchVersionBuf = 0;
1333         }
1334     }
1335
1336     if( lpLangBuf && pcchLangBuf && *pcchLangBuf )
1337     {
1338         DWORD lang = GetUserDefaultLangID();
1339
1340         FIXME("Retrieve language from file\n");
1341         wsprintfW(tmp, szLangFormat, lang);
1342         lstrcpynW(lpLangBuf, tmp, *pcchLangBuf);
1343         *pcchLangBuf = strlenW(lpLangBuf);
1344     }
1345
1346 end:
1347     HeapFree(GetProcessHeap(), 0, lpVer);
1348     return ret;
1349 }
1350
1351
1352 /******************************************************************
1353  *      DllMain
1354  */
1355 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1356 {
1357     switch(fdwReason)
1358     {
1359     case DLL_PROCESS_ATTACH:
1360         msi_hInstance = hinstDLL;
1361         DisableThreadLibraryCalls(hinstDLL);
1362         msi_dialog_register_class();
1363         break;
1364     case DLL_PROCESS_DETACH:
1365         msi_dialog_unregister_class();
1366         /* FIXME: Cleanup */
1367         break;
1368     }
1369     return TRUE;
1370 }
1371
1372 typedef struct tagIClassFactoryImpl
1373 {
1374     IClassFactoryVtbl *lpVtbl;
1375 } IClassFactoryImpl;
1376
1377 static HRESULT WINAPI MsiCF_QueryInterface(LPCLASSFACTORY iface,
1378                 REFIID riid,LPVOID *ppobj)
1379 {
1380     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1381     FIXME("%p %s %p\n",This,debugstr_guid(riid),ppobj);
1382     return E_NOINTERFACE;
1383 }
1384
1385 static ULONG WINAPI MsiCF_AddRef(LPCLASSFACTORY iface)
1386 {
1387     return 2;
1388 }
1389
1390 static ULONG WINAPI MsiCF_Release(LPCLASSFACTORY iface)
1391 {
1392     return 1;
1393 }
1394
1395 static HRESULT WINAPI MsiCF_CreateInstance(LPCLASSFACTORY iface,
1396     LPUNKNOWN pOuter, REFIID riid, LPVOID *ppobj)
1397 {
1398     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1399
1400     FIXME("%p %p %s %p\n", This, pOuter, debugstr_guid(riid), ppobj);
1401     return E_FAIL;
1402 }
1403
1404 static HRESULT WINAPI MsiCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
1405 {
1406     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1407
1408     FIXME("%p %d\n", This, dolock);
1409     return S_OK;
1410 }
1411
1412 static IClassFactoryVtbl MsiCF_Vtbl =
1413 {
1414     MsiCF_QueryInterface,
1415     MsiCF_AddRef,
1416     MsiCF_Release,
1417     MsiCF_CreateInstance,
1418     MsiCF_LockServer
1419 };
1420
1421 static IClassFactoryImpl Msi_CF = { &MsiCF_Vtbl };
1422
1423 /******************************************************************
1424  *      DllGetClassObject [MSI.@]
1425  */
1426 HRESULT WINAPI MSI_DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
1427 {
1428     TRACE("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1429
1430     if( IsEqualCLSID (rclsid, &CLSID_IMsiServer) ||
1431         IsEqualCLSID (rclsid, &CLSID_IMsiServerMessage) ||
1432         IsEqualCLSID (rclsid, &CLSID_IMsiServerX1) ||
1433         IsEqualCLSID (rclsid, &CLSID_IMsiServerX2) ||
1434         IsEqualCLSID (rclsid, &CLSID_IMsiServerX3) )
1435     {
1436         *ppv = (LPVOID) &Msi_CF;
1437         return S_OK;
1438     }
1439     return CLASS_E_CLASSNOTAVAILABLE;
1440 }
1441
1442 /******************************************************************
1443  *      DllGetVersion [MSI.@]
1444  */
1445 HRESULT WINAPI MSI_DllGetVersion(DLLVERSIONINFO *pdvi)
1446 {
1447     TRACE("%p\n",pdvi);
1448   
1449     if (pdvi->cbSize != sizeof(DLLVERSIONINFO))
1450         return E_INVALIDARG;
1451   
1452     pdvi->dwMajorVersion = MSI_MAJORVERSION;
1453     pdvi->dwMinorVersion = MSI_MINORVERSION;
1454     pdvi->dwBuildNumber = MSI_BUILDNUMBER;
1455     pdvi->dwPlatformID = 1;
1456   
1457     return S_OK;
1458 }
1459
1460 /******************************************************************
1461  *      DllCanUnloadNow [MSI.@]
1462  */
1463 BOOL WINAPI MSI_DllCanUnloadNow(void)
1464 {
1465     return S_FALSE;
1466 }
1467
1468 UINT WINAPI MsiGetFeatureUsageW(LPCWSTR szProduct, LPCWSTR szFeature,
1469                                 DWORD* pdwUseCount, WORD* pwDateUsed)
1470 {
1471     FIXME("%s %s %p %p\n",debugstr_w(szProduct), debugstr_w(szFeature),
1472           pdwUseCount, pwDateUsed);
1473     return ERROR_CALL_NOT_IMPLEMENTED;
1474 }
1475
1476 UINT WINAPI MsiGetFeatureUsageA(LPCSTR szProduct, LPCSTR szFeature,
1477                                 DWORD* pdwUseCount, WORD* pwDateUsed)
1478 {
1479     FIXME("%s %s %p %p\n", debugstr_a(szProduct), debugstr_a(szFeature),
1480           pdwUseCount, pwDateUsed);
1481     return ERROR_CALL_NOT_IMPLEMENTED;
1482 }
1483
1484 INSTALLSTATE WINAPI MsiUseFeatureExW(LPCWSTR szProduct, LPCWSTR szFeature, 
1485                              DWORD dwInstallMode, DWORD dwReserved)
1486 {
1487     FIXME("%s %s %li %li\n", debugstr_w(szProduct), debugstr_w(szFeature),
1488           dwInstallMode, dwReserved);
1489
1490     /*
1491      * Polls all the components of the feature to find install state and then
1492      *  writes:
1493      *    Software\\Microsoft\\Windows\\CurrentVersion\\
1494      *    Installer\\Products\\<squishguid>\\<feature>
1495      *      "Usage"=dword:........
1496      */  
1497  
1498     return INSTALLSTATE_LOCAL; 
1499 }
1500
1501 INSTALLSTATE WINAPI MsiUseFeatureExA(LPCSTR szProduct, LPCSTR szFeature, 
1502                              DWORD dwInstallMode, DWORD dwReserved)
1503 {
1504     FIXME("%s %s %li %li\n", debugstr_a(szProduct), debugstr_a(szFeature),
1505           dwInstallMode, dwReserved);
1506    
1507     return INSTALLSTATE_LOCAL; 
1508 }
1509
1510 INSTALLSTATE WINAPI MsiUseFeatureW(LPCWSTR szProduct, LPCWSTR szFeature)
1511 {
1512     FIXME("%s %s\n", debugstr_w(szProduct), debugstr_w(szFeature));
1513    
1514     return INSTALLSTATE_LOCAL; 
1515 }
1516
1517 INSTALLSTATE WINAPI MsiUseFeatureA(LPCSTR szProduct, LPCSTR szFeature)
1518 {
1519     FIXME("%s %s\n", debugstr_a(szProduct), debugstr_a(szFeature));
1520    
1521     return INSTALLSTATE_LOCAL; 
1522 }
1523
1524 UINT WINAPI MsiProvideQualifiedComponentExW(LPCWSTR szComponent, 
1525                 LPCWSTR szQualifier, DWORD dwInstallMode, LPWSTR szProduct,
1526                 DWORD Unused1, DWORD Unused2, LPWSTR lpPathBuf, 
1527                 DWORD* pcchPathBuf)
1528 {
1529     FIXME("%s %s %li %s %li %li %p %p\n", debugstr_w(szComponent),
1530           debugstr_w(szQualifier), dwInstallMode, debugstr_w(szProduct),
1531           Unused1, Unused2, lpPathBuf, pcchPathBuf);
1532
1533     return ERROR_INDEX_ABSENT;
1534 }
1535
1536 USERINFOSTATE WINAPI MsiGetUserInfoW(LPCWSTR szProduct, LPWSTR lpUserNameBuf, 
1537                 DWORD* pcchUserNameBuf, LPWSTR lpOrgNameBuf, 
1538                 DWORD* pcchOrgNameBuf, LPWSTR lpSerialBuf, DWORD* pcchSerialBuf)
1539 {
1540     FIXME("%s %p %p %p %p %p %p\n",debugstr_w(szProduct), lpUserNameBuf,
1541           pcchUserNameBuf, lpOrgNameBuf, pcchOrgNameBuf, lpSerialBuf,
1542           pcchSerialBuf);
1543   
1544     return USERINFOSTATE_UNKNOWN; 
1545 }
1546
1547 USERINFOSTATE WINAPI MsiGetUserInfoA(LPCSTR szProduct, LPSTR lpUserNameBuf, 
1548                 DWORD* pcchUserNameBuf, LPSTR lpOrgNameBuf, 
1549                 DWORD* pcchOrgNameBuf, LPSTR lpSerialBuf, DWORD* pcchSerialBuf)
1550 {
1551     FIXME("%s %p %p %p %p %p %p\n",debugstr_a(szProduct), lpUserNameBuf,
1552           pcchUserNameBuf, lpOrgNameBuf, pcchOrgNameBuf, lpSerialBuf,
1553           pcchSerialBuf);
1554   
1555     return USERINFOSTATE_UNKNOWN; 
1556 }
1557
1558 UINT WINAPI MsiCollectUserInfoW(LPCWSTR szProduct)
1559 {
1560     FIXME("%s\n",debugstr_w(szProduct));
1561     return ERROR_CALL_NOT_IMPLEMENTED;
1562 }
1563
1564 UINT WINAPI MsiCollectUserInfoA(LPCSTR szProduct)
1565 {
1566     FIXME("%s\n",debugstr_a(szProduct));
1567     return ERROR_CALL_NOT_IMPLEMENTED;
1568 }
1569
1570 UINT WINAPI MsiCreateAndVerifyInstallerDirectory(void)
1571 {
1572     FIXME("\n");
1573     return ERROR_CALL_NOT_IMPLEMENTED;
1574 }
1575
1576 UINT WINAPI MsiGetShortcutTargetA( LPCSTR szShortcutTarget,
1577                                    LPSTR szProductCode, LPSTR szFeatureId,
1578                                    LPSTR szComponentCode )
1579 {
1580     FIXME("\n");
1581     return ERROR_CALL_NOT_IMPLEMENTED;
1582 }
1583
1584 UINT WINAPI MsiGetShortcutTargetW( LPCWSTR szShortcutTarget,
1585                                    LPWSTR szProductCode, LPWSTR szFeatureId,
1586                                    LPWSTR szComponentCode )
1587 {
1588     FIXME("\n");
1589     return ERROR_CALL_NOT_IMPLEMENTED;
1590 }
1591
1592 UINT WINAPI MsiReinstallFeatureW( LPCWSTR szProduct, LPCWSTR szFeature, 
1593                                   DWORD dwReinstallMode )
1594 {
1595     FIXME("%s %s %li\n", debugstr_w(szProduct), debugstr_w(szFeature),
1596                            dwReinstallMode);
1597     return ERROR_SUCCESS;
1598 }
1599
1600 UINT WINAPI MsiReinstallFeatureA( LPCSTR szProduct, LPCSTR szFeature, 
1601                                   DWORD dwReinstallMode )
1602 {
1603     FIXME("%s %s %li\n", debugstr_a(szProduct), debugstr_a(szFeature),
1604                            dwReinstallMode);
1605     return ERROR_SUCCESS;
1606 }