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

Windows Kernel

Development Platform:

Visual C++

  1. // installwv.cpp : Installs a file from a resource
  2. #include "priv.h"
  3. #include "installwv.h"
  4. #include "regstr.h"
  5. #ifndef _WIN64
  6. #include <iert.h>
  7. #endif
  8. #define WINVER_KEY     "SOFTWARE\Microsoft\Windows\CurrentVersion"
  9. #define PROGFILES_KEY  "ProgramFilesDir"
  10. #define IfFalseRet(val, hr) {if ((val) == 0) return (hr);}
  11. HRESULT WriteResource(HANDLE hDestFile, HINSTANCE hResourceInst, HRSRC hRsrc)
  12. {
  13.     HGLOBAL hGlob;
  14.     LPVOID  pTemp;
  15.     ULONG   cbWritten;
  16.     ULONG   cbResource;
  17.     HRESULT hr = E_FAIL;
  18.     // Load the resource
  19. hGlob = LoadResource(hResourceInst, hRsrc);
  20. if (EVAL(pTemp = LockResource(hGlob)))
  21.     {
  22.         // Write out the resource
  23.         cbResource = SizeofResource(hResourceInst, hRsrc);
  24.         if (EVAL(WriteFile(hDestFile, pTemp, cbResource, &cbWritten, NULL)))
  25.             hr = S_OK;
  26.     }
  27.     return hr;
  28. }
  29. HRESULT GetInstallInfoFromResource(HINSTANCE hResourceInst, UINT uID, INSTALL_INFO *piiFile)
  30. {
  31.     TCHAR szEntry[MAX_PATH];
  32.     HRESULT hr = E_FAIL;
  33.     if (piiFile == NULL) 
  34.         return E_INVALIDARG;
  35.     // Get the string table entry
  36.     piiFile->dwDestAttrib = FILE_ATTRIBUTE_HIDDEN;
  37.     piiFile->szSource = NULL;
  38.     piiFile->szDest = NULL;
  39.     if (EVAL(LoadString(hResourceInst, uID, szEntry, ARRAYSIZE(szEntry))))    
  40.     {
  41.         // Are we at the end of the list (empty string)
  42.         if (szEntry[0])
  43.         {
  44.             // No.
  45.             piiFile->szSource = StrDup(szEntry);
  46.             piiFile->szDest = StrDup(TEXT("ftp.htt"));
  47.             hr = S_OK;
  48.         }
  49.         else
  50.             hr = E_FAIL;
  51.     }
  52.     return hr;
  53. }
  54. HRESULT InstallInfoFreeMembers(INSTALL_INFO *piiFile)
  55. {
  56.     if (piiFile) {
  57.         if (piiFile->szSource) {
  58.             LocalFree(piiFile->szSource);
  59.             piiFile->szSource = NULL;
  60.         }
  61.         if (piiFile->szDest) {
  62.             LocalFree(piiFile->szDest);
  63.             piiFile->szDest = NULL;
  64.         }
  65.     }
  66.     return S_OK;
  67. }
  68. HRESULT InstallFilesFromResourceID(HINSTANCE hResourceInst, 
  69.                                    UINT uID,                                
  70.                                    LPTSTR pszDestDir)
  71. {
  72.     HRESULT hr = S_OK;
  73.     INSTALL_INFO iiFile;
  74.     if (!EVAL(pszDestDir))
  75.         return E_INVALIDARG;
  76.     if (S_OK == GetInstallInfoFromResource(hResourceInst, uID, &iiFile))
  77.     {
  78.         hr = InstallFileFromResource(hResourceInst, &iiFile, pszDestDir);
  79.         InstallInfoFreeMembers(&iiFile);
  80.         uID += 1;
  81.     }
  82.     return hr;
  83. }
  84. HRESULT InstallFileFromResource(HINSTANCE hResourceInst, 
  85.                                 INSTALL_INFO *piiFile, 
  86.                                 LPTSTR pszDestDir)
  87. {
  88.     HRESULT hr = E_FAIL;
  89.     HRSRC   hRsrc;
  90.     LPTSTR  lpszExt;
  91.     // Find the resource
  92.     if (EVAL(hRsrc = FindResource(hResourceInst, piiFile->szSource, RT_HTML)))
  93.     {
  94.         // Create destination file
  95.         if (PathIsDirectory(pszDestDir))
  96.         {
  97.             TCHAR   szDestPath[MAX_PATH];
  98.             StrNCpy(szDestPath, pszDestDir, MAX_PATH);
  99.             if (PathAppend(szDestPath, piiFile->szDest))
  100.             {
  101.                 HANDLE  hDestFile;
  102.                 SetFileAttributes(szDestPath, FILE_ATTRIBUTE_NORMAL); // Make sure we can overwrite
  103.                 hDestFile = CreateFile(szDestPath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, piiFile->dwDestAttrib, NULL); 
  104.                 if (hDestFile != INVALID_HANDLE_VALUE) 
  105.                 {
  106.                     // Write out the resource
  107.                     hr = WriteResource(hDestFile, hResourceInst, hRsrc);
  108.                     CloseHandle(hDestFile);
  109.                     // Make sure the file attributes are set correctly.  Overwritting a file doesn't
  110.                     // always update the attributes
  111.                     SetFileAttributes(szDestPath, piiFile->dwDestAttrib);
  112.                     if ((lpszExt = PathFindExtension(szDestPath)) && (StrCmpI(lpszExt, TEXT(".htt")) == 0))
  113.                     {
  114.                         hr = SHRegisterValidateTemplate(szDestPath, SHRVT_REGISTER);
  115.                     }
  116.                 }
  117.             }
  118.         }
  119.     }
  120.     return hr;
  121. }
  122. HRESULT InstallWebViewFiles(HINSTANCE hInstResource)
  123. {
  124.     HRESULT hr = E_FAIL;
  125.     TCHAR   szDestPath[MAX_PATH];
  126.     // Set up %windir% path
  127.     if (EVAL(SHGetSystemWindowsDirectory(szDestPath, ARRAYSIZE(szDestPath)) &&
  128.              PathIsDirectory(szDestPath)))
  129.     {
  130.         // Install %windir%web files
  131.         if (EVAL(PathAppend(szDestPath, TEXT("Web"))))
  132.         {
  133.             if (PathIsDirectory(szDestPath) || CreateDirectory(szDestPath, NULL))
  134.             {
  135.                 UINT idWebViewTemp = IDS_INSTALL_TEMPLATE;
  136.                 if (SHELL_VERSION_NT5 <= GetShellVersion())
  137.                     idWebViewTemp = IDS_INSTALL_TEMPLATE_NT5;
  138.                 EVAL(SUCCEEDED(hr = InstallFilesFromResourceID(hInstResource, idWebViewTemp, szDestPath))); 
  139.                 EVAL(SetFileAttributes(szDestPath, FILE_ATTRIBUTE_SYSTEM));
  140.             }
  141.         }
  142.     }
  143.         
  144.     return hr;
  145. }