Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
2.7 KiB

  1. // update.cpp
  2. //
  3. // Copyright 2000 Microsoft Corporation, all rights reserved
  4. //
  5. // Created 2-00 - anbrad
  6. //
  7. #include "update.h"
  8. #include "main.h"
  9. #include <shlwapi.h>
  10. #include "resource.h"
  11. const TCHAR c_szNetwatch[] = TEXT("\\\\scratch\\scratch\\anbrad\\netwatch.exe");
  12. bool WaitForOrigProcess()
  13. {
  14. DWORD dw;
  15. LPCTSTR szCmd = GetCommandLine();
  16. LPCTSTR szSpace;
  17. szSpace = szCmd;
  18. while (*szSpace)
  19. {
  20. if (*szSpace == ' ')
  21. break;
  22. ++szSpace;
  23. }
  24. if (*szSpace)
  25. {
  26. ++szSpace;
  27. dw = atol(szSpace);
  28. WaitForSingleObject ((HANDLE)dw, INFINITE);
  29. return true;
  30. }
  31. return false;
  32. }
  33. bool CheckForUpdate()
  34. {
  35. bool rt;
  36. bool bDel;
  37. FILETIME ftCurrent;
  38. FILETIME ftScratch;
  39. WIN32_FIND_DATA ffd;
  40. HANDLE h = INVALID_HANDLE_VALUE;
  41. HANDLE hDel = INVALID_HANDLE_VALUE;
  42. HANDLE hOurProcess = NULL;
  43. TCHAR szModule[MAX_PATH];
  44. TCHAR szDeleteModule[MAX_PATH];
  45. TCHAR szCommand[128];
  46. bDel = WaitForOrigProcess();
  47. h = FindFirstFile(c_szNetwatch, &ffd);
  48. if (INVALID_HANDLE_VALUE == h)
  49. {
  50. rt = true;
  51. goto Exit;
  52. }
  53. ftScratch = ffd.ftLastWriteTime;
  54. GetModuleFileName(g_hInst, szModule, sizeof(szModule)/sizeof(TCHAR));
  55. FindClose(h);
  56. h = FindFirstFile(szModule, &ffd);
  57. if (INVALID_HANDLE_VALUE == h)
  58. {
  59. rt = true;
  60. goto Exit;
  61. }
  62. // netwatch1.exe
  63. _tcscpy (szDeleteModule, szModule);
  64. PathRemoveExtension(szDeleteModule);
  65. _tcscat(szDeleteModule, "1");
  66. PathAddExtension(szDeleteModule, ".exe");
  67. if (bDel)
  68. DeleteFile(szDeleteModule);
  69. ftCurrent = ffd.ftLastWriteTime;
  70. if ((ftCurrent.dwHighDateTime > ftScratch.dwHighDateTime) ||
  71. ((ftCurrent.dwHighDateTime == ftScratch.dwHighDateTime) &&
  72. (ftCurrent.dwLowDateTime >= ftScratch.dwLowDateTime)))
  73. {
  74. rt = true;
  75. goto Exit;
  76. }
  77. // If we get here then we need to UPDATE
  78. //
  79. // Rename current file
  80. //
  81. if (!MoveFile(szModule, szDeleteModule))
  82. {
  83. rt = true;
  84. goto Exit;
  85. }
  86. if (!CopyFile(c_szNetwatch, szModule, TRUE))
  87. {
  88. rt = true;
  89. goto Exit;
  90. }
  91. hOurProcess = OpenProcess(SYNCHRONIZE, TRUE, GetCurrentProcessId());
  92. wsprintf(szCommand, "%s %d", szModule, hOurProcess);
  93. STARTUPINFO si;
  94. ZeroMemory(&si, sizeof(si));
  95. si.cb = sizeof(si);
  96. PROCESS_INFORMATION pi;
  97. CreateProcess(NULL, szCommand, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
  98. rt = false;
  99. Exit:
  100. if (INVALID_HANDLE_VALUE != h)
  101. FindClose(h);
  102. if (hOurProcess)
  103. CloseHandle(hOurProcess);
  104. return rt; // keep going
  105. }