openfile.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 3k
Category:

Windows Kernel

Development Platform:

Visual C++

  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. #include "pathstr.h"
  13. //
  14. // Open a file using ShellExecuteEx and the "open" verb.
  15. //
  16. DWORD
  17. OpenOfflineFile(
  18.     LPCTSTR pszFile
  19.     )
  20. {
  21.     DWORD dwErr = ERROR_SUCCESS;
  22.     CPath strFullPath(pszFile);
  23.     if (!strFullPath.IsEmpty())
  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(strFullPath, &pszCscLocalName))
  31.         {
  32.             dwErr = GetLastError();
  33.         }
  34.         else
  35.         {
  36.             DBGASSERT((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.             CPath strCscTempName(pszCscLocalName);
  43.             strCscTempName.RemoveFileSpec();
  44.             strFullPath.SetPath(strCscTempName);
  45.             //
  46.             // Remove any read-only attribute in case there's still a copy left
  47.             // from a previous "open" operation.  We'll need to overwrite the
  48.             // existing copy.
  49.             //
  50.             DWORD dwAttrib = GetFileAttributes(strFullPath);
  51.             if ((DWORD)-1 != dwAttrib)
  52.             {
  53.                 SetFileAttributes(strFullPath, dwAttrib & ~FILE_ATTRIBUTE_READONLY);
  54.             }
  55.             //
  56.             // Rename the file to use the proper name.
  57.             //
  58.             if (!MoveFileEx(pszCscLocalName, strFullPath, MOVEFILE_REPLACE_EXISTING))
  59.             {
  60.                 dwErr = GetLastError();
  61.             }
  62.             else
  63.             {
  64.                 //
  65.                 // Set the file's READONLY bit so that the user can't save 
  66.                 // changes to the file.  They can, however, save it somewhere
  67.                 // else from within the opening app if they want.
  68.                 //
  69.                 dwAttrib = GetFileAttributes(strFullPath);
  70.                 if (!SetFileAttributes(strFullPath, dwAttrib | FILE_ATTRIBUTE_READONLY))
  71.                 {
  72.                     dwErr = GetLastError();
  73.                 }
  74.             }
  75.             LocalFree(pszCscLocalName);
  76.         }
  77.         //
  78.         // If after all that... we don't have an error,  then we
  79.         // can try to open it.
  80.         //
  81.         if (ERROR_SUCCESS == dwErr)
  82.         {
  83.             SHELLEXECUTEINFO si;
  84.             ZeroMemory(&si, sizeof(si));
  85.             si.cbSize       = sizeof(si);
  86.             si.fMask        = SEE_MASK_FLAG_NO_UI;
  87.             si.hwnd         = NULL;
  88.             si.lpFile       = strFullPath;
  89.             si.lpVerb       = NULL;         // default to "Open".
  90.             si.lpParameters = NULL;
  91.             si.lpDirectory  = NULL;
  92.             si.nShow        = SW_NORMAL;
  93.             if (!ShellExecuteEx(&si))
  94.             {
  95.                 dwErr = GetLastError();
  96.             }
  97.         }
  98.     }
  99.     return dwErr;
  100. }