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

Windows Kernel

Development Platform:

Visual C++

  1. #include "pch.h"
  2. #pragma hdrstop
  3. #include <tchar.h>
  4. #include <process.h>
  5. #include "alloc.h"
  6. #include "cachview.h"
  7. #include "sharedlg.h"
  8. #include "viewerp.h"
  9. #include "cnxcache.h"
  10. #include "msgbox.h"
  11. #include "uuid.h"
  12. #include "idlhelp.h"
  13. CnxNameCache g_TheCnxNameCache;
  14. HINSTANCE Viewer::g_hInstance = NULL;
  15. bool      Viewer::m_bShowDebugInfo = false;
  16. //
  17. // Originally, m_bShowSparseFiles was initialized as false then
  18. // was changeable through the registry.  We decided to always show sparse
  19. // files so it is now initialized as true.  I've left the original
  20. // logic in case we change our minds again.  [brianau - 2/23/98]
  21. //
  22. bool      Viewer::m_bShowSparseFiles = true;
  23. void SetDebugParams(
  24.     void
  25.     )
  26. {
  27.     //
  28.     // Change these to alter the debugger output.
  29.     //
  30.     DBGMODULE(TEXT("CSCUI"));  // Name of module displayed with messages.
  31.     DBGTRACEMASK(DM_NONE);     // What components are traced.
  32.     DBGTRACELEVEL(DL_MID);     // How much detail to trace.
  33.     DBGPRINTMASK(DM_NONE);     // What components to print.
  34.     DBGPRINTLEVEL(DL_MID);     // How much detail to print.
  35.     DBGTRACEVERBOSE(false);    // Include file/line info in trace output?
  36.     DBGPRINTVERBOSE(false);    // Include file/line info in print output?
  37. }
  38. void LoadModuleHandle(
  39.     void
  40.     )
  41. {
  42.     if (NULL == Viewer::g_hInstance)
  43.         Viewer::g_hInstance = GetModuleHandle(TEXT("cscui"));
  44.     if (NULL == Viewer::g_hInstance)
  45.         DWORD dwError = GetLastError();
  46. }
  47. bool
  48. ActivateRunningViewer(
  49.     void
  50.     )
  51. {
  52.     HWND hwnd = FindWindowEx(NULL, NULL, WC_NETCACHE_VIEWER, NULL);
  53.     if (NULL != hwnd)
  54.     {
  55.         ShowWindow(hwnd, SW_SHOWNORMAL);
  56.         return boolify(SetForegroundWindow(hwnd));
  57.     }
  58.     return false;
  59. }
  60. //
  61. // This function invokes the new cache viewer shell namespace extension.
  62. // 
  63. INT 
  64. CSCViewCacheInternalNSE(
  65.     void
  66.     )
  67. {
  68.     SHELLEXECUTEINFO shei = { 0 };
  69.     shei.cbSize     = sizeof(shei);
  70.     shei.fMask      = SEE_MASK_IDLIST | SEE_MASK_INVOKEIDLIST;
  71.     shei.nShow      = SW_SHOWNORMAL;
  72.     if (SUCCEEDED(CreateOfflineFolderIDList((LPITEMIDLIST *)(&shei.lpIDList))))
  73.     {
  74.         ShellExecuteEx(&shei);
  75.         ILFree((LPITEMIDLIST)(shei.lpIDList));
  76.     }
  77.     return 0;
  78. }
  79. INT
  80. CSCViewCacheInternalW(
  81.     INT iView,
  82.     LPCWSTR pszShareW,
  83.     bool bWait
  84.     )
  85. {
  86.     INT iResult = 1;
  87.     SetDebugParams();
  88.     LoadModuleHandle();
  89.     try
  90.     {
  91.         if (g_pSettings->CacheViewerMode() < CSettings::eViewerReadOnly )
  92.         {
  93.             DBGERROR((TEXT("Policy prohibits opening cache viewer.")));
  94.             CscMessageBox(NULL, 
  95.                           MB_OK | MB_ICONWARNING,
  96.                           Viewer::g_hInstance,
  97.                           IDS_ERR_POLICY_NOVIEWCACHE);
  98.             return -1;
  99.         }
  100.         const WCHAR szBlankW[] = L"";
  101.         if (NULL == pszShareW)
  102.             pszShareW = szBlankW;
  103.         CacheWindow::ViewType eView = CacheWindow::ViewType(iView);
  104.         CString strShare(pszShareW);
  105.         if (bWait)
  106.         {
  107.             CacheWindow viewer(Viewer::g_hInstance);
  108.             iResult = viewer.Run(eView, strShare);
  109.         }
  110.         else
  111.         {
  112.             if (!ActivateRunningViewer())
  113.             {
  114.                 //
  115.                 // Run the viewer in a separate process.
  116.                 // This will start rundll32.exe and call CSCViewCacheRunDll 
  117.                 // to run the viewer modally in that process.
  118.                 //
  119.                 CString strCmdLine;
  120.                 STARTUPINFO si = {0};
  121.                 PROCESS_INFORMATION pi ={0};
  122.                 BOOL bSuccess = FALSE;
  123. #ifdef UNICODE            
  124.                 strCmdLine.Format(TEXT("rundll32.exe cscui.dll,CSCViewCacheRunDll %1!d! %2"), 
  125.                                   int(eView), strShare.Cstr());
  126. #else
  127.                 strCmdLine.Format(TEXT("rundll32.exe cscui.dll,CSCViewCacheRunDllA %1!d! %2"), 
  128.                                   int(eView), strShare.Cstr());
  129. #endif
  130.                 si.cb = sizeof(si);
  131.                 si.lpDesktop = TEXT("WinSta0\Default");
  132.                 bSuccess = CreateProcess(NULL,
  133.                                          strCmdLine,
  134.                                          NULL,
  135.                                          NULL,
  136.                                          FALSE,
  137.                                          0,
  138.                                          NULL,
  139.                                          NULL,
  140.                                          &si,
  141.                                          &pi);
  142.                 if (bSuccess)
  143.                 {
  144.                     CloseHandle(pi.hProcess);
  145.                     CloseHandle(pi.hThread);
  146.                 }
  147.             }
  148.         }
  149.     }
  150.     catch(CException& e)
  151.     {
  152.         DBGERROR((TEXT("C++ exception %d caught in CSCViewCacheInternalW"), e.dwError));
  153.         CscWin32Message(NULL, e.dwError, CSCUI::SEV_ERROR);
  154.     }
  155.     return iResult;
  156. }
  157. INT
  158. CSCViewCacheInternalA(
  159.     INT iInitialView,
  160.     LPCSTR pszShareA,
  161.     bool bWait
  162.     )
  163. {
  164.     INT iResult = 1;
  165.     const CHAR szBlankA[] = "";
  166.     if (NULL == pszShareA)
  167.         pszShareA = szBlankA;
  168.     USES_CONVERSION;
  169.     iResult = CSCViewCacheInternalW(iInitialView, A2W(pszShareA), bWait);
  170.     return iResult;
  171. }
  172. //
  173. // Trivial class for passing parameters to share dialog thread proc.
  174. //
  175. class ShareDialogThreadParams
  176. {
  177.     public:
  178.         ShareDialogThreadParams(HWND hwndParent,
  179.                                 LPCTSTR pszShare)
  180.                                 : m_hwndParent(hwndParent),
  181.                                   m_hModule(NULL),
  182.                                   m_strShare(pszShare) { }
  183.     
  184.         HWND    m_hwndParent;
  185.         HMODULE m_hModule;
  186.         CString m_strShare;
  187.         void SetModuleHandle(HINSTANCE hModule)
  188.             { m_hModule = hModule; }
  189. };
  190. //
  191. // The share dialog's thread proc.
  192. //
  193. UINT WINAPI
  194. ShareDialogThreadProc(
  195.     LPVOID pvParam
  196.     )
  197. {
  198.     ShareDialogThreadParams *ptp = reinterpret_cast<ShareDialogThreadParams *>(pvParam);
  199.     DBGASSERT((NULL != ptp));
  200.     HMODULE hModule = ptp->m_hModule; // Save local copy.
  201.     try
  202.     {
  203.         SharePropSheet dlg(Viewer::g_hInstance,
  204.                            &g_cRefCount,
  205.                            ptp->m_hwndParent,
  206.                            ptp->m_strShare);
  207.         dlg.Run();
  208.     }
  209.     catch(CException& e)
  210.     {
  211.         DBGERROR((TEXT("C++ exception %d caught in ShareDialogThreadProc"), e.dwError));
  212.         CscWin32Message(NULL, e.dwError, CSCUI::SEV_ERROR);
  213.     }
  214.     delete ptp;
  215.     if (NULL != hModule)
  216.         FreeLibraryAndExitThread(hModule, 0);
  217.     return 0;
  218. }
  219. //
  220. // HACK:
  221. // If pszShareW points to an empty string, we only show the CSC configuration
  222. // page.  The share summary-options combo property sheet already existed.
  223. // Later on we needed to display only the options in a prop sheet all by itself.
  224. // No time to do a good redesign to take this into account so we just pass 
  225. // in an empty share name string.
  226. //
  227. VOID
  228. CSCViewShareSummaryInternalW(
  229.     LPCWSTR pszShareW,
  230.     HWND hwndParent,
  231.     BOOL bModal
  232.     )
  233. {
  234.     SetDebugParams();
  235.     LoadModuleHandle();
  236.     try
  237.     {
  238.         CString strShare(pszShareW);
  239.         if (bModal)
  240.         {
  241.             SharePropSheet dlg(Viewer::g_hInstance,
  242.                                &g_cRefCount,
  243.                                hwndParent,
  244.                                strShare);
  245.             dlg.Run();
  246.         }
  247.         else
  248.         {
  249.             //
  250.             // This thread param buffer will be deleted by the
  251.             // thread proc.
  252.             //
  253.             ShareDialogThreadParams *ptp = new ShareDialogThreadParams(hwndParent,
  254.                                                                        strShare);
  255.             if (NULL != ptp)
  256.             {
  257.                 //
  258.                 // LoadLibrary on ourselves so that we stay in memory even
  259.                 // if the caller calls FreeLibrary.  We'll call FreeLibrary
  260.                 // when the thread proc exits.
  261.                 //
  262.                 ptp->SetModuleHandle(LoadLibrary(TEXT("cscui.dll")));
  263.                 DWORD idThread;
  264.                 HANDLE hThread = (HANDLE)_beginthreadex(NULL,
  265.                                            0,          // Default stack size
  266.                                            ShareDialogThreadProc,
  267.                                            (LPVOID)ptp,
  268.                                            0,
  269.                                            (UINT *)&idThread);
  270.                 if (INVALID_HANDLE_VALUE != hThread)
  271.                 {
  272.                     CloseHandle(hThread);
  273.                 }
  274.             }
  275.         }
  276.     }
  277.     catch(CException& e)
  278.     {
  279.         DBGERROR((TEXT("C++ exception %d caught in CSCViewShareSummaryInternalW"), e.dwError));
  280.         CscWin32Message(NULL, e. dwError, CSCUI::SEV_ERROR);
  281.     }
  282. }
  283. VOID
  284. CSCViewShareSummaryInternalA(
  285.     LPCSTR pszShareA,
  286.     HWND hwndParent, 
  287.     BOOL bModal
  288.     )
  289. {
  290.     USES_CONVERSION;
  291.     CSCViewShareSummaryInternalW(A2W(pszShareA), hwndParent, bModal);
  292. }
  293. VOID
  294. CSCViewOptionsInternal(
  295.     HWND hwndParent,
  296.     BOOL bModal
  297.     )
  298. {
  299.     CSCViewShareSummaryInternalW(L"", hwndParent, bModal);
  300. }