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