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];
338 scm = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CREATE_SERVICE);
341 fprintf(stderr, "Failed to open the service control manager.\n");
345 GetSystemDirectory(path, MAX_PATH);
346 lstrcatA(path, "\\msiexec.exe");
348 service = CreateServiceA(scm, "MSIServer", "MSIServer", GENERIC_ALL,
349 SERVICE_WIN32_SHARE_PROCESS, SERVICE_DEMAND_START,
350 SERVICE_ERROR_NORMAL, path, NULL, NULL,
353 if (service) CloseServiceHandle(service);
354 else if (GetLastError() != ERROR_SERVICE_EXISTS)
356 fprintf(stderr, "Failed to create MSI service\n");
359 CloseServiceHandle(scm);
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_prefix(argvW[i], "x"))
551 FunctionInstall = TRUE;
552 PackageName = argvW[i]+2;
558 PackageName = argvW[i];
560 WINE_TRACE("PackageName = %s\n", wine_dbgstr_w(PackageName));
561 StringListAppend(&property_list, RemoveAll);
563 else if(msi_option_prefix(argvW[i], "j"))
566 int len = lstrlenW(argvW[i]);
567 FunctionAdvertise = TRUE;
568 for(j = 2; j < len; j++)
574 AdvertiseMode = ADVERTISEFLAGS_USERASSIGN;
578 AdvertiseMode = ADVERTISEFLAGS_MACHINEASSIGN;
581 fprintf(stderr, "Unknown option \"%c\" in Advertise mode\n", argvW[i][j]);
588 WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
589 PackageName = argvW[i];
591 else if(msi_strequal(argvW[i], "u"))
593 FunctionAdvertise = TRUE;
594 AdvertiseMode = ADVERTISEFLAGS_USERASSIGN;
598 WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
599 PackageName = argvW[i];
601 else if(msi_strequal(argvW[i], "m"))
603 FunctionAdvertise = TRUE;
604 AdvertiseMode = ADVERTISEFLAGS_MACHINEASSIGN;
608 WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
609 PackageName = argvW[i];
611 else if(msi_option_equal(argvW[i], "t"))
616 WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
617 StringListAppend(&transform_list, argvW[i]);
619 else if(msi_option_equal(argvW[i], "g"))
624 WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
625 Language = msi_atou(argvW[i]);
627 else if(msi_option_prefix(argvW[i], "l"))
630 int len = lstrlenW(argvW[i]);
631 for(j = 2; j < len; j++)
637 LogMode |= INSTALLLOGMODE_INFO;
641 LogMode |= INSTALLLOGMODE_WARNING;
645 LogMode |= INSTALLLOGMODE_ERROR;
649 LogMode |= INSTALLLOGMODE_ACTIONSTART;
653 LogMode |= INSTALLLOGMODE_ACTIONDATA;
657 LogMode |= INSTALLLOGMODE_USER;
661 LogMode |= INSTALLLOGMODE_COMMONDATA;
665 LogMode |= INSTALLLOGMODE_FATALEXIT;
669 LogMode |= INSTALLLOGMODE_OUTOFDISKSPACE;
673 LogMode |= INSTALLLOGMODE_PROPERTYDUMP;
677 LogMode |= INSTALLLOGMODE_VERBOSE;
680 LogMode = INSTALLLOGMODE_FATALEXIT |
681 INSTALLLOGMODE_ERROR |
682 INSTALLLOGMODE_WARNING |
683 INSTALLLOGMODE_USER |
684 INSTALLLOGMODE_INFO |
685 INSTALLLOGMODE_RESOLVESOURCE |
686 INSTALLLOGMODE_OUTOFDISKSPACE |
687 INSTALLLOGMODE_ACTIONSTART |
688 INSTALLLOGMODE_ACTIONDATA |
689 INSTALLLOGMODE_COMMONDATA |
690 INSTALLLOGMODE_PROPERTYDUMP |
691 INSTALLLOGMODE_PROGRESS |
692 INSTALLLOGMODE_INITIALIZE |
693 INSTALLLOGMODE_TERMINATE |
694 INSTALLLOGMODE_SHOWDIALOG;
697 LogAttributes |= INSTALLLOGATTRIBUTES_APPEND;
700 LogAttributes |= INSTALLLOGATTRIBUTES_FLUSHEACHLINE;
709 WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
710 LogFileName = argvW[i];
711 if(MsiEnableLogW(LogMode, LogFileName, LogAttributes) != ERROR_SUCCESS)
713 fprintf(stderr, "Logging in %s (0x%08x, %u) failed\n",
714 wine_dbgstr_w(LogFileName), LogMode, LogAttributes);
718 else if(msi_option_equal(argvW[i], "p"))
720 FunctionPatch = TRUE;
724 WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
725 PatchFileName = argvW[i];
727 else if(msi_option_prefix(argvW[i], "q"))
729 if(lstrlenW(argvW[i]) == 2 || msi_strequal(argvW[i]+2, "n"))
731 InstallUILevel = INSTALLUILEVEL_NONE;
733 else if(msi_strequal(argvW[i]+2, "b"))
735 InstallUILevel = INSTALLUILEVEL_BASIC;
737 else if(msi_strequal(argvW[i]+2, "r"))
739 InstallUILevel = INSTALLUILEVEL_REDUCED;
741 else if(msi_strequal(argvW[i]+2, "f"))
743 InstallUILevel = INSTALLUILEVEL_FULL|INSTALLUILEVEL_ENDDIALOG;
745 else if(msi_strequal(argvW[i]+2, "n+"))
747 InstallUILevel = INSTALLUILEVEL_NONE|INSTALLUILEVEL_ENDDIALOG;
749 else if(msi_strequal(argvW[i]+2, "b+"))
751 InstallUILevel = INSTALLUILEVEL_BASIC|INSTALLUILEVEL_ENDDIALOG;
753 else if(msi_strequal(argvW[i]+2, "b-"))
755 InstallUILevel = INSTALLUILEVEL_BASIC|INSTALLUILEVEL_PROGRESSONLY;
757 else if(msi_strequal(argvW[i]+2, "b+!"))
759 InstallUILevel = INSTALLUILEVEL_BASIC|INSTALLUILEVEL_ENDDIALOG;
760 WINE_FIXME("Unknown modifier: !\n");
764 fprintf(stderr, "Unknown option \"%s\" for UI level\n",
765 wine_dbgstr_w(argvW[i]+2));
768 else if(msi_option_equal(argvW[i], "y"))
770 FunctionDllRegisterServer = TRUE;
774 WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
777 else if(msi_option_equal(argvW[i], "z"))
779 FunctionDllUnregisterServer = TRUE;
783 WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
786 else if(msi_option_equal(argvW[i], "h") || msi_option_equal(argvW[i], "?"))
790 else if(msi_option_equal(argvW[i], "m"))
792 FunctionUnknown = TRUE;
793 WINE_FIXME("Unknown parameter /m\n");
795 else if(msi_option_equal(argvW[i], "D"))
797 FunctionUnknown = TRUE;
798 WINE_FIXME("Unknown parameter /D\n");
801 StringListAppend(&property_list, argvW[i]);
805 MsiSetInternalUI(InstallUILevel, NULL);
807 Properties = build_properties( property_list );
809 if(FunctionInstallAdmin && FunctionPatch)
810 FunctionInstall = FALSE;
815 if(IsProductCode(PackageName))
816 ReturnCode = MsiConfigureProductExW(PackageName, 0, INSTALLSTATE_DEFAULT, Properties);
818 ReturnCode = MsiInstallProductW(PackageName, Properties);
820 else if(FunctionRepair)
822 if(IsProductCode(PackageName))
823 WINE_FIXME("Product code treatment not implemented yet\n");
825 ReturnCode = MsiReinstallProductW(PackageName, RepairMode);
827 else if(FunctionAdvertise)
829 LPWSTR Transforms = build_transforms( property_list );
830 ReturnCode = MsiAdvertiseProductW(PackageName, (LPWSTR) AdvertiseMode, Transforms, Language);
832 else if(FunctionPatch)
834 ReturnCode = MsiApplyPatchW(PatchFileName, PackageName, InstallType, Properties);
836 else if(FunctionDllRegisterServer)
838 ReturnCode = DoDllRegisterServer(DllName);
840 else if(FunctionDllUnregisterServer)
842 ReturnCode = DoDllUnregisterServer(DllName);
844 else if (FunctionRegServer)
846 ReturnCode = DoRegServer();
848 else if (FunctionUnregServer)
850 WINE_FIXME( "/unregserver not implemented yet, ignoring\n" );
852 else if (FunctionUnknown)
854 WINE_FIXME( "Unknown function, ignoring\n" );