msiexec.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. /*
  2. * msiexec.exe implementation
  3. *
  4. * Copyright 2004 Vincent Béron
  5. * Copyright 2005 Mike McCormack
  6. *
  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.
  11. *
  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.
  16. *
  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
  20. */
  21. #define WIN32_LEAN_AND_MEAN
  22. #include <windows.h>
  23. #include <commctrl.h>
  24. #include <msi.h>
  25. #include <winsvc.h>
  26. #include <objbase.h>
  27. #include <stdio.h>
  28. #include "wine/debug.h"
  29. #include "wine/heap.h"
  30. #include "initguid.h"
  31. DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
  32. WINE_DEFAULT_DEBUG_CHANNEL(msiexec);
  33. typedef HRESULT (WINAPI *DLLREGISTERSERVER)(void);
  34. typedef HRESULT (WINAPI *DLLUNREGISTERSERVER)(void);
  35. DWORD DoService(void);
  36. struct string_list
  37. {
  38. struct string_list *next;
  39. WCHAR str[1];
  40. };
  41. static void ShowUsage(int ExitCode)
  42. {
  43. WCHAR msiexec_version[40];
  44. WCHAR filename[MAX_PATH];
  45. LPWSTR msi_res;
  46. LPWSTR msiexec_help;
  47. HMODULE hmsi = GetModuleHandleA("msi.dll");
  48. DWORD len;
  49. DWORD res;
  50. /* MsiGetFileVersion need the full path */
  51. *filename = 0;
  52. res = GetModuleFileNameW(hmsi, filename, ARRAY_SIZE(filename));
  53. if (!res)
  54. WINE_ERR("GetModuleFileName failed: %d\n", GetLastError());
  55. len = ARRAY_SIZE(msiexec_version);
  56. *msiexec_version = 0;
  57. res = MsiGetFileVersionW(filename, msiexec_version, &len, NULL, NULL);
  58. if (res)
  59. WINE_ERR("MsiGetFileVersion failed with %d\n", res);
  60. /* Return the length of the resource.
  61. No typo: The LPWSTR parameter must be a LPWSTR * for this mode */
  62. len = LoadStringW(hmsi, 10, (LPWSTR) &msi_res, 0);
  63. msi_res = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
  64. msiexec_help = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) + sizeof(msiexec_version));
  65. if (msi_res && msiexec_help) {
  66. *msi_res = 0;
  67. LoadStringW(hmsi, 10, msi_res, len + 1);
  68. swprintf(msiexec_help, len + 1 + ARRAY_SIZE(msiexec_version), msi_res, msiexec_version);
  69. MsiMessageBoxW(0, msiexec_help, NULL, 0, GetUserDefaultLangID(), 0);
  70. }
  71. HeapFree(GetProcessHeap(), 0, msi_res);
  72. HeapFree(GetProcessHeap(), 0, msiexec_help);
  73. ExitProcess(ExitCode);
  74. }
  75. static BOOL IsProductCode(LPWSTR str)
  76. {
  77. GUID ProductCode;
  78. if(lstrlenW(str) != 38)
  79. return FALSE;
  80. return ( (CLSIDFromString(str, &ProductCode) == NOERROR) );
  81. }
  82. static VOID StringListAppend(struct string_list **list, LPCWSTR str)
  83. {
  84. struct string_list *entry;
  85. entry = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(struct string_list, str[lstrlenW(str) + 1]));
  86. if(!entry)
  87. {
  88. WINE_ERR("Out of memory!\n");
  89. ExitProcess(1);
  90. }
  91. lstrcpyW(entry->str, str);
  92. entry->next = NULL;
  93. /*
  94. * Ignoring o(n^2) time complexity to add n strings for simplicity,
  95. * add the string to the end of the list to preserve the order.
  96. */
  97. while( *list )
  98. list = &(*list)->next;
  99. *list = entry;
  100. }
  101. static LPWSTR build_properties(struct string_list *property_list)
  102. {
  103. struct string_list *list;
  104. LPWSTR ret, p, value;
  105. DWORD len;
  106. BOOL needs_quote;
  107. if(!property_list)
  108. return NULL;
  109. /* count the space we need */
  110. len = 1;
  111. for(list = property_list; list; list = list->next)
  112. len += lstrlenW(list->str) + 3;
  113. ret = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
  114. /* add a space before each string, and quote the value */
  115. p = ret;
  116. for(list = property_list; list; list = list->next)
  117. {
  118. value = wcschr(list->str,'=');
  119. if(!value)
  120. continue;
  121. len = value - list->str;
  122. *p++ = ' ';
  123. memcpy(p, list->str, len * sizeof(WCHAR));
  124. p += len;
  125. *p++ = '=';
  126. /* check if the value contains spaces and maybe quote it */
  127. value++;
  128. needs_quote = wcschr(value,' ') ? 1 : 0;
  129. if(needs_quote)
  130. *p++ = '"';
  131. len = lstrlenW(value);
  132. memcpy(p, value, len * sizeof(WCHAR));
  133. p += len;
  134. if(needs_quote)
  135. *p++ = '"';
  136. }
  137. *p = 0;
  138. WINE_TRACE("properties -> %s\n", wine_dbgstr_w(ret) );
  139. return ret;
  140. }
  141. static LPWSTR build_transforms(struct string_list *transform_list)
  142. {
  143. struct string_list *list;
  144. LPWSTR ret, p;
  145. DWORD len;
  146. /* count the space we need */
  147. len = 1;
  148. for(list = transform_list; list; list = list->next)
  149. len += lstrlenW(list->str) + 1;
  150. ret = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
  151. /* add all the transforms with a semicolon between each one */
  152. p = ret;
  153. for(list = transform_list; list; list = list->next)
  154. {
  155. len = lstrlenW(list->str);
  156. lstrcpynW(p, list->str, len );
  157. p += len;
  158. if(list->next)
  159. *p++ = ';';
  160. }
  161. *p = 0;
  162. return ret;
  163. }
  164. static DWORD msi_atou(LPCWSTR str)
  165. {
  166. DWORD ret = 0;
  167. while(*str >= '0' && *str <= '9')
  168. {
  169. ret *= 10;
  170. ret += (*str - '0');
  171. str++;
  172. }
  173. return ret;
  174. }
  175. /* str1 is the same as str2, ignoring case */
  176. static BOOL msi_strequal(LPCWSTR str1, LPCSTR str2)
  177. {
  178. DWORD len, ret;
  179. LPWSTR strW;
  180. len = MultiByteToWideChar( CP_ACP, 0, str2, -1, NULL, 0);
  181. if( !len )
  182. return FALSE;
  183. if( lstrlenW(str1) != (len-1) )
  184. return FALSE;
  185. strW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*len);
  186. MultiByteToWideChar( CP_ACP, 0, str2, -1, strW, len);
  187. ret = CompareStringW(GetThreadLocale(), NORM_IGNORECASE, str1, len, strW, len);
  188. HeapFree(GetProcessHeap(), 0, strW);
  189. return (ret == CSTR_EQUAL);
  190. }
  191. /* prefix is hyphen or dash, and str1 is the same as str2, ignoring case */
  192. static BOOL msi_option_equal(LPCWSTR str1, LPCSTR str2)
  193. {
  194. if (str1[0] != '/' && str1[0] != '-')
  195. return FALSE;
  196. /* skip over the hyphen or slash */
  197. return msi_strequal(str1 + 1, str2);
  198. }
  199. /* str2 is at the beginning of str1, ignoring case */
  200. static BOOL msi_strprefix(LPCWSTR str1, LPCSTR str2)
  201. {
  202. DWORD len, ret;
  203. LPWSTR strW;
  204. len = MultiByteToWideChar( CP_ACP, 0, str2, -1, NULL, 0);
  205. if( !len )
  206. return FALSE;
  207. if( lstrlenW(str1) < (len-1) )
  208. return FALSE;
  209. strW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*len);
  210. MultiByteToWideChar( CP_ACP, 0, str2, -1, strW, len);
  211. ret = CompareStringW(GetThreadLocale(), NORM_IGNORECASE, str1, len-1, strW, len-1);
  212. HeapFree(GetProcessHeap(), 0, strW);
  213. return (ret == CSTR_EQUAL);
  214. }
  215. /* prefix is hyphen or dash, and str2 is at the beginning of str1, ignoring case */
  216. static BOOL msi_option_prefix(LPCWSTR str1, LPCSTR str2)
  217. {
  218. if (str1[0] != '/' && str1[0] != '-')
  219. return FALSE;
  220. /* skip over the hyphen or slash */
  221. return msi_strprefix(str1 + 1, str2);
  222. }
  223. static VOID *LoadProc(LPCWSTR DllName, LPCSTR ProcName, HMODULE* DllHandle)
  224. {
  225. VOID* (*proc)(void);
  226. *DllHandle = LoadLibraryExW(DllName, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
  227. if(!*DllHandle)
  228. {
  229. fprintf(stderr, "Unable to load dll %s\n", wine_dbgstr_w(DllName));
  230. ExitProcess(1);
  231. }
  232. proc = (VOID *) GetProcAddress(*DllHandle, ProcName);
  233. if(!proc)
  234. {
  235. fprintf(stderr, "Dll %s does not implement function %s\n",
  236. wine_dbgstr_w(DllName), ProcName);
  237. FreeLibrary(*DllHandle);
  238. ExitProcess(1);
  239. }
  240. return proc;
  241. }
  242. static DWORD DoDllRegisterServer(LPCWSTR DllName)
  243. {
  244. HRESULT hr;
  245. DLLREGISTERSERVER pfDllRegisterServer = NULL;
  246. HMODULE DllHandle = NULL;
  247. pfDllRegisterServer = LoadProc(DllName, "DllRegisterServer", &DllHandle);
  248. hr = pfDllRegisterServer();
  249. if(FAILED(hr))
  250. {
  251. fprintf(stderr, "Failed to register dll %s\n", wine_dbgstr_w(DllName));
  252. return 1;
  253. }
  254. printf("Successfully registered dll %s\n", wine_dbgstr_w(DllName));
  255. if(DllHandle)
  256. FreeLibrary(DllHandle);
  257. return 0;
  258. }
  259. static DWORD DoDllUnregisterServer(LPCWSTR DllName)
  260. {
  261. HRESULT hr;
  262. DLLUNREGISTERSERVER pfDllUnregisterServer = NULL;
  263. HMODULE DllHandle = NULL;
  264. pfDllUnregisterServer = LoadProc(DllName, "DllUnregisterServer", &DllHandle);
  265. hr = pfDllUnregisterServer();
  266. if(FAILED(hr))
  267. {
  268. fprintf(stderr, "Failed to unregister dll %s\n", wine_dbgstr_w(DllName));
  269. return 1;
  270. }
  271. printf("Successfully unregistered dll %s\n", wine_dbgstr_w(DllName));
  272. if(DllHandle)
  273. FreeLibrary(DllHandle);
  274. return 0;
  275. }
  276. static DWORD DoRegServer(void)
  277. {
  278. SC_HANDLE scm, service;
  279. WCHAR path[MAX_PATH+12];
  280. DWORD len, ret = 0;
  281. if (!(scm = OpenSCManagerW(NULL, SERVICES_ACTIVE_DATABASEW, SC_MANAGER_CREATE_SERVICE)))
  282. {
  283. fprintf(stderr, "Failed to open the service control manager.\n");
  284. return 1;
  285. }
  286. len = GetSystemDirectoryW(path, MAX_PATH);
  287. lstrcpyW(path + len, L"\\msiexec /V");
  288. if ((service = CreateServiceW(scm, L"MSIServer", L"MSIServer", GENERIC_ALL,
  289. SERVICE_WIN32_SHARE_PROCESS, SERVICE_DEMAND_START,
  290. SERVICE_ERROR_NORMAL, path, NULL, NULL, NULL, NULL, NULL)))
  291. {
  292. CloseServiceHandle(service);
  293. }
  294. else if (GetLastError() != ERROR_SERVICE_EXISTS)
  295. {
  296. fprintf(stderr, "Failed to create MSI service\n");
  297. ret = 1;
  298. }
  299. CloseServiceHandle(scm);
  300. return ret;
  301. }
  302. static DWORD DoUnregServer(void)
  303. {
  304. SC_HANDLE scm, service;
  305. DWORD ret = 0;
  306. if (!(scm = OpenSCManagerW(NULL, SERVICES_ACTIVE_DATABASEW, SC_MANAGER_CONNECT)))
  307. {
  308. fprintf(stderr, "Failed to open service control manager\n");
  309. return 1;
  310. }
  311. if ((service = OpenServiceW(scm, L"MSIServer", DELETE)))
  312. {
  313. if (!DeleteService(service))
  314. {
  315. fprintf(stderr, "Failed to delete MSI service\n");
  316. ret = 1;
  317. }
  318. CloseServiceHandle(service);
  319. }
  320. else if (GetLastError() != ERROR_SERVICE_DOES_NOT_EXIST)
  321. {
  322. fprintf(stderr, "Failed to open MSI service\n");
  323. ret = 1;
  324. }
  325. CloseServiceHandle(scm);
  326. return ret;
  327. }
  328. extern UINT CDECL __wine_msi_call_dll_function(DWORD client_pid, const GUID *guid);
  329. static DWORD client_pid;
  330. static DWORD CALLBACK custom_action_thread(void *arg)
  331. {
  332. GUID guid = *(GUID *)arg;
  333. heap_free(arg);
  334. return __wine_msi_call_dll_function(client_pid, &guid);
  335. }
  336. static int custom_action_server(const WCHAR *arg)
  337. {
  338. GUID guid, *thread_guid;
  339. DWORD64 thread64;
  340. WCHAR buffer[24];
  341. HANDLE thread;
  342. HANDLE pipe;
  343. DWORD size;
  344. TRACE("%s\n", debugstr_w(arg));
  345. if (!(client_pid = wcstol(arg, NULL, 10)))
  346. {
  347. ERR("Invalid parameter %s\n", debugstr_w(arg));
  348. return 1;
  349. }
  350. swprintf(buffer, ARRAY_SIZE(buffer), L"\\\\.\\pipe\\msica_%x_%d", client_pid, sizeof(void *) * 8);
  351. pipe = CreateFileW(buffer, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  352. if (pipe == INVALID_HANDLE_VALUE)
  353. {
  354. ERR("Failed to create custom action server pipe: %u\n", GetLastError());
  355. return GetLastError();
  356. }
  357. /* We need this to unmarshal streams, and some apps expect it to be present. */
  358. CoInitializeEx(NULL, COINIT_MULTITHREADED);
  359. while (ReadFile(pipe, &guid, sizeof(guid), &size, NULL) && size == sizeof(guid))
  360. {
  361. if (IsEqualGUID(&guid, &GUID_NULL))
  362. {
  363. /* package closed; time to shut down */
  364. CoUninitialize();
  365. return 0;
  366. }
  367. thread_guid = heap_alloc(sizeof(GUID));
  368. memcpy(thread_guid, &guid, sizeof(GUID));
  369. thread = CreateThread(NULL, 0, custom_action_thread, thread_guid, 0, NULL);
  370. /* give the thread handle to the client to wait on, since we might have
  371. * to run a nested action and can't block during this one */
  372. thread64 = (DWORD_PTR)thread;
  373. if (!WriteFile(pipe, &thread64, sizeof(thread64), &size, NULL) || size != sizeof(thread64))
  374. {
  375. ERR("Failed to write to custom action server pipe: %u\n", GetLastError());
  376. CoUninitialize();
  377. return GetLastError();
  378. }
  379. }
  380. ERR("Failed to read from custom action server pipe: %u\n", GetLastError());
  381. CoUninitialize();
  382. return GetLastError();
  383. }
  384. /*
  385. * state machine to break up the command line properly
  386. */
  387. enum chomp_state
  388. {
  389. CS_WHITESPACE,
  390. CS_TOKEN,
  391. CS_QUOTE
  392. };
  393. static int chomp( const WCHAR *in, WCHAR *out )
  394. {
  395. enum chomp_state state = CS_TOKEN;
  396. const WCHAR *p;
  397. int count = 1;
  398. BOOL ignore;
  399. for (p = in; *p; p++)
  400. {
  401. ignore = TRUE;
  402. switch (state)
  403. {
  404. case CS_WHITESPACE:
  405. switch (*p)
  406. {
  407. case ' ':
  408. break;
  409. case '"':
  410. state = CS_QUOTE;
  411. count++;
  412. break;
  413. default:
  414. count++;
  415. ignore = FALSE;
  416. state = CS_TOKEN;
  417. }
  418. break;
  419. case CS_TOKEN:
  420. switch (*p)
  421. {
  422. case '"':
  423. state = CS_QUOTE;
  424. break;
  425. case ' ':
  426. state = CS_WHITESPACE;
  427. if (out) *out++ = 0;
  428. break;
  429. default:
  430. if (p > in && p[-1] == '"')
  431. {
  432. if (out) *out++ = 0;
  433. count++;
  434. }
  435. ignore = FALSE;
  436. }
  437. break;
  438. case CS_QUOTE:
  439. switch (*p)
  440. {
  441. case '"':
  442. state = CS_TOKEN;
  443. break;
  444. default:
  445. ignore = FALSE;
  446. }
  447. break;
  448. }
  449. if (!ignore && out) *out++ = *p;
  450. }
  451. if (out) *out = 0;
  452. return count;
  453. }
  454. static void process_args( WCHAR *cmdline, int *pargc, WCHAR ***pargv )
  455. {
  456. WCHAR **argv, *p;
  457. int i, count;
  458. *pargc = 0;
  459. *pargv = NULL;
  460. count = chomp( cmdline, NULL );
  461. if (!(p = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(cmdline) + count + 1) * sizeof(WCHAR) )))
  462. return;
  463. count = chomp( cmdline, p );
  464. if (!(argv = HeapAlloc( GetProcessHeap(), 0, (count + 1) * sizeof(WCHAR *) )))
  465. {
  466. HeapFree( GetProcessHeap(), 0, p );
  467. return;
  468. }
  469. for (i = 0; i < count; i++)
  470. {
  471. argv[i] = p;
  472. p += lstrlenW( p ) + 1;
  473. }
  474. argv[i] = NULL;
  475. *pargc = count;
  476. *pargv = argv;
  477. }
  478. static BOOL process_args_from_reg( const WCHAR *ident, int *pargc, WCHAR ***pargv )
  479. {
  480. LONG r;
  481. HKEY hkey;
  482. DWORD sz = 0, type = 0;
  483. WCHAR *buf;
  484. BOOL ret = FALSE;
  485. r = RegOpenKeyW(HKEY_LOCAL_MACHINE,
  486. L"Software\\Microsoft\\Windows\\CurrentVersion\\Installer\\RunOnceEntries", &hkey);
  487. if(r != ERROR_SUCCESS)
  488. return FALSE;
  489. r = RegQueryValueExW(hkey, ident, 0, &type, 0, &sz);
  490. if(r == ERROR_SUCCESS && type == REG_SZ)
  491. {
  492. int len = lstrlenW( *pargv[0] );
  493. if (!(buf = HeapAlloc( GetProcessHeap(), 0, sz + (len + 1) * sizeof(WCHAR) )))
  494. {
  495. RegCloseKey( hkey );
  496. return FALSE;
  497. }
  498. memcpy( buf, *pargv[0], len * sizeof(WCHAR) );
  499. buf[len++] = ' ';
  500. r = RegQueryValueExW(hkey, ident, 0, &type, (LPBYTE)(buf + len), &sz);
  501. if( r == ERROR_SUCCESS )
  502. {
  503. process_args(buf, pargc, pargv);
  504. ret = TRUE;
  505. }
  506. HeapFree(GetProcessHeap(), 0, buf);
  507. }
  508. RegCloseKey(hkey);
  509. return ret;
  510. }
  511. static WCHAR *get_path_with_extension(const WCHAR *package_name)
  512. {
  513. static const WCHAR ext[] = L".msi";
  514. unsigned int p;
  515. WCHAR *path;
  516. if (!(path = heap_alloc(lstrlenW(package_name) * sizeof(WCHAR) + sizeof(ext))))
  517. {
  518. WINE_ERR("No memory.\n");
  519. return NULL;
  520. }
  521. lstrcpyW(path, package_name);
  522. p = lstrlenW(path);
  523. while (p && path[p] != '.' && path[p] != L'\\' && path[p] != '/')
  524. --p;
  525. if (path[p] == '.')
  526. {
  527. heap_free(path);
  528. return NULL;
  529. }
  530. lstrcatW(path, ext);
  531. return path;
  532. }
  533. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  534. {
  535. int i;
  536. BOOL FunctionInstall = FALSE;
  537. BOOL FunctionInstallAdmin = FALSE;
  538. BOOL FunctionRepair = FALSE;
  539. BOOL FunctionAdvertise = FALSE;
  540. BOOL FunctionPatch = FALSE;
  541. BOOL FunctionDllRegisterServer = FALSE;
  542. BOOL FunctionDllUnregisterServer = FALSE;
  543. BOOL FunctionRegServer = FALSE;
  544. BOOL FunctionUnregServer = FALSE;
  545. BOOL FunctionServer = FALSE;
  546. BOOL FunctionUnknown = FALSE;
  547. LPWSTR PackageName = NULL;
  548. LPWSTR Properties = NULL;
  549. struct string_list *property_list = NULL;
  550. DWORD RepairMode = 0;
  551. DWORD_PTR AdvertiseMode = 0;
  552. struct string_list *transform_list = NULL;
  553. LANGID Language = 0;
  554. DWORD LogMode = 0;
  555. LPWSTR LogFileName = NULL;
  556. DWORD LogAttributes = 0;
  557. LPWSTR PatchFileName = NULL;
  558. INSTALLTYPE InstallType = INSTALLTYPE_DEFAULT;
  559. INSTALLUILEVEL InstallUILevel = INSTALLUILEVEL_FULL;
  560. LPWSTR DllName = NULL;
  561. DWORD ReturnCode;
  562. int argc;
  563. LPWSTR *argvW = NULL;
  564. WCHAR *path;
  565. InitCommonControls();
  566. /* parse the command line */
  567. process_args( GetCommandLineW(), &argc, &argvW );
  568. /*
  569. * If the args begin with /@ IDENT then we need to load the real
  570. * command line out of the RunOnceEntries key in the registry.
  571. * We do that before starting to process the real commandline,
  572. * then overwrite the commandline again.
  573. */
  574. if(argc>1 && msi_option_equal(argvW[1], "@"))
  575. {
  576. if(!process_args_from_reg( argvW[2], &argc, &argvW ))
  577. return 1;
  578. }
  579. if (argc == 3 && msi_option_equal(argvW[1], "Embedding"))
  580. return custom_action_server(argvW[2]);
  581. for(i = 1; i < argc; i++)
  582. {
  583. WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
  584. if (msi_option_equal(argvW[i], "regserver"))
  585. {
  586. FunctionRegServer = TRUE;
  587. }
  588. else if (msi_option_equal(argvW[i], "unregserver") || msi_option_equal(argvW[i], "unregister")
  589. || msi_option_equal(argvW[i], "unreg"))
  590. {
  591. FunctionUnregServer = TRUE;
  592. }
  593. else if(msi_option_prefix(argvW[i], "i") || msi_option_prefix(argvW[i], "package"))
  594. {
  595. LPWSTR argvWi = argvW[i];
  596. int argLen = (msi_option_prefix(argvW[i], "i") ? 2 : 8);
  597. FunctionInstall = TRUE;
  598. if(lstrlenW(argvW[i]) > argLen)
  599. argvWi += argLen;
  600. else
  601. {
  602. i++;
  603. if(i >= argc)
  604. ShowUsage(1);
  605. WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
  606. argvWi = argvW[i];
  607. }
  608. PackageName = argvWi;
  609. }
  610. else if(msi_option_equal(argvW[i], "a"))
  611. {
  612. FunctionInstall = TRUE;
  613. FunctionInstallAdmin = TRUE;
  614. InstallType = INSTALLTYPE_NETWORK_IMAGE;
  615. i++;
  616. if(i >= argc)
  617. ShowUsage(1);
  618. WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
  619. PackageName = argvW[i];
  620. StringListAppend(&property_list, L"ACTION=ADMIN");
  621. WINE_FIXME("Administrative installs are not currently supported\n");
  622. }
  623. else if(msi_option_prefix(argvW[i], "f"))
  624. {
  625. int j;
  626. int len = lstrlenW(argvW[i]);
  627. FunctionRepair = TRUE;
  628. for(j = 2; j < len; j++)
  629. {
  630. switch(argvW[i][j])
  631. {
  632. case 'P':
  633. case 'p':
  634. RepairMode |= REINSTALLMODE_FILEMISSING;
  635. break;
  636. case 'O':
  637. case 'o':
  638. RepairMode |= REINSTALLMODE_FILEOLDERVERSION;
  639. break;
  640. case 'E':
  641. case 'e':
  642. RepairMode |= REINSTALLMODE_FILEEQUALVERSION;
  643. break;
  644. case 'D':
  645. case 'd':
  646. RepairMode |= REINSTALLMODE_FILEEXACT;
  647. break;
  648. case 'C':
  649. case 'c':
  650. RepairMode |= REINSTALLMODE_FILEVERIFY;
  651. break;
  652. case 'A':
  653. case 'a':
  654. RepairMode |= REINSTALLMODE_FILEREPLACE;
  655. break;
  656. case 'U':
  657. case 'u':
  658. RepairMode |= REINSTALLMODE_USERDATA;
  659. break;
  660. case 'M':
  661. case 'm':
  662. RepairMode |= REINSTALLMODE_MACHINEDATA;
  663. break;
  664. case 'S':
  665. case 's':
  666. RepairMode |= REINSTALLMODE_SHORTCUT;
  667. break;
  668. case 'V':
  669. case 'v':
  670. RepairMode |= REINSTALLMODE_PACKAGE;
  671. break;
  672. default:
  673. fprintf(stderr, "Unknown option \"%c\" in Repair mode\n", argvW[i][j]);
  674. break;
  675. }
  676. }
  677. if(len == 2)
  678. {
  679. RepairMode = REINSTALLMODE_FILEMISSING |
  680. REINSTALLMODE_FILEEQUALVERSION |
  681. REINSTALLMODE_FILEVERIFY |
  682. REINSTALLMODE_MACHINEDATA |
  683. REINSTALLMODE_SHORTCUT;
  684. }
  685. i++;
  686. if(i >= argc)
  687. ShowUsage(1);
  688. WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
  689. PackageName = argvW[i];
  690. }
  691. else if(msi_option_prefix(argvW[i], "x") || msi_option_equal(argvW[i], "uninstall"))
  692. {
  693. FunctionInstall = TRUE;
  694. if(msi_option_prefix(argvW[i], "x")) PackageName = argvW[i]+2;
  695. if(!PackageName || !PackageName[0])
  696. {
  697. i++;
  698. if (i >= argc)
  699. ShowUsage(1);
  700. PackageName = argvW[i];
  701. }
  702. WINE_TRACE("PackageName = %s\n", wine_dbgstr_w(PackageName));
  703. StringListAppend(&property_list, L"REMOVE=ALL");
  704. }
  705. else if(msi_option_prefix(argvW[i], "j"))
  706. {
  707. int j;
  708. int len = lstrlenW(argvW[i]);
  709. FunctionAdvertise = TRUE;
  710. for(j = 2; j < len; j++)
  711. {
  712. switch(argvW[i][j])
  713. {
  714. case 'U':
  715. case 'u':
  716. AdvertiseMode = ADVERTISEFLAGS_USERASSIGN;
  717. break;
  718. case 'M':
  719. case 'm':
  720. AdvertiseMode = ADVERTISEFLAGS_MACHINEASSIGN;
  721. break;
  722. default:
  723. fprintf(stderr, "Unknown option \"%c\" in Advertise mode\n", argvW[i][j]);
  724. break;
  725. }
  726. }
  727. i++;
  728. if(i >= argc)
  729. ShowUsage(1);
  730. WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
  731. PackageName = argvW[i];
  732. }
  733. else if(msi_strequal(argvW[i], "u"))
  734. {
  735. FunctionAdvertise = TRUE;
  736. AdvertiseMode = ADVERTISEFLAGS_USERASSIGN;
  737. i++;
  738. if(i >= argc)
  739. ShowUsage(1);
  740. WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
  741. PackageName = argvW[i];
  742. }
  743. else if(msi_strequal(argvW[i], "m"))
  744. {
  745. FunctionAdvertise = TRUE;
  746. AdvertiseMode = ADVERTISEFLAGS_MACHINEASSIGN;
  747. i++;
  748. if(i >= argc)
  749. ShowUsage(1);
  750. WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
  751. PackageName = argvW[i];
  752. }
  753. else if(msi_option_equal(argvW[i], "t"))
  754. {
  755. i++;
  756. if(i >= argc)
  757. ShowUsage(1);
  758. WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
  759. StringListAppend(&transform_list, argvW[i]);
  760. }
  761. else if(msi_option_equal(argvW[i], "g"))
  762. {
  763. i++;
  764. if(i >= argc)
  765. ShowUsage(1);
  766. WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
  767. Language = msi_atou(argvW[i]);
  768. }
  769. else if(msi_option_prefix(argvW[i], "l"))
  770. {
  771. int j;
  772. int len = lstrlenW(argvW[i]);
  773. for(j = 2; j < len; j++)
  774. {
  775. switch(argvW[i][j])
  776. {
  777. case 'I':
  778. case 'i':
  779. LogMode |= INSTALLLOGMODE_INFO;
  780. break;
  781. case 'W':
  782. case 'w':
  783. LogMode |= INSTALLLOGMODE_WARNING;
  784. break;
  785. case 'E':
  786. case 'e':
  787. LogMode |= INSTALLLOGMODE_ERROR;
  788. break;
  789. case 'A':
  790. case 'a':
  791. LogMode |= INSTALLLOGMODE_ACTIONSTART;
  792. break;
  793. case 'R':
  794. case 'r':
  795. LogMode |= INSTALLLOGMODE_ACTIONDATA;
  796. break;
  797. case 'U':
  798. case 'u':
  799. LogMode |= INSTALLLOGMODE_USER;
  800. break;
  801. case 'C':
  802. case 'c':
  803. LogMode |= INSTALLLOGMODE_COMMONDATA;
  804. break;
  805. case 'M':
  806. case 'm':
  807. LogMode |= INSTALLLOGMODE_FATALEXIT;
  808. break;
  809. case 'O':
  810. case 'o':
  811. LogMode |= INSTALLLOGMODE_OUTOFDISKSPACE;
  812. break;
  813. case 'P':
  814. case 'p':
  815. LogMode |= INSTALLLOGMODE_PROPERTYDUMP;
  816. break;
  817. case 'V':
  818. case 'v':
  819. LogMode |= INSTALLLOGMODE_VERBOSE;
  820. break;
  821. case '*':
  822. LogMode = INSTALLLOGMODE_FATALEXIT |
  823. INSTALLLOGMODE_ERROR |
  824. INSTALLLOGMODE_WARNING |
  825. INSTALLLOGMODE_USER |
  826. INSTALLLOGMODE_INFO |
  827. INSTALLLOGMODE_RESOLVESOURCE |
  828. INSTALLLOGMODE_OUTOFDISKSPACE |
  829. INSTALLLOGMODE_ACTIONSTART |
  830. INSTALLLOGMODE_ACTIONDATA |
  831. INSTALLLOGMODE_COMMONDATA |
  832. INSTALLLOGMODE_PROPERTYDUMP |
  833. INSTALLLOGMODE_PROGRESS |
  834. INSTALLLOGMODE_INITIALIZE |
  835. INSTALLLOGMODE_TERMINATE |
  836. INSTALLLOGMODE_SHOWDIALOG;
  837. break;
  838. case '+':
  839. LogAttributes |= INSTALLLOGATTRIBUTES_APPEND;
  840. break;
  841. case '!':
  842. LogAttributes |= INSTALLLOGATTRIBUTES_FLUSHEACHLINE;
  843. break;
  844. default:
  845. break;
  846. }
  847. }
  848. i++;
  849. if(i >= argc)
  850. ShowUsage(1);
  851. WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
  852. LogFileName = argvW[i];
  853. if(MsiEnableLogW(LogMode, LogFileName, LogAttributes) != ERROR_SUCCESS)
  854. {
  855. fprintf(stderr, "Logging in %s (0x%08x, %u) failed\n",
  856. wine_dbgstr_w(LogFileName), LogMode, LogAttributes);
  857. ExitProcess(1);
  858. }
  859. }
  860. else if(msi_option_equal(argvW[i], "p") || msi_option_equal(argvW[i], "update"))
  861. {
  862. FunctionPatch = TRUE;
  863. i++;
  864. if(i >= argc)
  865. ShowUsage(1);
  866. WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
  867. PatchFileName = argvW[i];
  868. }
  869. else if(msi_option_prefix(argvW[i], "q"))
  870. {
  871. if(lstrlenW(argvW[i]) == 2 || msi_strequal(argvW[i]+2, "n") ||
  872. msi_strequal(argvW[i] + 2, "uiet"))
  873. {
  874. InstallUILevel = INSTALLUILEVEL_NONE;
  875. }
  876. else if(msi_strequal(argvW[i]+2, "r"))
  877. {
  878. InstallUILevel = INSTALLUILEVEL_REDUCED;
  879. }
  880. else if(msi_strequal(argvW[i]+2, "f"))
  881. {
  882. InstallUILevel = INSTALLUILEVEL_FULL|INSTALLUILEVEL_ENDDIALOG;
  883. }
  884. else if(msi_strequal(argvW[i]+2, "n+"))
  885. {
  886. InstallUILevel = INSTALLUILEVEL_NONE|INSTALLUILEVEL_ENDDIALOG;
  887. }
  888. else if(msi_strprefix(argvW[i]+2, "b"))
  889. {
  890. const WCHAR *ptr = argvW[i] + 3;
  891. InstallUILevel = INSTALLUILEVEL_BASIC;
  892. while (*ptr)
  893. {
  894. if (msi_strprefix(ptr, "+"))
  895. InstallUILevel |= INSTALLUILEVEL_ENDDIALOG;
  896. if (msi_strprefix(ptr, "-"))
  897. InstallUILevel |= INSTALLUILEVEL_PROGRESSONLY;
  898. if (msi_strprefix(ptr, "!"))
  899. {
  900. WINE_FIXME("Unhandled modifier: !\n");
  901. InstallUILevel |= INSTALLUILEVEL_HIDECANCEL;
  902. }
  903. ptr++;
  904. }
  905. }
  906. else
  907. {
  908. fprintf(stderr, "Unknown option \"%s\" for UI level\n",
  909. wine_dbgstr_w(argvW[i]+2));
  910. }
  911. }
  912. else if(msi_option_equal(argvW[i], "passive"))
  913. {
  914. InstallUILevel = INSTALLUILEVEL_BASIC|INSTALLUILEVEL_PROGRESSONLY|INSTALLUILEVEL_HIDECANCEL;
  915. StringListAppend(&property_list, L"REBOOTPROMPT=\"S\"");
  916. }
  917. else if(msi_option_equal(argvW[i], "y"))
  918. {
  919. FunctionDllRegisterServer = TRUE;
  920. i++;
  921. if(i >= argc)
  922. ShowUsage(1);
  923. WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
  924. DllName = argvW[i];
  925. }
  926. else if(msi_option_equal(argvW[i], "z"))
  927. {
  928. FunctionDllUnregisterServer = TRUE;
  929. i++;
  930. if(i >= argc)
  931. ShowUsage(1);
  932. WINE_TRACE("argvW[%d] = %s\n", i, wine_dbgstr_w(argvW[i]));
  933. DllName = argvW[i];
  934. }
  935. else if(msi_option_equal(argvW[i], "help") || msi_option_equal(argvW[i], "?"))
  936. {
  937. ShowUsage(0);
  938. }
  939. else if(msi_option_equal(argvW[i], "m"))
  940. {
  941. FunctionUnknown = TRUE;
  942. WINE_FIXME("Unknown parameter /m\n");
  943. }
  944. else if(msi_option_equal(argvW[i], "D"))
  945. {
  946. FunctionUnknown = TRUE;
  947. WINE_FIXME("Unknown parameter /D\n");
  948. }
  949. else if (msi_option_equal(argvW[i], "V"))
  950. {
  951. FunctionServer = TRUE;
  952. }
  953. else
  954. StringListAppend(&property_list, argvW[i]);
  955. }
  956. /* start the GUI */
  957. MsiSetInternalUI(InstallUILevel, NULL);
  958. Properties = build_properties( property_list );
  959. if(FunctionInstallAdmin && FunctionPatch)
  960. FunctionInstall = FALSE;
  961. ReturnCode = 1;
  962. if(FunctionInstall)
  963. {
  964. if(IsProductCode(PackageName))
  965. ReturnCode = MsiConfigureProductExW(PackageName, 0, INSTALLSTATE_DEFAULT, Properties);
  966. else
  967. {
  968. if ((ReturnCode = MsiInstallProductW(PackageName, Properties)) == ERROR_FILE_NOT_FOUND
  969. && (path = get_path_with_extension(PackageName)))
  970. {
  971. ReturnCode = MsiInstallProductW(path, Properties);
  972. heap_free(path);
  973. }
  974. }
  975. }
  976. else if(FunctionRepair)
  977. {
  978. if(IsProductCode(PackageName))
  979. WINE_FIXME("Product code treatment not implemented yet\n");
  980. else
  981. {
  982. if ((ReturnCode = MsiReinstallProductW(PackageName, RepairMode)) == ERROR_FILE_NOT_FOUND
  983. && (path = get_path_with_extension(PackageName)))
  984. {
  985. ReturnCode = MsiReinstallProductW(path, RepairMode);
  986. heap_free(path);
  987. }
  988. }
  989. }
  990. else if(FunctionAdvertise)
  991. {
  992. LPWSTR Transforms = build_transforms( property_list );
  993. ReturnCode = MsiAdvertiseProductW(PackageName, (LPWSTR) AdvertiseMode, Transforms, Language);
  994. }
  995. else if(FunctionPatch)
  996. {
  997. ReturnCode = MsiApplyPatchW(PatchFileName, PackageName, InstallType, Properties);
  998. }
  999. else if(FunctionDllRegisterServer)
  1000. {
  1001. ReturnCode = DoDllRegisterServer(DllName);
  1002. }
  1003. else if(FunctionDllUnregisterServer)
  1004. {
  1005. ReturnCode = DoDllUnregisterServer(DllName);
  1006. }
  1007. else if (FunctionRegServer)
  1008. {
  1009. ReturnCode = DoRegServer();
  1010. }
  1011. else if (FunctionUnregServer)
  1012. {
  1013. ReturnCode = DoUnregServer();
  1014. }
  1015. else if (FunctionServer)
  1016. {
  1017. ReturnCode = DoService();
  1018. }
  1019. else if (FunctionUnknown)
  1020. {
  1021. WINE_FIXME( "Unknown function, ignoring\n" );
  1022. }
  1023. else
  1024. ShowUsage(1);
  1025. return ReturnCode;
  1026. }