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.

109 lines
3.4 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. // File: openfile.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "pch.h"
  11. #pragma hdrstop
  12. //
  13. // Open a file using ShellExecuteEx and the "open" verb.
  14. //
  15. DWORD
  16. OpenOfflineFile(
  17. LPCTSTR pszFile
  18. )
  19. {
  20. DWORD dwErr = ERROR_SUCCESS;
  21. TCHAR szFullPath[MAX_PATH];
  22. lstrcpyn(szFullPath, pszFile, ARRAYSIZE(szFullPath));
  23. if (TEXT('\0') != szFullPath[0])
  24. {
  25. //
  26. // Have CSC create a local copy. It creates the file with a
  27. // unique (and cryptic) name.
  28. //
  29. LPTSTR pszCscLocalName = NULL;
  30. if (!CSCCopyReplica(szFullPath, &pszCscLocalName))
  31. {
  32. dwErr = GetLastError();
  33. }
  34. else
  35. {
  36. TraceAssert(NULL != pszCscLocalName);
  37. //
  38. // Combine the temporary path and the original filespec to form
  39. // a name that will be meaningful to the user when the file is opened
  40. // in it's application.
  41. //
  42. TCHAR szCscTempName[MAX_PATH];
  43. lstrcpyn(szCscTempName, pszCscLocalName, ARRAYSIZE(szCscTempName));
  44. ::PathRemoveFileSpec(szCscTempName);
  45. ::PathAppend(szCscTempName, ::PathFindFileName(szFullPath));
  46. lstrcpyn(szFullPath, szCscTempName, ARRAYSIZE(szFullPath));
  47. //
  48. // Remove any read-only attribute in case there's still a copy left
  49. // from a previous "open" operation. We'll need to overwrite the
  50. // existing copy.
  51. //
  52. DWORD dwAttrib = GetFileAttributes(szFullPath);
  53. if ((DWORD)-1 != dwAttrib)
  54. {
  55. SetFileAttributes(szFullPath, dwAttrib & ~FILE_ATTRIBUTE_READONLY);
  56. }
  57. //
  58. // Rename the file to use the proper name.
  59. //
  60. if (!MoveFileEx(pszCscLocalName, szFullPath, MOVEFILE_REPLACE_EXISTING))
  61. {
  62. dwErr = GetLastError();
  63. }
  64. else
  65. {
  66. //
  67. // Set the file's READONLY bit so that the user can't save
  68. // changes to the file. They can, however, save it somewhere
  69. // else from within the opening app if they want.
  70. //
  71. dwAttrib = GetFileAttributes(szFullPath);
  72. if (!SetFileAttributes(szFullPath, dwAttrib | FILE_ATTRIBUTE_READONLY))
  73. {
  74. dwErr = GetLastError();
  75. }
  76. }
  77. LocalFree(pszCscLocalName);
  78. }
  79. //
  80. // If after all that... we don't have an error, then we
  81. // can try to open it.
  82. //
  83. if (ERROR_SUCCESS == dwErr)
  84. {
  85. SHELLEXECUTEINFO si;
  86. ZeroMemory(&si, sizeof(si));
  87. si.cbSize = sizeof(si);
  88. si.fMask = SEE_MASK_FLAG_NO_UI;
  89. si.hwnd = NULL;
  90. si.lpFile = szFullPath;
  91. si.lpVerb = NULL; // default to "Open".
  92. si.lpParameters = NULL;
  93. si.lpDirectory = NULL;
  94. si.nShow = SW_NORMAL;
  95. if (!ShellExecuteEx(&si))
  96. {
  97. dwErr = GetLastError();
  98. }
  99. }
  100. }
  101. return dwErr;
  102. }