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