Assorted spelling fixes.
[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 preferred 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     if (path[0]=='0')
1173     {
1174         FIXME("Registry entry.. check entry\n");
1175             rrc = INSTALLSTATE_LOCAL;
1176     }
1177     else
1178     {
1179         /* PROBABLY a file */
1180         if ( GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES )
1181             rrc = INSTALLSTATE_LOCAL;
1182         else
1183             rrc = INSTALLSTATE_ABSENT;
1184     }
1185
1186     if( pcchBuf )
1187     {
1188         sz = sz / sizeof(WCHAR);
1189         if( *pcchBuf >= sz )
1190             strcpyW( lpPathBuf, path );
1191         *pcchBuf = sz;
1192     }
1193
1194 end:
1195     HeapFree(GetProcessHeap(), 0, path );
1196     RegCloseKey(hkey);
1197     return rrc;
1198 }
1199
1200 INSTALLSTATE WINAPI MsiQueryFeatureStateA(LPCSTR szProduct, LPCSTR szFeature)
1201 {
1202     INSTALLSTATE rc;
1203     UINT len;
1204     LPWSTR szwProduct= NULL;
1205     LPWSTR szwFeature= NULL;
1206
1207     if( szProduct )
1208     {
1209         len = MultiByteToWideChar( CP_ACP, 0, szProduct, -1, NULL, 0 );
1210         szwProduct= HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1211         if( !szwProduct)
1212             return ERROR_OUTOFMEMORY;
1213         MultiByteToWideChar( CP_ACP, 0, szProduct, -1, szwProduct, len );
1214     }
1215
1216     if( szFeature )
1217     {
1218         len = MultiByteToWideChar( CP_ACP, 0, szFeature, -1, NULL, 0 );
1219         szwFeature= HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1220         if( !szwFeature)
1221         {
1222             HeapFree( GetProcessHeap(), 0, szwProduct);
1223             return ERROR_OUTOFMEMORY;
1224         }
1225         MultiByteToWideChar( CP_ACP, 0, szFeature, -1, szwFeature, len );
1226     }
1227
1228     rc = MsiQueryFeatureStateW(szwProduct, szwFeature);
1229
1230     HeapFree( GetProcessHeap(), 0, szwProduct);
1231     HeapFree( GetProcessHeap(), 0, szwFeature);
1232
1233     return rc;
1234 }
1235
1236 INSTALLSTATE WINAPI MsiQueryFeatureStateW(LPCWSTR szProduct, LPCWSTR szFeature)
1237 {
1238     FIXME("%s %s\n", debugstr_w(szProduct), debugstr_w(szFeature));
1239     /*
1240      * Iterates all the features components and the features parents components
1241      */
1242     return INSTALLSTATE_LOCAL;
1243 }
1244
1245 UINT WINAPI MsiGetFileVersionA(LPCSTR szFilePath, LPSTR lpVersionBuf,
1246                 DWORD* pcchVersionBuf, LPSTR lpLangBuf, DWORD* pcchLangBuf)
1247 {
1248     LPWSTR szwFilePath = NULL, lpwVersionBuff = NULL, lpwLangBuff = NULL;
1249     UINT len, ret = ERROR_OUTOFMEMORY;
1250     
1251     if( szFilePath )
1252     {
1253         len = MultiByteToWideChar( CP_ACP, 0, szFilePath, -1, NULL, 0 );
1254         szwFilePath = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1255         if( !szwFilePath )
1256             goto end;
1257         MultiByteToWideChar( CP_ACP, 0, szFilePath, -1, szwFilePath, len );
1258     }
1259     
1260     if( lpVersionBuf && pcchVersionBuf && *pcchVersionBuf )
1261     {
1262         lpwVersionBuff = HeapAlloc(GetProcessHeap(), 0, *pcchVersionBuf*sizeof(WCHAR));
1263         if( !lpwVersionBuff )
1264             goto end;
1265     }
1266
1267     if( lpLangBuf && pcchLangBuf && *pcchLangBuf )
1268     {
1269         lpwLangBuff = HeapAlloc(GetProcessHeap(), 0, *pcchVersionBuf*sizeof(WCHAR));
1270         if( !lpwLangBuff )
1271             goto end;
1272     }
1273         
1274     ret = MsiGetFileVersionW(szwFilePath, lpwVersionBuff, pcchVersionBuf,
1275                              lpwLangBuff, pcchLangBuf);
1276     
1277     if( lpwVersionBuff )
1278         WideCharToMultiByte(CP_ACP, 0, lpwVersionBuff, -1,
1279                             lpVersionBuf, *pcchVersionBuf, NULL, NULL);
1280     if( lpwLangBuff )
1281         WideCharToMultiByte(CP_ACP, 0, lpwLangBuff, -1,
1282                             lpLangBuf, *pcchLangBuf, NULL, NULL);
1283     
1284 end:
1285     HeapFree(GetProcessHeap(), 0, szwFilePath);
1286     HeapFree(GetProcessHeap(), 0, lpwVersionBuff);
1287     HeapFree(GetProcessHeap(), 0, lpwLangBuff);
1288     
1289     return ret;
1290 }
1291
1292 UINT WINAPI MsiGetFileVersionW(LPCWSTR szFilePath, LPWSTR lpVersionBuf,
1293                 DWORD* pcchVersionBuf, LPWSTR lpLangBuf, DWORD* pcchLangBuf)
1294 {
1295     static const WCHAR szVersionResource[] = {'\\',0};
1296     static const WCHAR szVersionFormat[] = {
1297         '%','d','.','%','d','.','%','d','.','%','d',0};
1298     static const WCHAR szLangFormat[] = {'%','d',0};
1299     UINT ret = 0;
1300     DWORD dwVerLen;
1301     LPVOID lpVer = NULL;
1302     VS_FIXEDFILEINFO *ffi;
1303     UINT puLen;
1304     WCHAR tmp[32];
1305
1306     TRACE("%s %p %ld %p %ld\n", debugstr_w(szFilePath),
1307           lpVersionBuf, pcchVersionBuf?*pcchVersionBuf:0,
1308           lpLangBuf, pcchLangBuf?*pcchLangBuf:0);
1309
1310     dwVerLen = GetFileVersionInfoSizeW(szFilePath, NULL);
1311     if( !dwVerLen )
1312         return GetLastError();
1313
1314     lpVer = HeapAlloc(GetProcessHeap(), 0, dwVerLen);
1315     if( !lpVer )
1316     {
1317         ret = ERROR_OUTOFMEMORY;
1318         goto end;
1319     }
1320
1321     if( !GetFileVersionInfoW(szFilePath, 0, dwVerLen, lpVer) )
1322     {
1323         ret = GetLastError();
1324         goto end;
1325     }
1326     if( lpVersionBuf && pcchVersionBuf && *pcchVersionBuf )
1327     {
1328         if( VerQueryValueW(lpVer, szVersionResource, (LPVOID*)&ffi, &puLen) &&
1329             (puLen > 0) )
1330         {
1331             wsprintfW(tmp, szVersionFormat,
1332                   HIWORD(ffi->dwFileVersionMS), LOWORD(ffi->dwFileVersionMS),
1333                   HIWORD(ffi->dwFileVersionLS), LOWORD(ffi->dwFileVersionLS));
1334             lstrcpynW(lpVersionBuf, tmp, *pcchVersionBuf);
1335             *pcchVersionBuf = strlenW(lpVersionBuf);
1336         }
1337         else
1338         {
1339             *lpVersionBuf = 0;
1340             *pcchVersionBuf = 0;
1341         }
1342     }
1343
1344     if( lpLangBuf && pcchLangBuf && *pcchLangBuf )
1345     {
1346         DWORD lang = GetUserDefaultLangID();
1347
1348         FIXME("Retrieve language from file\n");
1349         wsprintfW(tmp, szLangFormat, lang);
1350         lstrcpynW(lpLangBuf, tmp, *pcchLangBuf);
1351         *pcchLangBuf = strlenW(lpLangBuf);
1352     }
1353
1354 end:
1355     HeapFree(GetProcessHeap(), 0, lpVer);
1356     return ret;
1357 }
1358
1359
1360 /******************************************************************
1361  *      DllMain
1362  */
1363 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1364 {
1365     switch(fdwReason)
1366     {
1367     case DLL_PROCESS_ATTACH:
1368         msi_hInstance = hinstDLL;
1369         DisableThreadLibraryCalls(hinstDLL);
1370         msi_dialog_register_class();
1371         break;
1372     case DLL_PROCESS_DETACH:
1373         msi_dialog_unregister_class();
1374         /* FIXME: Cleanup */
1375         break;
1376     }
1377     return TRUE;
1378 }
1379
1380 typedef struct tagIClassFactoryImpl
1381 {
1382     IClassFactoryVtbl *lpVtbl;
1383 } IClassFactoryImpl;
1384
1385 static HRESULT WINAPI MsiCF_QueryInterface(LPCLASSFACTORY iface,
1386                 REFIID riid,LPVOID *ppobj)
1387 {
1388     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1389     FIXME("%p %s %p\n",This,debugstr_guid(riid),ppobj);
1390     return E_NOINTERFACE;
1391 }
1392
1393 static ULONG WINAPI MsiCF_AddRef(LPCLASSFACTORY iface)
1394 {
1395     return 2;
1396 }
1397
1398 static ULONG WINAPI MsiCF_Release(LPCLASSFACTORY iface)
1399 {
1400     return 1;
1401 }
1402
1403 static HRESULT WINAPI MsiCF_CreateInstance(LPCLASSFACTORY iface,
1404     LPUNKNOWN pOuter, REFIID riid, LPVOID *ppobj)
1405 {
1406     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1407
1408     FIXME("%p %p %s %p\n", This, pOuter, debugstr_guid(riid), ppobj);
1409     return E_FAIL;
1410 }
1411
1412 static HRESULT WINAPI MsiCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
1413 {
1414     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1415
1416     FIXME("%p %d\n", This, dolock);
1417     return S_OK;
1418 }
1419
1420 static IClassFactoryVtbl MsiCF_Vtbl =
1421 {
1422     MsiCF_QueryInterface,
1423     MsiCF_AddRef,
1424     MsiCF_Release,
1425     MsiCF_CreateInstance,
1426     MsiCF_LockServer
1427 };
1428
1429 static IClassFactoryImpl Msi_CF = { &MsiCF_Vtbl };
1430
1431 /******************************************************************
1432  *      DllGetClassObject [MSI.@]
1433  */
1434 HRESULT WINAPI MSI_DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
1435 {
1436     TRACE("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1437
1438     if( IsEqualCLSID (rclsid, &CLSID_IMsiServer) ||
1439         IsEqualCLSID (rclsid, &CLSID_IMsiServerMessage) ||
1440         IsEqualCLSID (rclsid, &CLSID_IMsiServerX1) ||
1441         IsEqualCLSID (rclsid, &CLSID_IMsiServerX2) ||
1442         IsEqualCLSID (rclsid, &CLSID_IMsiServerX3) )
1443     {
1444         *ppv = (LPVOID) &Msi_CF;
1445         return S_OK;
1446     }
1447     return CLASS_E_CLASSNOTAVAILABLE;
1448 }
1449
1450 /******************************************************************
1451  *      DllGetVersion [MSI.@]
1452  */
1453 HRESULT WINAPI MSI_DllGetVersion(DLLVERSIONINFO *pdvi)
1454 {
1455     TRACE("%p\n",pdvi);
1456   
1457     if (pdvi->cbSize != sizeof(DLLVERSIONINFO))
1458         return E_INVALIDARG;
1459   
1460     pdvi->dwMajorVersion = MSI_MAJORVERSION;
1461     pdvi->dwMinorVersion = MSI_MINORVERSION;
1462     pdvi->dwBuildNumber = MSI_BUILDNUMBER;
1463     pdvi->dwPlatformID = 1;
1464   
1465     return S_OK;
1466 }
1467
1468 /******************************************************************
1469  *      DllCanUnloadNow [MSI.@]
1470  */
1471 BOOL WINAPI MSI_DllCanUnloadNow(void)
1472 {
1473     return S_FALSE;
1474 }
1475
1476 UINT WINAPI MsiGetFeatureUsageW(LPCWSTR szProduct, LPCWSTR szFeature,
1477                                 DWORD* pdwUseCount, WORD* pwDateUsed)
1478 {
1479     FIXME("%s %s %p %p\n",debugstr_w(szProduct), debugstr_w(szFeature),
1480           pdwUseCount, pwDateUsed);
1481     return ERROR_CALL_NOT_IMPLEMENTED;
1482 }
1483
1484 UINT WINAPI MsiGetFeatureUsageA(LPCSTR szProduct, LPCSTR szFeature,
1485                                 DWORD* pdwUseCount, WORD* pwDateUsed)
1486 {
1487     FIXME("%s %s %p %p\n", debugstr_a(szProduct), debugstr_a(szFeature),
1488           pdwUseCount, pwDateUsed);
1489     return ERROR_CALL_NOT_IMPLEMENTED;
1490 }
1491
1492 INSTALLSTATE WINAPI MsiUseFeatureExW(LPCWSTR szProduct, LPCWSTR szFeature, 
1493                              DWORD dwInstallMode, DWORD dwReserved)
1494 {
1495     FIXME("%s %s %li %li\n", debugstr_w(szProduct), debugstr_w(szFeature),
1496           dwInstallMode, dwReserved);
1497
1498     /*
1499      * Polls all the components of the feature to find install state and then
1500      *  writes:
1501      *    Software\\Microsoft\\Windows\\CurrentVersion\\
1502      *    Installer\\Products\\<squishguid>\\<feature>
1503      *      "Usage"=dword:........
1504      */  
1505  
1506     return INSTALLSTATE_LOCAL; 
1507 }
1508
1509 INSTALLSTATE WINAPI MsiUseFeatureExA(LPCSTR szProduct, LPCSTR szFeature, 
1510                              DWORD dwInstallMode, DWORD dwReserved)
1511 {
1512     FIXME("%s %s %li %li\n", debugstr_a(szProduct), debugstr_a(szFeature),
1513           dwInstallMode, dwReserved);
1514    
1515     return INSTALLSTATE_LOCAL; 
1516 }
1517
1518 INSTALLSTATE WINAPI MsiUseFeatureW(LPCWSTR szProduct, LPCWSTR szFeature)
1519 {
1520     FIXME("%s %s\n", debugstr_w(szProduct), debugstr_w(szFeature));
1521    
1522     return INSTALLSTATE_LOCAL; 
1523 }
1524
1525 INSTALLSTATE WINAPI MsiUseFeatureA(LPCSTR szProduct, LPCSTR szFeature)
1526 {
1527     FIXME("%s %s\n", debugstr_a(szProduct), debugstr_a(szFeature));
1528    
1529     return INSTALLSTATE_LOCAL; 
1530 }
1531
1532 UINT WINAPI MsiProvideQualifiedComponentExW(LPCWSTR szComponent, 
1533                 LPCWSTR szQualifier, DWORD dwInstallMode, LPWSTR szProduct,
1534                 DWORD Unused1, DWORD Unused2, LPWSTR lpPathBuf, 
1535                 DWORD* pcchPathBuf)
1536 {
1537     FIXME("%s %s %li %s %li %li %p %p\n", debugstr_w(szComponent),
1538           debugstr_w(szQualifier), dwInstallMode, debugstr_w(szProduct),
1539           Unused1, Unused2, lpPathBuf, pcchPathBuf);
1540
1541     return ERROR_INDEX_ABSENT;
1542 }
1543
1544 USERINFOSTATE WINAPI MsiGetUserInfoW(LPCWSTR szProduct, LPWSTR lpUserNameBuf, 
1545                 DWORD* pcchUserNameBuf, LPWSTR lpOrgNameBuf, 
1546                 DWORD* pcchOrgNameBuf, LPWSTR lpSerialBuf, DWORD* pcchSerialBuf)
1547 {
1548     FIXME("%s %p %p %p %p %p %p\n",debugstr_w(szProduct), lpUserNameBuf,
1549           pcchUserNameBuf, lpOrgNameBuf, pcchOrgNameBuf, lpSerialBuf,
1550           pcchSerialBuf);
1551   
1552     return USERINFOSTATE_UNKNOWN; 
1553 }
1554
1555 USERINFOSTATE WINAPI MsiGetUserInfoA(LPCSTR szProduct, LPSTR lpUserNameBuf, 
1556                 DWORD* pcchUserNameBuf, LPSTR lpOrgNameBuf, 
1557                 DWORD* pcchOrgNameBuf, LPSTR lpSerialBuf, DWORD* pcchSerialBuf)
1558 {
1559     FIXME("%s %p %p %p %p %p %p\n",debugstr_a(szProduct), lpUserNameBuf,
1560           pcchUserNameBuf, lpOrgNameBuf, pcchOrgNameBuf, lpSerialBuf,
1561           pcchSerialBuf);
1562   
1563     return USERINFOSTATE_UNKNOWN; 
1564 }
1565
1566 UINT WINAPI MsiCollectUserInfoW(LPCWSTR szProduct)
1567 {
1568     FIXME("%s\n",debugstr_w(szProduct));
1569     return ERROR_CALL_NOT_IMPLEMENTED;
1570 }
1571
1572 UINT WINAPI MsiCollectUserInfoA(LPCSTR szProduct)
1573 {
1574     FIXME("%s\n",debugstr_a(szProduct));
1575     return ERROR_CALL_NOT_IMPLEMENTED;
1576 }
1577
1578 UINT WINAPI MsiCreateAndVerifyInstallerDirectory(void)
1579 {
1580     FIXME("\n");
1581     return ERROR_CALL_NOT_IMPLEMENTED;
1582 }
1583
1584 UINT WINAPI MsiGetShortcutTargetA( LPCSTR szShortcutTarget,
1585                                    LPSTR szProductCode, LPSTR szFeatureId,
1586                                    LPSTR szComponentCode )
1587 {
1588     FIXME("\n");
1589     return ERROR_CALL_NOT_IMPLEMENTED;
1590 }
1591
1592 UINT WINAPI MsiGetShortcutTargetW( LPCWSTR szShortcutTarget,
1593                                    LPWSTR szProductCode, LPWSTR szFeatureId,
1594                                    LPWSTR szComponentCode )
1595 {
1596     FIXME("\n");
1597     return ERROR_CALL_NOT_IMPLEMENTED;
1598 }
1599
1600 UINT WINAPI MsiReinstallFeatureW( LPCWSTR szProduct, LPCWSTR szFeature, 
1601                                   DWORD dwReinstallMode )
1602 {
1603     FIXME("%s %s %li\n", debugstr_w(szProduct), debugstr_w(szFeature),
1604                            dwReinstallMode);
1605     return ERROR_SUCCESS;
1606 }
1607
1608 UINT WINAPI MsiReinstallFeatureA( LPCSTR szProduct, LPCSTR szFeature, 
1609                                   DWORD dwReinstallMode )
1610 {
1611     FIXME("%s %s %li\n", debugstr_a(szProduct), debugstr_a(szFeature),
1612                            dwReinstallMode);
1613     return ERROR_SUCCESS;
1614 }