PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/sys/src/cmd/gs/src/dwinst.cpp

https://bitbucket.org/rminnich/sysfromiso
C++ | 956 lines | 817 code | 79 blank | 60 comment | 85 complexity | 7221e04716490f57ed61aef445fe590f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, MPL-2.0-no-copyleft-exception, Unlicense
  1. /* Copyright (C) 1999-2002, Ghostgum Software Pty Ltd. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. // $Id: dwinst.cpp,v 1.6 2004/11/18 06:48:41 ghostgum Exp $
  14. #define STRICT
  15. #include <windows.h>
  16. #include <objbase.h>
  17. #include <shlobj.h>
  18. #include <stdio.h>
  19. #include <io.h>
  20. #include <direct.h>
  21. #include "dwinst.h"
  22. #define UNINSTALLKEY TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall")
  23. #define UNINSTALLSTRINGKEY TEXT("UninstallString")
  24. #define DISPLAYNAMEKEY TEXT("DisplayName")
  25. #define UNINSTALL_FILE "uninstal.txt"
  26. char szSection[] = "////////////////////////////////\n";
  27. #ifdef _MSC_VER
  28. #define mktemp(x) _mktemp(x)
  29. #define chdir(x) _chdir(x)
  30. #define mkdir(x) _mkdir(x)
  31. #endif
  32. //////////////////////////////////////////////////////////////////////
  33. // Construction/Destruction
  34. //////////////////////////////////////////////////////////////////////
  35. CInstall::CInstall()
  36. {
  37. CoInitialize(NULL);
  38. m_szTargetDir[0] = '\0';
  39. m_szTargetGroup[0] = '\0';
  40. m_szPrograms[0] = '\0';
  41. m_szMainDir[0] = '\0';
  42. AddMessageFn = NULL;
  43. SetAllUsers(FALSE);
  44. }
  45. CInstall::~CInstall()
  46. {
  47. CoUninitialize();
  48. }
  49. void CInstall::CleanUp(void)
  50. {
  51. // delete all temporary files
  52. if (m_fLogNew)
  53. fclose(m_fLogNew);
  54. m_fLogNew = NULL;
  55. if (m_fLogOld)
  56. fclose(m_fLogOld);
  57. m_fLogOld = NULL;
  58. if (strlen(m_szRegistryNew))
  59. DeleteFile(m_szRegistryNew);
  60. m_szRegistryNew[0] = '\0';
  61. if (strlen(m_szRegistryOld))
  62. DeleteFile(m_szRegistryOld);
  63. m_szRegistryOld[0] = '\0';
  64. if (strlen(m_szShellNew))
  65. DeleteFile(m_szShellNew);
  66. m_szShellNew[0] = '\0';
  67. if (strlen(m_szShellOld))
  68. DeleteFile(m_szShellOld);
  69. m_szShellOld[0] = '\0';
  70. if (strlen(m_szFileNew))
  71. DeleteFile(m_szFileNew);
  72. m_szFileNew[0] = '\0';
  73. }
  74. void CInstall::SetMessageFunction(void(*fn)(const char *))
  75. {
  76. AddMessageFn = fn;
  77. }
  78. void CInstall::AddMessage(const char *message)
  79. {
  80. if (AddMessageFn)
  81. (*AddMessageFn)(message);
  82. }
  83. void CInstall::SetTargetDir(const char *szTargetDir)
  84. {
  85. strcpy(m_szTargetDir, szTargetDir);
  86. // remove trailing backslash
  87. char *p;
  88. p = m_szTargetDir + strlen(m_szTargetDir) - 1;
  89. if (*p == '\\')
  90. *p = '\0';
  91. }
  92. void CInstall::SetTargetGroup(const char *szTargetGroup)
  93. {
  94. strcpy(m_szTargetGroup, szTargetGroup);
  95. // remove trailing backslash
  96. char *p;
  97. p = m_szTargetGroup + strlen(m_szTargetGroup) - 1;
  98. if (*p == '\\')
  99. *p = '\0';
  100. }
  101. const char *CInstall::GetMainDir()
  102. {
  103. return m_szMainDir;
  104. }
  105. const char *CInstall::GetUninstallName()
  106. {
  107. return m_szUninstallName;
  108. }
  109. BOOL CInstall::Init(const char *szSourceDir, const char *szFileList)
  110. {
  111. FILE *f;
  112. strcpy(m_szSourceDir, szSourceDir);
  113. // remove trailing backslash
  114. char *p;
  115. p = m_szSourceDir + strlen(m_szSourceDir) - 1;
  116. if (*p == '\\')
  117. *p = '\0';
  118. strcpy(m_szFileList, szFileList);
  119. m_szRegistryNew[0] = m_szRegistryOld[0] =
  120. m_szShellNew[0] = m_szShellOld[0] =
  121. m_szFileNew[0] = '\0';
  122. // Open list of files
  123. SetCurrentDirectory(m_szSourceDir);
  124. f = fopen(m_szFileList, "r");
  125. if (f == (FILE *)NULL) {
  126. char buf[MAXSTR];
  127. wsprintf(buf, "Failed to open \042%s\042\n", m_szFileList);
  128. AddMessage(buf);
  129. return FALSE;
  130. }
  131. // get application and directory name
  132. m_szUninstallName[0] = '\0';
  133. if (!fgets(m_szUninstallName, sizeof(m_szUninstallName), f)) {
  134. AddMessage("Invalid file list\n");
  135. fclose(f);
  136. return FALSE;
  137. }
  138. if (*m_szUninstallName )
  139. m_szUninstallName[strlen(m_szUninstallName)-1] = '\0';
  140. m_szMainDir[0] = '\0';
  141. if (!fgets(m_szMainDir, sizeof(m_szMainDir), f)) {
  142. AddMessage("Invalid file list\n");
  143. fclose(f);
  144. return FALSE;
  145. }
  146. if (*m_szMainDir )
  147. m_szMainDir[strlen(m_szMainDir)-1] = '\0';
  148. fclose(f);
  149. // Create log directory
  150. strcpy(m_szLogDir, m_szTargetDir);
  151. strcat(m_szLogDir, "\\");
  152. strcat(m_szLogDir, m_szMainDir);
  153. MakeDir(m_szLogDir);
  154. return TRUE;
  155. }
  156. //////////////////////////////////////////
  157. // File installation methods
  158. BOOL CInstall::InstallFiles(BOOL bNoCopy, BOOL *pbQuit)
  159. {
  160. char szLogNew[MAXSTR];
  161. AddMessage(bNoCopy ? "Checking" : "Copying");
  162. AddMessage(" files listed in ");
  163. AddMessage(m_szFileList);
  164. AddMessage("\n");
  165. // Open list of files
  166. SetCurrentDirectory(m_szSourceDir);
  167. FILE *f = fopen(m_szFileList, "r");
  168. if (f == (FILE *)NULL) {
  169. AddMessage("Failed to open \042");
  170. AddMessage(m_szFileList);
  171. AddMessage("\042\n");
  172. return FALSE;
  173. }
  174. // skip application and directory name
  175. fgets(szLogNew, sizeof(szLogNew), f);
  176. fgets(szLogNew, sizeof(szLogNew), f);
  177. // Create target log
  178. m_fLogNew = MakeTemp(m_szFileNew);
  179. if (!m_fLogNew) {
  180. AddMessage("Failed to create FileNew temporary file\n");
  181. return FALSE;
  182. }
  183. // Copy files
  184. char line[MAXSTR];
  185. while (fgets(line, sizeof(line), f) != (char *)NULL) {
  186. if (*pbQuit)
  187. return FALSE;
  188. if (*line)
  189. line[strlen(line)-1] = '\0';
  190. if (!InstallFile(line, bNoCopy)) {
  191. fclose(f);
  192. fclose(m_fLogNew);
  193. return FALSE;
  194. }
  195. }
  196. fclose(f);
  197. fclose(m_fLogNew);
  198. m_fLogNew = NULL;
  199. return TRUE;
  200. }
  201. void CInstall::AppendFileNew(const char *filename)
  202. {
  203. FILE *f;
  204. /* mark backup file for uninstall */
  205. if ((f = fopen(m_szFileNew, "a")) != (FILE *)NULL) {
  206. fputs(filename, f);
  207. fputs("\n", f);
  208. fclose(f);
  209. }
  210. }
  211. // recursive mkdir
  212. // requires a full path to be specified, so ignores root \
  213. // apart from root \, must not contain trailing \
  214. // Examples:
  215. // c:\ (OK, but useless)
  216. // c:\gstools (OK)
  217. // c:\gstools\ (incorrect)
  218. // c:gstools (incorrect)
  219. // gstools (incorrect)
  220. // The following UNC names should work,
  221. // but didn't under Win3.1 because gs_chdir wouldn't accept UNC names
  222. // Needs to be tested under Windows 95.
  223. // \\server\sharename\gstools (OK)
  224. // \\server\sharename\ (OK, but useless)
  225. //
  226. BOOL CInstall::MakeDir(const char *dirname)
  227. {
  228. char newdir[MAXSTR];
  229. const char *p;
  230. if (strlen(dirname) < 3)
  231. return -1;
  232. if (isalpha(dirname[0]) && dirname[1]==':' && dirname[2]=='\\') {
  233. // drive mapped path
  234. p = dirname+3;
  235. }
  236. else if (dirname[1]=='\\' && dirname[1]=='\\') {
  237. // UNC path
  238. p = strchr(dirname+2, '\\'); // skip servername
  239. if (p == NULL)
  240. return -1;
  241. p++;
  242. p = strchr(p, '\\'); // skip sharename
  243. if (p == NULL)
  244. return -1;
  245. }
  246. else {
  247. // not full path so error
  248. return -1;
  249. }
  250. while (1) {
  251. strncpy(newdir, dirname, (int)(p-dirname));
  252. newdir[(int)(p-dirname)] = '\0';
  253. if (chdir(newdir)) {
  254. if (mkdir(newdir))
  255. return -1;
  256. }
  257. p++;
  258. if (p >= dirname + strlen(dirname))
  259. break; // all done
  260. p = strchr(p, '\\');
  261. if (p == NULL)
  262. p = dirname + strlen(dirname);
  263. }
  264. return SetCurrentDirectory(dirname);
  265. }
  266. void CInstall::ResetReadonly(const char *filename)
  267. {
  268. DWORD dwAttr = GetFileAttributes(filename);
  269. if (dwAttr & FILE_ATTRIBUTE_READONLY)
  270. SetFileAttributes(filename, dwAttr & (~FILE_ATTRIBUTE_READONLY));
  271. }
  272. BOOL CInstall::InstallFile(char *filename, BOOL bNoCopy)
  273. {
  274. char existing_name[MAXSTR];
  275. char new_name[MAXSTR];
  276. char dir_name[MAXSTR];
  277. strcpy(existing_name, m_szSourceDir);
  278. strcat(existing_name, "\\");
  279. strcat(existing_name, filename);
  280. strcpy(new_name, m_szTargetDir);
  281. strcat(new_name, "\\");
  282. strcat(new_name, filename);
  283. strcpy(dir_name, new_name);
  284. char *p = strrchr(dir_name, '\\');
  285. if (p) {
  286. *p = '\0';
  287. if (!MakeDir(dir_name)) {
  288. AddMessage("Failed to make directory ");
  289. AddMessage(dir_name);
  290. AddMessage("\n");
  291. return FALSE;
  292. }
  293. }
  294. AddMessage(" ");
  295. AddMessage(new_name);
  296. AddMessage("\n");
  297. if (bNoCopy) {
  298. // Don't copy files. Leave them where they are.
  299. // Check that all files exist
  300. FILE *f;
  301. if ((f = fopen(existing_name, "r")) == (FILE *)NULL) {
  302. AddMessage("Missing file ");
  303. AddMessage(existing_name);
  304. AddMessage("\n");
  305. return FALSE;
  306. }
  307. fclose(f);
  308. }
  309. else {
  310. if (!CopyFile(existing_name, new_name, FALSE)) {
  311. char message[MAXSTR+MAXSTR+100];
  312. wsprintf(message, "Failed to copy file %s to %s\n",
  313. existing_name, new_name);
  314. AddMessage(message);
  315. return FALSE;
  316. }
  317. ResetReadonly(new_name);
  318. fputs(new_name, m_fLogNew);
  319. fputs("\n", m_fLogNew);
  320. }
  321. return TRUE;
  322. }
  323. //////////////////////////////////////////
  324. // Shell methods
  325. BOOL CInstall::StartMenuBegin()
  326. {
  327. m_fLogNew = MakeTemp(m_szShellNew);
  328. if (!m_fLogNew) {
  329. AddMessage("Failed to create ShellNew temporary file\n");
  330. return FALSE;
  331. }
  332. m_fLogOld = MakeTemp(m_szShellOld);
  333. if (!m_fLogOld) {
  334. AddMessage("Failed to create ShellNew temporary file\n");
  335. return FALSE;
  336. }
  337. // make folder if needed
  338. char szLink[MAXSTR];
  339. strcpy(szLink, m_szPrograms);
  340. strcat(szLink, "\\");
  341. strcat(szLink, m_szTargetGroup);
  342. if (chdir(szLink) != 0) {
  343. if (mkdir(szLink) != 0) {
  344. char buf[MAXSTR+64];
  345. wsprintf(buf, "Couldn't make Programs folder \042%s'042", szLink);
  346. AddMessage(buf);
  347. StartMenuEnd();
  348. return FALSE;
  349. }
  350. }
  351. else {
  352. fprintf(m_fLogOld, "Group=%s\n\n", szLink);
  353. }
  354. fprintf(m_fLogNew, "Group=%s\n\n", szLink);
  355. return TRUE;
  356. }
  357. BOOL CInstall::StartMenuEnd()
  358. {
  359. if (m_fLogOld)
  360. fclose(m_fLogOld);
  361. m_fLogOld = NULL;
  362. if (m_fLogNew)
  363. fclose(m_fLogNew);
  364. m_fLogNew = NULL;
  365. return TRUE;
  366. }
  367. BOOL CInstall::StartMenuAdd(const char *szDescription,
  368. const char *szProgram, const char *szArguments)
  369. {
  370. if (!CreateShellLink(szDescription, szProgram, szArguments)) {
  371. AddMessage("Couldn't make shell link for ");
  372. AddMessage(szDescription);
  373. AddMessage("\n");
  374. StartMenuEnd();
  375. return FALSE;
  376. }
  377. return TRUE;
  378. }
  379. BOOL CInstall::CreateShellLink(LPCSTR description, LPCSTR program,
  380. LPCSTR arguments, LPCSTR icon, int nIconIndex)
  381. {
  382. HRESULT hres;
  383. IShellLink* psl;
  384. CHAR szLink[MAXSTR];
  385. strcpy(szLink, m_szPrograms);
  386. strcat(szLink, "\\");
  387. strcat(szLink, m_szTargetGroup);
  388. strcat(szLink, "\\");
  389. strcat(szLink, description);
  390. strcat(szLink, ".LNK");
  391. AddMessage("Adding shell link\n ");
  392. AddMessage(szLink);
  393. AddMessage("\n");
  394. // Ensure string is UNICODE.
  395. WCHAR wsz[MAX_PATH];
  396. MultiByteToWideChar(CP_ACP, 0, szLink, -1, wsz, MAX_PATH);
  397. // Save old shell link
  398. // Get a pointer to the IShellLink interface.
  399. hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
  400. IID_IShellLink, (void **)&psl);
  401. if (SUCCEEDED(hres)) {
  402. IPersistFile* ppf;
  403. // Query IShellLink for the IPersistFile interface.
  404. hres = psl->QueryInterface(IID_IPersistFile, (void **)&ppf);
  405. if (SUCCEEDED(hres)) {
  406. // Load the shell link.
  407. hres = ppf->Load(wsz, STGM_READ);
  408. if (SUCCEEDED(hres)) {
  409. // Resolve the link.
  410. hres = psl->Resolve(HWND_DESKTOP, SLR_ANY_MATCH);
  411. if (SUCCEEDED(hres)) {
  412. // found it, so save details
  413. CHAR szTemp[MAXSTR];
  414. WIN32_FIND_DATA wfd;
  415. int i;
  416. fprintf(m_fLogOld, "Name=%s\n", szLink);
  417. hres = psl->GetPath(szTemp, MAXSTR, (WIN32_FIND_DATA *)&wfd,
  418. SLGP_SHORTPATH );
  419. if (SUCCEEDED(hres))
  420. fprintf(m_fLogOld, "Path=%s\n", szTemp);
  421. hres = psl->GetDescription(szTemp, MAXSTR);
  422. if (SUCCEEDED(hres))
  423. fprintf(m_fLogOld, "Description=%s\n", szTemp);
  424. hres = psl->GetArguments(szTemp, MAXSTR);
  425. if (SUCCEEDED(hres) && (szTemp[0] != '\0'))
  426. fprintf(m_fLogOld, "Arguments=%s\n", szTemp);
  427. hres = psl->GetWorkingDirectory(szTemp, MAXSTR);
  428. if (SUCCEEDED(hres) && (szTemp[0] != '\0'))
  429. fprintf(m_fLogOld, "Directory=%s\n", szTemp);
  430. hres = psl->GetIconLocation(szTemp, MAXSTR, &i);
  431. if (SUCCEEDED(hres) && (szTemp[0] != '\0')) {
  432. fprintf(m_fLogOld, "IconLocation=%s\n", szTemp);
  433. fprintf(m_fLogOld, "IconIndex=%d\n", i);
  434. }
  435. fprintf(m_fLogOld, "\n");
  436. }
  437. }
  438. // Release pointer to IPersistFile.
  439. ppf->Release();
  440. }
  441. // Release pointer to IShellLink.
  442. psl->Release();
  443. }
  444. // Save new shell link
  445. // Get a pointer to the IShellLink interface.
  446. hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
  447. IID_IShellLink, (void **)&psl);
  448. if (SUCCEEDED(hres)) {
  449. IPersistFile* ppf;
  450. // Query IShellLink for the IPersistFile interface for
  451. // saving the shell link in persistent storage.
  452. hres = psl->QueryInterface(IID_IPersistFile, (void **)&ppf);
  453. if (SUCCEEDED(hres)) {
  454. fprintf(m_fLogNew, "Name=%s\n", szLink);
  455. // Set the path to the shell link target.
  456. hres = psl->SetPath(program);
  457. if (!SUCCEEDED(hres))
  458. AddMessage("SetPath failed!");
  459. fprintf(m_fLogNew, "Path=%s\n", program);
  460. // Set the description of the shell link.
  461. hres = psl->SetDescription(description);
  462. if (!SUCCEEDED(hres))
  463. AddMessage("SetDescription failed!");
  464. fprintf(m_fLogNew, "Description=%s\n", description);
  465. if (arguments != (LPCSTR)NULL) {
  466. // Set the arguments of the shell link target.
  467. hres = psl->SetArguments(arguments);
  468. if (!SUCCEEDED(hres))
  469. AddMessage("SetArguments failed!");
  470. fprintf(m_fLogNew, "Arguments=%s\n", arguments);
  471. }
  472. if (icon != (LPCSTR)NULL) {
  473. // Set the arguments of the shell link target.
  474. hres = psl->SetIconLocation(icon, nIconIndex);
  475. if (!SUCCEEDED(hres))
  476. AddMessage("SetIconLocation failed!");
  477. fprintf(m_fLogNew, "IconLocation=%s\n", icon);
  478. fprintf(m_fLogNew, "IconIndex=%d\n", nIconIndex);
  479. }
  480. // Save the link via the IPersistFile::Save method.
  481. hres = ppf->Save(wsz, TRUE);
  482. // Release pointer to IPersistFile.
  483. ppf->Release();
  484. }
  485. // Release pointer to IShellLink.
  486. psl->Release();
  487. fprintf(m_fLogNew, "\n");
  488. }
  489. return (hres == 0);
  490. }
  491. //////////////////////////////////////////
  492. // Registry methods
  493. void
  494. reg_quote(char *d, const char *s)
  495. {
  496. while (*s) {
  497. if (*s == '\\')
  498. *d++ = '\\';
  499. *d++ = *s++;
  500. }
  501. *d = *s;
  502. }
  503. BOOL CInstall::UpdateRegistryBegin()
  504. {
  505. const char regheader[]="REGEDIT4\n";
  506. m_fLogNew = MakeTemp(m_szRegistryNew);
  507. if (!m_fLogNew) {
  508. AddMessage("Failed to create RegistryNew temporary file\n");
  509. return FALSE;
  510. }
  511. fputs(regheader, m_fLogNew);
  512. m_fLogOld = MakeTemp(m_szRegistryOld);
  513. if (!m_fLogOld) {
  514. AddMessage("Failed to create RegistryOld temporary file\n");
  515. UpdateRegistryEnd();
  516. return FALSE;
  517. }
  518. fputs(regheader, m_fLogOld);
  519. return TRUE;
  520. }
  521. BOOL CInstall::UpdateRegistryEnd()
  522. {
  523. if (m_fLogNew)
  524. fclose(m_fLogNew);
  525. m_fLogNew = NULL;
  526. if (m_fLogOld)
  527. fclose(m_fLogOld);
  528. m_fLogOld = NULL;
  529. return TRUE;
  530. }
  531. BOOL CInstall::UpdateRegistryKey(const char *product, const char *version)
  532. {
  533. const char hkey_name[] = "HKEY_LOCAL_MACHINE";
  534. const HKEY hkey_key = HKEY_LOCAL_MACHINE;
  535. const char key_format[] = "\n[%s\\%s]\n";
  536. /* Create default registry entries */
  537. HKEY hkey;
  538. LONG lrc;
  539. char name[MAXSTR];
  540. // Create/Open application key
  541. sprintf(name, "SOFTWARE\\%s", product);
  542. lrc = RegOpenKey(hkey_key, name, &hkey);
  543. if (lrc == ERROR_SUCCESS) {
  544. fprintf(m_fLogOld, key_format, hkey_name, name);
  545. }
  546. else {
  547. lrc = RegCreateKey(hkey_key, name, &hkey);
  548. if (lrc == ERROR_SUCCESS)
  549. fprintf(m_fLogNew, key_format, hkey_name, name);
  550. }
  551. if (lrc == ERROR_SUCCESS)
  552. RegCloseKey(hkey);
  553. // Create/Open application version key
  554. sprintf(name, "SOFTWARE\\%s\\%s", product, version);
  555. AddMessage(" ");
  556. AddMessage(hkey_name);
  557. AddMessage("\\");
  558. AddMessage(name);
  559. AddMessage("\n");
  560. lrc = RegOpenKey(hkey_key, name, &hkey);
  561. if (lrc == ERROR_SUCCESS)
  562. fprintf(m_fLogOld, key_format, hkey_name, name);
  563. else
  564. lrc = RegCreateKey(hkey_key, name, &hkey);
  565. if (lrc == ERROR_SUCCESS) {
  566. fprintf(m_fLogNew, key_format, hkey_name, name);
  567. }
  568. else {
  569. UpdateRegistryEnd();
  570. }
  571. return TRUE;
  572. }
  573. BOOL CInstall::UpdateRegistryValue(const char *product, const char *version,
  574. const char *name, const char *value)
  575. {
  576. char appver[MAXSTR];
  577. BOOL flag = FALSE;
  578. HKEY hkey;
  579. // Open application/version key
  580. sprintf(appver, "SOFTWARE\\%s\\%s", product, version);
  581. if (RegOpenKey(HKEY_LOCAL_MACHINE, appver, &hkey)
  582. == ERROR_SUCCESS) {
  583. flag = SetRegistryValue(hkey, name, value);
  584. RegCloseKey(hkey);
  585. }
  586. return flag;
  587. }
  588. BOOL CInstall::SetRegistryValue(HKEY hkey, const char *value_name, const char *value)
  589. {
  590. char buf[MAXSTR];
  591. char qbuf[MAXSTR];
  592. DWORD cbData;
  593. DWORD keytype;
  594. cbData = sizeof(buf);
  595. keytype = REG_SZ;
  596. if (RegQueryValueEx(hkey, value_name, 0, &keytype,
  597. (LPBYTE)buf, &cbData) == ERROR_SUCCESS) {
  598. reg_quote(qbuf, buf);
  599. fprintf(m_fLogOld, "\042%s\042=\042%s\042\n", value_name, qbuf);
  600. }
  601. reg_quote(qbuf, value);
  602. fprintf(m_fLogNew, "\042%s\042=\042%s\042\n", value_name, qbuf);
  603. AddMessage(" ");
  604. AddMessage(value_name);
  605. AddMessage("=");
  606. AddMessage(value);
  607. AddMessage("\n");
  608. if (RegSetValueEx(hkey, value_name, 0, REG_SZ,
  609. (CONST BYTE *)value, strlen(value)+1) != ERROR_SUCCESS)
  610. return FALSE;
  611. return TRUE;
  612. }
  613. ////////////////////////////////////
  614. // Uninstall
  615. BOOL CInstall::WriteUninstall(const char *szProg, BOOL bNoCopy)
  616. {
  617. LONG rc;
  618. HKEY hkey;
  619. HKEY hsubkey;
  620. char buffer[MAXSTR];
  621. char ungsprog[MAXSTR];
  622. lstrcpy(ungsprog, m_szTargetDir);
  623. lstrcat(ungsprog, "\\");
  624. lstrcat(ungsprog, szProg);
  625. lstrcpy(buffer, m_szSourceDir);
  626. lstrcat(buffer, "\\");
  627. lstrcat(buffer, szProg);
  628. if (bNoCopy) {
  629. // Don't copy files. Leave them where they are.
  630. // Check that all files exist
  631. FILE *f;
  632. if ((f = fopen(buffer, "r")) == (FILE *)NULL) {
  633. AddMessage("Missing file ");
  634. AddMessage(buffer);
  635. AddMessage("\n");
  636. return FALSE;
  637. }
  638. fclose(f);
  639. }
  640. else if (!CopyFile(buffer, ungsprog, FALSE)) {
  641. char message[MAXSTR+MAXSTR+100];
  642. wsprintf(message, "Failed to copy file %s to %s", buffer, ungsprog);
  643. AddMessage(message);
  644. return FALSE;
  645. }
  646. ResetReadonly(ungsprog);
  647. /* write registry entries for uninstall */
  648. if ((rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, UNINSTALLKEY, 0,
  649. KEY_ALL_ACCESS, &hkey)) != ERROR_SUCCESS) {
  650. /* failed to open key, so try to create it */
  651. rc = RegCreateKey(HKEY_LOCAL_MACHINE, UNINSTALLKEY, &hkey);
  652. }
  653. if (rc == ERROR_SUCCESS) {
  654. // Uninstall key for program
  655. if (RegCreateKey(hkey, m_szUninstallName, &hsubkey) == ERROR_SUCCESS) {
  656. RegSetValueEx(hsubkey, DISPLAYNAMEKEY, 0, REG_SZ,
  657. (CONST BYTE *)m_szUninstallName, lstrlen(m_szUninstallName)+1);
  658. lstrcpy(buffer, ungsprog);
  659. lstrcat(buffer, " \042");
  660. lstrcat(buffer, m_szTargetDir);
  661. lstrcat(buffer, "\\");
  662. lstrcat(buffer, m_szMainDir);
  663. lstrcat(buffer, "\\");
  664. lstrcat(buffer, UNINSTALL_FILE);
  665. lstrcat(buffer, "\042");
  666. AddMessage(" ");
  667. AddMessage(m_szUninstallName);
  668. AddMessage("=");
  669. AddMessage(buffer);
  670. AddMessage("\n");
  671. RegSetValueEx(hsubkey, UNINSTALLSTRINGKEY, 0, REG_SZ,
  672. (CONST BYTE *)buffer, lstrlen(buffer)+1);
  673. RegCloseKey(hsubkey);
  674. }
  675. RegCloseKey(hkey);
  676. }
  677. return TRUE;
  678. }
  679. void
  680. CInstall::CopyFileContents(FILE *df, FILE *sf)
  681. {
  682. char buf[MAXSTR];
  683. int count;
  684. while ((count = fread(buf, 1, sizeof(buf), sf)) != 0)
  685. fwrite(buf, 1, count, df);
  686. }
  687. FILE *CInstall::MakeTemp(char *fname)
  688. {
  689. char *temp;
  690. if ( (temp = getenv("TEMP")) == NULL )
  691. strcpy(fname, m_szTargetDir);
  692. else
  693. strcpy(fname, temp);
  694. /* Prevent X's in path from being converted by mktemp. */
  695. for ( temp = fname; *temp; temp++ ) {
  696. *temp = (char)tolower(*temp);
  697. if (*temp == '/')
  698. *temp = '\\';
  699. }
  700. if ( strlen(fname) && (fname[strlen(fname)-1] != '\\') )
  701. strcat(fname, "\\");
  702. strcat(fname, "gsXXXXXX");
  703. mktemp(fname);
  704. AddMessage("Creating temporary file ");
  705. AddMessage(fname);
  706. AddMessage("\n");
  707. return fopen(fname, "w");
  708. }
  709. BOOL CInstall::MakeLog()
  710. {
  711. FILE *f, *lf;
  712. char szFileName[MAXSTR];
  713. char szLogDir[MAXSTR];
  714. strcpy(szLogDir, m_szTargetDir);
  715. strcat(szLogDir, "\\");
  716. strcat(szLogDir, m_szMainDir);
  717. strcat(szLogDir, "\\");
  718. strcpy(szFileName, szLogDir);
  719. strcat(szFileName, UNINSTALL_FILE);
  720. lf = fopen(szFileName, "w");
  721. if (lf == (FILE *)NULL) {
  722. AddMessage("Can't create uninstall log");
  723. CleanUp();
  724. return FALSE;
  725. }
  726. fputs(szSection, lf);
  727. fputs("UninstallName\n", lf);
  728. fputs(m_szUninstallName, lf);
  729. fputs("\n\n", lf);
  730. if (strlen(m_szRegistryNew) &&
  731. (f = fopen(m_szRegistryNew, "r")) != (FILE *)NULL) {
  732. fputs(szSection, lf);
  733. fputs("RegistryNew\n", lf);
  734. CopyFileContents(lf, f);
  735. fputs("\n", lf);
  736. fclose(f);
  737. DeleteFile(m_szRegistryNew);
  738. m_szRegistryNew[0] = '\0';
  739. }
  740. if (strlen(m_szRegistryOld) &&
  741. (f = fopen(m_szRegistryOld, "r")) != (FILE *)NULL) {
  742. fputs(szSection, lf);
  743. fputs("RegistryOld\n", lf);
  744. CopyFileContents(lf, f);
  745. fputs("\n", lf);
  746. fclose(f);
  747. DeleteFile(m_szRegistryOld);
  748. m_szRegistryOld[0] = '\0';
  749. }
  750. if (strlen(m_szShellNew) &&
  751. (f = fopen(m_szShellNew, "r")) != (FILE *)NULL) {
  752. fputs(szSection, lf);
  753. fputs("ShellNew\n", lf);
  754. CopyFileContents(lf, f);
  755. fputs("\n", lf);
  756. fclose(f);
  757. DeleteFile(m_szShellNew);
  758. m_szShellNew[0] = '\0';
  759. }
  760. if (strlen(m_szShellOld) &&
  761. (f = fopen(m_szShellOld, "r")) != (FILE *)NULL) {
  762. fputs(szSection, lf);
  763. fputs("ShellOld\n", lf);
  764. CopyFileContents(lf, f);
  765. fputs("\n", lf);
  766. fclose(f);
  767. DeleteFile(m_szShellOld);
  768. m_szShellOld[0] = '\0';
  769. }
  770. if (strlen(m_szFileNew) &&
  771. (f = fopen(m_szFileNew, "r")) != (FILE *)NULL) {
  772. fputs(szSection, lf);
  773. fputs("FileNew\n", lf);
  774. CopyFileContents(lf, f);
  775. fputs("\n", lf);
  776. fclose(f);
  777. DeleteFile(m_szFileNew);
  778. m_szFileNew[0] = '\0';
  779. }
  780. fputs(szSection, lf);
  781. fclose(lf);
  782. return TRUE;
  783. }
  784. BOOL CInstall::GetPrograms(BOOL bUseCommon, char *buf, int buflen)
  785. {
  786. // Get the directory for the Program menu. This is
  787. // stored in the Registry under HKEY_CURRENT_USER\Software\
  788. // Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Programs.
  789. LONG rc;
  790. HKEY hCU;
  791. DWORD dwType;
  792. ULONG ulSize = buflen;
  793. HKEY hrkey = HKEY_CURRENT_USER;
  794. if (bUseCommon)
  795. hrkey = HKEY_LOCAL_MACHINE;
  796. if (RegOpenKeyEx(hrkey,
  797. "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
  798. 0,KEY_QUERY_VALUE,
  799. &hCU) == ERROR_SUCCESS) {
  800. rc = RegQueryValueEx( hCU,
  801. bUseCommon ? "Common Programs" : "Programs",
  802. NULL,
  803. &dwType,
  804. (unsigned char *)buf,
  805. &ulSize);
  806. RegCloseKey(hCU);
  807. return TRUE;
  808. }
  809. return FALSE;
  810. #ifdef NOTUSED
  811. // This is an alternate version, but it needs
  812. // Internet Explorer 4.0 with Web Integrated Desktop.
  813. // It does not work with the standard
  814. // Windows 95, Windows NT 4.0, Internet Explorer 3.0,
  815. // and Internet Explorer 4.0 without Web Integrated Desktop.
  816. HRESULT rc;
  817. m_szPrograms[0] = '\0';
  818. int nFolder = CSIDL_PROGRAMS;
  819. if (bUseCommon)
  820. nFolder = CSIDL_COMMON_PROGRAMS;
  821. rc = SHGetSpecialFolderPath(HWND_DESKTOP, m_szPrograms,
  822. nFolder, FALSE);
  823. return (rc == NOERROR);
  824. #endif
  825. }
  826. BOOL CInstall::SetAllUsers(BOOL bUseCommon)
  827. {
  828. m_bUseCommon = bUseCommon;
  829. return GetPrograms(bUseCommon, m_szPrograms, sizeof(m_szPrograms));
  830. }
  831. //////////////////////////////////////////////////////////////////////