2 * msiexec.exe implementation
4 * Copyright 2004 Vincent Béron
5 * Copyright 2005 Mike McCormack
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define WIN32_LEAN_AND_MEAN
30 #include "wine/debug.h"
31 #include "wine/unicode.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(msiexec);
35 typedef HRESULT (WINAPI *DLLREGISTERSERVER)(void);
36 typedef HRESULT (WINAPI *DLLUNREGISTERSERVER)(void);
40 struct string_list *next;
44 static const char UsageStr[] =
46 " Install a product:\n"
47 " msiexec {package|productcode} [property]\n"
48 " msiexec /i {package|productcode} [property]\n"
49 " msiexec /a package [property]\n"
50 " Repair an installation:\n"
51 " msiexec /f[p|o|e|d|c|a|u|m|s|v] {package|productcode}\n"
52 " Uninstall a product:\n"
53 " msiexec /x {package|productcode} [property]\n"
54 " Advertise a product:\n"
55 " msiexec /j[u|m] package [/t transform] [/g languageid]\n"
56 " msiexec {u|m} package [/t transform] [/g languageid]\n"
58 " msiexec /p patchpackage [property]\n"
59 " msiexec /p patchpackage /a package [property]\n"
60 " Modifiers for above operations:\n"
61 " msiexec /l[*][i|w|e|a|r|u|c|m|o|p|v|][+|!] logfile\n"
62 " msiexec /q{|n|b|r|f|n+|b+|b-}\n"
63 " Register a module:\n"
64 " msiexec /y module\n"
65 " Unregister a module:\n"
66 " msiexec /z module\n"
67 " Display usage and copyright:\n"
69 "NOTE: Product code on commandline unimplemented as of yet\n"
71 "Copyright 2004 Vincent Béron\n";
73 static const WCHAR ActionAdmin[] = {
74 'A','C','T','I','O','N','=','A','D','M','I','N',0 };
75 static const WCHAR RemoveAll[] = {
76 'R','E','M','O','V','E','=','A','L','L',0 };
78 static const WCHAR InstallRunOnce[] = {
79 'S','o','f','t','w','a','r','e','\\',
80 'M','i','c','r','o','s','o','f','t','\\',
81 'W','i','n','d','o','w','s','\\',
82 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
83 'I','n','s','t','a','l','l','e','r','\\',
84 'R','u','n','O','n','c','e','E','n','t','r','i','e','s',0};
86 static void ShowUsage(int ExitCode)
89 ExitProcess(ExitCode);
92 static BOOL IsProductCode(LPWSTR str)
96 if(lstrlenW(str) != 38)
98 return ( (CLSIDFromString(str, &ProductCode) == NOERROR) );
102 static VOID StringListAppend(struct string_list **list, LPCWSTR str)
104 struct string_list *entry;
107 size = sizeof *entry + lstrlenW(str) * sizeof (WCHAR);
108 entry = HeapAlloc(GetProcessHeap(), 0, size);
111 WINE_ERR("Out of memory!\n");
114 lstrcpyW(entry->str, str);
118 * Ignoring o(n^2) time complexity to add n strings for simplicity,
119 * add the string to the end of the list to preserve the order.
122 list = &(*list)->next;
126 static LPWSTR build_properties(struct string_list *property_list)
128 struct string_list *list;
129 LPWSTR ret, p, value;
136 /* count the space we need */
138 for(list = property_list; list; list = list->next)
139 len += lstrlenW(list->str) + 3;
141 ret = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
143 /* add a space before each string, and quote the value */
145 for(list = property_list; list; list = list->next)
147 value = strchrW(list->str,'=');
150 len = value - list->str;
152 memcpy(p, list->str, len * sizeof(WCHAR));
156 /* check if the value contains spaces and maybe quote it */
158 needs_quote = strchrW(value,' ') ? 1 : 0;
161 len = lstrlenW(value);
162 memcpy(p, value, len * sizeof(WCHAR));
169 WINE_TRACE("properties -> %s\n", wine_dbgstr_w(ret) );
174 static LPWSTR build_transforms(struct string_list *transform_list)
176 struct string_list *list;
180 /* count the space we need */
182 for(list = transform_list; list; list = list->next)
183 len += lstrlenW(list->str) + 1;
185 ret = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
187 /* add all the transforms with a semicolon between each one */
189 for(list = transform_list; list; list = list->next)
191 len = lstrlenW(list->str);
192 lstrcpynW(p, list->str, len );
202 static DWORD msi_atou(LPCWSTR str)
205 while(*str >= '0' && *str <= '9')
214 /* str1 is the same as str2, ignoring case */
215 static BOOL msi_strequal(LPCWSTR str1, LPCSTR str2)
220 len = MultiByteToWideChar( CP_ACP, 0, str2, -1, NULL, 0);
223 if( lstrlenW(str1) != (len-1) )
225 strW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*len);
226 MultiByteToWideChar( CP_ACP, 0, str2, -1, strW, len);
227 ret = CompareStringW(GetThreadLocale(), NORM_IGNORECASE, str1, len, strW, len);
228 HeapFree(GetProcessHeap(), 0, strW);
229 return (ret == CSTR_EQUAL);
232 /* prefix is hyphen or dash, and str1 is the same as str2, ignoring case */
233 static BOOL msi_option_equal(LPCWSTR str1, LPCSTR str2)
235 if (str1[0] != '/' && str1[0] != '-')
238 /* skip over the hyphen or slash */
239 return msi_strequal(str1 + 1, str2);
242 /* str2 is at the beginning of str1, ignoring case */
243 static BOOL msi_strprefix(LPCWSTR str1, LPCSTR str2)
248 len = MultiByteToWideChar( CP_ACP, 0, str2, -1, NULL, 0);
251 if( lstrlenW(str1) < (len-1) )
253 strW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*len);
254 MultiByteToWideChar( CP_ACP, 0, str2, -1, strW, len);
255 ret = CompareStringW(GetThreadLocale(), NORM_IGNORECASE, str1, len-1, strW, len-1);
256 HeapFree(GetProcessHeap(), 0, strW);
257 return (ret == CSTR_EQUAL);
260 /* prefix is hyphen or dash, and str2 is at the beginning of str1, ignoring case */
261 static BOOL msi_option_prefix(LPCWSTR str1, LPCSTR str2)
263 if (str1[0] != '/' && str1[0] != '-')
266 /* skip over the hyphen or slash */
267 return msi_strprefix(str1 + 1, str2);
270 static VOID *LoadProc(LPCWSTR DllName, LPCSTR ProcName, HMODULE* DllHandle)
274 *DllHandle = LoadLibraryExW(DllName, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
277 fprintf(stderr, "Unable to load dll %s\n", wine_dbgstr_w(DllName));
280 proc = (VOID *) GetProcAddress(*DllHandle, ProcName);
283 fprintf(stderr, "Dll %s does not implement function %s\n",
284 wine_dbgstr_w(DllName), ProcName);
285 FreeLibrary(*DllHandle);
292 static DWORD DoDllRegisterServer(LPCWSTR DllName)
295 DLLREGISTERSERVER pfDllRegisterServer = NULL;
296 HMODULE DllHandle = NULL;
298 pfDllRegisterServer = LoadProc(DllName, "DllRegisterServer", &DllHandle);
300 hr = pfDllRegisterServer();
303 fprintf(stderr, "Failed to register dll %s\n", wine_dbgstr_w(DllName));
306 printf("Successfully registered dll %s\n", wine_dbgstr_w(DllName));
308 FreeLibrary(DllHandle);
312 static DWORD DoDllUnregisterServer(LPCWSTR DllName)
315 DLLUNREGISTERSERVER pfDllUnregisterServer = NULL;
316 HMODULE DllHandle = NULL;
318 pfDllUnregisterServer = LoadProc(DllName, "DllUnregisterServer", &DllHandle);
320 hr = pfDllUnregisterServer();
323 fprintf(stderr, "Failed to unregister dll %s\n", wine_dbgstr_w(DllName));
326 printf("Successfully unregistered dll %s\n", wine_dbgstr_w(DllName));
328 FreeLibrary(DllHandle);
332 static DWORD DoRegServer(void)
334 SC_HANDLE scm, service;
335 CHAR path[MAX_PATH+12];
337 scm = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CREATE_SERVICE);
340 fprintf(stderr, "Failed to open the service control manager.\n");
344 GetSystemDirectory(path, MAX_PATH);
345 lstrcatA(path, "\\msiexec.exe");
347 service = CreateServiceA(scm, "MSIServer", "MSIServer", GENERIC_ALL,
348 SERVICE_WIN32_SHARE_PROCESS, SERVICE_DEMAND_START,
349 SERVICE_ERROR_NORMAL, path, NULL, NULL,
353 fprintf(stderr, "Failed to create MSI service\n");
354 CloseServiceHandle(scm);
358 CloseServiceHandle(scm);
359 CloseServiceHandle(service);
363 static BOOL process_args_from_reg( LPWSTR ident, int *pargc, WCHAR ***pargv )
366 HKEY hkey = 0, hkeyArgs = 0;
367 DWORD sz = 0, type = 0;
371 r = RegOpenKeyW(HKEY_LOCAL_MACHINE, InstallRunOnce, &hkey);
372 if(r != ERROR_SUCCESS)
374 r = RegQueryValueExW(hkey, ident, 0, &type, 0, &sz);
375 if(r == ERROR_SUCCESS && type == REG_SZ)
377 buf = HeapAlloc(GetProcessHeap(), 0, sz);
378 r = RegQueryValueExW(hkey, ident, 0, &type, (LPBYTE)buf, &sz);
379 if( r == ERROR_SUCCESS )
381 *pargv = CommandLineToArgvW(buf, pargc);
385 RegCloseKey(hkeyArgs);
389 int main(int argc, char **argv)
392 BOOL FunctionInstall = FALSE;
393 BOOL FunctionInstallAdmin = FALSE;
394 BOOL FunctionRepair = FALSE;
395 BOOL FunctionAdvertise = FALSE;
396 BOOL FunctionPatch = FALSE;
397 BOOL FunctionDllRegisterServer = FALSE;
398 BOOL FunctionDllUnregisterServer = FALSE;
399 BOOL FunctionRegServer = FALSE;
400 BOOL FunctionUnregServer = FALSE;
401 BOOL FunctionUnknown = FALSE;
403 LPWSTR PackageName = NULL;
404 LPWSTR Properties = NULL;
405 struct string_list *property_list = NULL;
407 DWORD RepairMode = 0;
409 DWORD_PTR AdvertiseMode = 0;
410 struct string_list *transform_list = NULL;
414 LPWSTR LogFileName = NULL;
415 DWORD LogAttributes = 0;
417 LPWSTR PatchFileName = NULL;
418 INSTALLTYPE InstallType = INSTALLTYPE_DEFAULT;
420 INSTALLUILEVEL InstallUILevel = INSTALLUILEVEL_FULL;
422 LPWSTR DllName = NULL;
424 LPWSTR *argvW = NULL;
426 /* overwrite the command line */
427 argvW = CommandLineToArgvW( GetCommandLineW(), &argc );
430 * If the args begin with /@ IDENT then we need to load the real
431 * command line out of the RunOnceEntries key in the registry.
432 * We do that before starting to process the real commandline,
433 * then overwrite the commandline again.
435 if(argc>1 && msi_option_equal(argvW[1], "@"))
437 if(!process_args_from_reg( argvW[2], &argc, &argvW ))
441 for(i = 1; i < argc; i++)
443 WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
445 if (msi_option_equal(argvW[i], "regserver"))
447 FunctionRegServer = TRUE;
449 else if (msi_option_equal(argvW[i], "unregserver") || msi_option_equal(argvW[i], "unregister"))
451 FunctionUnregServer = TRUE;
453 else if(msi_option_prefix(argvW[i], "i"))
455 LPWSTR argvWi = argvW[i];
456 FunctionInstall = TRUE;
457 if(lstrlenW(argvWi) > 2)
464 WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
467 PackageName = argvWi;
469 else if(msi_option_equal(argvW[i], "a"))
471 FunctionInstall = TRUE;
472 FunctionInstallAdmin = TRUE;
473 InstallType = INSTALLTYPE_NETWORK_IMAGE;
477 WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
478 PackageName = argvW[i];
479 StringListAppend(&property_list, ActionAdmin);
481 else if(msi_option_prefix(argvW[i], "f"))
484 int len = lstrlenW(argvW[i]);
485 FunctionRepair = TRUE;
486 for(j = 2; j < len; j++)
492 RepairMode |= REINSTALLMODE_FILEMISSING;
496 RepairMode |= REINSTALLMODE_FILEOLDERVERSION;
500 RepairMode |= REINSTALLMODE_FILEEQUALVERSION;
504 RepairMode |= REINSTALLMODE_FILEEXACT;
508 RepairMode |= REINSTALLMODE_FILEVERIFY;
512 RepairMode |= REINSTALLMODE_FILEREPLACE;
516 RepairMode |= REINSTALLMODE_USERDATA;
520 RepairMode |= REINSTALLMODE_MACHINEDATA;
524 RepairMode |= REINSTALLMODE_SHORTCUT;
528 RepairMode |= REINSTALLMODE_PACKAGE;
531 fprintf(stderr, "Unknown option \"%c\" in Repair mode\n", argvW[i][j]);
537 RepairMode = REINSTALLMODE_FILEMISSING |
538 REINSTALLMODE_FILEEQUALVERSION |
539 REINSTALLMODE_FILEVERIFY |
540 REINSTALLMODE_MACHINEDATA |
541 REINSTALLMODE_SHORTCUT;
546 WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
547 PackageName = argvW[i];
549 else if(msi_option_equal(argvW[i], "x"))
551 FunctionInstall = TRUE;
555 WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
556 PackageName = argvW[i];
557 StringListAppend(&property_list, RemoveAll);
559 else if(msi_option_prefix(argvW[i], "j"))
562 int len = lstrlenW(argvW[i]);
563 FunctionAdvertise = TRUE;
564 for(j = 2; j < len; j++)
570 AdvertiseMode = ADVERTISEFLAGS_USERASSIGN;
574 AdvertiseMode = ADVERTISEFLAGS_MACHINEASSIGN;
577 fprintf(stderr, "Unknown option \"%c\" in Advertise mode\n", argvW[i][j]);
584 WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
585 PackageName = argvW[i];
587 else if(msi_strequal(argvW[i], "u"))
589 FunctionAdvertise = TRUE;
590 AdvertiseMode = ADVERTISEFLAGS_USERASSIGN;
594 WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
595 PackageName = argvW[i];
597 else if(msi_strequal(argvW[i], "m"))
599 FunctionAdvertise = TRUE;
600 AdvertiseMode = ADVERTISEFLAGS_MACHINEASSIGN;
604 WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
605 PackageName = argvW[i];
607 else if(msi_option_equal(argvW[i], "t"))
612 WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
613 StringListAppend(&transform_list, argvW[i]);
615 else if(msi_option_equal(argvW[i], "g"))
620 WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
621 Language = msi_atou(argvW[i]);
623 else if(msi_option_prefix(argvW[i], "l"))
626 int len = lstrlenW(argvW[i]);
627 for(j = 2; j < len; j++)
633 LogMode |= INSTALLLOGMODE_INFO;
637 LogMode |= INSTALLLOGMODE_WARNING;
641 LogMode |= INSTALLLOGMODE_ERROR;
645 LogMode |= INSTALLLOGMODE_ACTIONSTART;
649 LogMode |= INSTALLLOGMODE_ACTIONDATA;
653 LogMode |= INSTALLLOGMODE_USER;
657 LogMode |= INSTALLLOGMODE_COMMONDATA;
661 LogMode |= INSTALLLOGMODE_FATALEXIT;
665 LogMode |= INSTALLLOGMODE_OUTOFDISKSPACE;
669 LogMode |= INSTALLLOGMODE_PROPERTYDUMP;
673 LogMode |= INSTALLLOGMODE_VERBOSE;
676 LogMode = INSTALLLOGMODE_FATALEXIT |
677 INSTALLLOGMODE_ERROR |
678 INSTALLLOGMODE_WARNING |
679 INSTALLLOGMODE_USER |
680 INSTALLLOGMODE_INFO |
681 INSTALLLOGMODE_RESOLVESOURCE |
682 INSTALLLOGMODE_OUTOFDISKSPACE |
683 INSTALLLOGMODE_ACTIONSTART |
684 INSTALLLOGMODE_ACTIONDATA |
685 INSTALLLOGMODE_COMMONDATA |
686 INSTALLLOGMODE_PROPERTYDUMP |
687 INSTALLLOGMODE_PROGRESS |
688 INSTALLLOGMODE_INITIALIZE |
689 INSTALLLOGMODE_TERMINATE |
690 INSTALLLOGMODE_SHOWDIALOG;
693 LogAttributes |= INSTALLLOGATTRIBUTES_APPEND;
696 LogAttributes |= INSTALLLOGATTRIBUTES_FLUSHEACHLINE;
705 WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
706 LogFileName = argvW[i];
707 if(MsiEnableLogW(LogMode, LogFileName, LogAttributes) != ERROR_SUCCESS)
709 fprintf(stderr, "Logging in %s (0x%08lx, %lu) failed\n",
710 wine_dbgstr_w(LogFileName), LogMode, LogAttributes);
714 else if(msi_option_equal(argvW[i], "p"))
716 FunctionPatch = TRUE;
720 WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
721 PatchFileName = argvW[i];
723 else if(msi_option_prefix(argvW[i], "q"))
725 if(lstrlenW(argvW[i]) == 2 || msi_strequal(argvW[i]+2, "n"))
727 InstallUILevel = INSTALLUILEVEL_NONE;
729 else if(msi_strequal(argvW[i]+2, "b"))
731 InstallUILevel = INSTALLUILEVEL_BASIC;
733 else if(msi_strequal(argvW[i]+2, "r"))
735 InstallUILevel = INSTALLUILEVEL_REDUCED;
737 else if(msi_strequal(argvW[i]+2, "f"))
739 InstallUILevel = INSTALLUILEVEL_FULL|INSTALLUILEVEL_ENDDIALOG;
741 else if(msi_strequal(argvW[i]+2, "n+"))
743 InstallUILevel = INSTALLUILEVEL_NONE|INSTALLUILEVEL_ENDDIALOG;
745 else if(msi_strequal(argvW[i]+2, "b+"))
747 InstallUILevel = INSTALLUILEVEL_BASIC|INSTALLUILEVEL_ENDDIALOG;
749 else if(msi_strequal(argvW[i]+2, "b-"))
751 InstallUILevel = INSTALLUILEVEL_BASIC|INSTALLUILEVEL_PROGRESSONLY;
753 else if(msi_strequal(argvW[i]+2, "b+!"))
755 InstallUILevel = INSTALLUILEVEL_BASIC|INSTALLUILEVEL_ENDDIALOG;
756 WINE_FIXME("Unknown modifier: !\n");
760 fprintf(stderr, "Unknown option \"%s\" for UI level\n",
761 wine_dbgstr_w(argvW[i]+2));
764 else if(msi_option_equal(argvW[i], "y"))
766 FunctionDllRegisterServer = TRUE;
770 WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
773 else if(msi_option_equal(argvW[i], "z"))
775 FunctionDllUnregisterServer = TRUE;
779 WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
782 else if(msi_option_equal(argvW[i], "h") || msi_option_equal(argvW[i], "?"))
786 else if(msi_option_equal(argvW[i], "m"))
788 FunctionUnknown = TRUE;
789 WINE_FIXME("Unknown parameter /m\n");
791 else if(msi_option_equal(argvW[i], "D"))
793 FunctionUnknown = TRUE;
794 WINE_FIXME("Unknown parameter /D\n");
797 StringListAppend(&property_list, argvW[i]);
801 MsiSetInternalUI(InstallUILevel, NULL);
803 Properties = build_properties( property_list );
805 if(FunctionInstallAdmin && FunctionPatch)
806 FunctionInstall = FALSE;
811 if(IsProductCode(PackageName))
812 ReturnCode = MsiConfigureProductExW(PackageName, 0, INSTALLSTATE_DEFAULT, Properties);
814 ReturnCode = MsiInstallProductW(PackageName, Properties);
816 else if(FunctionRepair)
818 if(IsProductCode(PackageName))
819 WINE_FIXME("Product code treatment not implemented yet\n");
821 ReturnCode = MsiReinstallProductW(PackageName, RepairMode);
823 else if(FunctionAdvertise)
825 LPWSTR Transforms = build_transforms( property_list );
826 ReturnCode = MsiAdvertiseProductW(PackageName, (LPWSTR) AdvertiseMode, Transforms, Language);
828 else if(FunctionPatch)
830 ReturnCode = MsiApplyPatchW(PatchFileName, PackageName, InstallType, Properties);
832 else if(FunctionDllRegisterServer)
834 ReturnCode = DoDllRegisterServer(DllName);
836 else if(FunctionDllUnregisterServer)
838 ReturnCode = DoDllUnregisterServer(DllName);
840 else if (FunctionRegServer)
842 ReturnCode = DoRegServer();
844 else if (FunctionUnregServer)
846 WINE_FIXME( "/unregserver not implemented yet, ignoring\n" );
848 else if (FunctionUnknown)
850 WINE_FIXME( "Unknown function, ignoring\n" );