Fix more -Wstrict-prototypes warnings.
[wine] / dlls / msi / action.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2004,2005 Aric Stewart 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 /*
22  * Pages I need
23  *
24 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/msi/setup/installexecutesequence_table.asp
25
26 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/msi/setup/standard_actions_reference.asp
27  */
28
29 #include <stdarg.h>
30
31 #define COBJMACROS
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winerror.h"
36 #include "winreg.h"
37 #include "wine/debug.h"
38 #include "msidefs.h"
39 #include "msipriv.h"
40 #include "winuser.h"
41 #include "shlobj.h"
42 #include "wine/unicode.h"
43 #include "winver.h"
44 #include "action.h"
45
46 #define REG_PROGRESS_VALUE 13200
47 #define COMPONENT_PROGRESS_VALUE 24000
48
49 WINE_DEFAULT_DEBUG_CHANNEL(msi);
50
51 /*
52  * Prototypes
53  */
54 static UINT ACTION_ProcessExecSequence(MSIPACKAGE *package, BOOL UIran);
55 static UINT ACTION_ProcessUISequence(MSIPACKAGE *package);
56 static UINT ACTION_PerformActionSequence(MSIPACKAGE *package, UINT seq, BOOL UI);
57
58 /* 
59  * action handlers
60  */
61 typedef UINT (*STANDARDACTIONHANDLER)(MSIPACKAGE*);
62
63 static UINT ACTION_LaunchConditions(MSIPACKAGE *package);
64 static UINT ACTION_CostInitialize(MSIPACKAGE *package);
65 static UINT ACTION_CreateFolders(MSIPACKAGE *package);
66 static UINT ACTION_CostFinalize(MSIPACKAGE *package);
67 static UINT ACTION_FileCost(MSIPACKAGE *package);
68 static UINT ACTION_WriteRegistryValues(MSIPACKAGE *package);
69 static UINT ACTION_InstallInitialize(MSIPACKAGE *package);
70 static UINT ACTION_InstallValidate(MSIPACKAGE *package);
71 static UINT ACTION_ProcessComponents(MSIPACKAGE *package);
72 static UINT ACTION_RegisterTypeLibraries(MSIPACKAGE *package);
73 static UINT ACTION_RegisterUser(MSIPACKAGE *package);
74 static UINT ACTION_CreateShortcuts(MSIPACKAGE *package);
75 static UINT ACTION_PublishProduct(MSIPACKAGE *package);
76 static UINT ACTION_WriteIniValues(MSIPACKAGE *package);
77 static UINT ACTION_SelfRegModules(MSIPACKAGE *package);
78 static UINT ACTION_PublishFeatures(MSIPACKAGE *package);
79 static UINT ACTION_RegisterProduct(MSIPACKAGE *package);
80 static UINT ACTION_InstallExecute(MSIPACKAGE *package);
81 static UINT ACTION_InstallFinalize(MSIPACKAGE *package);
82 static UINT ACTION_ForceReboot(MSIPACKAGE *package);
83 static UINT ACTION_ResolveSource(MSIPACKAGE *package);
84 static UINT ACTION_ExecuteAction(MSIPACKAGE *package);
85 static UINT ACTION_RegisterFonts(MSIPACKAGE *package);
86 static UINT ACTION_PublishComponents(MSIPACKAGE *package);
87
88  
89 /*
90  * consts and values used
91  */
92 static const WCHAR c_colon[] = {'C',':','\\',0};
93
94 const static WCHAR szCreateFolders[] =
95     {'C','r','e','a','t','e','F','o','l','d','e','r','s',0};
96 const static WCHAR szCostFinalize[] =
97     {'C','o','s','t','F','i','n','a','l','i','z','e',0};
98 const WCHAR szInstallFiles[] =
99     {'I','n','s','t','a','l','l','F','i','l','e','s',0};
100 const WCHAR szDuplicateFiles[] =
101     {'D','u','p','l','i','c','a','t','e','F','i','l','e','s',0};
102 const static WCHAR szWriteRegistryValues[] =
103     {'W','r','i','t','e','R','e','g','i','s','t','r','y',
104             'V','a','l','u','e','s',0};
105 const static WCHAR szCostInitialize[] =
106     {'C','o','s','t','I','n','i','t','i','a','l','i','z','e',0};
107 const static WCHAR szFileCost[] = 
108     {'F','i','l','e','C','o','s','t',0};
109 const static WCHAR szInstallInitialize[] = 
110     {'I','n','s','t','a','l','l','I','n','i','t','i','a','l','i','z','e',0};
111 const static WCHAR szInstallValidate[] = 
112     {'I','n','s','t','a','l','l','V','a','l','i','d','a','t','e',0};
113 const static WCHAR szLaunchConditions[] = 
114     {'L','a','u','n','c','h','C','o','n','d','i','t','i','o','n','s',0};
115 const static WCHAR szProcessComponents[] = 
116     {'P','r','o','c','e','s','s','C','o','m','p','o','n','e','n','t','s',0};
117 const static WCHAR szRegisterTypeLibraries[] = 
118     {'R','e','g','i','s','t','e','r','T','y','p','e',
119             'L','i','b','r','a','r','i','e','s',0};
120 const WCHAR szRegisterClassInfo[] = 
121     {'R','e','g','i','s','t','e','r','C','l','a','s','s','I','n','f','o',0};
122 const WCHAR szRegisterProgIdInfo[] = 
123     {'R','e','g','i','s','t','e','r','P','r','o','g','I','d','I','n','f','o',0};
124 const static WCHAR szCreateShortcuts[] = 
125     {'C','r','e','a','t','e','S','h','o','r','t','c','u','t','s',0};
126 const static WCHAR szPublishProduct[] = 
127     {'P','u','b','l','i','s','h','P','r','o','d','u','c','t',0};
128 const static WCHAR szWriteIniValues[] = 
129     {'W','r','i','t','e','I','n','i','V','a','l','u','e','s',0};
130 const static WCHAR szSelfRegModules[] = 
131     {'S','e','l','f','R','e','g','M','o','d','u','l','e','s',0};
132 const static WCHAR szPublishFeatures[] = 
133     {'P','u','b','l','i','s','h','F','e','a','t','u','r','e','s',0};
134 const static WCHAR szRegisterProduct[] = 
135     {'R','e','g','i','s','t','e','r','P','r','o','d','u','c','t',0};
136 const static WCHAR szInstallExecute[] = 
137     {'I','n','s','t','a','l','l','E','x','e','c','u','t','e',0};
138 const static WCHAR szInstallExecuteAgain[] = 
139     {'I','n','s','t','a','l','l','E','x','e','c','u','t','e',
140             'A','g','a','i','n',0};
141 const static WCHAR szInstallFinalize[] = 
142     {'I','n','s','t','a','l','l','F','i','n','a','l','i','z','e',0};
143 const static WCHAR szForceReboot[] = 
144     {'F','o','r','c','e','R','e','b','o','o','t',0};
145 const static WCHAR szResolveSource[] =
146     {'R','e','s','o','l','v','e','S','o','u','r','c','e',0};
147 const WCHAR szAppSearch[] = 
148     {'A','p','p','S','e','a','r','c','h',0};
149 const static WCHAR szAllocateRegistrySpace[] = 
150     {'A','l','l','o','c','a','t','e','R','e','g','i','s','t','r','y',
151             'S','p','a','c','e',0};
152 const static WCHAR szBindImage[] = 
153     {'B','i','n','d','I','m','a','g','e',0};
154 const static WCHAR szCCPSearch[] = 
155     {'C','C','P','S','e','a','r','c','h',0};
156 const static WCHAR szDeleteServices[] = 
157     {'D','e','l','e','t','e','S','e','r','v','i','c','e','s',0};
158 const static WCHAR szDisableRollback[] = 
159     {'D','i','s','a','b','l','e','R','o','l','l','b','a','c','k',0};
160 const static WCHAR szExecuteAction[] = 
161     {'E','x','e','c','u','t','e','A','c','t','i','o','n',0};
162 const WCHAR szFindRelatedProducts[] = 
163     {'F','i','n','d','R','e','l','a','t','e','d',
164             'P','r','o','d','u','c','t','s',0};
165 const static WCHAR szInstallAdminPackage[] = 
166     {'I','n','s','t','a','l','l','A','d','m','i','n',
167             'P','a','c','k','a','g','e',0};
168 const static WCHAR szInstallSFPCatalogFile[] = 
169     {'I','n','s','t','a','l','l','S','F','P','C','a','t','a','l','o','g',
170             'F','i','l','e',0};
171 const static WCHAR szIsolateComponents[] = 
172     {'I','s','o','l','a','t','e','C','o','m','p','o','n','e','n','t','s',0};
173 const WCHAR szMigrateFeatureStates[] = 
174     {'M','i','g','r','a','t','e','F','e','a','t','u','r','e',
175             'S','t','a','t','e','s',0};
176 const WCHAR szMoveFiles[] = 
177     {'M','o','v','e','F','i','l','e','s',0};
178 const static WCHAR szMsiPublishAssemblies[] = 
179     {'M','s','i','P','u','b','l','i','s','h',
180             'A','s','s','e','m','b','l','i','e','s',0};
181 const static WCHAR szMsiUnpublishAssemblies[] = 
182     {'M','s','i','U','n','p','u','b','l','i','s','h',
183             'A','s','s','e','m','b','l','i','e','s',0};
184 const static WCHAR szInstallODBC[] = 
185     {'I','n','s','t','a','l','l','O','D','B','C',0};
186 const static WCHAR szInstallServices[] = 
187     {'I','n','s','t','a','l','l','S','e','r','v','i','c','e','s',0};
188 const WCHAR szPatchFiles[] = 
189     {'P','a','t','c','h','F','i','l','e','s',0};
190 const static WCHAR szPublishComponents[] = 
191     {'P','u','b','l','i','s','h','C','o','m','p','o','n','e','n','t','s',0};
192 const static WCHAR szRegisterComPlus[] =
193     {'R','e','g','i','s','t','e','r','C','o','m','P','l','u','s',0};
194 const WCHAR szRegisterExtensionInfo[] =
195     {'R','e','g','i','s','t','e','r','E','x','t','e','n','s','i','o','n',
196             'I','n','f','o',0};
197 const static WCHAR szRegisterFonts[] =
198     {'R','e','g','i','s','t','e','r','F','o','n','t','s',0};
199 const WCHAR szRegisterMIMEInfo[] =
200     {'R','e','g','i','s','t','e','r','M','I','M','E','I','n','f','o',0};
201 const static WCHAR szRegisterUser[] =
202     {'R','e','g','i','s','t','e','r','U','s','e','r',0};
203 const WCHAR szRemoveDuplicateFiles[] =
204     {'R','e','m','o','v','e','D','u','p','l','i','c','a','t','e',
205             'F','i','l','e','s',0};
206 const static WCHAR szRemoveEnvironmentStrings[] =
207     {'R','e','m','o','v','e','E','n','v','i','r','o','n','m','e','n','t',
208             'S','t','r','i','n','g','s',0};
209 const WCHAR szRemoveExistingProducts[] =
210     {'R','e','m','o','v','e','E','x','i','s','t','i','n','g',
211             'P','r','o','d','u','c','t','s',0};
212 const WCHAR szRemoveFiles[] =
213     {'R','e','m','o','v','e','F','i','l','e','s',0};
214 const static WCHAR szRemoveFolders[] =
215     {'R','e','m','o','v','e','F','o','l','d','e','r','s',0};
216 const static WCHAR szRemoveIniValues[] =
217     {'R','e','m','o','v','e','I','n','i','V','a','l','u','e','s',0};
218 const static WCHAR szRemoveODBC[] =
219     {'R','e','m','o','v','e','O','D','B','C',0};
220 const static WCHAR szRemoveRegistryValues[] =
221     {'R','e','m','o','v','e','R','e','g','i','s','t','r','y',
222             'V','a','l','u','e','s',0};
223 const static WCHAR szRemoveShortcuts[] =
224     {'R','e','m','o','v','e','S','h','o','r','t','c','u','t','s',0};
225 const static WCHAR szRMCCPSearch[] =
226     {'R','M','C','C','P','S','e','a','r','c','h',0};
227 const static WCHAR szScheduleReboot[] =
228     {'S','c','h','e','d','u','l','e','R','e','b','o','o','t',0};
229 const static WCHAR szSelfUnregModules[] =
230     {'S','e','l','f','U','n','r','e','g','M','o','d','u','l','e','s',0};
231 const static WCHAR szSetODBCFolders[] =
232     {'S','e','t','O','D','B','C','F','o','l','d','e','r','s',0};
233 const static WCHAR szStartServices[] =
234     {'S','t','a','r','t','S','e','r','v','i','c','e','s',0};
235 const static WCHAR szStopServices[] =
236     {'S','t','o','p','S','e','r','v','i','c','e','s',0};
237 const static WCHAR szUnpublishComponents[] =
238     {'U','n','p','u','b','l','i','s','h',
239             'C','o','m','p','o','n','e','n','t','s',0};
240 const static WCHAR szUnpublishFeatures[] =
241     {'U','n','p','u','b','l','i','s','h','F','e','a','t','u','r','e','s',0};
242 const WCHAR szUnregisterClassInfo[] =
243     {'U','n','r','e','g','i','s','t','e','r','C','l','a','s','s',
244             'I','n','f','o',0};
245 const static WCHAR szUnregisterComPlus[] =
246     {'U','n','r','e','g','i','s','t','e','r','C','o','m','P','l','u','s',0};
247 const WCHAR szUnregisterExtensionInfo[] =
248     {'U','n','r','e','g','i','s','t','e','r',
249             'E','x','t','e','n','s','i','o','n','I','n','f','o',0};
250 const static WCHAR szUnregisterFonts[] =
251     {'U','n','r','e','g','i','s','t','e','r','F','o','n','t','s',0};
252 const WCHAR szUnregisterMIMEInfo[] =
253     {'U','n','r','e','g','i','s','t','e','r','M','I','M','E','I','n','f','o',0};
254 const WCHAR szUnregisterProgIdInfo[] =
255     {'U','n','r','e','g','i','s','t','e','r','P','r','o','g','I','d',
256             'I','n','f','o',0};
257 const static WCHAR szUnregisterTypeLibraries[] =
258     {'U','n','r','e','g','i','s','t','e','r','T','y','p','e',
259             'L','i','b','r','a','r','i','e','s',0};
260 const static WCHAR szValidateProductID[] =
261     {'V','a','l','i','d','a','t','e','P','r','o','d','u','c','t','I','D',0};
262 const static WCHAR szWriteEnvironmentStrings[] =
263     {'W','r','i','t','e','E','n','v','i','r','o','n','m','e','n','t',
264             'S','t','r','i','n','g','s',0};
265
266 struct _actions {
267     LPCWSTR action;
268     STANDARDACTIONHANDLER handler;
269 };
270
271 static struct _actions StandardActions[] = {
272     { szAllocateRegistrySpace, NULL},
273     { szAppSearch, ACTION_AppSearch },
274     { szBindImage, NULL},
275     { szCCPSearch, NULL},
276     { szCostFinalize, ACTION_CostFinalize },
277     { szCostInitialize, ACTION_CostInitialize },
278     { szCreateFolders, ACTION_CreateFolders },
279     { szCreateShortcuts, ACTION_CreateShortcuts },
280     { szDeleteServices, NULL},
281     { szDisableRollback, NULL},
282     { szDuplicateFiles, ACTION_DuplicateFiles },
283     { szExecuteAction, ACTION_ExecuteAction },
284     { szFileCost, ACTION_FileCost },
285     { szFindRelatedProducts, ACTION_FindRelatedProducts },
286     { szForceReboot, ACTION_ForceReboot },
287     { szInstallAdminPackage, NULL},
288     { szInstallExecute, ACTION_InstallExecute },
289     { szInstallExecuteAgain, ACTION_InstallExecute },
290     { szInstallFiles, ACTION_InstallFiles},
291     { szInstallFinalize, ACTION_InstallFinalize },
292     { szInstallInitialize, ACTION_InstallInitialize },
293     { szInstallSFPCatalogFile, NULL},
294     { szInstallValidate, ACTION_InstallValidate },
295     { szIsolateComponents, NULL},
296     { szLaunchConditions, ACTION_LaunchConditions },
297     { szMigrateFeatureStates, NULL},
298     { szMoveFiles, NULL},
299     { szMsiPublishAssemblies, NULL},
300     { szMsiUnpublishAssemblies, NULL},
301     { szInstallODBC, NULL},
302     { szInstallServices, NULL},
303     { szPatchFiles, NULL},
304     { szProcessComponents, ACTION_ProcessComponents },
305     { szPublishComponents, ACTION_PublishComponents },
306     { szPublishFeatures, ACTION_PublishFeatures },
307     { szPublishProduct, ACTION_PublishProduct },
308     { szRegisterClassInfo, ACTION_RegisterClassInfo },
309     { szRegisterComPlus, NULL},
310     { szRegisterExtensionInfo, ACTION_RegisterExtensionInfo },
311     { szRegisterFonts, ACTION_RegisterFonts },
312     { szRegisterMIMEInfo, ACTION_RegisterMIMEInfo },
313     { szRegisterProduct, ACTION_RegisterProduct },
314     { szRegisterProgIdInfo, ACTION_RegisterProgIdInfo },
315     { szRegisterTypeLibraries, ACTION_RegisterTypeLibraries },
316     { szRegisterUser, ACTION_RegisterUser},
317     { szRemoveDuplicateFiles, NULL},
318     { szRemoveEnvironmentStrings, NULL},
319     { szRemoveExistingProducts, NULL},
320     { szRemoveFiles, NULL},
321     { szRemoveFolders, NULL},
322     { szRemoveIniValues, NULL},
323     { szRemoveODBC, NULL},
324     { szRemoveRegistryValues, NULL},
325     { szRemoveShortcuts, NULL},
326     { szResolveSource, ACTION_ResolveSource},
327     { szRMCCPSearch, NULL},
328     { szScheduleReboot, NULL},
329     { szSelfRegModules, ACTION_SelfRegModules },
330     { szSelfUnregModules, NULL},
331     { szSetODBCFolders, NULL},
332     { szStartServices, NULL},
333     { szStopServices, NULL},
334     { szUnpublishComponents, NULL},
335     { szUnpublishFeatures, NULL},
336     { szUnregisterClassInfo, NULL},
337     { szUnregisterComPlus, NULL},
338     { szUnregisterExtensionInfo, NULL},
339     { szUnregisterFonts, NULL},
340     { szUnregisterMIMEInfo, NULL},
341     { szUnregisterProgIdInfo, NULL},
342     { szUnregisterTypeLibraries, NULL},
343     { szValidateProductID, NULL},
344     { szWriteEnvironmentStrings, NULL},
345     { szWriteIniValues, ACTION_WriteIniValues },
346     { szWriteRegistryValues, ACTION_WriteRegistryValues},
347     { NULL, NULL},
348 };
349
350 static void ce_actiontext(MSIPACKAGE* package, LPCWSTR action)
351 {
352     static const WCHAR szActionText[] = 
353         {'A','c','t','i','o','n','T','e','x','t',0};
354     MSIRECORD *row;
355
356     row = MSI_CreateRecord(1);
357     MSI_RecordSetStringW(row,1,action);
358     ControlEvent_FireSubscribedEvent(package,szActionText, row);
359     msiobj_release(&row->hdr);
360 }
361
362 static void ui_actionstart(MSIPACKAGE *package, LPCWSTR action)
363 {
364     static const WCHAR template_s[]=
365         {'A','c','t','i','o','n',' ','%','s',':',' ','%','s','.',' ', '%','s',
366          '.',0};
367     static const WCHAR format[] = 
368         {'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0};
369     static const WCHAR Query_t[] = 
370         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
371          '`','A','c','t','i','o', 'n','T','e','x','t','`',' ',
372          'W','H','E','R','E', ' ','`','A','c','t','i','o','n','`',' ','=', 
373          ' ','\'','%','s','\'',0};
374     WCHAR message[1024];
375     WCHAR timet[0x100];
376     MSIRECORD * row = 0;
377     LPCWSTR ActionText;
378
379     GetTimeFormatW(LOCALE_USER_DEFAULT, 0, NULL, format, timet, 0x100);
380
381     row = MSI_QueryGetRecord( package->db, Query_t, action );
382     if (!row)
383         return;
384
385     ActionText = MSI_RecordGetString(row,2);
386
387     sprintfW(message,template_s,timet,action,ActionText);
388     msiobj_release(&row->hdr);
389
390     row = MSI_CreateRecord(1);
391     MSI_RecordSetStringW(row,1,message);
392  
393     MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONSTART, row);
394     msiobj_release(&row->hdr);
395 }
396
397 static void ui_actioninfo(MSIPACKAGE *package, LPCWSTR action, BOOL start, 
398                           UINT rc)
399 {
400     MSIRECORD * row;
401     static const WCHAR template_s[]=
402         {'A','c','t','i','o','n',' ','s','t','a','r','t',' ','%','s',':',' ',
403          '%','s', '.',0};
404     static const WCHAR template_e[]=
405         {'A','c','t','i','o','n',' ','e','n','d','e','d',' ','%','s',':',' ',
406          '%','s', '.',' ','R','e','t','u','r','n',' ','v','a','l','u','e',' ',
407          '%','i','.',0};
408     static const WCHAR format[] = 
409         {'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0};
410     WCHAR message[1024];
411     WCHAR timet[0x100];
412
413     GetTimeFormatW(LOCALE_USER_DEFAULT, 0, NULL, format, timet, 0x100);
414     if (start)
415         sprintfW(message,template_s,timet,action);
416     else
417         sprintfW(message,template_e,timet,action,rc);
418     
419     row = MSI_CreateRecord(1);
420     MSI_RecordSetStringW(row,1,message);
421  
422     MSI_ProcessMessage(package, INSTALLMESSAGE_INFO, row);
423     msiobj_release(&row->hdr);
424 }
425
426 /****************************************************
427  * TOP level entry points 
428  *****************************************************/
429
430 UINT ACTION_DoTopLevelINSTALL(MSIPACKAGE *package, LPCWSTR szPackagePath,
431                               LPCWSTR szCommandLine)
432 {
433     DWORD sz;
434     WCHAR buffer[10];
435     UINT rc;
436     BOOL ui = FALSE;
437     static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
438     static const WCHAR szAction[] = {'A','C','T','I','O','N',0};
439     static const WCHAR szInstall[] = {'I','N','S','T','A','L','L',0};
440
441     MSI_SetPropertyW(package, szAction, szInstall);
442
443     package->script = HeapAlloc(GetProcessHeap(),0,sizeof(MSISCRIPT));
444     memset(package->script,0,sizeof(MSISCRIPT));
445
446     if (szPackagePath)   
447     {
448         LPWSTR p, check, path;
449  
450         package->PackagePath = strdupW(szPackagePath);
451         path = strdupW(szPackagePath);
452         p = strrchrW(path,'\\');    
453         if (p)
454         {
455             p++;
456             *p=0;
457         }
458         else
459         {
460             HeapFree(GetProcessHeap(),0,path);
461             path = HeapAlloc(GetProcessHeap(),0,MAX_PATH*sizeof(WCHAR));
462             GetCurrentDirectoryW(MAX_PATH,path);
463             strcatW(path,cszbs);
464         }
465
466         check = load_dynamic_property(package, cszSourceDir,NULL);
467         if (!check)
468             MSI_SetPropertyW(package, cszSourceDir, path);
469         else
470             HeapFree(GetProcessHeap(), 0, check);
471
472         HeapFree(GetProcessHeap(), 0, path);
473     }
474
475     if (szCommandLine)
476     {
477         LPWSTR ptr,ptr2;
478         ptr = (LPWSTR)szCommandLine;
479        
480         while (*ptr)
481         {
482             WCHAR *prop = NULL;
483             WCHAR *val = NULL;
484
485             TRACE("Looking at %s\n",debugstr_w(ptr));
486
487             ptr2 = strchrW(ptr,'=');
488             if (ptr2)
489             {
490                 BOOL quote=FALSE;
491                 DWORD len = 0;
492
493                 while (*ptr == ' ') ptr++;
494                 len = ptr2-ptr;
495                 prop = HeapAlloc(GetProcessHeap(),0,(len+1)*sizeof(WCHAR));
496                 memcpy(prop,ptr,len*sizeof(WCHAR));
497                 prop[len]=0;
498                 ptr2++;
499            
500                 len = 0; 
501                 ptr = ptr2; 
502                 while (*ptr && (quote || (!quote && *ptr!=' ')))
503                 {
504                     if (*ptr == '"')
505                         quote = !quote;
506                     ptr++;
507                     len++;
508                 }
509                
510                 if (*ptr2=='"')
511                 {
512                     ptr2++;
513                     len -= 2;
514                 }
515                 val = HeapAlloc(GetProcessHeap(),0,(len+1)*sizeof(WCHAR));
516                 memcpy(val,ptr2,len*sizeof(WCHAR));
517                 val[len] = 0;
518
519                 if (strlenW(prop) > 0)
520                 {
521                     TRACE("Found commandline property (%s) = (%s)\n", 
522                                        debugstr_w(prop), debugstr_w(val));
523                     MSI_SetPropertyW(package,prop,val);
524                 }
525                 HeapFree(GetProcessHeap(),0,val);
526                 HeapFree(GetProcessHeap(),0,prop);
527             }
528             ptr++;
529         }
530     }
531   
532     sz = 10; 
533     if (MSI_GetPropertyW(package,szUILevel,buffer,&sz) == ERROR_SUCCESS)
534     {
535         if (atoiW(buffer) >= INSTALLUILEVEL_REDUCED)
536         {
537             rc = ACTION_ProcessUISequence(package);
538             ui = TRUE;
539             if (rc == ERROR_SUCCESS)
540                 rc = ACTION_ProcessExecSequence(package,TRUE);
541         }
542         else
543             rc = ACTION_ProcessExecSequence(package,FALSE);
544     }
545     else
546         rc = ACTION_ProcessExecSequence(package,FALSE);
547     
548     if (rc == -1)
549     {
550         /* install was halted but should be considered a success */
551         rc = ERROR_SUCCESS;
552     }
553
554     package->script->CurrentlyScripting= FALSE;
555
556     /* process the ending type action */
557     if (rc == ERROR_SUCCESS)
558         ACTION_PerformActionSequence(package,-1,ui);
559     else if (rc == ERROR_INSTALL_USEREXIT) 
560         ACTION_PerformActionSequence(package,-2,ui);
561     else if (rc == ERROR_INSTALL_SUSPEND) 
562         ACTION_PerformActionSequence(package,-4,ui);
563     else  /* failed */
564         ACTION_PerformActionSequence(package,-3,ui);
565
566     /* finish up running custom actions */
567     ACTION_FinishCustomActions(package);
568     
569     return rc;
570 }
571
572 static UINT ACTION_PerformActionSequence(MSIPACKAGE *package, UINT seq, BOOL UI)
573 {
574     UINT rc = ERROR_SUCCESS;
575     MSIRECORD * row = 0;
576     static const WCHAR ExecSeqQuery[] =
577         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
578          '`','I','n','s','t','a','l','l','E','x','e','c','u','t','e',
579          'S','e','q','u','e','n','c','e','`',' ', 'W','H','E','R','E',' ',
580          '`','S','e','q','u','e','n','c','e','`',' ', '=',' ','%','i',0};
581
582     static const WCHAR UISeqQuery[] =
583         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
584      '`','I','n','s','t','a','l','l','U','I','S','e','q','u','e','n','c','e',
585      '`', ' ', 'W','H','E','R','E',' ','`','S','e','q','u','e','n','c','e','`',
586          ' ', '=',' ','%','i',0};
587
588     if (UI)
589         row = MSI_QueryGetRecord(package->db, UISeqQuery, seq);
590     else
591         row = MSI_QueryGetRecord(package->db, ExecSeqQuery, seq);
592
593     if (row)
594     {
595         LPCWSTR action, cond;
596
597         TRACE("Running the actions\n"); 
598
599         /* check conditions */
600         cond = MSI_RecordGetString(row,2);
601         if (cond)
602         {
603             /* this is a hack to skip errors in the condition code */
604             if (MSI_EvaluateConditionW(package, cond) == MSICONDITION_FALSE)
605                 goto end;
606         }
607
608         action = MSI_RecordGetString(row,1);
609         if (!action)
610         {
611             ERR("failed to fetch action\n");
612             rc = ERROR_FUNCTION_FAILED;
613             goto end;
614         }
615
616         if (UI)
617             rc = ACTION_PerformUIAction(package,action);
618         else
619             rc = ACTION_PerformAction(package,action,FALSE);
620 end:
621         msiobj_release(&row->hdr);
622     }
623     else
624         rc = ERROR_SUCCESS;
625
626     return rc;
627 }
628
629 static UINT ACTION_ProcessExecSequence(MSIPACKAGE *package, BOOL UIran)
630 {
631     MSIQUERY * view;
632     UINT rc;
633     static const WCHAR ExecSeqQuery[] =
634         {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
635          '`','I','n','s','t','a','l','l','E','x','e','c','u','t','e',
636          'S','e','q','u','e','n','c','e','`',' ', 'W','H','E','R','E',' ',
637          '`','S','e','q','u','e','n','c','e','`',' ', '>',' ','%','i',' ',
638          'O','R','D','E','R',' ', 'B','Y',' ',
639          '`','S','e','q','u','e','n','c','e','`',0 };
640     MSIRECORD * row = 0;
641     static const WCHAR IVQuery[] =
642         {'S','E','L','E','C','T',' ','`','S','e','q','u','e','n','c','e','`',
643          ' ', 'F','R','O','M',' ','`','I','n','s','t','a','l','l',
644          'E','x','e','c','u','t','e','S','e','q','u','e','n','c','e','`',' ',
645          'W','H','E','R','E',' ','`','A','c','t','i','o','n','`',' ','=',
646          ' ','\'', 'I','n','s','t','a','l','l',
647          'V','a','l','i','d','a','t','e','\'', 0};
648     INT seq = 0;
649
650
651     if (package->script->ExecuteSequenceRun)
652     {
653         TRACE("Execute Sequence already Run\n");
654         return ERROR_SUCCESS;
655     }
656
657     package->script->ExecuteSequenceRun = TRUE;
658     
659     /* get the sequence number */
660     if (UIran)
661     {
662         row = MSI_QueryGetRecord(package->db, IVQuery);
663         if( !row )
664             return ERROR_FUNCTION_FAILED;
665         seq = MSI_RecordGetInteger(row,1);
666         msiobj_release(&row->hdr);
667     }
668
669     rc = MSI_OpenQuery(package->db, &view, ExecSeqQuery, seq);
670     if (rc == ERROR_SUCCESS)
671     {
672         rc = MSI_ViewExecute(view, 0);
673
674         if (rc != ERROR_SUCCESS)
675         {
676             MSI_ViewClose(view);
677             msiobj_release(&view->hdr);
678             goto end;
679         }
680        
681         TRACE("Running the actions\n"); 
682
683         while (1)
684         {
685             LPCWSTR cond, action;
686
687             rc = MSI_ViewFetch(view,&row);
688             if (rc != ERROR_SUCCESS)
689             {
690                 rc = ERROR_SUCCESS;
691                 break;
692             }
693
694             action = MSI_RecordGetString(row,1);
695             if (!action)
696             {
697                 ERR("Error in retrieving action name\n");
698                 rc = ERROR_FUNCTION_FAILED;
699                 msiobj_release(&row->hdr);
700                 break;
701             }
702
703             /* check conditions */
704             cond = MSI_RecordGetString(row,2);
705             if (cond)
706             {
707                 /* this is a hack to skip errors in the condition code */
708                 if (MSI_EvaluateConditionW(package, cond) == MSICONDITION_FALSE)
709                 {
710                     TRACE("Skipping action: %s (condition is false)\n",
711                                     debugstr_w(action));
712                     msiobj_release(&row->hdr);
713                     continue; 
714                 }
715             }
716
717             rc = ACTION_PerformAction(package,action,FALSE);
718
719             if (rc == ERROR_FUNCTION_NOT_CALLED)
720                 rc = ERROR_SUCCESS;
721
722             if (rc != ERROR_SUCCESS)
723             {
724                 ERR("Execution halted due to error (%i)\n",rc);
725                 msiobj_release(&row->hdr);
726                 break;
727             }
728
729             msiobj_release(&row->hdr);
730         }
731
732         MSI_ViewClose(view);
733         msiobj_release(&view->hdr);
734     }
735
736 end:
737     return rc;
738 }
739
740
741 static UINT ACTION_ProcessUISequence(MSIPACKAGE *package)
742 {
743     MSIQUERY * view;
744     UINT rc;
745     static const WCHAR ExecSeqQuery [] =
746         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
747          '`','I','n','s','t','a','l','l',
748          'U','I','S','e','q','u','e','n','c','e','`',
749          ' ','W','H','E','R','E',' ', 
750          '`','S','e','q','u','e','n','c','e','`',' ',
751          '>',' ','0',' ','O','R','D','E','R',' ','B','Y',' ',
752          '`','S','e','q','u','e','n','c','e','`',0};
753     
754     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
755     
756     if (rc == ERROR_SUCCESS)
757     {
758         rc = MSI_ViewExecute(view, 0);
759
760         if (rc != ERROR_SUCCESS)
761         {
762             MSI_ViewClose(view);
763             msiobj_release(&view->hdr);
764             goto end;
765         }
766        
767         TRACE("Running the actions \n"); 
768
769         while (1)
770         {
771             LPCWSTR action, cond;
772             MSIRECORD * row = 0;
773
774             rc = MSI_ViewFetch(view,&row);
775             if (rc != ERROR_SUCCESS)
776             {
777                 rc = ERROR_SUCCESS;
778                 break;
779             }
780
781             action = MSI_RecordGetString(row,1);
782             if (!action)
783             {
784                 ERR("failed to fetch action\n");
785                 rc = ERROR_FUNCTION_FAILED;
786                 msiobj_release(&row->hdr);
787                 break;
788             }
789
790             /* check conditions */
791             cond = MSI_RecordGetString(row,2);
792             if (cond)
793             {
794                 /* this is a hack to skip errors in the condition code */
795                 if (MSI_EvaluateConditionW(package,cond) == MSICONDITION_FALSE)
796                 {
797                     TRACE("Skipping action: %s (condition is false)\n",
798                                     debugstr_w(action));
799                     msiobj_release(&row->hdr);
800                     continue; 
801                 }
802             }
803
804             rc = ACTION_PerformUIAction(package,action);
805
806             if (rc == ERROR_FUNCTION_NOT_CALLED)
807                 rc = ERROR_SUCCESS;
808
809             if (rc != ERROR_SUCCESS)
810             {
811                 ERR("Execution halted due to error (%i)\n",rc);
812                 msiobj_release(&row->hdr);
813                 break;
814             }
815
816             msiobj_release(&row->hdr);
817         }
818
819         MSI_ViewClose(view);
820         msiobj_release(&view->hdr);
821     }
822
823 end:
824     return rc;
825 }
826
827 /********************************************************
828  * ACTION helper functions and functions that perform the actions
829  *******************************************************/
830 static BOOL ACTION_HandleStandardAction(MSIPACKAGE *package, LPCWSTR action, 
831                                         UINT* rc, BOOL force )
832 {
833     BOOL ret = FALSE; 
834     BOOL run = force;
835     int i;
836
837     if (!run && !package->script->CurrentlyScripting)
838         run = TRUE;
839    
840     if (!run)
841     {
842         if (strcmpW(action,szInstallFinalize) == 0 ||
843             strcmpW(action,szInstallExecute) == 0 ||
844             strcmpW(action,szInstallExecuteAgain) == 0) 
845                 run = TRUE;
846     }
847     
848     i = 0;
849     while (StandardActions[i].action != NULL)
850     {
851         if (strcmpW(StandardActions[i].action, action)==0)
852         {
853             ce_actiontext(package, action);
854             if (!run)
855             {
856                 ui_actioninfo(package, action, TRUE, 0);
857                 *rc = schedule_action(package,INSTALL_SCRIPT,action);
858                 ui_actioninfo(package, action, FALSE, *rc);
859             }
860             else
861             {
862                 ui_actionstart(package, action);
863                 if (StandardActions[i].handler)
864                 {
865                     *rc = StandardActions[i].handler(package);
866                 }
867                 else
868                 {
869                     FIXME("UNHANDLED Standard Action %s\n",debugstr_w(action));
870                     *rc = ERROR_SUCCESS;
871                 }
872             }
873             ret = TRUE;
874             break;
875         }
876         i++;
877     }
878     return ret;
879 }
880
881 static BOOL ACTION_HandleDialogBox( MSIPACKAGE *package, LPCWSTR dialog, UINT* rc )
882 {
883     BOOL ret = FALSE;
884
885     if (ACTION_DialogBox(package,dialog) == ERROR_SUCCESS)
886     {
887         *rc = package->CurrentInstallState;
888         ret = TRUE;
889     }
890     return ret;
891 }
892
893 static BOOL ACTION_HandleCustomAction( MSIPACKAGE* package, LPCWSTR action,
894                                        UINT* rc, BOOL force )
895 {
896     BOOL ret=FALSE;
897     UINT arc;
898
899     arc = ACTION_CustomAction(package,action, force);
900
901     if (arc != ERROR_CALL_NOT_IMPLEMENTED)
902     {
903         *rc = arc;
904         ret = TRUE;
905     }
906     return ret;
907 }
908
909 /* 
910  * A lot of actions are really important even if they don't do anything
911  * explicit... Lots of properties are set at the beginning of the installation
912  * CostFinalize does a bunch of work to translate the directories and such
913  * 
914  * But until I get write access to the database that is hard, so I am going to
915  * hack it to see if I can get something to run.
916  */
917 UINT ACTION_PerformAction(MSIPACKAGE *package, const WCHAR *action, BOOL force)
918 {
919     UINT rc = ERROR_SUCCESS; 
920     BOOL handled;
921
922     TRACE("Performing action (%s)\n",debugstr_w(action));
923
924     handled = ACTION_HandleStandardAction(package, action, &rc, force);
925
926     if (!handled)
927         handled = ACTION_HandleCustomAction(package, action, &rc, force);
928
929     if (!handled)
930     {
931         FIXME("UNHANDLED MSI ACTION %s\n",debugstr_w(action));
932         rc = ERROR_FUNCTION_NOT_CALLED;
933     }
934
935     package->CurrentInstallState = rc;
936     return rc;
937 }
938
939 UINT ACTION_PerformUIAction(MSIPACKAGE *package, const WCHAR *action)
940 {
941     UINT rc = ERROR_SUCCESS;
942     BOOL handled = FALSE;
943
944     TRACE("Performing action (%s)\n",debugstr_w(action));
945
946     handled = ACTION_HandleStandardAction(package, action, &rc,TRUE);
947
948     if (!handled)
949         handled = ACTION_HandleCustomAction(package, action, &rc, FALSE);
950
951     if (!handled)
952         handled = ACTION_HandleDialogBox(package, action, &rc);
953
954     msi_dialog_check_messages( NULL );
955
956     if (!handled)
957     {
958         FIXME("UNHANDLED MSI ACTION %s\n",debugstr_w(action));
959         rc = ERROR_FUNCTION_NOT_CALLED;
960     }
961
962     package->CurrentInstallState = rc;
963     return rc;
964 }
965
966 /*
967  * Also we cannot enable/disable components either, so for now I am just going 
968  * to do all the directories for all the components.
969  */
970 static UINT ACTION_CreateFolders(MSIPACKAGE *package)
971 {
972     static const WCHAR ExecSeqQuery[] =
973         {'S','E','L','E','C','T',' ',
974          '`','D','i','r','e','c','t','o','r','y','_','`',
975          ' ','F','R','O','M',' ',
976          '`','C','r','e','a','t','e','F','o','l','d','e','r','`',0 };
977     UINT rc;
978     MSIQUERY *view;
979     MSIFOLDER *folder;
980
981     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view );
982     if (rc != ERROR_SUCCESS)
983         return ERROR_SUCCESS;
984
985     rc = MSI_ViewExecute(view, 0);
986     if (rc != ERROR_SUCCESS)
987     {
988         MSI_ViewClose(view);
989         msiobj_release(&view->hdr);
990         return rc;
991     }
992     
993     while (1)
994     {
995         LPCWSTR dir;
996         LPWSTR full_path;
997         MSIRECORD *row = NULL, *uirow;
998
999         rc = MSI_ViewFetch(view,&row);
1000         if (rc != ERROR_SUCCESS)
1001         {
1002             rc = ERROR_SUCCESS;
1003             break;
1004         }
1005
1006         dir = MSI_RecordGetString(row,1);
1007         if (!dir)
1008         {
1009             ERR("Unable to get folder id \n");
1010             msiobj_release(&row->hdr);
1011             continue;
1012         }
1013
1014         full_path = resolve_folder(package,dir,FALSE,FALSE,&folder);
1015         if (!full_path)
1016         {
1017             ERR("Unable to resolve folder id %s\n",debugstr_w(dir));
1018             msiobj_release(&row->hdr);
1019             continue;
1020         }
1021
1022         TRACE("Folder is %s\n",debugstr_w(full_path));
1023
1024         /* UI stuff */
1025         uirow = MSI_CreateRecord(1);
1026         MSI_RecordSetStringW(uirow,1,full_path);
1027         ui_actiondata(package,szCreateFolders,uirow);
1028         msiobj_release( &uirow->hdr );
1029
1030         if (folder->State == 0)
1031             create_full_pathW(full_path);
1032
1033         folder->State = 3;
1034
1035         msiobj_release(&row->hdr);
1036         HeapFree(GetProcessHeap(),0,full_path);
1037     }
1038     MSI_ViewClose(view);
1039     msiobj_release(&view->hdr);
1040    
1041     return rc;
1042 }
1043
1044 static int load_component(MSIPACKAGE* package, MSIRECORD * row)
1045 {
1046     int index = package->loaded_components;
1047     DWORD sz;
1048
1049     /* fill in the data */
1050
1051     package->loaded_components++;
1052     if (package->loaded_components == 1)
1053         package->components = HeapAlloc(GetProcessHeap(),0,
1054                                         sizeof(MSICOMPONENT));
1055     else
1056         package->components = HeapReAlloc(GetProcessHeap(),0,
1057             package->components, package->loaded_components * 
1058             sizeof(MSICOMPONENT));
1059
1060     memset(&package->components[index],0,sizeof(MSICOMPONENT));
1061
1062     sz = IDENTIFIER_SIZE;       
1063     MSI_RecordGetStringW(row,1,package->components[index].Component,&sz);
1064
1065     TRACE("Loading Component %s\n",
1066            debugstr_w(package->components[index].Component));
1067
1068     sz = 0x100;
1069     if (!MSI_RecordIsNull(row,2))
1070         MSI_RecordGetStringW(row,2,package->components[index].ComponentId,&sz);
1071             
1072     sz = IDENTIFIER_SIZE;       
1073     MSI_RecordGetStringW(row,3,package->components[index].Directory,&sz);
1074
1075     package->components[index].Attributes = MSI_RecordGetInteger(row,4);
1076
1077     sz = 0x100;       
1078     MSI_RecordGetStringW(row,5,package->components[index].Condition,&sz);
1079
1080     sz = IDENTIFIER_SIZE;       
1081     MSI_RecordGetStringW(row,6,package->components[index].KeyPath,&sz);
1082
1083     package->components[index].Installed = INSTALLSTATE_ABSENT;
1084     package->components[index].Action = INSTALLSTATE_UNKNOWN;
1085     package->components[index].ActionRequest = INSTALLSTATE_UNKNOWN;
1086
1087     package->components[index].Enabled = TRUE;
1088
1089     return index;
1090 }
1091
1092 static void load_feature(MSIPACKAGE* package, MSIRECORD * row)
1093 {
1094     int index = package->loaded_features;
1095     DWORD sz;
1096     static const WCHAR Query1[] = 
1097         {'S','E','L','E','C','T',' ',
1098          '`','C','o','m','p','o','n','e','n','t','_','`',
1099          ' ','F','R','O','M',' ','`','F','e','a','t','u','r','e',
1100          'C','o','m','p','o','n','e','n','t','s','`',' ',
1101          'W','H','E','R','E',' ',
1102          '`','F','e', 'a','t','u','r','e','_','`',' ','=','\'','%','s','\'',0};
1103     static const WCHAR Query2[] = 
1104         {'S','E','L','E','C','T',' ','*',' ','F','R', 'O','M',' ', 
1105          '`','C','o','m','p','o','n','e','n','t','`',' ',
1106          'W','H','E','R','E',' ', 
1107          '`','C','o','m','p','o','n','e','n','t','`',' ',
1108          '=','\'','%','s','\'',0};
1109     MSIQUERY * view;
1110     MSIQUERY * view2;
1111     MSIRECORD * row2;
1112     MSIRECORD * row3;
1113     UINT    rc;
1114
1115     /* fill in the data */
1116
1117     package->loaded_features ++;
1118     if (package->loaded_features == 1)
1119         package->features = HeapAlloc(GetProcessHeap(),0,sizeof(MSIFEATURE));
1120     else
1121         package->features = HeapReAlloc(GetProcessHeap(),0,package->features,
1122                                 package->loaded_features * sizeof(MSIFEATURE));
1123
1124     memset(&package->features[index],0,sizeof(MSIFEATURE));
1125     
1126     sz = IDENTIFIER_SIZE;       
1127     MSI_RecordGetStringW(row,1,package->features[index].Feature,&sz);
1128
1129     TRACE("Loading feature %s\n",debugstr_w(package->features[index].Feature));
1130
1131     sz = IDENTIFIER_SIZE;
1132     if (!MSI_RecordIsNull(row,2))
1133         MSI_RecordGetStringW(row,2,package->features[index].Feature_Parent,&sz);
1134
1135     sz = 0x100;
1136      if (!MSI_RecordIsNull(row,3))
1137         MSI_RecordGetStringW(row,3,package->features[index].Title,&sz);
1138
1139      sz = 0x100;
1140      if (!MSI_RecordIsNull(row,4))
1141         MSI_RecordGetStringW(row,4,package->features[index].Description,&sz);
1142
1143     if (!MSI_RecordIsNull(row,5))
1144         package->features[index].Display = MSI_RecordGetInteger(row,5);
1145   
1146     package->features[index].Level= MSI_RecordGetInteger(row,6);
1147
1148      sz = IDENTIFIER_SIZE;
1149      if (!MSI_RecordIsNull(row,7))
1150         MSI_RecordGetStringW(row,7,package->features[index].Directory,&sz);
1151
1152     package->features[index].Attributes= MSI_RecordGetInteger(row,8);
1153
1154     package->features[index].Installed = INSTALLSTATE_ABSENT;
1155     package->features[index].Action = INSTALLSTATE_UNKNOWN;
1156     package->features[index].ActionRequest = INSTALLSTATE_UNKNOWN;
1157
1158     /* load feature components */
1159
1160     rc = MSI_OpenQuery(package->db, &view, Query1, package->features[index].Feature);
1161     if (rc != ERROR_SUCCESS)
1162         return;
1163     rc = MSI_ViewExecute(view,0);
1164     if (rc != ERROR_SUCCESS)
1165     {
1166         MSI_ViewClose(view);
1167         msiobj_release(&view->hdr);
1168         return;
1169     }
1170     while (1)
1171     {
1172         LPCWSTR component;
1173         DWORD rc;
1174         INT c_indx;
1175         INT cnt = package->features[index].ComponentCount;
1176
1177         rc = MSI_ViewFetch(view,&row2);
1178         if (rc != ERROR_SUCCESS)
1179             break;
1180
1181         component = MSI_RecordGetString(row2,1);
1182
1183         /* check to see if the component is already loaded */
1184         c_indx = get_loaded_component(package,component);
1185         if (c_indx != -1)
1186         {
1187             TRACE("Component %s already loaded at %i\n", debugstr_w(component),
1188                   c_indx);
1189             package->features[index].Components[cnt] = c_indx;
1190             package->features[index].ComponentCount ++;
1191             msiobj_release( &row2->hdr );
1192             continue;
1193         }
1194
1195         rc = MSI_OpenQuery(package->db, &view2, Query2, component);
1196         if (rc != ERROR_SUCCESS)
1197         {
1198             msiobj_release( &row2->hdr );
1199             continue;
1200         }
1201         rc = MSI_ViewExecute(view2,0);
1202         if (rc != ERROR_SUCCESS)
1203         {
1204             msiobj_release( &row2->hdr );
1205             MSI_ViewClose(view2);
1206             msiobj_release( &view2->hdr );  
1207             continue;
1208         }
1209         while (1)
1210         {
1211             DWORD rc;
1212
1213             rc = MSI_ViewFetch(view2,&row3);
1214             if (rc != ERROR_SUCCESS)
1215                 break;
1216             c_indx = load_component(package,row3);
1217             msiobj_release( &row3->hdr );
1218
1219             package->features[index].Components[cnt] = c_indx;
1220             package->features[index].ComponentCount ++;
1221             TRACE("Loaded new component to index %i\n",c_indx);
1222         }
1223         MSI_ViewClose(view2);
1224         msiobj_release( &view2->hdr );
1225         msiobj_release( &row2->hdr );
1226     }
1227     MSI_ViewClose(view);
1228     msiobj_release(&view->hdr);
1229 }
1230
1231 static UINT load_file(MSIPACKAGE* package, MSIRECORD * row)
1232 {
1233     DWORD index = package->loaded_files;
1234     LPCWSTR component;
1235
1236     /* fill in the data */
1237
1238     package->loaded_files++;
1239     if (package->loaded_files== 1)
1240         package->files = HeapAlloc(GetProcessHeap(),0,sizeof(MSIFILE));
1241     else
1242         package->files = HeapReAlloc(GetProcessHeap(),0,
1243             package->files , package->loaded_files * sizeof(MSIFILE));
1244
1245     memset(&package->files[index],0,sizeof(MSIFILE));
1246  
1247     package->files[index].File = load_dynamic_stringW(row, 1);
1248
1249     component = MSI_RecordGetString(row, 2);
1250     package->files[index].ComponentIndex = get_loaded_component(package,
1251                     component);
1252
1253     if (package->files[index].ComponentIndex == -1)
1254         ERR("Unfound Component %s\n",debugstr_w(component));
1255
1256     package->files[index].FileName = load_dynamic_stringW(row,3);
1257     reduce_to_longfilename(package->files[index].FileName);
1258
1259     package->files[index].ShortName = load_dynamic_stringW(row,3);
1260     reduce_to_shortfilename(package->files[index].ShortName);
1261     
1262     package->files[index].FileSize = MSI_RecordGetInteger(row,4);
1263     package->files[index].Version = load_dynamic_stringW(row, 5);
1264     package->files[index].Language = load_dynamic_stringW(row, 6);
1265     package->files[index].Attributes= MSI_RecordGetInteger(row,7);
1266     package->files[index].Sequence= MSI_RecordGetInteger(row,8);
1267
1268     package->files[index].Temporary = FALSE;
1269     package->files[index].State = 0;
1270
1271     TRACE("File Loaded (%s)\n",debugstr_w(package->files[index].File));  
1272  
1273     return ERROR_SUCCESS;
1274 }
1275
1276 static UINT load_all_files(MSIPACKAGE *package)
1277 {
1278     MSIQUERY * view;
1279     MSIRECORD * row;
1280     UINT rc;
1281     static const WCHAR Query[] =
1282         {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
1283          '`','F','i','l','e','`',' ', 'O','R','D','E','R',' ','B','Y',' ',
1284          '`','S','e','q','u','e','n','c','e','`', 0};
1285
1286     if (!package)
1287         return ERROR_INVALID_HANDLE;
1288
1289     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
1290     if (rc != ERROR_SUCCESS)
1291         return ERROR_SUCCESS;
1292    
1293     rc = MSI_ViewExecute(view, 0);
1294     if (rc != ERROR_SUCCESS)
1295     {
1296         MSI_ViewClose(view);
1297         msiobj_release(&view->hdr);
1298         return ERROR_SUCCESS;
1299     }
1300
1301     while (1)
1302     {
1303         rc = MSI_ViewFetch(view,&row);
1304         if (rc != ERROR_SUCCESS)
1305         {
1306             rc = ERROR_SUCCESS;
1307             break;
1308         }
1309         load_file(package,row);
1310         msiobj_release(&row->hdr);
1311     }
1312     MSI_ViewClose(view);
1313     msiobj_release(&view->hdr);
1314
1315     return ERROR_SUCCESS;
1316 }
1317
1318
1319 /*
1320  * I am not doing any of the costing functionality yet. 
1321  * Mostly looking at doing the Component and Feature loading
1322  *
1323  * The native MSI does A LOT of modification to tables here. Mostly adding
1324  * a lot of temporary columns to the Feature and Component tables. 
1325  *
1326  *    note: Native msi also tracks the short filename. But I am only going to
1327  *          track the long ones.  Also looking at this directory table
1328  *          it appears that the directory table does not get the parents
1329  *          resolved base on property only based on their entries in the 
1330  *          directory table.
1331  */
1332 static UINT ACTION_CostInitialize(MSIPACKAGE *package)
1333 {
1334     MSIQUERY * view;
1335     MSIRECORD * row;
1336     UINT rc;
1337     static const WCHAR Query_all[] =
1338         {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
1339          '`','F','e','a','t','u','r','e','`',0};
1340     static const WCHAR szCosting[] =
1341         {'C','o','s','t','i','n','g','C','o','m','p','l','e','t','e',0 };
1342     static const WCHAR szZero[] = { '0', 0 };
1343     WCHAR buffer[3];
1344     DWORD sz = 3;
1345
1346     MSI_GetPropertyW(package, szCosting, buffer, &sz);
1347     if (buffer[0]=='1')
1348         return ERROR_SUCCESS;
1349     
1350     MSI_SetPropertyW(package, szCosting, szZero);
1351     MSI_SetPropertyW(package, cszRootDrive , c_colon);
1352
1353     rc = MSI_DatabaseOpenViewW(package->db,Query_all,&view);
1354     if (rc != ERROR_SUCCESS)
1355         return rc;
1356     rc = MSI_ViewExecute(view,0);
1357     if (rc != ERROR_SUCCESS)
1358     {
1359         MSI_ViewClose(view);
1360         msiobj_release(&view->hdr);
1361         return rc;
1362     }
1363     while (1)
1364     {
1365         DWORD rc;
1366
1367         rc = MSI_ViewFetch(view,&row);
1368         if (rc != ERROR_SUCCESS)
1369             break;
1370        
1371         load_feature(package,row); 
1372         msiobj_release(&row->hdr);
1373     }
1374     MSI_ViewClose(view);
1375     msiobj_release(&view->hdr);
1376
1377     load_all_files(package);
1378
1379     return ERROR_SUCCESS;
1380 }
1381
1382 static UINT execute_script(MSIPACKAGE *package, UINT script )
1383 {
1384     int i;
1385     UINT rc = ERROR_SUCCESS;
1386
1387     TRACE("Executing Script %i\n",script);
1388
1389     for (i = 0; i < package->script->ActionCount[script]; i++)
1390     {
1391         LPWSTR action;
1392         action = package->script->Actions[script][i];
1393         ui_actionstart(package, action);
1394         TRACE("Executing Action (%s)\n",debugstr_w(action));
1395         rc = ACTION_PerformAction(package, action, TRUE);
1396         HeapFree(GetProcessHeap(),0,package->script->Actions[script][i]);
1397         if (rc != ERROR_SUCCESS)
1398             break;
1399     }
1400     HeapFree(GetProcessHeap(),0,package->script->Actions[script]);
1401
1402     package->script->ActionCount[script] = 0;
1403     package->script->Actions[script] = NULL;
1404     return rc;
1405 }
1406
1407 static UINT ACTION_FileCost(MSIPACKAGE *package)
1408 {
1409     return ERROR_SUCCESS;
1410 }
1411
1412
1413 static INT load_folder(MSIPACKAGE *package, const WCHAR* dir)
1414 {
1415     static const WCHAR Query[] =
1416         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1417          '`','D','i','r','e','c', 't','o','r','y','`',' ',
1418          'W','H','E','R','E',' ', '`', 'D','i','r','e','c','t', 'o','r','y','`',
1419          ' ','=',' ','\'','%','s','\'',
1420          0};
1421     LPWSTR ptargetdir, targetdir, srcdir;
1422     LPCWSTR parent;
1423     LPWSTR shortname = NULL;
1424     MSIRECORD * row = 0;
1425     INT index = -1;
1426     DWORD i;
1427
1428     TRACE("Looking for dir %s\n",debugstr_w(dir));
1429
1430     for (i = 0; i < package->loaded_folders; i++)
1431     {
1432         if (strcmpW(package->folders[i].Directory,dir)==0)
1433         {
1434             TRACE(" %s retuning on index %lu\n",debugstr_w(dir),i);
1435             return i;
1436         }
1437     }
1438
1439     TRACE("Working to load %s\n",debugstr_w(dir));
1440
1441     index = package->loaded_folders++;
1442     if (package->loaded_folders==1)
1443         package->folders = HeapAlloc(GetProcessHeap(),0,
1444                                         sizeof(MSIFOLDER));
1445     else
1446         package->folders= HeapReAlloc(GetProcessHeap(),0,
1447             package->folders, package->loaded_folders* 
1448             sizeof(MSIFOLDER));
1449
1450     memset(&package->folders[index],0,sizeof(MSIFOLDER));
1451
1452     package->folders[index].Directory = strdupW(dir);
1453
1454     row = MSI_QueryGetRecord(package->db, Query, dir);
1455     if (!row)
1456         return -1;
1457
1458     ptargetdir = targetdir = load_dynamic_stringW(row,3);
1459
1460     /* split src and target dir */
1461     if (strchrW(targetdir,':'))
1462     {
1463         srcdir=strchrW(targetdir,':');
1464         *srcdir=0;
1465         srcdir ++;
1466     }
1467     else
1468         srcdir=NULL;
1469
1470     /* for now only pick long filename versions */
1471     if (strchrW(targetdir,'|'))
1472     {
1473         shortname = targetdir;
1474         targetdir = strchrW(targetdir,'|'); 
1475         *targetdir = 0;
1476         targetdir ++;
1477     }
1478     /* for the sourcedir pick the short filename */
1479     if (srcdir && strchrW(srcdir,'|'))
1480     {
1481         LPWSTR p = strchrW(srcdir,'|'); 
1482         *p = 0;
1483     }
1484
1485     /* now check for root dirs */
1486     if (targetdir[0] == '.' && targetdir[1] == 0)
1487         targetdir = NULL;
1488         
1489     if (srcdir && srcdir[0] == '.' && srcdir[1] == 0)
1490         srcdir = NULL;
1491
1492     if (targetdir)
1493     {
1494         TRACE("   TargetDefault = %s\n",debugstr_w(targetdir));
1495         HeapFree(GetProcessHeap(),0, package->folders[index].TargetDefault);
1496         package->folders[index].TargetDefault = strdupW(targetdir);
1497     }
1498
1499     if (srcdir)
1500         package->folders[index].SourceDefault = strdupW(srcdir);
1501     else if (shortname)
1502         package->folders[index].SourceDefault = strdupW(shortname);
1503     else if (targetdir)
1504         package->folders[index].SourceDefault = strdupW(targetdir);
1505     HeapFree(GetProcessHeap(), 0, ptargetdir);
1506         TRACE("   SourceDefault = %s\n",debugstr_w(package->folders[index].SourceDefault));
1507
1508     parent = MSI_RecordGetString(row,2);
1509     if (parent) 
1510     {
1511         i = load_folder(package,parent);
1512         package->folders[index].ParentIndex = i;
1513         TRACE("Parent is index %i... %s %s\n",
1514                     package->folders[index].ParentIndex,
1515         debugstr_w(package->folders[package->folders[index].ParentIndex].Directory),
1516                     debugstr_w(parent));
1517     }
1518     else
1519         package->folders[index].ParentIndex = -2;
1520
1521     package->folders[index].Property = load_dynamic_property(package, dir,NULL);
1522
1523     msiobj_release(&row->hdr);
1524     TRACE(" %s retuning on index %i\n",debugstr_w(dir),index);
1525     return index;
1526 }
1527
1528 /* scan for and update current install states */
1529 static void ACTION_UpdateInstallStates(MSIPACKAGE *package)
1530 {
1531     int i;
1532     LPWSTR productcode;
1533
1534     productcode = load_dynamic_property(package,szProductCode,NULL);
1535
1536     for (i = 0; i < package->loaded_components; i++)
1537     {
1538         INSTALLSTATE res;
1539         res = MsiGetComponentPathW(productcode, 
1540                         package->components[i].ComponentId , NULL, NULL);
1541         if (res < 0)
1542             res = INSTALLSTATE_ABSENT;
1543         package->components[i].Installed = res;
1544     }
1545
1546     for (i = 0; i < package->loaded_features; i++)
1547     {
1548         INSTALLSTATE res = -10;
1549         int j;
1550         for (j = 0; j < package->features[i].ComponentCount; j++)
1551         {
1552             MSICOMPONENT* component = &package->components[package->features[i].
1553                                                            Components[j]];
1554             if (res == -10)
1555                 res = component->Installed;
1556             else
1557             {
1558                 if (res == component->Installed)
1559                     continue;
1560
1561                 if (res != component->Installed)
1562                         res = INSTALLSTATE_INCOMPLETE;
1563             }
1564         }
1565     }
1566 }
1567
1568 static BOOL process_state_property (MSIPACKAGE* package, LPCWSTR property, 
1569                                     INSTALLSTATE state)
1570 {
1571     static const WCHAR all[]={'A','L','L',0};
1572     LPWSTR override = NULL;
1573     INT i;
1574     BOOL rc = FALSE;
1575
1576     override = load_dynamic_property(package, property, NULL);
1577     if (override)
1578     {
1579         rc = TRUE;
1580         for(i = 0; i < package->loaded_features; i++)
1581         {
1582             if (strcmpiW(override,all)==0)
1583             {
1584                 package->features[i].ActionRequest= state;
1585                 package->features[i].Action = state;
1586             }
1587             else
1588             {
1589                 LPWSTR ptr = override;
1590                 LPWSTR ptr2 = strchrW(override,',');
1591
1592                 while (ptr)
1593                 {
1594                     if ((ptr2 && 
1595                         strncmpW(ptr,package->features[i].Feature, ptr2-ptr)==0)
1596                         || (!ptr2 &&
1597                         strcmpW(ptr,package->features[i].Feature)==0))
1598                     {
1599                         package->features[i].ActionRequest= state;
1600                         package->features[i].Action = state;
1601                         break;
1602                     }
1603                     if (ptr2)
1604                     {
1605                         ptr=ptr2+1;
1606                         ptr2 = strchrW(ptr,',');
1607                     }
1608                     else
1609                         break;
1610                 }
1611             }
1612         }
1613         HeapFree(GetProcessHeap(),0,override);
1614     } 
1615
1616     return rc;
1617 }
1618
1619 static UINT SetFeatureStates(MSIPACKAGE *package)
1620 {
1621     LPWSTR level;
1622     INT install_level;
1623     DWORD i;
1624     INT j;
1625     static const WCHAR szlevel[] =
1626         {'I','N','S','T','A','L','L','L','E','V','E','L',0};
1627     static const WCHAR szAddLocal[] =
1628         {'A','D','D','L','O','C','A','L',0};
1629     static const WCHAR szRemove[] =
1630         {'R','E','M','O','V','E',0};
1631     BOOL override = FALSE;
1632
1633     /* I do not know if this is where it should happen.. but */
1634
1635     TRACE("Checking Install Level\n");
1636
1637     level = load_dynamic_property(package,szlevel,NULL);
1638     if (level)
1639     {
1640         install_level = atoiW(level);
1641         HeapFree(GetProcessHeap(), 0, level);
1642     }
1643     else
1644         install_level = 1;
1645
1646     /* ok hereis the _real_ rub
1647      * all these activation/deactivation things happen in order and things
1648      * later on the list override things earlier on the list.
1649      * 1) INSTALLLEVEL processing
1650      * 2) ADDLOCAL
1651      * 3) REMOVE
1652      * 4) ADDSOURCE
1653      * 5) ADDDEFAULT
1654      * 6) REINSTALL
1655      * 7) COMPADDLOCAL
1656      * 8) COMPADDSOURCE
1657      * 9) FILEADDLOCAL
1658      * 10) FILEADDSOURCE
1659      * 11) FILEADDDEFAULT
1660      * I have confirmed that if ADDLOCAL is stated then the INSTALLLEVEL is
1661      * ignored for all the features. seems strange, especially since it is not
1662      * documented anywhere, but it is how it works. 
1663      *
1664      * I am still ignoring a lot of these. But that is ok for now, ADDLOCAL and
1665      * REMOVE are the big ones, since we don't handle administrative installs
1666      * yet anyway.
1667      */
1668     override |= process_state_property(package,szAddLocal,INSTALLSTATE_LOCAL);
1669     override |= process_state_property(package,szRemove,INSTALLSTATE_ABSENT);
1670
1671     if (!override)
1672     {
1673         for(i = 0; i < package->loaded_features; i++)
1674         {
1675             BOOL feature_state = ((package->features[i].Level > 0) &&
1676                              (package->features[i].Level <= install_level));
1677
1678             if ((feature_state) && 
1679                (package->features[i].Action == INSTALLSTATE_UNKNOWN))
1680             {
1681                 if (package->features[i].Attributes & 
1682                                 msidbFeatureAttributesFavorSource)
1683                 {
1684                     package->features[i].ActionRequest = INSTALLSTATE_SOURCE;
1685                     package->features[i].Action = INSTALLSTATE_SOURCE;
1686                 }
1687                 else if (package->features[i].Attributes &
1688                                 msidbFeatureAttributesFavorAdvertise)
1689                 {
1690                     package->features[i].ActionRequest =INSTALLSTATE_ADVERTISED;
1691                     package->features[i].Action =INSTALLSTATE_ADVERTISED;
1692                 }
1693                 else
1694                 {
1695                     package->features[i].ActionRequest = INSTALLSTATE_LOCAL;
1696                     package->features[i].Action = INSTALLSTATE_LOCAL;
1697                 }
1698             }
1699         }
1700     }
1701     else
1702     {
1703         /* set the Preselected Property */
1704         static const WCHAR szPreselected[] = {'P','r','e','s','e','l','e','c','t','e','d',0};
1705         static const WCHAR szOne[] = { '1', 0 };
1706
1707         MSI_SetPropertyW(package,szPreselected,szOne);
1708     }
1709
1710     /*
1711      * now we want to enable or disable components base on feature 
1712     */
1713
1714     for(i = 0; i < package->loaded_features; i++)
1715     {
1716         MSIFEATURE* feature = &package->features[i];
1717         TRACE("Examining Feature %s (Installed %i, Action %i, Request %i)\n",
1718             debugstr_w(feature->Feature), feature->Installed, feature->Action,
1719             feature->ActionRequest);
1720
1721         for( j = 0; j < feature->ComponentCount; j++)
1722         {
1723             MSICOMPONENT* component = &package->components[
1724                                                     feature->Components[j]];
1725
1726             if (!component->Enabled)
1727             {
1728                 component->Action = INSTALLSTATE_UNKNOWN;
1729                 component->ActionRequest = INSTALLSTATE_UNKNOWN;
1730             }
1731             else
1732             {
1733                 if (feature->Action == INSTALLSTATE_LOCAL)
1734                 {
1735                     component->Action = INSTALLSTATE_LOCAL;
1736                     component->ActionRequest = INSTALLSTATE_LOCAL;
1737                 }
1738                 else if (feature->ActionRequest == INSTALLSTATE_SOURCE)
1739                 {
1740                     if ((component->Action == INSTALLSTATE_UNKNOWN) ||
1741                         (component->Action == INSTALLSTATE_ABSENT) ||
1742                         (component->Action == INSTALLSTATE_ADVERTISED))
1743                            
1744                     {
1745                         component->Action = INSTALLSTATE_SOURCE;
1746                         component->ActionRequest = INSTALLSTATE_SOURCE;
1747                     }
1748                 }
1749                 else if (feature->ActionRequest == INSTALLSTATE_ADVERTISED)
1750                 {
1751                     if ((component->Action == INSTALLSTATE_UNKNOWN) ||
1752                         (component->Action == INSTALLSTATE_ABSENT))
1753                            
1754                     {
1755                         component->Action = INSTALLSTATE_ADVERTISED;
1756                         component->ActionRequest = INSTALLSTATE_ADVERTISED;
1757                     }
1758                 }
1759                 else if (feature->ActionRequest == INSTALLSTATE_ABSENT)
1760                 {
1761                     if (component->Action == INSTALLSTATE_UNKNOWN)
1762                     {
1763                         component->Action = INSTALLSTATE_ABSENT;
1764                         component->ActionRequest = INSTALLSTATE_ABSENT;
1765                     }
1766                 }
1767             }
1768         }
1769     } 
1770
1771     for(i = 0; i < package->loaded_components; i++)
1772     {
1773         MSICOMPONENT* component= &package->components[i];
1774
1775         TRACE("Result: Component %s (Installed %i, Action %i, Request %i)\n",
1776             debugstr_w(component->Component), component->Installed, 
1777             component->Action, component->ActionRequest);
1778     }
1779
1780
1781     return ERROR_SUCCESS;
1782 }
1783
1784 /* 
1785  * A lot is done in this function aside from just the costing.
1786  * The costing needs to be implemented at some point but for now I am going
1787  * to focus on the directory building
1788  *
1789  */
1790 static UINT ACTION_CostFinalize(MSIPACKAGE *package)
1791 {
1792     static const WCHAR ExecSeqQuery[] =
1793         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1794          '`','D','i','r','e','c','t','o','r','y','`',0};
1795     static const WCHAR ConditionQuery[] =
1796         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1797          '`','C','o','n','d','i','t','i','o','n','`',0};
1798     static const WCHAR szCosting[] =
1799         {'C','o','s','t','i','n','g','C','o','m','p','l','e','t','e',0 };
1800     static const WCHAR szlevel[] =
1801         {'I','N','S','T','A','L','L','L','E','V','E','L',0};
1802     static const WCHAR szOne[] = { '1', 0 };
1803     UINT rc;
1804     MSIQUERY * view;
1805     DWORD i;
1806     LPWSTR level;
1807     DWORD sz = 3;
1808     WCHAR buffer[3];
1809
1810     MSI_GetPropertyW(package, szCosting, buffer, &sz);
1811     if (buffer[0]=='1')
1812         return ERROR_SUCCESS;
1813
1814     TRACE("Building Directory properties\n");
1815
1816     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
1817     if (rc == ERROR_SUCCESS)
1818     {
1819         rc = MSI_ViewExecute(view, 0);
1820         if (rc != ERROR_SUCCESS)
1821         {
1822             MSI_ViewClose(view);
1823             msiobj_release(&view->hdr);
1824             return rc;
1825         }
1826
1827         while (1)
1828         {
1829             LPCWSTR name;
1830             LPWSTR path;
1831             MSIRECORD * row = 0;
1832
1833             rc = MSI_ViewFetch(view,&row);
1834             if (rc != ERROR_SUCCESS)
1835             {
1836                 rc = ERROR_SUCCESS;
1837                 break;
1838             }
1839
1840             name = MSI_RecordGetString(row,1);
1841
1842             /* This helper function now does ALL the work */
1843             TRACE("Dir %s ...\n",debugstr_w(name));
1844             load_folder(package,name);
1845             path = resolve_folder(package,name,FALSE,TRUE,NULL);
1846             TRACE("resolves to %s\n",debugstr_w(path));
1847             HeapFree( GetProcessHeap(), 0, path);
1848
1849             msiobj_release(&row->hdr);
1850         }
1851         MSI_ViewClose(view);
1852         msiobj_release(&view->hdr);
1853     }
1854
1855     TRACE("File calculations %i files\n",package->loaded_files);
1856
1857     for (i = 0; i < package->loaded_files; i++)
1858     {
1859         MSICOMPONENT* comp = NULL;
1860         MSIFILE* file= NULL;
1861
1862         file = &package->files[i];
1863         if (file->ComponentIndex >= 0)
1864             comp = &package->components[file->ComponentIndex];
1865
1866         if (file->Temporary == TRUE)
1867             continue;
1868
1869         if (comp)
1870         {
1871             LPWSTR p;
1872
1873             /* calculate target */
1874             p = resolve_folder(package, comp->Directory, FALSE, FALSE, NULL);
1875
1876             HeapFree(GetProcessHeap(),0,file->TargetPath);
1877
1878             TRACE("file %s is named %s\n",
1879                    debugstr_w(file->File),debugstr_w(file->FileName));       
1880
1881             file->TargetPath = build_directory_name(2, p, file->FileName);
1882
1883             HeapFree(GetProcessHeap(),0,p);
1884
1885             TRACE("file %s resolves to %s\n",
1886                    debugstr_w(file->File),debugstr_w(file->TargetPath));       
1887
1888             if (GetFileAttributesW(file->TargetPath) == INVALID_FILE_ATTRIBUTES)
1889             {
1890                 file->State = 1;
1891                 comp->Cost += file->FileSize;
1892             }
1893             else
1894             {
1895                 if (file->Version)
1896                 {
1897                     DWORD handle;
1898                     DWORD versize;
1899                     UINT sz;
1900                     LPVOID version;
1901                     static const WCHAR name[] = 
1902                         {'\\',0};
1903                     static const WCHAR name_fmt[] = 
1904                         {'%','u','.','%','u','.','%','u','.','%','u',0};
1905                     WCHAR filever[0x100];
1906                     VS_FIXEDFILEINFO *lpVer;
1907
1908                     TRACE("Version comparison.. \n");
1909                     versize = GetFileVersionInfoSizeW(file->TargetPath,&handle);
1910                     version = HeapAlloc(GetProcessHeap(),0,versize);
1911                     GetFileVersionInfoW(file->TargetPath, 0, versize, version);
1912
1913                     VerQueryValueW(version, name, (LPVOID*)&lpVer, &sz);
1914
1915                     sprintfW(filever,name_fmt,
1916                         HIWORD(lpVer->dwFileVersionMS),
1917                         LOWORD(lpVer->dwFileVersionMS),
1918                         HIWORD(lpVer->dwFileVersionLS),
1919                         LOWORD(lpVer->dwFileVersionLS));
1920
1921                     TRACE("new %s old %s\n", debugstr_w(file->Version),
1922                           debugstr_w(filever));
1923                     if (strcmpiW(filever,file->Version)<0)
1924                     {
1925                         file->State = 2;
1926                         FIXME("cost should be diff in size\n");
1927                         comp->Cost += file->FileSize;
1928                     }
1929                     else
1930                         file->State = 3;
1931                     HeapFree(GetProcessHeap(),0,version);
1932                 }
1933                 else
1934                     file->State = 3;
1935             }
1936         } 
1937     }
1938
1939     TRACE("Evaluating Condition Table\n");
1940
1941     rc = MSI_DatabaseOpenViewW(package->db, ConditionQuery, &view);
1942     if (rc == ERROR_SUCCESS)
1943     {
1944         rc = MSI_ViewExecute(view, 0);
1945         if (rc != ERROR_SUCCESS)
1946         {
1947             MSI_ViewClose(view);
1948             msiobj_release(&view->hdr);
1949             return rc;
1950         }
1951     
1952         while (1)
1953         {
1954             LPCWSTR Feature;
1955             MSIRECORD * row = 0;
1956             int feature_index;
1957
1958             rc = MSI_ViewFetch(view,&row);
1959
1960             if (rc != ERROR_SUCCESS)
1961             {
1962                 rc = ERROR_SUCCESS;
1963                 break;
1964             }
1965
1966             Feature = MSI_RecordGetString(row,1);
1967
1968             feature_index = get_loaded_feature(package,Feature);
1969             if (feature_index < 0)
1970                 ERR("FAILED to find loaded feature %s\n",debugstr_w(Feature));
1971             else
1972             {
1973                 LPCWSTR Condition;
1974                 Condition = MSI_RecordGetString(row,3);
1975
1976                 if (MSI_EvaluateConditionW(package,Condition) == 
1977                     MSICONDITION_TRUE)
1978                 {
1979                     int level = MSI_RecordGetInteger(row,2);
1980                     TRACE("Reseting feature %s to level %i\n",
1981                            debugstr_w(Feature), level);
1982                     package->features[feature_index].Level = level;
1983                 }
1984             }
1985
1986             msiobj_release(&row->hdr);
1987         }
1988         MSI_ViewClose(view);
1989         msiobj_release(&view->hdr);
1990     }
1991
1992     TRACE("Enabling or Disabling Components\n");
1993     for (i = 0; i < package->loaded_components; i++)
1994     {
1995         if (package->components[i].Condition[0])
1996         {
1997             if (MSI_EvaluateConditionW(package,
1998                 package->components[i].Condition) == MSICONDITION_FALSE)
1999             {
2000                 TRACE("Disabling component %s\n",
2001                       debugstr_w(package->components[i].Component));
2002                 package->components[i].Enabled = FALSE;
2003             }
2004         }
2005     }
2006
2007     MSI_SetPropertyW(package,szCosting,szOne);
2008     /* set default run level if not set */
2009     level = load_dynamic_property(package,szlevel,NULL);
2010     if (!level)
2011         MSI_SetPropertyW(package,szlevel, szOne);
2012     else
2013         HeapFree(GetProcessHeap(),0,level);
2014
2015     ACTION_UpdateInstallStates(package);
2016
2017     return SetFeatureStates(package);
2018 }
2019
2020
2021
2022 /* OK this value is "interpreted" and then formatted based on the 
2023    first few characters */
2024 static LPSTR parse_value(MSIPACKAGE *package, LPCWSTR value, DWORD *type, 
2025                          DWORD *size)
2026 {
2027     LPSTR data = NULL;
2028     if (value[0]=='#' && value[1]!='#' && value[1]!='%')
2029     {
2030         if (value[1]=='x')
2031         {
2032             LPWSTR ptr;
2033             CHAR byte[5];
2034             LPWSTR deformated = NULL;
2035             int count;
2036
2037             deformat_string(package, &value[2], &deformated);
2038
2039             /* binary value type */
2040             ptr = deformated;
2041             *type = REG_BINARY;
2042             if (strlenW(ptr)%2)
2043                 *size = (strlenW(ptr)/2)+1;
2044             else
2045                 *size = strlenW(ptr)/2;
2046
2047             data = HeapAlloc(GetProcessHeap(),0,*size);
2048
2049             byte[0] = '0'; 
2050             byte[1] = 'x'; 
2051             byte[4] = 0; 
2052             count = 0;
2053             /* if uneven pad with a zero in front */
2054             if (strlenW(ptr)%2)
2055             {
2056                 byte[2]= '0';
2057                 byte[3]= *ptr;
2058                 ptr++;
2059                 data[count] = (BYTE)strtol(byte,NULL,0);
2060                 count ++;
2061                 TRACE("Uneven byte count\n");
2062             }
2063             while (*ptr)
2064             {
2065                 byte[2]= *ptr;
2066                 ptr++;
2067                 byte[3]= *ptr;
2068                 ptr++;
2069                 data[count] = (BYTE)strtol(byte,NULL,0);
2070                 count ++;
2071             }
2072             HeapFree(GetProcessHeap(),0,deformated);
2073
2074             TRACE("Data %li bytes(%i)\n",*size,count);
2075         }
2076         else
2077         {
2078             LPWSTR deformated;
2079             LPWSTR p;
2080             DWORD d = 0;
2081             deformat_string(package, &value[1], &deformated);
2082
2083             *type=REG_DWORD; 
2084             *size = sizeof(DWORD);
2085             data = HeapAlloc(GetProcessHeap(),0,*size);
2086             p = deformated;
2087             if (*p == '-')
2088                 p++;
2089             while (*p)
2090             {
2091                 if ( (*p < '0') || (*p > '9') )
2092                     break;
2093                 d *= 10;
2094                 d += (*p - '0');
2095                 p++;
2096             }
2097             if (deformated[0] == '-')
2098                 d = -d;
2099             *(LPDWORD)data = d;
2100             TRACE("DWORD %li\n",*(LPDWORD)data);
2101
2102             HeapFree(GetProcessHeap(),0,deformated);
2103         }
2104     }
2105     else
2106     {
2107         static const WCHAR szMulti[] = {'[','~',']',0};
2108         LPCWSTR ptr;
2109         *type=REG_SZ;
2110
2111         if (value[0]=='#')
2112         {
2113             if (value[1]=='%')
2114             {
2115                 ptr = &value[2];
2116                 *type=REG_EXPAND_SZ;
2117             }
2118             else
2119                 ptr = &value[1];
2120          }
2121          else
2122             ptr=value;
2123
2124         if (strstrW(value,szMulti))
2125             *type = REG_MULTI_SZ;
2126
2127         *size = deformat_string(package, ptr,(LPWSTR*)&data);
2128     }
2129     return data;
2130 }
2131
2132 static UINT ACTION_WriteRegistryValues(MSIPACKAGE *package)
2133 {
2134     UINT rc;
2135     MSIQUERY * view;
2136     MSIRECORD * row = 0;
2137     static const WCHAR ExecSeqQuery[] =
2138         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2139          '`','R','e','g','i','s','t','r','y','`',0 };
2140
2141     if (!package)
2142         return ERROR_INVALID_HANDLE;
2143
2144     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
2145     if (rc != ERROR_SUCCESS)
2146         return ERROR_SUCCESS;
2147
2148     rc = MSI_ViewExecute(view, 0);
2149     if (rc != ERROR_SUCCESS)
2150     {
2151         MSI_ViewClose(view);
2152         msiobj_release(&view->hdr);
2153         return rc;
2154     }
2155
2156     /* increment progress bar each time action data is sent */
2157     ui_progress(package,1,REG_PROGRESS_VALUE,1,0);
2158
2159     while (1)
2160     {
2161         static const WCHAR szHCR[] = 
2162             {'H','K','E','Y','_','C','L','A','S','S','E','S','_',
2163              'R','O','O','T','\\',0};
2164         static const WCHAR szHCU[] =
2165             {'H','K','E','Y','_','C','U','R','R','E','N','T','_',
2166              'U','S','E','R','\\',0};
2167         static const WCHAR szHLM[] =
2168             {'H','K','E','Y','_','L','O','C','A','L','_',
2169              'M','A','C','H','I','N','E','\\',0};
2170         static const WCHAR szHU[] =
2171             {'H','K','E','Y','_','U','S','E','R','S','\\',0};
2172
2173         LPSTR value_data = NULL;
2174         HKEY  root_key, hkey;
2175         DWORD type,size;
2176         LPWSTR  deformated;
2177         LPCWSTR szRoot, component, name, key, value;
2178         INT component_index;
2179         MSIRECORD * uirow;
2180         LPWSTR uikey;
2181         INT   root;
2182         BOOL check_first = FALSE;
2183
2184         rc = MSI_ViewFetch(view,&row);
2185         if (rc != ERROR_SUCCESS)
2186         {
2187             rc = ERROR_SUCCESS;
2188             break;
2189         }
2190         ui_progress(package,2,0,0,0);
2191
2192         value = NULL;
2193         key = NULL;
2194         uikey = NULL;
2195         name = NULL;
2196
2197         component = MSI_RecordGetString(row, 6);
2198         component_index = get_loaded_component(package,component);
2199
2200         if (!ACTION_VerifyComponentForAction(package, component_index, 
2201                                        INSTALLSTATE_LOCAL))
2202         {
2203             TRACE("Skipping write due to disabled component %s\n", 
2204                             debugstr_w(component));
2205             msiobj_release(&row->hdr);
2206
2207             package->components[component_index].Action =
2208                 package->components[component_index].Installed;
2209
2210             goto next;
2211         }
2212
2213         package->components[component_index].Action = INSTALLSTATE_LOCAL;
2214
2215         name = MSI_RecordGetString(row, 4);
2216         if( MSI_RecordIsNull(row,5) && name )
2217         {
2218             /* null values can have special meanings */
2219             if (name[0]=='-' && name[1] == 0)
2220             {
2221                 msiobj_release(&row->hdr);
2222                 goto next;
2223             }
2224             else if ((name[0]=='+' && name[1] == 0) || 
2225                      (name[0] == '*' && name[1] == 0))
2226                 name = NULL;
2227                 check_first = TRUE;
2228         }
2229
2230         root = MSI_RecordGetInteger(row,2);
2231         key = MSI_RecordGetString(row, 3);
2232    
2233         /* get the root key */
2234         switch (root)
2235         {
2236             case 0:  root_key = HKEY_CLASSES_ROOT; 
2237                      szRoot = szHCR;
2238                      break;
2239             case 1:  root_key = HKEY_CURRENT_USER;
2240                      szRoot = szHCU;
2241                      break;
2242             case 2:  root_key = HKEY_LOCAL_MACHINE;
2243                      szRoot = szHLM;
2244                      break;
2245             case 3:  root_key = HKEY_USERS; 
2246                      szRoot = szHU;
2247                      break;
2248             default:
2249                  ERR("Unknown root %i\n",root);
2250                  root_key=NULL;
2251                  szRoot = NULL;
2252                  break;
2253         }
2254         if (!root_key)
2255         {
2256             msiobj_release(&row->hdr);
2257             goto next;
2258         }
2259
2260         deformat_string(package, key , &deformated);
2261         size = strlenW(deformated) + strlenW(szRoot) + 1;
2262         uikey = HeapAlloc(GetProcessHeap(), 0, size*sizeof(WCHAR));
2263         strcpyW(uikey,szRoot);
2264         strcatW(uikey,deformated);
2265
2266         if (RegCreateKeyW( root_key, deformated, &hkey))
2267         {
2268             ERR("Could not create key %s\n",debugstr_w(deformated));
2269             msiobj_release(&row->hdr);
2270             HeapFree(GetProcessHeap(),0,deformated);
2271             goto next;
2272         }
2273         HeapFree(GetProcessHeap(),0,deformated);
2274
2275         value = MSI_RecordGetString(row,5);
2276         if (value)
2277             value_data = parse_value(package, value, &type, &size); 
2278         else
2279         {
2280             static const WCHAR szEmpty[] = {0};
2281             value_data = (LPSTR)strdupW(szEmpty);
2282             size = 0;
2283             type = REG_SZ;
2284         }
2285
2286         deformat_string(package, name, &deformated);
2287
2288         /* get the double nulls to terminate SZ_MULTI */
2289         if (type == REG_MULTI_SZ)
2290             size +=sizeof(WCHAR);
2291
2292         if (!check_first)
2293         {
2294             TRACE("Setting value %s of %s\n",debugstr_w(deformated),
2295                             debugstr_w(uikey));
2296             RegSetValueExW(hkey, deformated, 0, type, value_data, size);
2297         }
2298         else
2299         {
2300             DWORD sz = 0;
2301             rc = RegQueryValueExW(hkey, deformated, NULL, NULL, NULL, &sz);
2302             if (rc == ERROR_SUCCESS || rc == ERROR_MORE_DATA)
2303             {
2304                 TRACE("value %s of %s checked already exists\n",
2305                                 debugstr_w(deformated), debugstr_w(uikey));
2306             }
2307             else
2308             {
2309                 TRACE("Checked and setting value %s of %s\n",
2310                                 debugstr_w(deformated), debugstr_w(uikey));
2311                 if (deformated || size)
2312                     RegSetValueExW(hkey, deformated, 0, type, value_data, size);
2313             }
2314         }
2315
2316         uirow = MSI_CreateRecord(3);
2317         MSI_RecordSetStringW(uirow,2,deformated);
2318         MSI_RecordSetStringW(uirow,1,uikey);
2319
2320         if (type == REG_SZ)
2321             MSI_RecordSetStringW(uirow,3,(LPWSTR)value_data);
2322         else
2323             MSI_RecordSetStringW(uirow,3,value);
2324
2325         ui_actiondata(package,szWriteRegistryValues,uirow);
2326         msiobj_release( &uirow->hdr );
2327
2328         HeapFree(GetProcessHeap(),0,value_data);
2329         HeapFree(GetProcessHeap(),0,deformated);
2330
2331         msiobj_release(&row->hdr);
2332         RegCloseKey(hkey);
2333 next:
2334         HeapFree(GetProcessHeap(),0,uikey);
2335     }
2336     MSI_ViewClose(view);
2337     msiobj_release(&view->hdr);
2338     return rc;
2339 }
2340
2341 static UINT ACTION_InstallInitialize(MSIPACKAGE *package)
2342 {
2343     package->script->CurrentlyScripting = TRUE;
2344
2345     return ERROR_SUCCESS;
2346 }
2347
2348
2349 static UINT ACTION_InstallValidate(MSIPACKAGE *package)
2350 {
2351     DWORD progress = 0;
2352     DWORD total = 0;
2353     static const WCHAR q1[]=
2354         {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
2355          '`','R','e','g','i','s','t','r','y','`',0};
2356     UINT rc;
2357     MSIQUERY * view;
2358     MSIRECORD * row = 0;
2359     int i;
2360
2361     TRACE(" InstallValidate \n");
2362
2363     rc = MSI_DatabaseOpenViewW(package->db, q1, &view);
2364     if (rc != ERROR_SUCCESS)
2365         return ERROR_SUCCESS;
2366
2367     rc = MSI_ViewExecute(view, 0);
2368     if (rc != ERROR_SUCCESS)
2369     {
2370         MSI_ViewClose(view);
2371         msiobj_release(&view->hdr);
2372         return rc;
2373     }
2374     while (1)
2375     {
2376         rc = MSI_ViewFetch(view,&row);
2377         if (rc != ERROR_SUCCESS)
2378         {
2379             rc = ERROR_SUCCESS;
2380             break;
2381         }
2382         progress +=1;
2383
2384         msiobj_release(&row->hdr);
2385     }
2386     MSI_ViewClose(view);
2387     msiobj_release(&view->hdr);
2388
2389     total = total + progress * REG_PROGRESS_VALUE;
2390     total = total + package->loaded_components * COMPONENT_PROGRESS_VALUE;
2391     for (i=0; i < package->loaded_files; i++)
2392         total += package->files[i].FileSize;
2393     ui_progress(package,0,total,0,0);
2394
2395     for(i = 0; i < package->loaded_features; i++)
2396     {
2397         MSIFEATURE* feature = &package->features[i];
2398         TRACE("Feature: %s; Installed: %i; Action %i; Request %i\n",
2399             debugstr_w(feature->Feature), feature->Installed, feature->Action,
2400             feature->ActionRequest);
2401     }
2402     
2403     return ERROR_SUCCESS;
2404 }
2405
2406 static UINT ACTION_LaunchConditions(MSIPACKAGE *package)
2407 {
2408     UINT rc;
2409     MSIQUERY * view = NULL;
2410     MSIRECORD * row = 0;
2411     static const WCHAR ExecSeqQuery[] =
2412         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2413          '`','L','a','u','n','c','h','C','o','n','d','i','t','i','o','n','`',0};
2414     static const WCHAR title[]=
2415         {'I','n','s','t','a','l','l',' ','F','a', 'i','l','e','d',0};
2416
2417     TRACE("Checking launch conditions\n");
2418
2419     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
2420     if (rc != ERROR_SUCCESS)
2421         return ERROR_SUCCESS;
2422
2423     rc = MSI_ViewExecute(view, 0);
2424     if (rc != ERROR_SUCCESS)
2425     {
2426         MSI_ViewClose(view);
2427         msiobj_release(&view->hdr);
2428         return rc;
2429     }
2430
2431     rc = ERROR_SUCCESS;
2432     while (rc == ERROR_SUCCESS)
2433     {
2434         LPCWSTR cond = NULL; 
2435         LPCWSTR message = NULL;
2436
2437         rc = MSI_ViewFetch(view,&row);
2438         if (rc != ERROR_SUCCESS)
2439         {
2440             rc = ERROR_SUCCESS;
2441             break;
2442         }
2443
2444         cond = MSI_RecordGetString(row,1);
2445
2446         if (MSI_EvaluateConditionW(package,cond) != MSICONDITION_TRUE)
2447         {
2448             LPWSTR deformated;
2449             message = MSI_RecordGetString(row,2);
2450             deformat_string(package,message,&deformated); 
2451             MessageBoxW(NULL,deformated,title,MB_OK);
2452             HeapFree(GetProcessHeap(),0,deformated);
2453             rc = ERROR_FUNCTION_FAILED;
2454         }
2455         msiobj_release(&row->hdr);
2456     }
2457     MSI_ViewClose(view);
2458     msiobj_release(&view->hdr);
2459     return rc;
2460 }
2461
2462 static LPWSTR resolve_keypath( MSIPACKAGE* package, INT
2463                             component_index)
2464 {
2465     MSICOMPONENT* cmp = &package->components[component_index];
2466
2467     if (cmp->KeyPath[0]==0)
2468     {
2469         LPWSTR p = resolve_folder(package,cmp->Directory,FALSE,FALSE,NULL);
2470         return p;
2471     }
2472     if (cmp->Attributes & msidbComponentAttributesRegistryKeyPath)
2473     {
2474         MSIRECORD * row = 0;
2475         UINT root,len;
2476         LPWSTR deformated,buffer,deformated_name;
2477         LPCWSTR key,name;
2478         static const WCHAR ExecSeqQuery[] =
2479             {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2480              '`','R','e','g','i','s','t','r','y','`',' ',
2481              'W','H','E','R','E',' ', '`','R','e','g','i','s','t','r','y','`',
2482              ' ','=',' ' ,'\'','%','s','\'',0 };
2483         static const WCHAR fmt[]={'%','0','2','i',':','\\','%','s','\\',0};
2484         static const WCHAR fmt2[]=
2485             {'%','0','2','i',':','\\','%','s','\\','%','s',0};
2486
2487         row = MSI_QueryGetRecord(package->db, ExecSeqQuery,cmp->KeyPath);
2488         if (!row)
2489             return NULL;
2490
2491         root = MSI_RecordGetInteger(row,2);
2492         key = MSI_RecordGetString(row, 3);
2493         name = MSI_RecordGetString(row, 4);
2494         deformat_string(package, key , &deformated);
2495         deformat_string(package, name, &deformated_name);
2496
2497         len = strlenW(deformated) + 6;
2498         if (deformated_name)
2499             len+=strlenW(deformated_name);
2500
2501         buffer = HeapAlloc(GetProcessHeap(),0, len *sizeof(WCHAR));
2502
2503         if (deformated_name)
2504             sprintfW(buffer,fmt2,root,deformated,deformated_name);
2505         else
2506             sprintfW(buffer,fmt,root,deformated);
2507
2508         HeapFree(GetProcessHeap(),0,deformated);
2509         HeapFree(GetProcessHeap(),0,deformated_name);
2510         msiobj_release(&row->hdr);
2511
2512         return buffer;
2513     }
2514     else if (cmp->Attributes & msidbComponentAttributesODBCDataSource)
2515     {
2516         FIXME("UNIMPLEMENTED keypath as ODBC Source\n");
2517         return NULL;
2518     }
2519     else
2520     {
2521         int j;
2522         j = get_loaded_file(package,cmp->KeyPath);
2523
2524         if (j>=0)
2525         {
2526             LPWSTR p = strdupW(package->files[j].TargetPath);
2527             return p;
2528         }
2529     }
2530     return NULL;
2531 }
2532
2533 static HKEY openSharedDLLsKey(void)
2534 {
2535     HKEY hkey=0;
2536     static const WCHAR path[] =
2537         {'S','o','f','t','w','a','r','e','\\',
2538          'M','i','c','r','o','s','o','f','t','\\',
2539          'W','i','n','d','o','w','s','\\',
2540          'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2541          'S','h','a','r','e','d','D','L','L','s',0};
2542
2543     RegCreateKeyW(HKEY_LOCAL_MACHINE,path,&hkey);
2544     return hkey;
2545 }
2546
2547 static UINT ACTION_GetSharedDLLsCount(LPCWSTR dll)
2548 {
2549     HKEY hkey;
2550     DWORD count=0;
2551     DWORD type;
2552     DWORD sz = sizeof(count);
2553     DWORD rc;
2554     
2555     hkey = openSharedDLLsKey();
2556     rc = RegQueryValueExW(hkey, dll, NULL, &type, (LPBYTE)&count, &sz);
2557     if (rc != ERROR_SUCCESS)
2558         count = 0;
2559     RegCloseKey(hkey);
2560     return count;
2561 }
2562
2563 static UINT ACTION_WriteSharedDLLsCount(LPCWSTR path, UINT count)
2564 {
2565     HKEY hkey;
2566
2567     hkey = openSharedDLLsKey();
2568     if (count > 0)
2569         RegSetValueExW(hkey,path,0,REG_DWORD,
2570                     (LPBYTE)&count,sizeof(count));
2571     else
2572         RegDeleteValueW(hkey,path);
2573     RegCloseKey(hkey);
2574     return count;
2575 }
2576
2577 /*
2578  * Return TRUE if the count should be written out and FALSE if not
2579  */
2580 static void ACTION_RefCountComponent( MSIPACKAGE* package, UINT index)
2581 {
2582     INT count = 0;
2583     BOOL write = FALSE;
2584     INT j;
2585
2586     /* only refcount DLLs */
2587     if (package->components[index].KeyPath[0]==0 || 
2588         package->components[index].Attributes & 
2589             msidbComponentAttributesRegistryKeyPath || 
2590         package->components[index].Attributes & 
2591             msidbComponentAttributesODBCDataSource)
2592         write = FALSE;
2593     else
2594     {
2595         count = ACTION_GetSharedDLLsCount(package->components[index].
2596                         FullKeypath);
2597         write = (count > 0);
2598
2599         if (package->components[index].Attributes & 
2600                     msidbComponentAttributesSharedDllRefCount)
2601             write = TRUE;
2602     }
2603
2604     /* increment counts */
2605     for (j = 0; j < package->loaded_features; j++)
2606     {
2607         int i;
2608
2609         if (!ACTION_VerifyFeatureForAction(package,j,INSTALLSTATE_LOCAL))
2610             continue;
2611
2612         for (i = 0; i < package->features[j].ComponentCount; i++)
2613         {
2614             if (package->features[j].Components[i] == index)
2615                 count++;
2616         }
2617     }
2618     /* decrement counts */
2619     for (j = 0; j < package->loaded_features; j++)
2620     {
2621         int i;
2622         if (!ACTION_VerifyFeatureForAction(package,j,INSTALLSTATE_ABSENT))
2623             continue;
2624
2625         for (i = 0; i < package->features[j].ComponentCount; i++)
2626         {
2627             if (package->features[j].Components[i] == index)
2628                 count--;
2629         }
2630     }
2631
2632     /* ref count all the files in the component */
2633     if (write)
2634         for (j = 0; j < package->loaded_files; j++)
2635         {
2636             if (package->files[j].Temporary)
2637                 continue;
2638             if (package->files[j].ComponentIndex == index)
2639                 ACTION_WriteSharedDLLsCount(package->files[j].TargetPath,count);
2640         }
2641     
2642     /* add a count for permenent */
2643     if (package->components[index].Attributes &
2644                                 msidbComponentAttributesPermanent)
2645         count ++;
2646     
2647     package->components[index].RefCount = count;
2648
2649     if (write)
2650         ACTION_WriteSharedDLLsCount(package->components[index].FullKeypath,
2651             package->components[index].RefCount);
2652 }
2653
2654 /*
2655  * Ok further analysis makes me think that this work is
2656  * actually done in the PublishComponents and PublishFeatures
2657  * step, and not here.  It appears like the keypath and all that is
2658  * resolved in this step, however actually written in the Publish steps.
2659  * But we will leave it here for now because it is unclear
2660  */
2661 static UINT ACTION_ProcessComponents(MSIPACKAGE *package)
2662 {
2663     LPWSTR productcode;
2664     WCHAR squished_pc[GUID_SIZE];
2665     WCHAR squished_cc[GUID_SIZE];
2666     UINT rc;
2667     DWORD i;
2668     HKEY hkey=0,hkey2=0;
2669
2670     if (!package)
2671         return ERROR_INVALID_HANDLE;
2672
2673     /* writes the Component and Features values to the registry */
2674     productcode = load_dynamic_property(package,szProductCode,&rc);
2675     if (!productcode)
2676         return rc;
2677
2678     rc = MSIREG_OpenComponents(&hkey);
2679     if (rc != ERROR_SUCCESS)
2680         goto end;
2681       
2682     squash_guid(productcode,squished_pc);
2683     ui_progress(package,1,COMPONENT_PROGRESS_VALUE,1,0);
2684     for (i = 0; i < package->loaded_components; i++)
2685     {
2686         ui_progress(package,2,0,0,0);
2687         if (package->components[i].ComponentId[0]!=0)
2688         {
2689             WCHAR *keypath = NULL;
2690             MSIRECORD * uirow;
2691
2692             squash_guid(package->components[i].ComponentId,squished_cc);
2693            
2694             keypath = resolve_keypath(package,i);
2695             package->components[i].FullKeypath = keypath;
2696
2697             /* do the refcounting */
2698             ACTION_RefCountComponent( package, i);
2699
2700             TRACE("Component %s (%s), Keypath=%s, RefCount=%i\n", 
2701                             debugstr_w(package->components[i].Component),
2702                             debugstr_w(squished_cc),
2703                             debugstr_w(package->components[i].FullKeypath), 
2704                             package->components[i].RefCount);
2705             /*
2706             * Write the keypath out if the component is to be registered
2707             * and delete the key if the component is to be deregistered
2708             */
2709             if (ACTION_VerifyComponentForAction(package, i,
2710                                     INSTALLSTATE_LOCAL))
2711             {
2712                 rc = RegCreateKeyW(hkey,squished_cc,&hkey2);
2713                 if (rc != ERROR_SUCCESS)
2714                     continue;
2715
2716                 if (keypath)
2717                 {
2718                     RegSetValueExW(hkey2,squished_pc,0,REG_SZ,(LPVOID)keypath,
2719                                 (strlenW(keypath)+1)*sizeof(WCHAR));
2720
2721                     if (package->components[i].Attributes & 
2722                                 msidbComponentAttributesPermanent)
2723                     {
2724                         static const WCHAR szPermKey[] =
2725                             { '0','0','0','0','0','0','0','0','0','0','0','0',
2726                               '0','0','0','0','0','0','0', '0','0','0','0','0',
2727                               '0','0','0','0','0','0','0','0',0};
2728
2729                         RegSetValueExW(hkey2,szPermKey,0,REG_SZ,
2730                                         (LPVOID)keypath,
2731                                         (strlenW(keypath)+1)*sizeof(WCHAR));
2732                     }
2733                     
2734                     RegCloseKey(hkey2);
2735         
2736                     /* UI stuff */
2737                     uirow = MSI_CreateRecord(3);
2738                     MSI_RecordSetStringW(uirow,1,productcode);
2739                     MSI_RecordSetStringW(uirow,2,package->components[i].
2740                                                             ComponentId);
2741                     MSI_RecordSetStringW(uirow,3,keypath);
2742                     ui_actiondata(package,szProcessComponents,uirow);
2743                     msiobj_release( &uirow->hdr );
2744                }
2745             }
2746             else if (ACTION_VerifyComponentForAction(package, i,
2747                                     INSTALLSTATE_ABSENT))
2748             {
2749                 DWORD res;
2750
2751                 rc = RegOpenKeyW(hkey,squished_cc,&hkey2);
2752                 if (rc != ERROR_SUCCESS)
2753                     continue;
2754
2755                 RegDeleteValueW(hkey2,squished_pc);
2756
2757                 /* if the key is empty delete it */
2758                 res = RegEnumKeyExW(hkey2,0,NULL,0,0,NULL,0,NULL);
2759                 RegCloseKey(hkey2);
2760                 if (res == ERROR_NO_MORE_ITEMS)
2761                     RegDeleteKeyW(hkey,squished_cc);
2762         
2763                 /* UI stuff */
2764                 uirow = MSI_CreateRecord(2);
2765                 MSI_RecordSetStringW(uirow,1,productcode);
2766                 MSI_RecordSetStringW(uirow,2,package->components[i].
2767                                 ComponentId);
2768                 ui_actiondata(package,szProcessComponents,uirow);
2769                 msiobj_release( &uirow->hdr );
2770             }
2771         }
2772     } 
2773 end:
2774     HeapFree(GetProcessHeap(), 0, productcode);
2775     RegCloseKey(hkey);
2776     return rc;
2777 }
2778
2779 typedef struct {
2780     CLSID       clsid;
2781     LPWSTR      source;
2782
2783     LPWSTR      path;
2784     ITypeLib    *ptLib;
2785 } typelib_struct;
2786
2787 static BOOL CALLBACK Typelib_EnumResNameProc( HMODULE hModule, LPCWSTR lpszType, 
2788                                        LPWSTR lpszName, LONG_PTR lParam)
2789 {
2790     TLIBATTR *attr;
2791     typelib_struct *tl_struct = (typelib_struct*) lParam;
2792     static const WCHAR fmt[] = {'%','s','\\','%','i',0};
2793     int sz; 
2794     HRESULT res;
2795
2796     if (!IS_INTRESOURCE(lpszName))
2797     {
2798         ERR("Not Int Resource Name %s\n",debugstr_w(lpszName));
2799         return TRUE;
2800     }
2801
2802     sz = strlenW(tl_struct->source)+4;
2803     sz *= sizeof(WCHAR);
2804
2805     if ((INT)lpszName == 1)
2806         tl_struct->path = strdupW(tl_struct->source);
2807     else
2808     {
2809         tl_struct->path = HeapAlloc(GetProcessHeap(),0,sz);
2810         sprintfW(tl_struct->path,fmt,tl_struct->source, lpszName);
2811     }
2812
2813     TRACE("trying %s\n", debugstr_w(tl_struct->path));
2814     res = LoadTypeLib(tl_struct->path,&tl_struct->ptLib);
2815     if (!SUCCEEDED(res))
2816     {
2817         HeapFree(GetProcessHeap(),0,tl_struct->path);
2818         tl_struct->path = NULL;
2819
2820         return TRUE;
2821     }
2822
2823     ITypeLib_GetLibAttr(tl_struct->ptLib, &attr);
2824     if (IsEqualGUID(&(tl_struct->clsid),&(attr->guid)))
2825     {
2826         ITypeLib_ReleaseTLibAttr(tl_struct->ptLib, attr);
2827         return FALSE;
2828     }
2829
2830     HeapFree(GetProcessHeap(),0,tl_struct->path);
2831     tl_struct->path = NULL;
2832
2833     ITypeLib_ReleaseTLibAttr(tl_struct->ptLib, attr);
2834     ITypeLib_Release(tl_struct->ptLib);
2835
2836     return TRUE;
2837 }
2838
2839 static UINT ACTION_RegisterTypeLibraries(MSIPACKAGE *package)
2840 {
2841     /* 
2842      * OK this is a bit confusing.. I am given a _Component key and I believe
2843      * that the file that is being registered as a type library is the "key file
2844      * of that component" which I interpret to mean "The file in the KeyPath of
2845      * that component".
2846      */
2847     UINT rc;
2848     MSIQUERY * view;
2849     MSIRECORD * row = 0;
2850     static const WCHAR Query[] =
2851         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2852          '`','T','y','p','e','L','i','b','`',0};
2853
2854     if (!package)
2855         return ERROR_INVALID_HANDLE;
2856
2857     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
2858     if (rc != ERROR_SUCCESS)
2859         return ERROR_SUCCESS;
2860
2861     rc = MSI_ViewExecute(view, 0);
2862     if (rc != ERROR_SUCCESS)
2863     {
2864         MSI_ViewClose(view);
2865         msiobj_release(&view->hdr);
2866         return rc;
2867     }
2868
2869     while (1)
2870     {   
2871         LPCWSTR component;
2872         INT index;
2873         typelib_struct tl_struct;
2874         HMODULE module;
2875         static const WCHAR szTYPELIB[] = {'T','Y','P','E','L','I','B',0};
2876
2877         rc = MSI_ViewFetch(view,&row);
2878         if (rc != ERROR_SUCCESS)
2879         {
2880             rc = ERROR_SUCCESS;
2881             break;
2882         }
2883
2884         component = MSI_RecordGetString(row,3);
2885         index = get_loaded_component(package,component);
2886         if (index < 0)
2887         {
2888             msiobj_release(&row->hdr);
2889             continue;
2890         }
2891
2892         if (!ACTION_VerifyComponentForAction(package, index,
2893                                 INSTALLSTATE_LOCAL))
2894         {
2895             TRACE("Skipping typelib reg due to disabled component\n");
2896             msiobj_release(&row->hdr);
2897
2898             package->components[index].Action =
2899                 package->components[index].Installed;
2900
2901             continue;
2902         }
2903
2904         package->components[index].Action = INSTALLSTATE_LOCAL;
2905
2906         index = get_loaded_file(package,package->components[index].KeyPath); 
2907    
2908         if (index < 0)
2909         {
2910             msiobj_release(&row->hdr);
2911             continue;
2912         }
2913
2914         module = LoadLibraryExW(package->files[index].TargetPath, NULL,
2915                         LOAD_LIBRARY_AS_DATAFILE);
2916         if (module != NULL)
2917         {
2918             LPWSTR guid;
2919             guid = load_dynamic_stringW(row,1);
2920             CLSIDFromString(guid, &tl_struct.clsid);
2921             HeapFree(GetProcessHeap(),0,guid);
2922             tl_struct.source = strdupW(package->files[index].TargetPath);
2923             tl_struct.path = NULL;
2924
2925             EnumResourceNamesW(module, szTYPELIB, Typelib_EnumResNameProc, 
2926                                (LONG_PTR)&tl_struct);
2927
2928             if (tl_struct.path != NULL)
2929             {
2930                 LPWSTR help = NULL;
2931                 LPCWSTR helpid;
2932                 HRESULT res;
2933
2934                 helpid = MSI_RecordGetString(row,6);
2935     
2936                 if (helpid)
2937                     help = resolve_folder(package,helpid,FALSE,FALSE,NULL);
2938                 res = RegisterTypeLib(tl_struct.ptLib,tl_struct.path,help);
2939                 HeapFree(GetProcessHeap(),0,help);
2940
2941                 if (!SUCCEEDED(res))
2942                     ERR("Failed to register type library %s\n", 
2943                             debugstr_w(tl_struct.path));
2944                 else
2945                 {
2946                     ui_actiondata(package,szRegisterTypeLibraries,row);
2947                 
2948                     TRACE("Registered %s\n", debugstr_w(tl_struct.path));
2949                 }
2950
2951                 ITypeLib_Release(tl_struct.ptLib);
2952                 HeapFree(GetProcessHeap(),0,tl_struct.path);
2953             }
2954             else
2955                 ERR("Failed to load type library %s\n", 
2956                     debugstr_w(tl_struct.source));
2957        
2958             FreeLibrary(module);
2959             HeapFree(GetProcessHeap(),0,tl_struct.source);
2960         }
2961         else
2962             ERR("Could not load file! %s\n",
2963                     debugstr_w(package->files[index].TargetPath));
2964         msiobj_release(&row->hdr);
2965     }
2966     MSI_ViewClose(view);
2967     msiobj_release(&view->hdr);
2968     return rc;
2969 }
2970
2971 static UINT ACTION_CreateShortcuts(MSIPACKAGE *package)
2972 {
2973     UINT rc;
2974     MSIQUERY * view;
2975     MSIRECORD * row = 0;
2976     static const WCHAR Query[] =
2977         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2978          '`','S','h','o','r','t','c','u','t','`',0};
2979     IShellLinkW *sl;
2980     IPersistFile *pf;
2981     HRESULT res;
2982
2983     if (!package)
2984         return ERROR_INVALID_HANDLE;
2985
2986     res = CoInitialize( NULL );
2987     if (FAILED (res))
2988     {
2989         ERR("CoInitialize failed\n");
2990         return ERROR_FUNCTION_FAILED;
2991     }
2992
2993     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
2994     if (rc != ERROR_SUCCESS)
2995         return ERROR_SUCCESS;
2996
2997     rc = MSI_ViewExecute(view, 0);
2998     if (rc != ERROR_SUCCESS)
2999     {
3000         MSI_ViewClose(view);
3001         msiobj_release(&view->hdr);
3002         return rc;
3003     }
3004
3005     while (1)
3006     {
3007         LPWSTR target_file, target_folder;
3008         LPCWSTR buffer;
3009         WCHAR filename[0x100];
3010         DWORD sz;
3011         DWORD index;
3012         static const WCHAR szlnk[]={'.','l','n','k',0};
3013
3014         rc = MSI_ViewFetch(view,&row);
3015         if (rc != ERROR_SUCCESS)
3016         {
3017             rc = ERROR_SUCCESS;
3018             break;
3019         }
3020         
3021         buffer = MSI_RecordGetString(row,4);
3022         index = get_loaded_component(package,buffer);
3023
3024         if (index < 0)
3025         {
3026             msiobj_release(&row->hdr);
3027             continue;
3028         }
3029
3030         if (!ACTION_VerifyComponentForAction(package, index,
3031                                 INSTALLSTATE_LOCAL))
3032         {
3033             TRACE("Skipping shortcut creation due to disabled component\n");
3034             msiobj_release(&row->hdr);
3035
3036             package->components[index].Action =
3037                 package->components[index].Installed;
3038
3039             continue;
3040         }
3041
3042         package->components[index].Action = INSTALLSTATE_LOCAL;
3043
3044         ui_actiondata(package,szCreateShortcuts,row);
3045
3046         res = CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
3047                               &IID_IShellLinkW, (LPVOID *) &sl );
3048
3049         if (FAILED(res))
3050         {
3051             ERR("Is IID_IShellLink\n");
3052             msiobj_release(&row->hdr);
3053             continue;
3054         }
3055
3056         res = IShellLinkW_QueryInterface( sl, &IID_IPersistFile,(LPVOID*) &pf );
3057         if( FAILED( res ) )
3058         {
3059             ERR("Is IID_IPersistFile\n");
3060             msiobj_release(&row->hdr);
3061             continue;
3062         }
3063
3064         buffer = MSI_RecordGetString(row,2);
3065         target_folder = resolve_folder(package, buffer,FALSE,FALSE,NULL);
3066
3067         /* may be needed because of a bug somehwere else */
3068         create_full_pathW(target_folder);
3069
3070         sz = 0x100;
3071         MSI_RecordGetStringW(row,3,filename,&sz);
3072         reduce_to_longfilename(filename);
3073         if (!strchrW(filename,'.') || strcmpiW(strchrW(filename,'.'),szlnk))
3074             strcatW(filename,szlnk);
3075         target_file = build_directory_name(2, target_folder, filename);
3076         HeapFree(GetProcessHeap(),0,target_folder);
3077
3078         buffer = MSI_RecordGetString(row,5);
3079         if (strchrW(buffer,'['))
3080         {
3081             LPWSTR deformated;
3082             deformat_string(package,buffer,&deformated);
3083             IShellLinkW_SetPath(sl,deformated);
3084             HeapFree(GetProcessHeap(),0,deformated);
3085         }
3086         else
3087         {
3088             LPWSTR keypath;
3089             FIXME("poorly handled shortcut format, advertised shortcut\n");
3090             keypath = strdupW(package->components[index].FullKeypath);
3091             IShellLinkW_SetPath(sl,keypath);
3092             HeapFree(GetProcessHeap(),0,keypath);
3093         }
3094
3095         if (!MSI_RecordIsNull(row,6))
3096         {
3097             LPWSTR deformated;
3098             buffer = MSI_RecordGetString(row,6);
3099             deformat_string(package,buffer,&deformated);
3100             IShellLinkW_SetArguments(sl,deformated);
3101             HeapFree(GetProcessHeap(),0,deformated);
3102         }
3103
3104         if (!MSI_RecordIsNull(row,7))
3105         {
3106             buffer = MSI_RecordGetString(row,7);
3107             IShellLinkW_SetDescription(sl,buffer);
3108         }
3109
3110         if (!MSI_RecordIsNull(row,8))
3111             IShellLinkW_SetHotkey(sl,MSI_RecordGetInteger(row,8));
3112
3113         if (!MSI_RecordIsNull(row,9))
3114         {
3115             WCHAR *Path = NULL;
3116             INT index; 
3117
3118             buffer = MSI_RecordGetString(row,9);
3119
3120             build_icon_path(package,buffer,&Path);
3121             index = MSI_RecordGetInteger(row,10);
3122
3123             IShellLinkW_SetIconLocation(sl,Path,index);
3124             HeapFree(GetProcessHeap(),0,Path);
3125         }
3126
3127         if (!MSI_RecordIsNull(row,11))
3128             IShellLinkW_SetShowCmd(sl,MSI_RecordGetInteger(row,11));
3129
3130         if (!MSI_RecordIsNull(row,12))
3131         {
3132             LPWSTR Path;
3133             buffer = MSI_RecordGetString(row,12);
3134             Path = resolve_folder(package, buffer, FALSE, FALSE, NULL);
3135             IShellLinkW_SetWorkingDirectory(sl,Path);
3136             HeapFree(GetProcessHeap(), 0, Path);
3137         }
3138
3139         TRACE("Writing shortcut to %s\n",debugstr_w(target_file));
3140         IPersistFile_Save(pf,target_file,FALSE);
3141     
3142         HeapFree(GetProcessHeap(),0,target_file);    
3143
3144         IPersistFile_Release( pf );
3145         IShellLinkW_Release( sl );
3146
3147         msiobj_release(&row->hdr);
3148     }
3149     MSI_ViewClose(view);
3150     msiobj_release(&view->hdr);
3151
3152
3153     CoUninitialize();
3154
3155     return rc;
3156 }
3157
3158
3159 /*
3160  * 99% of the work done here is only done for 
3161  * advertised installs. However this is where the
3162  * Icon table is processed and written out
3163  * so that is what I am going to do here.
3164  */
3165 static UINT ACTION_PublishProduct(MSIPACKAGE *package)
3166 {
3167     UINT rc;
3168     MSIQUERY * view;
3169     MSIRECORD * row = 0;
3170     static const WCHAR Query[]=
3171         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
3172          '`','I','c','o','n','`',0};
3173     DWORD sz;
3174     /* for registry stuff */
3175     LPWSTR productcode;
3176     HKEY hkey=0;
3177     HKEY hukey=0;
3178     static const WCHAR szProductName[] =
3179         {'P','r','o','d','u','c','t','N','a','m','e',0};
3180     static const WCHAR szPackageCode[] =
3181         {'P','a','c','k','a','g','e','C','o','d','e',0};
3182     static const WCHAR szLanguage[] =
3183         {'L','a','n','g','u','a','g','e',0};
3184     static const WCHAR szProductLanguage[] =
3185         {'P','r','o','d','u','c','t','L','a','n','g','u','a','g','e',0};
3186     static const WCHAR szProductIcon[] =
3187         {'P','r','o','d','u','c','t','I','c','o','n',0};
3188     static const WCHAR szARPProductIcon[] =
3189         {'A','R','P','P','R','O','D','U','C','T','I','C','O','N',0};
3190     static const WCHAR szProductVersion[] =
3191         {'P','r','o','d','u','c','t','V','e','r','s','i','o','n',0};
3192     static const WCHAR szVersion[] =
3193         {'V','e','r','s','i','o','n',0};
3194     DWORD langid;
3195     LPWSTR buffer;
3196     DWORD size;
3197     MSIHANDLE hDb, hSumInfo;
3198
3199     if (!package)
3200         return ERROR_INVALID_HANDLE;
3201
3202     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
3203     if (rc != ERROR_SUCCESS)
3204         goto next;
3205
3206     rc = MSI_ViewExecute(view, 0);
3207     if (rc != ERROR_SUCCESS)
3208     {
3209         MSI_ViewClose(view);
3210         msiobj_release(&view->hdr);
3211         goto next;
3212     }
3213
3214     while (1)
3215     {
3216         HANDLE the_file;
3217         LPWSTR FilePath=NULL;
3218         LPCWSTR FileName=NULL;
3219         CHAR buffer[1024];
3220
3221         rc = MSI_ViewFetch(view,&row);
3222         if (rc != ERROR_SUCCESS)
3223         {
3224             rc = ERROR_SUCCESS;
3225             break;
3226         }
3227     
3228         FileName = MSI_RecordGetString(row,1);
3229         if (!FileName)
3230         {
3231             ERR("Unable to get FileName\n");
3232             msiobj_release(&row->hdr);
3233             continue;
3234         }
3235
3236         build_icon_path(package,FileName,&FilePath);
3237
3238         TRACE("Creating icon file at %s\n",debugstr_w(FilePath));
3239         
3240         the_file = CreateFileW(FilePath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
3241                            FILE_ATTRIBUTE_NORMAL, NULL);
3242
3243         if (the_file == INVALID_HANDLE_VALUE)
3244         {
3245             ERR("Unable to create file %s\n",debugstr_w(FilePath));
3246             msiobj_release(&row->hdr);
3247             HeapFree(GetProcessHeap(),0,FilePath);
3248             continue;
3249         }
3250
3251         do 
3252         {
3253             DWORD write;
3254             sz = 1024;
3255             rc = MSI_RecordReadStream(row,2,buffer,&sz);
3256             if (rc != ERROR_SUCCESS)
3257             {
3258                 ERR("Failed to get stream\n");
3259                 CloseHandle(the_file);  
3260                 DeleteFileW(FilePath);
3261                 break;
3262             }
3263             WriteFile(the_file,buffer,sz,&write,NULL);
3264         } while (sz == 1024);
3265
3266         HeapFree(GetProcessHeap(),0,FilePath);
3267
3268         CloseHandle(the_file);
3269         msiobj_release(&row->hdr);
3270     }
3271     MSI_ViewClose(view);
3272     msiobj_release(&view->hdr);
3273
3274 next:
3275     /* ok there is a lot more done here but i need to figure out what */
3276     productcode = load_dynamic_property(package,szProductCode,&rc);
3277     if (!productcode)
3278         return rc;
3279
3280     rc = MSIREG_OpenProductsKey(productcode,&hkey,TRUE);
3281     if (rc != ERROR_SUCCESS)
3282         goto end;
3283
3284     rc = MSIREG_OpenUserProductsKey(productcode,&hukey,TRUE);
3285     if (rc != ERROR_SUCCESS)
3286         goto end;
3287
3288
3289     buffer = load_dynamic_property(package,szProductName,NULL);
3290     size = strlenW(buffer)*sizeof(WCHAR);
3291     RegSetValueExW(hukey,szProductName,0,REG_SZ, (BYTE *)buffer,size);
3292     HeapFree(GetProcessHeap(),0,buffer);
3293
3294     buffer = load_dynamic_property(package,szProductLanguage,NULL);
3295     size = sizeof(DWORD);
3296     langid = atoiW(buffer);
3297     RegSetValueExW(hukey,szLanguage,0,REG_DWORD, (BYTE *)&langid,size);
3298     HeapFree(GetProcessHeap(),0,buffer);
3299
3300     buffer = load_dynamic_property(package,szARPProductIcon,NULL);
3301     if (buffer)
3302     {
3303         LPWSTR path;
3304         build_icon_path(package,buffer,&path);
3305         size = strlenW(path) * sizeof(WCHAR);
3306         RegSetValueExW(hukey,szProductIcon,0,REG_SZ, (BYTE *)path,size);
3307     }
3308     HeapFree(GetProcessHeap(),0,buffer);
3309
3310     buffer = load_dynamic_property(package,szProductVersion,NULL);
3311     if (buffer)
3312     {
3313         DWORD verdword = build_version_dword(buffer);
3314         size = sizeof(DWORD);
3315         RegSetValueExW(hukey,szVersion,0,REG_DWORD, (BYTE *)&verdword,size);
3316     }
3317     HeapFree(GetProcessHeap(),0,buffer);
3318     
3319     FIXME("Need to write more keys to the user registry\n");
3320   
3321     hDb= alloc_msihandle( &package->db->hdr );
3322     rc = MsiGetSummaryInformationW(hDb, NULL, 0, &hSumInfo); 
3323     MsiCloseHandle(hDb);
3324     if (rc == ERROR_SUCCESS)
3325     {
3326         WCHAR guidbuffer[0x200];
3327         size = 0x200;
3328         rc = MsiSummaryInfoGetPropertyW(hSumInfo, 9, NULL, NULL, NULL,
3329                                         guidbuffer, &size);
3330         if (rc == ERROR_SUCCESS)
3331         {
3332             WCHAR squashed[GUID_SIZE];
3333             /* for now we only care about the first guid */
3334             LPWSTR ptr = strchrW(guidbuffer,';');
3335             if (ptr) *ptr = 0;
3336             squash_guid(guidbuffer,squashed);
3337             size = strlenW(squashed)*sizeof(WCHAR);
3338             RegSetValueExW(hukey,szPackageCode,0,REG_SZ, (LPSTR)squashed,
3339                            size);
3340         }
3341         else
3342         {
3343             ERR("Unable to query Revision_Number... \n");
3344             rc = ERROR_SUCCESS;
3345         }
3346         MsiCloseHandle(hSumInfo);
3347     }
3348     else
3349     {
3350         ERR("Unable to open Summary Information\n");
3351         rc = ERROR_SUCCESS;
3352     }
3353
3354 end:
3355
3356     HeapFree(GetProcessHeap(),0,productcode);    
3357     RegCloseKey(hkey);
3358     RegCloseKey(hukey);
3359
3360     return rc;
3361 }
3362
3363 static UINT ACTION_WriteIniValues(MSIPACKAGE *package)
3364 {
3365     UINT rc;
3366     MSIQUERY * view;
3367     MSIRECORD * row = 0;
3368     static const WCHAR ExecSeqQuery[] = 
3369         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
3370          '`','I','n','i','F','i','l','e','`',0};
3371     static const WCHAR szWindowsFolder[] =
3372           {'W','i','n','d','o','w','s','F','o','l','d','e','r',0};
3373
3374     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
3375     if (rc != ERROR_SUCCESS)
3376     {
3377         TRACE("no IniFile table\n");
3378         return ERROR_SUCCESS;
3379     }
3380
3381     rc = MSI_ViewExecute(view, 0);
3382     if (rc != ERROR_SUCCESS)
3383     {
3384         MSI_ViewClose(view);
3385         msiobj_release(&view->hdr);
3386         return rc;
3387     }
3388
3389     while (1)
3390     {
3391         LPCWSTR component,section,key,value,identifier,filename,dirproperty;
3392         LPWSTR deformated_section, deformated_key, deformated_value;
3393         LPWSTR folder, fullname = NULL;
3394         MSIRECORD * uirow;
3395         INT component_index,action;
3396
3397         rc = MSI_ViewFetch(view,&row);
3398         if (rc != ERROR_SUCCESS)
3399         {
3400             rc = ERROR_SUCCESS;
3401             break;
3402         }
3403
3404         component = MSI_RecordGetString(row, 8);
3405         component_index = get_loaded_component(package,component);
3406
3407         if (!ACTION_VerifyComponentForAction(package, component_index,
3408                                 INSTALLSTATE_LOCAL))
3409         {
3410             TRACE("Skipping ini file due to disabled component %s\n",
3411                             debugstr_w(component));
3412             msiobj_release(&row->hdr);
3413
3414             package->components[component_index].Action =
3415                 package->components[component_index].Installed;
3416
3417             continue;
3418         }
3419
3420         package->components[component_index].Action = INSTALLSTATE_LOCAL;
3421    
3422         identifier = MSI_RecordGetString(row,1); 
3423         filename = MSI_RecordGetString(row,2);
3424         dirproperty = MSI_RecordGetString(row,3);
3425         section = MSI_RecordGetString(row,4);
3426         key = MSI_RecordGetString(row,5);
3427         value = MSI_RecordGetString(row,6);
3428         action = MSI_RecordGetInteger(row,7);
3429
3430         deformat_string(package,section,&deformated_section);
3431         deformat_string(package,key,&deformated_key);
3432         deformat_string(package,value,&deformated_value);
3433
3434         if (dirproperty)
3435         {
3436             folder = resolve_folder(package, dirproperty, FALSE, FALSE, NULL);
3437             if (!folder)
3438                 folder = load_dynamic_property(package,dirproperty,NULL);
3439         }
3440         else
3441             folder = load_dynamic_property(package, szWindowsFolder, NULL);
3442
3443         if (!folder)
3444         {
3445             ERR("Unable to resolve folder! (%s)\n",debugstr_w(dirproperty));
3446             goto cleanup;
3447         }
3448
3449         fullname = build_directory_name(3, folder, filename, NULL);
3450
3451         if (action == 0)
3452         {
3453             TRACE("Adding value %s to section %s in %s\n",
3454                 debugstr_w(deformated_key), debugstr_w(deformated_section),
3455                 debugstr_w(fullname));
3456             WritePrivateProfileStringW(deformated_section, deformated_key,
3457                                        deformated_value, fullname);
3458         }
3459         else if (action == 1)
3460         {
3461             WCHAR returned[10];
3462             GetPrivateProfileStringW(deformated_section, deformated_key, NULL,
3463                                      returned, 10, fullname);
3464             if (returned[0] == 0)
3465             {
3466                 TRACE("Adding value %s to section %s in %s\n",
3467                     debugstr_w(deformated_key), debugstr_w(deformated_section),
3468                     debugstr_w(fullname));
3469
3470                 WritePrivateProfileStringW(deformated_section, deformated_key,
3471                                        deformated_value, fullname);
3472             }
3473         }
3474         else if (action == 3)
3475         {
3476             FIXME("Append to existing section not yet implemented\n");
3477         }
3478         
3479         uirow = MSI_CreateRecord(4);
3480         MSI_RecordSetStringW(uirow,1,identifier);
3481         MSI_RecordSetStringW(uirow,2,deformated_section);
3482         MSI_RecordSetStringW(uirow,3,deformated_key);
3483         MSI_RecordSetStringW(uirow,4,deformated_value);
3484         ui_actiondata(package,szWriteIniValues,uirow);
3485         msiobj_release( &uirow->hdr );
3486 cleanup:
3487         HeapFree(GetProcessHeap(),0,fullname);
3488         HeapFree(GetProcessHeap(),0,folder);
3489         HeapFree(GetProcessHeap(),0,deformated_key);
3490         HeapFree(GetProcessHeap(),0,deformated_value);
3491         HeapFree(GetProcessHeap(),0,deformated_section);
3492         msiobj_release(&row->hdr);
3493     }
3494     MSI_ViewClose(view);
3495     msiobj_release(&view->hdr);
3496     return rc;
3497 }
3498
3499 static UINT ACTION_SelfRegModules(MSIPACKAGE *package)
3500 {
3501     UINT rc;
3502     MSIQUERY * view;
3503     MSIRECORD * row = 0;
3504     static const WCHAR ExecSeqQuery[] = 
3505         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
3506          '`','S','e','l','f','R','e','g','`',0};
3507
3508     static const WCHAR ExeStr[] =
3509         {'r','e','g','s','v','r','3','2','.','e','x','e',' ','\"',0};
3510     static const WCHAR close[] =  {'\"',0};
3511     STARTUPINFOW si;
3512     PROCESS_INFORMATION info;
3513     BOOL brc;
3514
3515     memset(&si,0,sizeof(STARTUPINFOW));
3516
3517     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
3518     if (rc != ERROR_SUCCESS)
3519     {
3520         TRACE("no SelfReg table\n");
3521         return ERROR_SUCCESS;
3522     }
3523
3524     rc = MSI_ViewExecute(view, 0);
3525     if (rc != ERROR_SUCCESS)
3526     {
3527         MSI_ViewClose(view);
3528         msiobj_release(&view->hdr);
3529         return rc;
3530     }
3531
3532     while (1)
3533     {
3534         LPCWSTR filename;
3535         LPWSTR FullName;
3536         INT index;
3537         DWORD len;
3538
3539         rc = MSI_ViewFetch(view,&row);
3540         if (rc != ERROR_SUCCESS)
3541         {
3542             rc = ERROR_SUCCESS;
3543             break;
3544         }
3545
3546         filename = MSI_RecordGetString(row,1);
3547         index = get_loaded_file(package,filename);
3548
3549         if (index < 0)
3550         {
3551             ERR("Unable to find file id %s\n",debugstr_w(filename));
3552             msiobj_release(&row->hdr);
3553             continue;
3554         }
3555
3556         len = strlenW(ExeStr);
3557         len += strlenW(package->files[index].TargetPath);
3558         len +=2;
3559
3560         FullName = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
3561         strcpyW(FullName,ExeStr);
3562         strcatW(FullName,package->files[index].TargetPath);
3563         strcatW(FullName,close);
3564
3565         TRACE("Registering %s\n",debugstr_w(FullName));
3566         brc = CreateProcessW(NULL, FullName, NULL, NULL, FALSE, 0, NULL,
3567                   c_colon, &si, &info);
3568
3569         if (brc)
3570             msi_dialog_check_messages(info.hProcess);
3571  
3572         HeapFree(GetProcessHeap(),0,FullName);
3573         msiobj_release(&row->hdr);
3574     }
3575     MSI_ViewClose(view);
3576     msiobj_release(&view->hdr);
3577     return rc;
3578 }
3579
3580 static UINT ACTION_PublishFeatures(MSIPACKAGE *package)
3581 {
3582     LPWSTR productcode;
3583     UINT rc;
3584     DWORD i;
3585     HKEY hkey=0;
3586     HKEY hukey=0;
3587     
3588     if (!package)
3589         return ERROR_INVALID_HANDLE;
3590
3591     productcode = load_dynamic_property(package,szProductCode,&rc);
3592     if (!productcode)
3593         return rc;
3594
3595     rc = MSIREG_OpenFeaturesKey(productcode,&hkey,TRUE);
3596     if (rc != ERROR_SUCCESS)
3597         goto end;
3598
3599     rc = MSIREG_OpenUserFeaturesKey(productcode,&hukey,TRUE);
3600     if (rc != ERROR_SUCCESS)
3601         goto end;
3602
3603     /* here the guids are base 85 encoded */
3604     for (i = 0; i < package->loaded_features; i++)
3605     {
3606         LPWSTR data = NULL;
3607         GUID clsid;
3608         int j;
3609         INT size;
3610         BOOL absent = FALSE;
3611
3612         if (!ACTION_VerifyFeatureForAction(package,i,INSTALLSTATE_LOCAL) &&
3613             !ACTION_VerifyFeatureForAction(package,i,INSTALLSTATE_SOURCE) &&
3614             !ACTION_VerifyFeatureForAction(package,i,INSTALLSTATE_ADVERTISED))
3615             absent = TRUE;
3616
3617         size = package->features[i].ComponentCount*21;
3618         size +=1;
3619         if (package->features[i].Feature_Parent[0])
3620             size += strlenW(package->features[i].Feature_Parent)+2;
3621
3622         data = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
3623
3624         data[0] = 0;
3625         for (j = 0; j < package->features[i].ComponentCount; j++)
3626         {
3627             WCHAR buf[21];
3628             memset(buf,0,sizeof(buf));
3629             if (package->components
3630                 [package->features[i].Components[j]].ComponentId[0]!=0)
3631             {
3632                 TRACE("From %s\n",debugstr_w(package->components
3633                             [package->features[i].Components[j]].ComponentId));
3634                 CLSIDFromString(package->components
3635                             [package->features[i].Components[j]].ComponentId,
3636                             &clsid);
3637                 encode_base85_guid(&clsid,buf);
3638                 TRACE("to %s\n",debugstr_w(buf));
3639                 strcatW(data,buf);
3640             }
3641         }
3642         if (package->features[i].Feature_Parent[0])
3643         {
3644             static const WCHAR sep[] = {'\2',0};
3645             strcatW(data,sep);
3646             strcatW(data,package->features[i].Feature_Parent);
3647         }
3648
3649         size = (strlenW(data)+1)*sizeof(WCHAR);
3650         RegSetValueExW(hkey,package->features[i].Feature,0,REG_SZ,
3651                        (LPSTR)data,size);
3652         HeapFree(GetProcessHeap(),0,data);
3653
3654         if (!absent)
3655         {
3656             size = strlenW(package->features[i].Feature_Parent)*sizeof(WCHAR);
3657             RegSetValueExW(hukey,package->features[i].Feature,0,REG_SZ,
3658                        (LPSTR)package->features[i].Feature_Parent,size);
3659         }
3660         else
3661         {
3662             size = (strlenW(package->features[i].Feature_Parent)+2)*
3663                     sizeof(WCHAR);
3664             data = HeapAlloc(GetProcessHeap(),0,size);
3665             data[0] = 0x6;
3666             strcpyW(&data[1],package->features[i].Feature_Parent);
3667             RegSetValueExW(hukey,package->features[i].Feature,0,REG_SZ,
3668                        (LPSTR)data,size);
3669             HeapFree(GetProcessHeap(),0,data);
3670         }
3671     }
3672
3673 end:
3674     RegCloseKey(hkey);
3675     RegCloseKey(hukey);
3676     HeapFree(GetProcessHeap(), 0, productcode);
3677     return rc;
3678 }
3679
3680 static UINT ACTION_RegisterProduct(MSIPACKAGE *package)
3681 {
3682     HKEY hkey=0;
3683     LPWSTR buffer = NULL;
3684     LPWSTR productcode;
3685     UINT rc,i;
3686     DWORD size;
3687     static WCHAR szNONE[] = {0};
3688     static const WCHAR szWindowsInstaler[] = 
3689     {'W','i','n','d','o','w','s','I','n','s','t','a','l','l','e','r',0};
3690     static const WCHAR szPropKeys[][80] = 
3691     {
3692 {'A','R','P','A','U','T','H','O','R','I','Z','E','D','C','D','F','P','R','E','F','I','X',0},
3693 {'A','R','P','C','O','N','T','A','C','T',0},
3694 {'A','R','P','C','O','M','M','E','N','T','S',0},
3695 {'P','r','o','d','u','c','t','N','a','m','e',0},
3696 {'P','r','o','d','u','c','t','V','e','r','s','i','o','n',0},
3697 {'A','R','P','H','E','L','P','L','I','N','K',0},
3698 {'A','R','P','H','E','L','P','T','E','L','E','P','H','O','N','E',0},
3699 {'A','R','P','I','N','S','T','A','L','L','L','O','C','A','T','I','O','N',0},
3700 {'S','o','u','r','c','e','D','i','r',0},
3701 {'M','a','n','u','f','a','c','t','u','r','e','r',0},
3702 {'A','R','P','R','E','A','D','M','E',0},
3703 {'A','R','P','S','I','Z','E',0},
3704 {'A','R','P','U','R','L','I','N','F','O','A','B','O','U','T',0},
3705 {'A','R','P','U','R','L','U','P','D','A','T','E','I','N','F','O',0},
3706 {0},
3707     };
3708
3709     static const WCHAR szRegKeys[][80] = 
3710     {
3711 {'A','u','t','h','o','r','i','z','e','d','C','D','F','P','r','e','f','i','x',0},
3712 {'C','o','n','t','a','c','t',0},
3713 {'C','o','m','m','e','n','t','s',0},
3714 {'D','i','s','p','l','a','y','N','a','m','e',0},
3715 {'D','i','s','p','l','a','y','V','e','r','s','i','o','n',0},
3716 {'H','e','l','p','L','i','n','k',0},
3717 {'H','e','l','p','T','e','l','e','p','h','o','n','e',0},
3718 {'I','n','s','t','a','l','l','L','o','c','a','t','i','o','n',0},
3719 {'I','n','s','t','a','l','l','S','o','u','r','c','e',0},
3720 {'P','u','b','l','i','s','h','e','r',0},
3721 {'R','e','a','d','m','e',0},
3722 {'S','i','z','e',0},
3723 {'U','R','L','I','n','f','o','A','b','o','u','t',0},
3724 {'U','R','L','U','p','d','a','t','e','I','n','f','o',0},
3725 {0},
3726     };
3727
3728     static const WCHAR installerPathFmt[] = {
3729     '%','s','\\',
3730     'I','n','s','t','a','l','l','e','r','\\',0};
3731     static const WCHAR fmt[] = {
3732     '%','s','\\',
3733     'I','n','s','t','a','l','l','e','r','\\',
3734     '%','x','.','m','s','i',0};
3735     static const WCHAR szLocalPackage[]=
3736          {'L','o','c','a','l','P','a','c','k','a','g','e',0};
3737     static const WCHAR szUpgradeCode[] = 
3738         {'U','p','g','r','a','d','e','C','o','d','e',0};
3739     static const WCHAR modpath_fmt[] = 
3740         {'M','s','i','E','x','e','c','.','e','x','e',' ','/','I','[','P','r','o','d','u','c','t','C','o','d','e',']',0};
3741     static const WCHAR szModifyPath[] = 
3742         {'M','o','d','i','f','y','P','a','t','h',0};
3743     static const WCHAR szUninstallString[] = 
3744         {'U','n','i','n','s','t','a','l','l','S','t','r','i','n','g',0};
3745     static const WCHAR szEstimatedSize[] = 
3746         {'E','s','t','i','m','a','t','e','d','S','i','z','e',0};
3747     static const WCHAR szInstallDate[] = 
3748         {'I','n','s','t','a','l','l','D','a','t','e',0};
3749     static const WCHAR szLanguage[] =
3750         {'L','a','n','g','u','a','g','e',0};
3751     static const WCHAR szProductLanguage[] =
3752         {'P','r','o','d','u','c','t','L','a','n','g','u','a','g','e',0};
3753     static const WCHAR szProductVersion[] =
3754         {'P','r','o','d','u','c','t','V','e','r','s','i','o','n',0};
3755     static const WCHAR szVersion[] =
3756         {'V','e','r','s','i','o','n',0};
3757     static const WCHAR szVersionMajor[] =
3758         {'V','e','r','s','i','o','n','M','a','j','o','r',0};
3759     static const WCHAR szVersionMinor[] =
3760         {'V','e','r','s','i','o','n','M','i','n','o','r',0};
3761
3762     SYSTEMTIME systime;
3763     static const WCHAR date_fmt[] = {'%','i','%','i','%','i',0};
3764     LPWSTR upgrade_code;
3765     WCHAR windir[MAX_PATH], path[MAX_PATH], packagefile[MAX_PATH];
3766     INT num,start;
3767
3768     if (!package)
3769         return ERROR_INVALID_HANDLE;
3770
3771     productcode = load_dynamic_property(package,szProductCode,&rc);
3772     if (!productcode)
3773         return rc;
3774
3775     rc = MSIREG_OpenUninstallKey(productcode,&hkey,TRUE);
3776     if (rc != ERROR_SUCCESS)
3777         goto end;
3778
3779     /* dump all the info i can grab */
3780     FIXME("Flesh out more information \n");
3781
3782     i = 0;
3783     while (szPropKeys[i][0]!=0)
3784     {
3785         buffer = load_dynamic_property(package,szPropKeys[i],&rc);
3786         if (rc != ERROR_SUCCESS)
3787             buffer = szNONE;
3788         size = strlenW(buffer)*sizeof(WCHAR);
3789         RegSetValueExW(hkey,szRegKeys[i],0,REG_SZ,(LPSTR)buffer,size);
3790         HeapFree(GetProcessHeap(),0,buffer);
3791         i++;
3792     }
3793
3794     rc = 0x1;
3795     size = sizeof(rc);
3796     RegSetValueExW(hkey,szWindowsInstaler,0,REG_DWORD,(LPSTR)&rc,size);
3797     
3798     /* copy the package locally */
3799     num = GetTickCount() & 0xffff;
3800     if (!num) 
3801         num = 1;
3802     start = num;
3803     GetWindowsDirectoryW(windir, sizeof(windir) / sizeof(windir[0]));
3804     snprintfW(packagefile,sizeof(packagefile)/sizeof(packagefile[0]),fmt,
3805      windir,num);
3806     do 
3807     {
3808         HANDLE handle = CreateFileW(packagefile,GENERIC_WRITE, 0, NULL,
3809                                   CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
3810         if (handle != INVALID_HANDLE_VALUE)
3811         {
3812             CloseHandle(handle);
3813             break;
3814         }
3815         if (GetLastError() != ERROR_FILE_EXISTS &&
3816             GetLastError() != ERROR_SHARING_VIOLATION)
3817             break;
3818         if (!(++num & 0xffff)) num = 1;
3819         sprintfW(packagefile,fmt,num);
3820     } while (num != start);
3821
3822     snprintfW(path,sizeof(path)/sizeof(path[0]),installerPathFmt,windir);
3823     create_full_pathW(path);
3824     TRACE("Copying to local package %s\n",debugstr_w(packagefile));
3825     if (!CopyFileW(package->PackagePath,packagefile,FALSE))
3826         ERR("Unable to copy package (%s -> %s) (error %ld)\n",
3827             debugstr_w(package->PackagePath), debugstr_w(packagefile),
3828             GetLastError());
3829     size = strlenW(packagefile)*sizeof(WCHAR);
3830     RegSetValueExW(hkey,szLocalPackage,0,REG_SZ,(LPSTR)packagefile,size);
3831
3832     /* do ModifyPath and UninstallString */
3833     size = deformat_string(package,modpath_fmt,&buffer);
3834     RegSetValueExW(hkey,szModifyPath,0,REG_EXPAND_SZ,(LPSTR)buffer,size);
3835     RegSetValueExW(hkey,szUninstallString,0,REG_EXPAND_SZ,(LPSTR)buffer,size);
3836     HeapFree(GetProcessHeap(),0,buffer);
3837
3838     FIXME("Write real Estimated Size when we have it\n");
3839     size = 0;
3840     RegSetValueExW(hkey,szEstimatedSize,0,REG_DWORD,(LPSTR)&size,sizeof(DWORD));
3841    
3842     GetLocalTime(&systime);
3843     size = 9*sizeof(WCHAR);
3844     buffer= HeapAlloc(GetProcessHeap(),0,size);
3845     sprintfW(buffer,date_fmt,systime.wYear,systime.wMonth,systime.wDay);
3846     size = strlenW(buffer)*sizeof(WCHAR);
3847     RegSetValueExW(hkey,szInstallDate,0,REG_SZ,(LPSTR)buffer,size);
3848     HeapFree(GetProcessHeap(),0,buffer);
3849    
3850     buffer = load_dynamic_property(package,szProductLanguage,NULL);
3851     size = atoiW(buffer);
3852     RegSetValueExW(hkey,szLanguage,0,REG_DWORD, (LPSTR)&size,sizeof(DWORD));
3853     HeapFree(GetProcessHeap(),1,buffer);
3854
3855     buffer = load_dynamic_property(package,szProductVersion,NULL);
3856     if (buffer)
3857     {
3858         DWORD verdword = build_version_dword(buffer);
3859         DWORD vermajor = verdword>>24;
3860         DWORD verminor = (verdword>>16)&0x00FF;
3861         size = sizeof(DWORD);
3862         RegSetValueExW(hkey,szVersion,0,REG_DWORD,(LPSTR)&verdword,size);
3863         RegSetValueExW(hkey,szVersionMajor,0,REG_DWORD,(LPSTR)&vermajor,size);
3864         RegSetValueExW(hkey,szVersionMinor,0,REG_DWORD,(LPSTR)&verminor,size);
3865     }
3866     HeapFree(GetProcessHeap(),0,buffer);
3867     
3868     /* Handle Upgrade Codes */
3869     upgrade_code = load_dynamic_property(package,szUpgradeCode, NULL);
3870     if (upgrade_code)
3871     {
3872         HKEY hkey2;
3873         WCHAR squashed[33];
3874         MSIREG_OpenUpgradeCodesKey(upgrade_code, &hkey2, TRUE);
3875         squash_guid(productcode,squashed);
3876         RegSetValueExW(hkey2, squashed, 0,REG_SZ,NULL,0);
3877         RegCloseKey(hkey2);
3878         MSIREG_OpenUserUpgradeCodesKey(upgrade_code, &hkey2, TRUE);
3879         squash_guid(productcode,squashed);
3880         RegSetValueExW(hkey2, squashed, 0,REG_SZ,NULL,0);
3881         RegCloseKey(hkey2);
3882
3883         HeapFree(GetProcessHeap(),0,upgrade_code);
3884     }
3885     
3886 end:
3887     HeapFree(GetProcessHeap(),0,productcode);
3888     RegCloseKey(hkey);
3889
3890     return ERROR_SUCCESS;
3891 }
3892
3893 static UINT ACTION_InstallExecute(MSIPACKAGE *package)
3894 {
3895     UINT rc;
3896
3897     if (!package)
3898         return ERROR_INVALID_HANDLE;
3899
3900     rc = execute_script(package,INSTALL_SCRIPT);
3901
3902     return rc;
3903 }
3904
3905 static UINT ACTION_InstallFinalize(MSIPACKAGE *package)
3906 {
3907     UINT rc;
3908
3909     if (!package)
3910         return ERROR_INVALID_HANDLE;
3911
3912     /* turn off scheduleing */
3913     package->script->CurrentlyScripting= FALSE;
3914
3915     /* first do the same as an InstallExecute */
3916     rc = ACTION_InstallExecute(package);
3917     if (rc != ERROR_SUCCESS)
3918         return rc;
3919
3920     /* then handle Commit Actions */
3921     rc = execute_script(package,COMMIT_SCRIPT);
3922
3923     return rc;
3924 }
3925
3926 static UINT ACTION_ForceReboot(MSIPACKAGE *package)
3927 {
3928     static const WCHAR RunOnce[] = {
3929     'S','o','f','t','w','a','r','e','\\',
3930     'M','i','c','r','o','s','o','f','t','\\',
3931     'W','i','n','d','o','w','s','\\',
3932     'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
3933     'R','u','n','O','n','c','e',0};
3934     static const WCHAR InstallRunOnce[] = {
3935     'S','o','f','t','w','a','r','e','\\',
3936     'M','i','c','r','o','s','o','f','t','\\',
3937     'W','i','n','d','o','w','s','\\',
3938     'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
3939     'I','n','s','t','a','l','l','e','r','\\',
3940     'R','u','n','O','n','c','e','E','n','t','r','i','e','s',0};
3941
3942     static const WCHAR msiexec_fmt[] = {
3943     '%','s',
3944     '\\','M','s','i','E','x','e','c','.','e','x','e',' ','/','@',' ',
3945     '\"','%','s','\"',0};
3946     static const WCHAR install_fmt[] = {
3947     '/','I',' ','\"','%','s','\"',' ',
3948     'A','F','T','E','R','R','E','B','O','O','T','=','1',' ',
3949     'R','U','N','O','N','C','E','E','N','T','R','Y','=','\"','%','s','\"',0};
3950     WCHAR buffer[256], sysdir[MAX_PATH];
3951     HKEY hkey,hukey;
3952     LPWSTR productcode;
3953     WCHAR  squished_pc[100];
3954     INT rc;
3955     DWORD size;
3956     static const WCHAR szLUS[] = {
3957          'L','a','s','t','U','s','e','d','S','o','u','r','c','e',0};
3958     static const WCHAR szSourceList[] = {
3959          'S','o','u','r','c','e','L','i','s','t',0};
3960     static const WCHAR szPackageName[] = { 
3961         'P','a','c','k','a','g','e','N','a','m','e',0};
3962
3963     if (!package)
3964         return ERROR_INVALID_HANDLE;
3965
3966     productcode = load_dynamic_property(package,szProductCode,&rc);
3967     if (!productcode)
3968         return rc;
3969
3970     squash_guid(productcode,squished_pc);
3971
3972     GetSystemDirectoryW(sysdir, sizeof(sysdir)/sizeof(sysdir[0]));
3973     RegCreateKeyW(HKEY_LOCAL_MACHINE,RunOnce,&hkey);
3974     snprintfW(buffer,sizeof(buffer)/sizeof(buffer[0]),msiexec_fmt,sysdir,
3975      squished_pc);
3976
3977     size = strlenW(buffer)*sizeof(WCHAR);
3978     RegSetValueExW(hkey,squished_pc,0,REG_SZ,(LPSTR)buffer,size);
3979     RegCloseKey(hkey);
3980
3981     TRACE("Reboot command %s\n",debugstr_w(buffer));
3982
3983     RegCreateKeyW(HKEY_LOCAL_MACHINE,InstallRunOnce,&hkey);
3984     sprintfW(buffer,install_fmt,productcode,squished_pc);
3985
3986     size = strlenW(buffer)*sizeof(WCHAR);
3987     RegSetValueExW(hkey,squished_pc,0,REG_SZ,(LPSTR)buffer,size);
3988     RegCloseKey(hkey);
3989
3990     rc = MSIREG_OpenUserProductsKey(productcode,&hukey,TRUE);
3991     if (rc == ERROR_SUCCESS)
3992     {
3993         HKEY hukey2;
3994         LPWSTR buf;
3995         RegCreateKeyW(hukey, szSourceList, &hukey2);
3996         buf = load_dynamic_property(package,cszSourceDir,NULL);
3997         size = strlenW(buf)*sizeof(WCHAR);
3998         RegSetValueExW(hukey2,szLUS,0,REG_SZ,(LPSTR)buf,size);
3999         HeapFree(GetProcessHeap(),0,buf); 
4000
4001         buf = strrchrW(package->PackagePath,'\\');
4002         if (buf)
4003         {
4004             buf++;
4005             size = strlenW(buf)*sizeof(WCHAR);
4006             RegSetValueExW(hukey2,szPackageName,0,REG_SZ,(LPSTR)buf,size);
4007         }
4008
4009         RegCloseKey(hukey2);
4010     }
4011     HeapFree(GetProcessHeap(),0,productcode);
4012
4013     return ERROR_INSTALL_SUSPEND;
4014 }
4015
4016 UINT ACTION_ResolveSource(MSIPACKAGE* package)
4017 {
4018     /*
4019      * we are currently doing what should be done here in the top level Install
4020      * however for Adminastrative and uninstalls this step will be needed
4021      */
4022     return ERROR_SUCCESS;
4023 }
4024
4025 static UINT ACTION_RegisterUser(MSIPACKAGE *package)
4026 {
4027     static const WCHAR szProductID[]=
4028          {'P','r','o','d','u','c','t','I','D',0};
4029     HKEY hkey=0;
4030     LPWSTR buffer;
4031     LPWSTR productcode;
4032     LPWSTR productid;
4033     UINT rc,i;
4034     DWORD size;
4035
4036     static const WCHAR szPropKeys[][80] = 
4037     {
4038         {'P','r','o','d','u','c','t','I','D',0},
4039         {'U','S','E','R','N','A','M','E',0},
4040         {'C','O','M','P','A','N','Y','N','A','M','E',0},
4041         {0},
4042     };
4043
4044     static const WCHAR szRegKeys[][80] = 
4045     {
4046         {'P','r','o','d','u','c','t','I','D',0},
4047         {'R','e','g','O','w','n','e','r',0},
4048         {'R','e','g','C','o','m','p','a','n','y',0},
4049         {0},
4050     };
4051
4052     if (!package)
4053         return ERROR_INVALID_HANDLE;
4054
4055     productid = load_dynamic_property(package,szProductID,&rc);
4056     if (!productid)
4057         return ERROR_SUCCESS;
4058
4059     productcode = load_dynamic_property(package,szProductCode,&rc);
4060     if (!productcode)
4061         return rc;
4062
4063     rc = MSIREG_OpenUninstallKey(productcode,&hkey,TRUE);
4064     if (rc != ERROR_SUCCESS)
4065         goto end;
4066
4067     i = 0;
4068     while (szPropKeys[i][0]!=0)
4069     {
4070         buffer = load_dynamic_property(package,szPropKeys[i],&rc);
4071         if (rc == ERROR_SUCCESS)
4072         {
4073             size = strlenW(buffer)*sizeof(WCHAR);
4074             RegSetValueExW(hkey,szRegKeys[i],0,REG_SZ,(LPSTR)buffer,size);
4075         }
4076         else
4077             RegSetValueExW(hkey,szRegKeys[i],0,REG_SZ,NULL,0);
4078         i++;
4079     }
4080
4081 end:
4082     HeapFree(GetProcessHeap(),0,productcode);
4083     HeapFree(GetProcessHeap(),0,productid);
4084     RegCloseKey(hkey);
4085
4086     return ERROR_SUCCESS;
4087 }
4088
4089
4090 static UINT ACTION_ExecuteAction(MSIPACKAGE *package)
4091 {
4092     UINT rc;
4093     rc = ACTION_ProcessExecSequence(package,FALSE);
4094     return rc;
4095 }
4096
4097
4098 /*
4099  * Code based off of code located here
4100  * http://www.codeproject.com/gdi/fontnamefromfile.asp
4101  *
4102  * Using string index 4 (full font name) instead of 1 (family name)
4103  */
4104 static LPWSTR load_ttfname_from(LPCWSTR filename)
4105 {
4106     HANDLE handle;
4107     LPWSTR ret = NULL;
4108     int i;
4109
4110     typedef struct _tagTT_OFFSET_TABLE{
4111         USHORT uMajorVersion;
4112         USHORT uMinorVersion;
4113         USHORT uNumOfTables;
4114         USHORT uSearchRange;
4115         USHORT uEntrySelector;
4116         USHORT uRangeShift;
4117     }TT_OFFSET_TABLE;
4118
4119     typedef struct _tagTT_TABLE_DIRECTORY{
4120         char szTag[4]; /* table name */
4121         ULONG uCheckSum; /* Check sum */
4122         ULONG uOffset; /* Offset from beginning of file */
4123         ULONG uLength; /* length of the table in bytes */
4124     }TT_TABLE_DIRECTORY;
4125
4126     typedef struct _tagTT_NAME_TABLE_HEADER{
4127     USHORT uFSelector; /* format selector. Always 0 */
4128     USHORT uNRCount; /* Name Records count */
4129     USHORT uStorageOffset; /* Offset for strings storage, 
4130                             * from start of the table */
4131     }TT_NAME_TABLE_HEADER;
4132    
4133     typedef struct _tagTT_NAME_RECORD{
4134         USHORT uPlatformID;
4135         USHORT uEncodingID;
4136         USHORT uLanguageID;
4137         USHORT uNameID;
4138         USHORT uStringLength;
4139         USHORT uStringOffset; /* from start of storage area */
4140     }TT_NAME_RECORD;
4141
4142 #define SWAPWORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
4143 #define SWAPLONG(x) MAKELONG(SWAPWORD(HIWORD(x)), SWAPWORD(LOWORD(x)))
4144
4145     handle = CreateFileW(filename ,GENERIC_READ, 0, NULL, OPEN_EXISTING,
4146                     FILE_ATTRIBUTE_NORMAL, 0 );
4147     if (handle != INVALID_HANDLE_VALUE)
4148     {
4149         TT_TABLE_DIRECTORY tblDir;
4150         BOOL bFound = FALSE;
4151         TT_OFFSET_TABLE ttOffsetTable;
4152
4153         ReadFile(handle,&ttOffsetTable, sizeof(TT_OFFSET_TABLE),NULL,NULL);
4154         ttOffsetTable.uNumOfTables = SWAPWORD(ttOffsetTable.uNumOfTables);
4155         ttOffsetTable.uMajorVersion = SWAPWORD(ttOffsetTable.uMajorVersion);
4156         ttOffsetTable.uMinorVersion = SWAPWORD(ttOffsetTable.uMinorVersion);
4157         
4158         if (ttOffsetTable.uMajorVersion != 1 || 
4159                         ttOffsetTable.uMinorVersion != 0)
4160             return NULL;
4161
4162         for (i=0; i< ttOffsetTable.uNumOfTables; i++)
4163         {
4164             ReadFile(handle,&tblDir, sizeof(TT_TABLE_DIRECTORY),NULL,NULL);
4165             if (strncmp(tblDir.szTag,"name",4)==0)
4166             {
4167                 bFound = TRUE;
4168                 tblDir.uLength = SWAPLONG(tblDir.uLength);
4169                 tblDir.uOffset = SWAPLONG(tblDir.uOffset);
4170                 break;
4171             }
4172         }
4173
4174         if (bFound)
4175         {
4176             TT_NAME_TABLE_HEADER ttNTHeader;
4177             TT_NAME_RECORD ttRecord;
4178
4179             SetFilePointer(handle, tblDir.uOffset, NULL, FILE_BEGIN);
4180             ReadFile(handle,&ttNTHeader, sizeof(TT_NAME_TABLE_HEADER),
4181                             NULL,NULL);
4182
4183             ttNTHeader.uNRCount = SWAPWORD(ttNTHeader.uNRCount);
4184             ttNTHeader.uStorageOffset = SWAPWORD(ttNTHeader.uStorageOffset);
4185             bFound = FALSE;
4186             for(i=0; i<ttNTHeader.uNRCount; i++)
4187             {
4188                 ReadFile(handle,&ttRecord, sizeof(TT_NAME_RECORD),NULL,NULL);
4189                 ttRecord.uNameID = SWAPWORD(ttRecord.uNameID);
4190                 /* 4 is the Full Font Name */
4191                 if(ttRecord.uNameID == 4)
4192                 {
4193                     int nPos;
4194                     LPSTR buf;
4195                     static LPCSTR tt = " (TrueType)";
4196
4197                     ttRecord.uStringLength = SWAPWORD(ttRecord.uStringLength);
4198                     ttRecord.uStringOffset = SWAPWORD(ttRecord.uStringOffset);
4199                     nPos = SetFilePointer(handle, 0, NULL, FILE_CURRENT);
4200                     SetFilePointer(handle, tblDir.uOffset + 
4201                                     ttRecord.uStringOffset + 
4202                                     ttNTHeader.uStorageOffset,
4203                                     NULL, FILE_BEGIN);
4204                     buf = HeapAlloc(GetProcessHeap(), 0, 
4205                                     ttRecord.uStringLength + 1 + strlen(tt));
4206                     memset(buf, 0, ttRecord.uStringLength + 1 + strlen(tt));
4207                     ReadFile(handle, buf, ttRecord.uStringLength, NULL, NULL);
4208                     if (strlen(buf) > 0)
4209                     {
4210                         strcat(buf,tt);
4211                         ret = strdupAtoW(buf);
4212                         HeapFree(GetProcessHeap(),0,buf);
4213                         break;
4214                     }
4215
4216                     HeapFree(GetProcessHeap(),0,buf);
4217                     SetFilePointer(handle,nPos, NULL, FILE_BEGIN);
4218                 }
4219             }
4220         }
4221         CloseHandle(handle);
4222     }
4223     else
4224         ERR("Unable to open font file %s\n", debugstr_w(filename));
4225
4226     TRACE("Returning fontname %s\n",debugstr_w(ret));
4227     return ret;
4228 }
4229
4230 static UINT ACTION_RegisterFonts(MSIPACKAGE *package)
4231 {
4232     UINT rc;
4233     MSIQUERY * view;
4234     MSIRECORD * row = 0;
4235     static const WCHAR ExecSeqQuery[] =
4236         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
4237          '`','F','o','n','t','`',0};
4238     static const WCHAR regfont1[] =
4239         {'S','o','f','t','w','a','r','e','\\',
4240          'M','i','c','r','o','s','o','f','t','\\',
4241          'W','i','n','d','o','w','s',' ','N','T','\\',
4242          'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
4243          'F','o','n','t','s',0};
4244     static const WCHAR regfont2[] =
4245         {'S','o','f','t','w','a','r','e','\\',
4246          'M','i','c','r','o','s','o','f','t','\\',
4247          'W','i','n','d','o','w','s','\\',
4248          'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
4249          'F','o','n','t','s',0};
4250     HKEY hkey1;
4251     HKEY hkey2;
4252
4253     TRACE("%p\n", package);
4254
4255     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
4256     if (rc != ERROR_SUCCESS)
4257     {
4258         TRACE("MSI_DatabaseOpenViewW failed: %d\n", rc);
4259         return ERROR_SUCCESS;
4260     }
4261
4262     rc = MSI_ViewExecute(view, 0);
4263     if (rc != ERROR_SUCCESS)
4264     {
4265         MSI_ViewClose(view);
4266         msiobj_release(&view->hdr);
4267         TRACE("MSI_ViewExecute returned %d\n", rc);
4268         return ERROR_SUCCESS;
4269     }
4270
4271     RegCreateKeyW(HKEY_LOCAL_MACHINE,regfont1,&hkey1);
4272     RegCreateKeyW(HKEY_LOCAL_MACHINE,regfont2,&hkey2);
4273     
4274     while (1)
4275     {
4276         LPWSTR name;
4277         LPCWSTR file;
4278         UINT index;
4279         DWORD size;
4280
4281         rc = MSI_ViewFetch(view,&row);
4282         if (rc != ERROR_SUCCESS)
4283         {
4284             rc = ERROR_SUCCESS;
4285             break;
4286         }
4287
4288         file = MSI_RecordGetString(row,1);
4289         index = get_loaded_file(package,file);
4290         if (index < 0)
4291         {
4292             ERR("Unable to load file\n");
4293             continue;
4294         }
4295
4296         /* check to make sure that component is installed */
4297         if (!ACTION_VerifyComponentForAction(package, 
4298                 package->files[index].ComponentIndex, INSTALLSTATE_LOCAL))
4299         {
4300             TRACE("Skipping: Component not scheduled for install\n");
4301
4302             msiobj_release(&row->hdr);
4303
4304             continue;
4305         }
4306
4307         if (MSI_RecordIsNull(row,2))
4308             name = load_ttfname_from(package->files[index].TargetPath);
4309         else
4310             name = load_dynamic_stringW(row,2);
4311
4312         if (name)
4313         {
4314             size = strlenW(package->files[index].FileName) * sizeof(WCHAR);
4315             RegSetValueExW(hkey1,name,0,REG_SZ,
4316                         (LPBYTE)package->files[index].FileName,size);
4317             RegSetValueExW(hkey2,name,0,REG_SZ,
4318                         (LPBYTE)package->files[index].FileName,size);
4319         }
4320         
4321         HeapFree(GetProcessHeap(),0,name);
4322         msiobj_release(&row->hdr);
4323     }
4324     MSI_ViewClose(view);
4325     msiobj_release(&view->hdr);
4326
4327     RegCloseKey(hkey1);
4328     RegCloseKey(hkey2);
4329
4330     TRACE("returning %d\n", rc);
4331     return rc;
4332 }
4333
4334 static UINT ITERATE_PublishComponent(MSIRECORD *rec, LPVOID param)
4335 {
4336     MSIPACKAGE *package = (MSIPACKAGE*)param;
4337     LPCWSTR compgroupid=NULL;
4338     LPCWSTR feature=NULL;
4339     LPCWSTR text = NULL;
4340     LPCWSTR qualifier = NULL;
4341     LPCWSTR component = NULL;
4342     LPWSTR advertise = NULL;
4343     LPWSTR output = NULL;
4344     HKEY hkey;
4345     UINT rc = ERROR_SUCCESS;
4346     UINT index;
4347     DWORD sz = 0;
4348
4349     component = MSI_RecordGetString(rec,3);
4350     index = get_loaded_component(package,component);
4351
4352     if (!ACTION_VerifyComponentForAction(package, index,
4353                             INSTALLSTATE_LOCAL) && 
4354        !ACTION_VerifyComponentForAction(package, index,
4355                             INSTALLSTATE_SOURCE) &&
4356        !ACTION_VerifyComponentForAction(package, index,
4357                             INSTALLSTATE_ADVERTISED))
4358     {
4359         TRACE("Skipping: Component %s not scheduled for install\n",
4360                         debugstr_w(component));
4361
4362         return ERROR_SUCCESS;
4363     }
4364
4365     compgroupid = MSI_RecordGetString(rec,1);
4366
4367     rc = MSIREG_OpenUserComponentsKey(compgroupid, &hkey, TRUE);
4368     if (rc != ERROR_SUCCESS)
4369         goto end;
4370     
4371     text = MSI_RecordGetString(rec,4);
4372     qualifier = MSI_RecordGetString(rec,2);
4373     feature = MSI_RecordGetString(rec,5);
4374   
4375     advertise = create_component_advertise_string(package, 
4376                     &package->components[index], feature);
4377
4378     sz = strlenW(advertise);
4379
4380     if (text)
4381         sz += lstrlenW(text);
4382
4383     sz+=3;
4384     sz *= sizeof(WCHAR);
4385            
4386     output = HeapAlloc(GetProcessHeap(),0,sz);
4387     memset(output,0,sz);
4388     strcpyW(output,advertise);
4389
4390     if (text)
4391         strcatW(output,text);
4392
4393     sz = (lstrlenW(output)+2) * sizeof(WCHAR);
4394     RegSetValueExW(hkey, qualifier,0,REG_MULTI_SZ, (LPBYTE)output, sz);
4395     
4396 end:
4397     RegCloseKey(hkey);
4398     HeapFree(GetProcessHeap(),0,output);
4399     
4400     return rc;
4401 }
4402
4403 /*
4404  * At present I am ignorning the advertised components part of this and only
4405  * focusing on the qualified component sets
4406  */
4407 static UINT ACTION_PublishComponents(MSIPACKAGE *package)
4408 {
4409     UINT rc;
4410     MSIQUERY * view;
4411     static const WCHAR ExecSeqQuery[] =
4412         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
4413          '`','P','u','b','l','i','s','h',
4414          'C','o','m','p','o','n','e','n','t','`',0};
4415     
4416     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
4417     if (rc != ERROR_SUCCESS)
4418         return ERROR_SUCCESS;
4419
4420     rc = MSI_IterateRecords(view, NULL, ITERATE_PublishComponent, package);
4421     msiobj_release(&view->hdr);
4422
4423     return rc;
4424 }