Leaked source code of windows server 2003
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.

103 lines
3.5 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. if (NULL != pszFile && TEXT('\0') != *pszFile)
  22. {
  23. //
  24. // Have CSC create a local copy. It creates the file with a
  25. // unique (and cryptic) name.
  26. //
  27. LPTSTR pszCscLocalName = NULL;
  28. if (!CSCCopyReplica(pszFile, &pszCscLocalName))
  29. {
  30. dwErr = GetLastError();
  31. }
  32. else
  33. {
  34. TraceAssert(NULL != pszCscLocalName);
  35. //
  36. // Combine the temporary path and the original filespec to form
  37. // a name that will be meaningful to the user when the file is opened
  38. // in it's application.
  39. //
  40. TCHAR szCscTempName[MAX_PATH];
  41. if (FAILED(StringCchCopy(szCscTempName, ARRAYSIZE(szCscTempName), pszCscLocalName))
  42. || !::PathRemoveFileSpec(szCscTempName)
  43. || !::PathAppend(szCscTempName, ::PathFindFileName(pszFile)))
  44. {
  45. dwErr = ERROR_INVALID_NAME;
  46. }
  47. else
  48. {
  49. //
  50. // Remove any read-only attribute in case there's still a copy left
  51. // from a previous "open" operation. We'll need to overwrite the
  52. // existing copy.
  53. //
  54. DWORD dwAttrib = GetFileAttributes(szCscTempName);
  55. if ((DWORD)-1 != dwAttrib)
  56. {
  57. SetFileAttributes(szCscTempName, dwAttrib & ~FILE_ATTRIBUTE_READONLY);
  58. }
  59. //
  60. // Rename the file to use the proper name.
  61. //
  62. if (!MoveFileEx(pszCscLocalName, szCscTempName, MOVEFILE_REPLACE_EXISTING))
  63. {
  64. dwErr = GetLastError();
  65. }
  66. else
  67. {
  68. //
  69. // Set the file's READONLY bit so that the user can't save
  70. // changes to the file. They can, however, save it somewhere
  71. // else from within the opening app if they want.
  72. //
  73. dwAttrib = GetFileAttributes(szCscTempName);
  74. if (!SetFileAttributes(szCscTempName, dwAttrib | FILE_ATTRIBUTE_READONLY))
  75. {
  76. dwErr = GetLastError();
  77. }
  78. else
  79. {
  80. SHELLEXECUTEINFO si;
  81. ZeroMemory(&si, sizeof(si));
  82. si.cbSize = sizeof(si);
  83. si.fMask = SEE_MASK_FLAG_NO_UI;
  84. si.lpFile = szCscTempName;
  85. si.nShow = SW_NORMAL;
  86. if (!ShellExecuteEx(&si))
  87. {
  88. dwErr = GetLastError();
  89. }
  90. }
  91. }
  92. }
  93. LocalFree(pszCscLocalName);
  94. }
  95. }
  96. return dwErr;
  97. }