Prevent crash when no URL is specified.
[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     if (!package)
488     {
489         rc = ERROR_INVALID_HANDLE;
490         goto end;
491     }
492   
493     sz = strlenW(szInstalled);
494
495     if (szCommandLine)
496         sz += strlenW(szCommandLine);
497
498     commandline = HeapAlloc(GetProcessHeap(),0,sz * sizeof(WCHAR));
499
500     if (szCommandLine) 
501         strcpyW(commandline,szCommandLine);
502     else
503         commandline[0] = 0;
504
505     if (MsiQueryProductStateW(szProduct) != INSTALLSTATE_UNKNOWN)
506         strcatW(commandline,szInstalled);
507
508     rc = ACTION_DoTopLevelINSTALL(package, sourcepath, commandline);
509
510     msiobj_release( &package->hdr );
511
512     HeapFree(GetProcessHeap(),0,commandline);
513 end:
514     RegCloseKey(hkey);
515
516     return rc;
517 }
518
519 UINT WINAPI MsiConfigureProductExA(LPCSTR szProduct, int iInstallLevel,
520                         INSTALLSTATE eInstallState, LPCSTR szCommandLine)
521 {
522     LPWSTR szwProduct = NULL;
523     LPWSTR szwCommandLine = NULL;
524     UINT hr = ERROR_FUNCTION_FAILED;
525
526     if( szProduct )
527     {
528         UINT len = MultiByteToWideChar( CP_ACP, 0, szProduct, -1, NULL, 0 );
529         szwProduct = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
530         if( !szwProduct )
531             goto end;
532         MultiByteToWideChar( CP_ACP, 0, szProduct, -1, szwProduct, len );
533     }
534
535     if( szCommandLine)
536     {
537         UINT len = MultiByteToWideChar( CP_ACP, 0, szCommandLine, -1, NULL, 0 );
538         szwCommandLine= HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
539         if( !szwCommandLine)
540             goto end;
541         MultiByteToWideChar( CP_ACP, 0, szCommandLine, -1, szwCommandLine, len );
542     }
543
544     hr = MsiConfigureProductExW( szwProduct, iInstallLevel, eInstallState,
545                                 szwCommandLine );
546 end:
547     HeapFree( GetProcessHeap(), 0, szwProduct );
548     HeapFree( GetProcessHeap(), 0, szwCommandLine);
549
550     return hr;
551 }
552
553 UINT WINAPI MsiConfigureProductA(LPCSTR szProduct, int iInstallLevel, 
554                                  INSTALLSTATE eInstallState)
555 {
556     LPWSTR szwProduct = NULL;
557     UINT hr = ERROR_SUCCESS;
558
559     FIXME("%s %d %d\n",debugstr_a(szProduct), iInstallLevel, eInstallState);
560
561     if( szProduct )
562     {
563         UINT len = MultiByteToWideChar( CP_ACP, 0, szProduct, -1, NULL, 0 );
564         szwProduct = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
565         if( !szwProduct )
566             goto end;
567         MultiByteToWideChar( CP_ACP, 0, szProduct, -1, szwProduct, len );
568     }
569
570     hr = MsiConfigureProductW( szwProduct, iInstallLevel, eInstallState );
571
572 end:
573     HeapFree( GetProcessHeap(), 0, szwProduct );
574
575     return hr;
576 }
577
578 UINT WINAPI MsiConfigureProductW(LPCWSTR szProduct, int iInstallLevel, 
579                                  INSTALLSTATE eInstallState)
580 {
581     FIXME("%s %d %d\n", debugstr_w(szProduct), iInstallLevel, eInstallState);
582
583     return MsiConfigureProductExW(szProduct, iInstallLevel, eInstallState,
584                                   NULL);
585 }
586
587 UINT WINAPI MsiGetProductCodeA(LPCSTR szComponent, LPSTR szBuffer)
588 {
589     LPWSTR szwComponent = NULL;
590     UINT hr = ERROR_INSTALL_FAILURE;
591     WCHAR szwBuffer[GUID_SIZE];
592
593     FIXME("%s %s\n",debugstr_a(szComponent), debugstr_a(szBuffer));
594
595     if( szComponent )
596     {
597         UINT len = MultiByteToWideChar( CP_ACP, 0, szComponent, -1, NULL, 0 );
598         szwComponent = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
599         if( !szwComponent )
600             goto end;
601         MultiByteToWideChar( CP_ACP, 0, szComponent, -1, szwComponent, len );
602     }
603     else
604         return ERROR_INVALID_PARAMETER;
605
606     hr = MsiGetProductCodeW( szwComponent, szwBuffer );
607
608     if( ERROR_SUCCESS == hr )
609         WideCharToMultiByte(CP_ACP, 0, szwBuffer, -1, szBuffer, GUID_SIZE, NULL, NULL);
610
611 end:
612     HeapFree( GetProcessHeap(), 0, szwComponent );
613
614     return hr;
615 }
616
617 UINT WINAPI MsiGetProductCodeW(LPCWSTR szComponent, LPWSTR szBuffer)
618 {
619     FIXME("%s %s\n",debugstr_w(szComponent), debugstr_w(szBuffer));
620     if (NULL == szComponent)
621         return ERROR_INVALID_PARAMETER;
622     return ERROR_CALL_NOT_IMPLEMENTED;
623 }
624
625 UINT WINAPI MsiGetProductInfoA(LPCSTR szProduct, LPCSTR szAttribute,
626                  LPSTR szBuffer, DWORD *pcchValueBuf)
627 {
628     LPWSTR szwProduct = NULL, szwAttribute = NULL, szwBuffer = NULL;
629     UINT hr = ERROR_INSTALL_FAILURE;
630
631     FIXME("%s %s %p %p\n",debugstr_a(szProduct), debugstr_a(szAttribute),
632           szBuffer, pcchValueBuf);
633
634     if( NULL != szBuffer && NULL == pcchValueBuf )
635         return ERROR_INVALID_PARAMETER;
636     if( szProduct )
637     {
638         UINT len = MultiByteToWideChar( CP_ACP, 0, szProduct, -1, NULL, 0 );
639         szwProduct = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
640         if( !szwProduct )
641             goto end;
642         MultiByteToWideChar( CP_ACP, 0, szProduct, -1, szwProduct, len );
643     }
644     else
645         return ERROR_INVALID_PARAMETER;
646     
647     if( szAttribute )
648     {
649         UINT len = MultiByteToWideChar( CP_ACP, 0, szAttribute, -1, NULL, 0 );
650         szwAttribute = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
651         if( !szwAttribute )
652             goto end;
653         MultiByteToWideChar( CP_ACP, 0, szAttribute, -1, szwAttribute, len );
654     }
655     else
656     {
657         hr = ERROR_INVALID_PARAMETER;
658         goto end;
659     }
660
661     if( szBuffer )
662     {
663         szwBuffer = HeapAlloc( GetProcessHeap(), 0, (*pcchValueBuf) * sizeof(WCHAR) );
664         if( !szwBuffer )     
665             goto end;
666     }
667
668     hr = MsiGetProductInfoW( szwProduct, szwAttribute, szwBuffer, pcchValueBuf );
669
670     if( ERROR_SUCCESS == hr )
671         WideCharToMultiByte(CP_ACP, 0, szwBuffer, -1, szBuffer, *pcchValueBuf, NULL, NULL);
672
673 end:
674     HeapFree( GetProcessHeap(), 0, szwProduct );
675     HeapFree( GetProcessHeap(), 0, szwAttribute );
676     HeapFree( GetProcessHeap(), 0, szwBuffer );
677
678     return hr;    
679 }
680
681 UINT WINAPI MsiGetProductInfoW(LPCWSTR szProduct, LPCWSTR szAttribute,
682                 LPWSTR szBuffer, DWORD *pcchValueBuf)
683 {
684     MSIHANDLE hProduct;
685     UINT hr;
686     
687     FIXME("%s %s %p %p\n",debugstr_w(szProduct), debugstr_w(szAttribute),
688           szBuffer, pcchValueBuf);
689
690     if (NULL != szBuffer && NULL == pcchValueBuf)
691         return ERROR_INVALID_PARAMETER;
692     if (NULL == szProduct || NULL == szAttribute)
693         return ERROR_INVALID_PARAMETER;
694
695     hr = MsiOpenProductW(szProduct, &hProduct);
696     if (ERROR_SUCCESS != hr)
697         return hr;
698
699     hr = MsiGetPropertyW(hProduct, szAttribute, szBuffer, pcchValueBuf);
700     MsiCloseHandle(hProduct);
701     return hr;
702 }
703
704 UINT WINAPI MsiDatabaseImportA(LPCSTR szFolderPath, LPCSTR szFilename)
705 {
706     FIXME("%s %s\n",debugstr_a(szFolderPath), debugstr_a(szFilename));
707     return ERROR_CALL_NOT_IMPLEMENTED;
708 }
709
710 UINT WINAPI MsiDatabaseImportW(LPCWSTR szFolderPath, LPCWSTR szFilename)
711 {
712     FIXME("%s %s\n",debugstr_w(szFolderPath), debugstr_w(szFilename));
713     return ERROR_CALL_NOT_IMPLEMENTED;
714 }
715
716 UINT WINAPI MsiEnableLogA(DWORD dwLogMode, LPCSTR szLogFile, DWORD attributes)
717 {
718     LPWSTR szwLogFile = NULL;
719     UINT hr = ERROR_INSTALL_FAILURE;
720
721     FIXME("%08lx %s %08lx\n", dwLogMode, debugstr_a(szLogFile), attributes);
722
723     if( szLogFile )
724     {
725         UINT len = MultiByteToWideChar( CP_ACP, 0, szLogFile, -1, NULL, 0 );
726         szwLogFile = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
727         if( !szwLogFile )
728             goto end;
729         MultiByteToWideChar( CP_ACP, 0, szLogFile, -1, szwLogFile, len );
730     }
731     else
732         return ERROR_INVALID_PARAMETER;
733
734     hr = MsiEnableLogW( dwLogMode, szwLogFile, attributes );
735
736 end:
737     HeapFree( GetProcessHeap(), 0, szwLogFile );
738
739     return hr;
740 }
741
742 UINT WINAPI MsiEnableLogW(DWORD dwLogMode, LPCWSTR szLogFile, DWORD attributes)
743 {
744     HANDLE file = INVALID_HANDLE_VALUE;
745
746     TRACE("%08lx %s %08lx\n", dwLogMode, debugstr_w(szLogFile), attributes);
747
748     strcpyW(gszLogFile,szLogFile);
749     if (!(attributes & INSTALLLOGATTRIBUTES_APPEND))
750         DeleteFileW(szLogFile);
751     file = CreateFileW(szLogFile, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
752                            FILE_ATTRIBUTE_NORMAL, NULL);
753     if (file != INVALID_HANDLE_VALUE)
754         CloseHandle(file);
755     else
756         ERR("Unable to enable log %s\n",debugstr_w(szLogFile));
757
758     return ERROR_SUCCESS;
759 }
760
761 INSTALLSTATE WINAPI MsiQueryProductStateA(LPCSTR szProduct)
762 {
763     LPWSTR szwProduct;
764     UINT len;
765     INSTALLSTATE rc;
766
767     len = MultiByteToWideChar(CP_ACP,0,szProduct,-1,NULL,0);
768     szwProduct = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
769     MultiByteToWideChar(CP_ACP,0,szProduct,-1,szwProduct,len);
770     rc = MsiQueryProductStateW(szwProduct);
771     HeapFree(GetProcessHeap(),0,szwProduct);
772     return rc;
773 }
774
775 INSTALLSTATE WINAPI MsiQueryProductStateW(LPCWSTR szProduct)
776 {
777     UINT rc;
778     INSTALLSTATE rrc = INSTALLSTATE_UNKNOWN;
779     HKEY hkey=0;
780     static const WCHAR szWindowsInstaller[] = {
781          'W','i','n','d','o','w','s','I','n','s','t','a','l','l','e','r',0 };
782     DWORD sz;
783
784     TRACE("%s\n", debugstr_w(szProduct));
785
786     rc = MSIREG_OpenUserProductsKey(szProduct,&hkey,FALSE);
787     if (rc != ERROR_SUCCESS)
788         goto end;
789
790     RegCloseKey(hkey);
791
792     rc = MSIREG_OpenUninstallKey(szProduct,&hkey,FALSE);
793     if (rc != ERROR_SUCCESS)
794         goto end;
795
796     sz = sizeof(rrc);
797     rc = RegQueryValueExW(hkey,szWindowsInstaller,NULL,NULL,(LPVOID)&rrc, &sz);
798     if (rc != ERROR_SUCCESS)
799         goto end;
800
801     switch (rrc)
802     {
803     case 1:
804         /* default */
805         rrc = INSTALLSTATE_DEFAULT;
806         break;
807     default:
808         FIXME("Unknown install state read from registry (%i)\n",rrc);
809         rrc = INSTALLSTATE_UNKNOWN;
810         break;
811     }
812 end:
813     RegCloseKey(hkey);
814     return rrc;
815 }
816
817 INSTALLUILEVEL WINAPI MsiSetInternalUI(INSTALLUILEVEL dwUILevel, HWND *phWnd)
818 {
819     INSTALLUILEVEL old = gUILevel;
820     HWND oldwnd = gUIhwnd;
821
822     TRACE("%08x %p\n", dwUILevel, phWnd);
823
824     gUILevel = dwUILevel;
825     if (phWnd)
826     {
827         gUIhwnd = *phWnd;
828         *phWnd = oldwnd;
829     }
830     return old;
831 }
832
833 INSTALLUI_HANDLERA WINAPI MsiSetExternalUIA(INSTALLUI_HANDLERA puiHandler, 
834                                   DWORD dwMessageFilter, LPVOID pvContext)
835 {
836     INSTALLUI_HANDLERA prev = gUIHandlerA;
837
838     TRACE("%p %lx %p\n",puiHandler, dwMessageFilter,pvContext);
839     gUIHandlerA = puiHandler;
840     gUIFilter = dwMessageFilter;
841     gUIContext = pvContext;
842
843     return prev;
844 }
845
846 INSTALLUI_HANDLERW WINAPI MsiSetExternalUIW(INSTALLUI_HANDLERW puiHandler,
847                                   DWORD dwMessageFilter, LPVOID pvContext)
848 {
849     INSTALLUI_HANDLERW prev = gUIHandlerW;
850
851     TRACE("%p %lx %p\n",puiHandler,dwMessageFilter,pvContext);
852     gUIHandlerW = puiHandler;
853     gUIFilter = dwMessageFilter;
854     gUIContext = pvContext;
855
856     return prev;
857 }
858
859 /******************************************************************
860  *  MsiLoadStringW            [MSI.@]
861  *
862  * Loads a string from MSI's string resources.
863  *
864  * PARAMS
865  *
866  *   handle        [I]  only -1 is handled currently
867  *   id            [I]  id of the string to be loaded
868  *   lpBuffer      [O]  buffer for the string to be written to
869  *   nBufferMax    [I]  maximum size of the buffer in characters
870  *   lang          [I]  the preferred language for the string
871  *
872  * RETURNS
873  *
874  *   If successful, this function returns the language id of the string loaded
875  *   If the function fails, the function returns zero.
876  *
877  * NOTES
878  *
879  *   The type of the first parameter is unknown.  LoadString's prototype
880  *  suggests that it might be a module handle.  I have made it an MSI handle
881  *  for starters, as -1 is an invalid MSI handle, but not an invalid module
882  *  handle.  Maybe strings can be stored in an MSI database somehow.
883  */
884 LANGID WINAPI MsiLoadStringW( MSIHANDLE handle, UINT id, LPWSTR lpBuffer,
885                 int nBufferMax, LANGID lang )
886 {
887     HRSRC hres;
888     HGLOBAL hResData;
889     LPWSTR p;
890     DWORD i, len;
891
892     TRACE("%ld %u %p %d %d\n", handle, id, lpBuffer, nBufferMax, lang);
893
894     if( handle != -1 )
895         FIXME("don't know how to deal with handle = %08lx\n", handle);
896
897     if( !lang )
898         lang = GetUserDefaultLangID();
899
900     hres = FindResourceExW( msi_hInstance, (LPCWSTR) RT_STRING,
901                             (LPWSTR)1, lang );
902     if( !hres )
903         return 0;
904     hResData = LoadResource( msi_hInstance, hres );
905     if( !hResData )
906         return 0;
907     p = LockResource( hResData );
908     if( !p )
909         return 0;
910
911     for (i = 0; i < (id&0xf); i++)
912         p += *p + 1;
913     len = *p;
914
915     if( nBufferMax <= len )
916         return 0;
917
918     memcpy( lpBuffer, p+1, len * sizeof(WCHAR));
919     lpBuffer[ len ] = 0;
920
921     TRACE("found -> %s\n", debugstr_w(lpBuffer));
922
923     return lang;
924 }
925
926 LANGID WINAPI MsiLoadStringA( MSIHANDLE handle, UINT id, LPSTR lpBuffer,
927                 int nBufferMax, LANGID lang )
928 {
929     LPWSTR bufW;
930     LANGID r;
931     DWORD len;
932
933     bufW = HeapAlloc(GetProcessHeap(), 0, nBufferMax*sizeof(WCHAR));
934     r = MsiLoadStringW(handle, id, bufW, nBufferMax, lang);
935     if( r )
936     {
937         len = WideCharToMultiByte(CP_ACP, 0, bufW, -1, NULL, 0, NULL, NULL );
938         if( len <= nBufferMax )
939             WideCharToMultiByte( CP_ACP, 0, bufW, -1,
940                                  lpBuffer, nBufferMax, NULL, NULL );
941         else
942             r = 0;
943     }
944     HeapFree(GetProcessHeap(), 0, bufW);
945     return r;
946 }
947
948 INSTALLSTATE WINAPI MsiLocateComponentA(LPCSTR szComponent, LPSTR lpPathBuf,
949                 DWORD *pcchBuf)
950 {
951     FIXME("%s %p %08lx\n", debugstr_a(szComponent), lpPathBuf, *pcchBuf);
952     return INSTALLSTATE_UNKNOWN;
953 }
954
955 INSTALLSTATE WINAPI MsiLocateComponentW(LPCWSTR szComponent, LPSTR lpPathBuf,
956                 DWORD *pcchBuf)
957 {
958     FIXME("%s %p %08lx\n", debugstr_w(szComponent), lpPathBuf, *pcchBuf);
959     return INSTALLSTATE_UNKNOWN;
960 }
961
962 UINT WINAPI MsiMessageBoxA(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType,
963                 WORD wLanguageId, DWORD f)
964 {
965     FIXME("%p %s %s %u %08x %08lx\n",hWnd,debugstr_a(lpText),debugstr_a(lpCaption),
966           uType,wLanguageId,f);
967     return ERROR_CALL_NOT_IMPLEMENTED;
968 }
969
970 UINT WINAPI MsiMessageBoxW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType,
971                 WORD wLanguageId, DWORD f)
972 {
973     FIXME("%p %s %s %u %08x %08lx\n",hWnd,debugstr_w(lpText),debugstr_w(lpCaption),
974           uType,wLanguageId,f);
975     return ERROR_CALL_NOT_IMPLEMENTED;
976 }
977
978 UINT WINAPI MsiProvideAssemblyA( LPCSTR szAssemblyName, LPCSTR szAppContext,
979                 DWORD dwInstallMode, DWORD dwAssemblyInfo, LPSTR lpPathBuf,
980                 DWORD* pcchPathBuf ) 
981 {
982     FIXME("%s %s %08lx %08lx %p %p\n", debugstr_a(szAssemblyName),
983           debugstr_a(szAppContext), dwInstallMode, dwAssemblyInfo, lpPathBuf,
984           pcchPathBuf);
985     return ERROR_CALL_NOT_IMPLEMENTED;
986 }
987
988 UINT WINAPI MsiProvideAssemblyW( LPCWSTR szAssemblyName, LPCWSTR szAppContext,
989                 DWORD dwInstallMode, DWORD dwAssemblyInfo, LPWSTR lpPathBuf,
990                 DWORD* pcchPathBuf )
991 {
992     FIXME("%s %s %08lx %08lx %p %p\n", debugstr_w(szAssemblyName), 
993           debugstr_w(szAppContext), dwInstallMode, dwAssemblyInfo, lpPathBuf,
994           pcchPathBuf);
995     return ERROR_CALL_NOT_IMPLEMENTED;
996 }
997
998 UINT WINAPI MsiProvideComponentFromDescriptorA( LPCSTR szDescriptor,
999                 LPSTR szPath, DWORD *pcchPath, DWORD *pcchArgs )
1000 {
1001     FIXME("%s %p %p %p\n", debugstr_a(szDescriptor), szPath, pcchPath, pcchArgs );
1002     return ERROR_CALL_NOT_IMPLEMENTED;
1003 }
1004
1005 UINT WINAPI MsiProvideComponentFromDescriptorW( LPCWSTR szDescriptor,
1006                 LPWSTR szPath, DWORD *pcchPath, DWORD *pcchArgs )
1007 {
1008     FIXME("%s %p %p %p\n", debugstr_w(szDescriptor), szPath, pcchPath, pcchArgs );
1009     return ERROR_CALL_NOT_IMPLEMENTED;
1010 }
1011
1012 HRESULT WINAPI MsiGetFileSignatureInformationA( LPCSTR szSignedObjectPath,
1013                 DWORD dwFlags, PCCERT_CONTEXT* ppcCertContext, BYTE* pbHashData,
1014                 DWORD* pcbHashData)
1015 {
1016     FIXME("%s %08lx %p %p %p\n", debugstr_a(szSignedObjectPath), dwFlags,
1017           ppcCertContext, pbHashData, pcbHashData);
1018     return ERROR_CALL_NOT_IMPLEMENTED;
1019 }
1020
1021 HRESULT WINAPI MsiGetFileSignatureInformationW( LPCWSTR szSignedObjectPath,
1022                 DWORD dwFlags, PCCERT_CONTEXT* ppcCertContext, BYTE* pbHashData,
1023                 DWORD* pcbHashData)
1024 {
1025     FIXME("%s %08lx %p %p %p\n", debugstr_w(szSignedObjectPath), dwFlags,
1026           ppcCertContext, pbHashData, pcbHashData);
1027     return ERROR_CALL_NOT_IMPLEMENTED;
1028 }
1029
1030 UINT WINAPI MsiGetProductPropertyA( MSIHANDLE hProduct, LPCSTR szProperty,
1031                                     LPSTR szValue, DWORD *pccbValue )
1032 {
1033     FIXME("%ld %s %p %p\n", hProduct, debugstr_a(szProperty), szValue, pccbValue);
1034     return ERROR_CALL_NOT_IMPLEMENTED;
1035 }
1036
1037 UINT WINAPI MsiGetProductPropertyW( MSIHANDLE hProduct, LPCWSTR szProperty,
1038                                     LPWSTR szValue, DWORD *pccbValue )
1039 {
1040     FIXME("%ld %s %p %p\n", hProduct, debugstr_w(szProperty), szValue, pccbValue);
1041     return ERROR_CALL_NOT_IMPLEMENTED;
1042 }
1043
1044 UINT WINAPI MsiVerifyPackageA( LPCSTR szPackage )
1045 {
1046     UINT r, len;
1047     LPWSTR szPack = NULL;
1048
1049     TRACE("%s\n", debugstr_a(szPackage) );
1050
1051     if( szPackage )
1052     {
1053         len = MultiByteToWideChar( CP_ACP, 0, szPackage, -1, NULL, 0 );
1054         szPack = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1055         if( !szPack )
1056             return ERROR_OUTOFMEMORY;
1057         MultiByteToWideChar( CP_ACP, 0, szPackage, -1, szPack, len );
1058     }
1059
1060     r = MsiVerifyPackageW( szPack );
1061
1062     HeapFree( GetProcessHeap(), 0, szPack );
1063
1064     return r;
1065 }
1066
1067 UINT WINAPI MsiVerifyPackageW( LPCWSTR szPackage )
1068 {
1069     MSIHANDLE handle;
1070     UINT r;
1071
1072     TRACE("%s\n", debugstr_w(szPackage) );
1073
1074     r = MsiOpenDatabaseW( szPackage, MSIDBOPEN_READONLY, &handle );
1075     MsiCloseHandle( handle );
1076
1077     return r;
1078 }
1079
1080 INSTALLSTATE WINAPI MsiGetComponentPathA(LPCSTR szProduct, LPCSTR szComponent,
1081                                          LPSTR lpPathBuf, DWORD* pcchBuf)
1082 {
1083     LPWSTR szwProduct = NULL, szwComponent = NULL, lpwPathBuf= NULL;
1084     INSTALLSTATE rc;
1085     UINT len, incoming_len;
1086
1087     if( szProduct )
1088     {
1089         len = MultiByteToWideChar( CP_ACP, 0, szProduct, -1, NULL, 0 );
1090         szwProduct= HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1091         if( !szwProduct)
1092             return ERROR_OUTOFMEMORY;
1093         MultiByteToWideChar( CP_ACP, 0, szProduct, -1, szwProduct, len );
1094     }
1095
1096     if( szComponent )
1097     {
1098         len = MultiByteToWideChar( CP_ACP, 0, szComponent, -1, NULL, 0 );
1099         szwComponent= HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1100         if( !szwComponent )
1101         {
1102             HeapFree( GetProcessHeap(), 0, szwProduct);
1103             return ERROR_OUTOFMEMORY;
1104         }
1105         MultiByteToWideChar( CP_ACP, 0, szComponent, -1, szwComponent, len );
1106     }
1107
1108     if( pcchBuf && *pcchBuf > 0 )
1109         lpwPathBuf = HeapAlloc( GetProcessHeap(), 0, *pcchBuf * sizeof(WCHAR));
1110     else
1111         lpwPathBuf = NULL;
1112
1113     incoming_len = *pcchBuf;
1114     rc = MsiGetComponentPathW(szwProduct, szwComponent, lpwPathBuf, pcchBuf);
1115
1116     HeapFree( GetProcessHeap(), 0, szwProduct);
1117     HeapFree( GetProcessHeap(), 0, szwComponent);
1118     if (lpwPathBuf)
1119     {
1120         if (rc != INSTALLSTATE_UNKNOWN)
1121             WideCharToMultiByte(CP_ACP, 0, lpwPathBuf, incoming_len,
1122                             lpPathBuf, incoming_len, NULL, NULL);
1123         HeapFree( GetProcessHeap(), 0, lpwPathBuf);
1124     }
1125
1126     return rc;
1127 }
1128
1129 INSTALLSTATE WINAPI MsiGetComponentPathW(LPCWSTR szProduct, LPCWSTR szComponent,
1130                                          LPWSTR lpPathBuf, DWORD* pcchBuf)
1131 {
1132     WCHAR squished_pc[GUID_SIZE];
1133     UINT rc;
1134     INSTALLSTATE rrc = INSTALLSTATE_UNKNOWN;
1135     HKEY hkey = 0;
1136     LPWSTR path = NULL;
1137     DWORD sz, type;
1138
1139     TRACE("%s %s %p %p\n", debugstr_w(szProduct),
1140            debugstr_w(szComponent), lpPathBuf, pcchBuf);
1141
1142     if( lpPathBuf && !pcchBuf )
1143         return INSTALLSTATE_INVALIDARG;
1144
1145     squash_guid(szProduct,squished_pc);
1146
1147     rc = MSIREG_OpenProductsKey( szProduct, &hkey, FALSE);
1148     if( rc != ERROR_SUCCESS )
1149         goto end;
1150
1151     RegCloseKey(hkey);
1152
1153     rc = MSIREG_OpenComponentsKey( szComponent, &hkey, FALSE);
1154     if( rc != ERROR_SUCCESS )
1155         goto end;
1156
1157     sz = 0;
1158     type = 0;
1159     rc = RegQueryValueExW( hkey, squished_pc, NULL, &type, NULL, &sz );
1160     if( rc != ERROR_SUCCESS )
1161         goto end;
1162     if( type != REG_SZ )
1163         goto end;
1164
1165     sz += sizeof(WCHAR);
1166     path = HeapAlloc( GetProcessHeap(), 0, sz );
1167     if( !path )
1168         goto end;
1169
1170     rc = RegQueryValueExW( hkey, squished_pc, NULL, NULL, (LPVOID) path, &sz );
1171     if( rc != ERROR_SUCCESS )
1172         goto end;
1173
1174     TRACE("found path of (%s:%s)(%s)\n", debugstr_w(szComponent),
1175            debugstr_w(szProduct), debugstr_w(path));
1176
1177     if (path[0]=='0')
1178     {
1179         FIXME("Registry entry.. check entry\n");
1180             rrc = INSTALLSTATE_LOCAL;
1181     }
1182     else
1183     {
1184         /* PROBABLY a file */
1185         if ( GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES )
1186             rrc = INSTALLSTATE_LOCAL;
1187         else
1188             rrc = INSTALLSTATE_ABSENT;
1189     }
1190
1191     if( pcchBuf )
1192     {
1193         sz = sz / sizeof(WCHAR);
1194         if( *pcchBuf >= sz )
1195             strcpyW( lpPathBuf, path );
1196         *pcchBuf = sz;
1197     }
1198
1199 end:
1200     HeapFree(GetProcessHeap(), 0, path );
1201     RegCloseKey(hkey);
1202     return rrc;
1203 }
1204
1205 INSTALLSTATE WINAPI MsiQueryFeatureStateA(LPCSTR szProduct, LPCSTR szFeature)
1206 {
1207     INSTALLSTATE rc;
1208     UINT len;
1209     LPWSTR szwProduct= NULL;
1210     LPWSTR szwFeature= NULL;
1211
1212     if( szProduct )
1213     {
1214         len = MultiByteToWideChar( CP_ACP, 0, szProduct, -1, NULL, 0 );
1215         szwProduct= HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1216         if( !szwProduct)
1217             return ERROR_OUTOFMEMORY;
1218         MultiByteToWideChar( CP_ACP, 0, szProduct, -1, szwProduct, len );
1219     }
1220
1221     if( szFeature )
1222     {
1223         len = MultiByteToWideChar( CP_ACP, 0, szFeature, -1, NULL, 0 );
1224         szwFeature= HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1225         if( !szwFeature)
1226         {
1227             HeapFree( GetProcessHeap(), 0, szwProduct);
1228             return ERROR_OUTOFMEMORY;
1229         }
1230         MultiByteToWideChar( CP_ACP, 0, szFeature, -1, szwFeature, len );
1231     }
1232
1233     rc = MsiQueryFeatureStateW(szwProduct, szwFeature);
1234
1235     HeapFree( GetProcessHeap(), 0, szwProduct);
1236     HeapFree( GetProcessHeap(), 0, szwFeature);
1237
1238     return rc;
1239 }
1240
1241 INSTALLSTATE WINAPI MsiQueryFeatureStateW(LPCWSTR szProduct, LPCWSTR szFeature)
1242 {
1243     FIXME("%s %s\n", debugstr_w(szProduct), debugstr_w(szFeature));
1244     /*
1245      * Iterates all the features components and the features parents components
1246      */
1247     return INSTALLSTATE_LOCAL;
1248 }
1249
1250 UINT WINAPI MsiGetFileVersionA(LPCSTR szFilePath, LPSTR lpVersionBuf,
1251                 DWORD* pcchVersionBuf, LPSTR lpLangBuf, DWORD* pcchLangBuf)
1252 {
1253     LPWSTR szwFilePath = NULL, lpwVersionBuff = NULL, lpwLangBuff = NULL;
1254     UINT len, ret = ERROR_OUTOFMEMORY;
1255     
1256     if( szFilePath )
1257     {
1258         len = MultiByteToWideChar( CP_ACP, 0, szFilePath, -1, NULL, 0 );
1259         szwFilePath = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1260         if( !szwFilePath )
1261             goto end;
1262         MultiByteToWideChar( CP_ACP, 0, szFilePath, -1, szwFilePath, len );
1263     }
1264     
1265     if( lpVersionBuf && pcchVersionBuf && *pcchVersionBuf )
1266     {
1267         lpwVersionBuff = HeapAlloc(GetProcessHeap(), 0, *pcchVersionBuf*sizeof(WCHAR));
1268         if( !lpwVersionBuff )
1269             goto end;
1270     }
1271
1272     if( lpLangBuf && pcchLangBuf && *pcchLangBuf )
1273     {
1274         lpwLangBuff = HeapAlloc(GetProcessHeap(), 0, *pcchVersionBuf*sizeof(WCHAR));
1275         if( !lpwLangBuff )
1276             goto end;
1277     }
1278         
1279     ret = MsiGetFileVersionW(szwFilePath, lpwVersionBuff, pcchVersionBuf,
1280                              lpwLangBuff, pcchLangBuf);
1281     
1282     if( lpwVersionBuff )
1283         WideCharToMultiByte(CP_ACP, 0, lpwVersionBuff, -1,
1284                             lpVersionBuf, *pcchVersionBuf, NULL, NULL);
1285     if( lpwLangBuff )
1286         WideCharToMultiByte(CP_ACP, 0, lpwLangBuff, -1,
1287                             lpLangBuf, *pcchLangBuf, NULL, NULL);
1288     
1289 end:
1290     HeapFree(GetProcessHeap(), 0, szwFilePath);
1291     HeapFree(GetProcessHeap(), 0, lpwVersionBuff);
1292     HeapFree(GetProcessHeap(), 0, lpwLangBuff);
1293     
1294     return ret;
1295 }
1296
1297 UINT WINAPI MsiGetFileVersionW(LPCWSTR szFilePath, LPWSTR lpVersionBuf,
1298                 DWORD* pcchVersionBuf, LPWSTR lpLangBuf, DWORD* pcchLangBuf)
1299 {
1300     static const WCHAR szVersionResource[] = {'\\',0};
1301     static const WCHAR szVersionFormat[] = {
1302         '%','d','.','%','d','.','%','d','.','%','d',0};
1303     static const WCHAR szLangFormat[] = {'%','d',0};
1304     UINT ret = 0;
1305     DWORD dwVerLen;
1306     LPVOID lpVer = NULL;
1307     VS_FIXEDFILEINFO *ffi;
1308     UINT puLen;
1309     WCHAR tmp[32];
1310
1311     TRACE("%s %p %ld %p %ld\n", debugstr_w(szFilePath),
1312           lpVersionBuf, pcchVersionBuf?*pcchVersionBuf:0,
1313           lpLangBuf, pcchLangBuf?*pcchLangBuf:0);
1314
1315     dwVerLen = GetFileVersionInfoSizeW(szFilePath, NULL);
1316     if( !dwVerLen )
1317         return GetLastError();
1318
1319     lpVer = HeapAlloc(GetProcessHeap(), 0, dwVerLen);
1320     if( !lpVer )
1321     {
1322         ret = ERROR_OUTOFMEMORY;
1323         goto end;
1324     }
1325
1326     if( !GetFileVersionInfoW(szFilePath, 0, dwVerLen, lpVer) )
1327     {
1328         ret = GetLastError();
1329         goto end;
1330     }
1331     if( lpVersionBuf && pcchVersionBuf && *pcchVersionBuf )
1332     {
1333         if( VerQueryValueW(lpVer, szVersionResource, (LPVOID*)&ffi, &puLen) &&
1334             (puLen > 0) )
1335         {
1336             wsprintfW(tmp, szVersionFormat,
1337                   HIWORD(ffi->dwFileVersionMS), LOWORD(ffi->dwFileVersionMS),
1338                   HIWORD(ffi->dwFileVersionLS), LOWORD(ffi->dwFileVersionLS));
1339             lstrcpynW(lpVersionBuf, tmp, *pcchVersionBuf);
1340             *pcchVersionBuf = strlenW(lpVersionBuf);
1341         }
1342         else
1343         {
1344             *lpVersionBuf = 0;
1345             *pcchVersionBuf = 0;
1346         }
1347     }
1348
1349     if( lpLangBuf && pcchLangBuf && *pcchLangBuf )
1350     {
1351         DWORD lang = GetUserDefaultLangID();
1352
1353         FIXME("Retrieve language from file\n");
1354         wsprintfW(tmp, szLangFormat, lang);
1355         lstrcpynW(lpLangBuf, tmp, *pcchLangBuf);
1356         *pcchLangBuf = strlenW(lpLangBuf);
1357     }
1358
1359 end:
1360     HeapFree(GetProcessHeap(), 0, lpVer);
1361     return ret;
1362 }
1363
1364
1365 /******************************************************************
1366  *      DllMain
1367  */
1368 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1369 {
1370     switch(fdwReason)
1371     {
1372     case DLL_PROCESS_ATTACH:
1373         msi_hInstance = hinstDLL;
1374         DisableThreadLibraryCalls(hinstDLL);
1375         msi_dialog_register_class();
1376         break;
1377     case DLL_PROCESS_DETACH:
1378         msi_dialog_unregister_class();
1379         /* FIXME: Cleanup */
1380         break;
1381     }
1382     return TRUE;
1383 }
1384
1385 typedef struct tagIClassFactoryImpl
1386 {
1387     IClassFactoryVtbl *lpVtbl;
1388 } IClassFactoryImpl;
1389
1390 static HRESULT WINAPI MsiCF_QueryInterface(LPCLASSFACTORY iface,
1391                 REFIID riid,LPVOID *ppobj)
1392 {
1393     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1394     FIXME("%p %s %p\n",This,debugstr_guid(riid),ppobj);
1395     return E_NOINTERFACE;
1396 }
1397
1398 static ULONG WINAPI MsiCF_AddRef(LPCLASSFACTORY iface)
1399 {
1400     return 2;
1401 }
1402
1403 static ULONG WINAPI MsiCF_Release(LPCLASSFACTORY iface)
1404 {
1405     return 1;
1406 }
1407
1408 static HRESULT WINAPI MsiCF_CreateInstance(LPCLASSFACTORY iface,
1409     LPUNKNOWN pOuter, REFIID riid, LPVOID *ppobj)
1410 {
1411     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1412
1413     FIXME("%p %p %s %p\n", This, pOuter, debugstr_guid(riid), ppobj);
1414     return E_FAIL;
1415 }
1416
1417 static HRESULT WINAPI MsiCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
1418 {
1419     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
1420
1421     FIXME("%p %d\n", This, dolock);
1422     return S_OK;
1423 }
1424
1425 static IClassFactoryVtbl MsiCF_Vtbl =
1426 {
1427     MsiCF_QueryInterface,
1428     MsiCF_AddRef,
1429     MsiCF_Release,
1430     MsiCF_CreateInstance,
1431     MsiCF_LockServer
1432 };
1433
1434 static IClassFactoryImpl Msi_CF = { &MsiCF_Vtbl };
1435
1436 /******************************************************************
1437  *      DllGetClassObject [MSI.@]
1438  */
1439 HRESULT WINAPI MSI_DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
1440 {
1441     TRACE("%s %s %p\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1442
1443     if( IsEqualCLSID (rclsid, &CLSID_IMsiServer) ||
1444         IsEqualCLSID (rclsid, &CLSID_IMsiServerMessage) ||
1445         IsEqualCLSID (rclsid, &CLSID_IMsiServerX1) ||
1446         IsEqualCLSID (rclsid, &CLSID_IMsiServerX2) ||
1447         IsEqualCLSID (rclsid, &CLSID_IMsiServerX3) )
1448     {
1449         *ppv = (LPVOID) &Msi_CF;
1450         return S_OK;
1451     }
1452     return CLASS_E_CLASSNOTAVAILABLE;
1453 }
1454
1455 /******************************************************************
1456  *      DllGetVersion [MSI.@]
1457  */
1458 HRESULT WINAPI MSI_DllGetVersion(DLLVERSIONINFO *pdvi)
1459 {
1460     TRACE("%p\n",pdvi);
1461   
1462     if (pdvi->cbSize != sizeof(DLLVERSIONINFO))
1463         return E_INVALIDARG;
1464   
1465     pdvi->dwMajorVersion = MSI_MAJORVERSION;
1466     pdvi->dwMinorVersion = MSI_MINORVERSION;
1467     pdvi->dwBuildNumber = MSI_BUILDNUMBER;
1468     pdvi->dwPlatformID = 1;
1469   
1470     return S_OK;
1471 }
1472
1473 /******************************************************************
1474  *      DllCanUnloadNow [MSI.@]
1475  */
1476 BOOL WINAPI MSI_DllCanUnloadNow(void)
1477 {
1478     return S_FALSE;
1479 }
1480
1481 UINT WINAPI MsiGetFeatureUsageW(LPCWSTR szProduct, LPCWSTR szFeature,
1482                                 DWORD* pdwUseCount, WORD* pwDateUsed)
1483 {
1484     FIXME("%s %s %p %p\n",debugstr_w(szProduct), debugstr_w(szFeature),
1485           pdwUseCount, pwDateUsed);
1486     return ERROR_CALL_NOT_IMPLEMENTED;
1487 }
1488
1489 UINT WINAPI MsiGetFeatureUsageA(LPCSTR szProduct, LPCSTR szFeature,
1490                                 DWORD* pdwUseCount, WORD* pwDateUsed)
1491 {
1492     FIXME("%s %s %p %p\n", debugstr_a(szProduct), debugstr_a(szFeature),
1493           pdwUseCount, pwDateUsed);
1494     return ERROR_CALL_NOT_IMPLEMENTED;
1495 }
1496
1497 INSTALLSTATE WINAPI MsiUseFeatureExW(LPCWSTR szProduct, LPCWSTR szFeature, 
1498                              DWORD dwInstallMode, DWORD dwReserved)
1499 {
1500     FIXME("%s %s %li %li\n", debugstr_w(szProduct), debugstr_w(szFeature),
1501           dwInstallMode, dwReserved);
1502
1503     /*
1504      * Polls all the components of the feature to find install state and then
1505      *  writes:
1506      *    Software\\Microsoft\\Windows\\CurrentVersion\\
1507      *    Installer\\Products\\<squishguid>\\<feature>
1508      *      "Usage"=dword:........
1509      */  
1510  
1511     return INSTALLSTATE_LOCAL; 
1512 }
1513
1514 INSTALLSTATE WINAPI MsiUseFeatureExA(LPCSTR szProduct, LPCSTR szFeature, 
1515                              DWORD dwInstallMode, DWORD dwReserved)
1516 {
1517     FIXME("%s %s %li %li\n", debugstr_a(szProduct), debugstr_a(szFeature),
1518           dwInstallMode, dwReserved);
1519    
1520     return INSTALLSTATE_LOCAL; 
1521 }
1522
1523 INSTALLSTATE WINAPI MsiUseFeatureW(LPCWSTR szProduct, LPCWSTR szFeature)
1524 {
1525     FIXME("%s %s\n", debugstr_w(szProduct), debugstr_w(szFeature));
1526    
1527     return INSTALLSTATE_LOCAL; 
1528 }
1529
1530 INSTALLSTATE WINAPI MsiUseFeatureA(LPCSTR szProduct, LPCSTR szFeature)
1531 {
1532     FIXME("%s %s\n", debugstr_a(szProduct), debugstr_a(szFeature));
1533    
1534     return INSTALLSTATE_LOCAL; 
1535 }
1536
1537 UINT WINAPI MsiProvideQualifiedComponentExW(LPCWSTR szComponent, 
1538                 LPCWSTR szQualifier, DWORD dwInstallMode, LPWSTR szProduct,
1539                 DWORD Unused1, DWORD Unused2, LPWSTR lpPathBuf, 
1540                 DWORD* pcchPathBuf)
1541 {
1542     FIXME("%s %s %li %s %li %li %p %p\n", debugstr_w(szComponent),
1543           debugstr_w(szQualifier), dwInstallMode, debugstr_w(szProduct),
1544           Unused1, Unused2, lpPathBuf, pcchPathBuf);
1545
1546     return ERROR_INDEX_ABSENT;
1547 }
1548
1549 USERINFOSTATE WINAPI MsiGetUserInfoW(LPCWSTR szProduct, LPWSTR lpUserNameBuf, 
1550                 DWORD* pcchUserNameBuf, LPWSTR lpOrgNameBuf, 
1551                 DWORD* pcchOrgNameBuf, LPWSTR lpSerialBuf, DWORD* pcchSerialBuf)
1552 {
1553     FIXME("%s %p %p %p %p %p %p\n",debugstr_w(szProduct), lpUserNameBuf,
1554           pcchUserNameBuf, lpOrgNameBuf, pcchOrgNameBuf, lpSerialBuf,
1555           pcchSerialBuf);
1556   
1557     return USERINFOSTATE_UNKNOWN; 
1558 }
1559
1560 USERINFOSTATE WINAPI MsiGetUserInfoA(LPCSTR szProduct, LPSTR lpUserNameBuf, 
1561                 DWORD* pcchUserNameBuf, LPSTR lpOrgNameBuf, 
1562                 DWORD* pcchOrgNameBuf, LPSTR lpSerialBuf, DWORD* pcchSerialBuf)
1563 {
1564     FIXME("%s %p %p %p %p %p %p\n",debugstr_a(szProduct), lpUserNameBuf,
1565           pcchUserNameBuf, lpOrgNameBuf, pcchOrgNameBuf, lpSerialBuf,
1566           pcchSerialBuf);
1567   
1568     return USERINFOSTATE_UNKNOWN; 
1569 }
1570
1571 UINT WINAPI MsiCollectUserInfoW(LPCWSTR szProduct)
1572 {
1573     FIXME("%s\n",debugstr_w(szProduct));
1574     return ERROR_CALL_NOT_IMPLEMENTED;
1575 }
1576
1577 UINT WINAPI MsiCollectUserInfoA(LPCSTR szProduct)
1578 {
1579     FIXME("%s\n",debugstr_a(szProduct));
1580     return ERROR_CALL_NOT_IMPLEMENTED;
1581 }
1582
1583 UINT WINAPI MsiCreateAndVerifyInstallerDirectory(void)
1584 {
1585     FIXME("\n");
1586     return ERROR_CALL_NOT_IMPLEMENTED;
1587 }
1588
1589 UINT WINAPI MsiGetShortcutTargetA( LPCSTR szShortcutTarget,
1590                                    LPSTR szProductCode, LPSTR szFeatureId,
1591                                    LPSTR szComponentCode )
1592 {
1593     FIXME("\n");
1594     return ERROR_CALL_NOT_IMPLEMENTED;
1595 }
1596
1597 UINT WINAPI MsiGetShortcutTargetW( LPCWSTR szShortcutTarget,
1598                                    LPWSTR szProductCode, LPWSTR szFeatureId,
1599                                    LPWSTR szComponentCode )
1600 {
1601     FIXME("\n");
1602     return ERROR_CALL_NOT_IMPLEMENTED;
1603 }
1604
1605 UINT WINAPI MsiReinstallFeatureW( LPCWSTR szProduct, LPCWSTR szFeature, 
1606                                   DWORD dwReinstallMode )
1607 {
1608     FIXME("%s %s %li\n", debugstr_w(szProduct), debugstr_w(szFeature),
1609                            dwReinstallMode);
1610     return ERROR_SUCCESS;
1611 }
1612
1613 UINT WINAPI MsiReinstallFeatureA( LPCSTR szProduct, LPCSTR szFeature, 
1614                                   DWORD dwReinstallMode )
1615 {
1616     FIXME("%s %s %li\n", debugstr_a(szProduct), debugstr_a(szFeature),
1617                            dwReinstallMode);
1618     return ERROR_SUCCESS;
1619 }