home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winui / shell / regview / regview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-27  |  9.8 KB  |  335 lines

  1. /**************************************************************************
  2.    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  3.    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  4.    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  5.    PARTICULAR PURPOSE.
  6.  
  7.    Copyright 1997 Microsoft Corporation.  All Rights Reserved.
  8. **************************************************************************/
  9.  
  10. /**************************************************************************
  11.  
  12.    File:          RegView.cpp
  13.    
  14.    Description:   Contains DLLMain and standard OLE COM object creation stuff.
  15.  
  16. **************************************************************************/
  17.  
  18. /**************************************************************************
  19.    #include statements
  20. **************************************************************************/
  21.  
  22. #include "ShlView.h"
  23. #include "ClsFact.h"
  24. #include "Utility.h"
  25. #include <olectl.h>
  26.  
  27. /**************************************************************************
  28.    GUID stuff
  29. **************************************************************************/
  30.  
  31. //this part is only done once
  32. //if you need to use the GUID in another file, just include Guid.h
  33. #pragma data_seg(".text")
  34. #define INITGUID
  35. #include <initguid.h>
  36. #include <shlguid.h>
  37. #include "Guid.h"
  38. #pragma data_seg()
  39.  
  40. /**************************************************************************
  41.    private function prototypes
  42. **************************************************************************/
  43.  
  44. extern "C" BOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID);
  45.  
  46. /**************************************************************************
  47.    global variables
  48. **************************************************************************/
  49.  
  50. HINSTANCE   g_hInst;
  51. UINT        g_DllRefCount;
  52. int         g_nColumn1;
  53. int         g_nColumn2;
  54. BOOL        g_bViewKeys;
  55. BOOL        g_bShowIDW;
  56. HIMAGELIST  g_himlLarge;
  57. HIMAGELIST  g_himlSmall;
  58.  
  59. /**************************************************************************
  60.  
  61.    DllMain
  62.  
  63. **************************************************************************/
  64.  
  65. extern "C" BOOL WINAPI DllMain(  HINSTANCE hInstance, 
  66.                                  DWORD dwReason, 
  67.                                  LPVOID lpReserved)
  68. {
  69. switch(dwReason)
  70.    {
  71.    case DLL_PROCESS_ATTACH:
  72.       g_hInst = hInstance;
  73.  
  74.       GetGlobalSettings();
  75.  
  76.       //create the global image lists
  77.       CreateImageLists();
  78.       break;
  79.  
  80.    case DLL_PROCESS_DETACH:
  81.       SaveGlobalSettings();
  82.  
  83.       //destroy the global image lists
  84.       DestroyImageLists();
  85.       break;
  86.    }
  87.    
  88. return TRUE;
  89. }                                 
  90.  
  91. /**************************************************************************
  92.  
  93.    DllCanUnloadNow
  94.  
  95. **************************************************************************/
  96.  
  97. STDAPI DllCanUnloadNow(void)
  98. {
  99. return (g_DllRefCount ? S_FALSE : S_OK);
  100. }
  101.  
  102. /**************************************************************************
  103.  
  104.    DllGetClassObject
  105.  
  106. **************************************************************************/
  107.  
  108. STDAPI DllGetClassObject(  REFCLSID rclsid, 
  109.                            REFIID riid, 
  110.                            LPVOID *ppReturn)
  111. {
  112. *ppReturn = NULL;
  113.  
  114. //if we don't support this classid, return the proper error code
  115. if(!IsEqualCLSID(rclsid, CLSID_RegViewNameSpace))
  116.    return CLASS_E_CLASSNOTAVAILABLE;
  117.    
  118. //create a CClassFactory object and check it for validity
  119. CClassFactory *pClassFactory = new CClassFactory();
  120. if(NULL == pClassFactory)
  121.    return E_OUTOFMEMORY;
  122.    
  123. //get the QueryInterface return for our return value
  124. HRESULT hResult = pClassFactory->QueryInterface(riid, ppReturn);
  125.  
  126. //call Release to decement the ref count - creating the object set it to one 
  127. //and QueryInterface incremented it - since its being used externally (not by 
  128. //us), we only want the ref count to be 1
  129. pClassFactory->Release();
  130.  
  131. //return the result from QueryInterface
  132. return hResult;
  133. }
  134.  
  135. /**************************************************************************
  136.  
  137.    DllRegisterServer
  138.  
  139. **************************************************************************/
  140.  
  141. typedef struct{
  142.    HKEY  hRootKey;
  143.    LPTSTR lpszSubKey;
  144.    LPTSTR lpszValueName;
  145.    LPTSTR lpszData;
  146. }REGSTRUCT, *LPREGSTRUCT;
  147.  
  148. STDAPI DllRegisterServer(void)
  149. {
  150. int      i;
  151. HKEY     hKey;
  152. LRESULT  lResult;
  153. DWORD    dwDisp;
  154. TCHAR    szSubKey[MAX_PATH];
  155. TCHAR    szCLSID[MAX_PATH];
  156. TCHAR    szModule[MAX_PATH];
  157. LPWSTR   pwsz;
  158.  
  159. //get the CLSID in string form
  160. StringFromIID(CLSID_RegViewNameSpace, &pwsz);
  161.  
  162. if(pwsz)
  163.    {
  164.    WideCharToLocal(szCLSID, pwsz, ARRAYSIZE(szCLSID));
  165.  
  166.    //free the string
  167.    LPMALLOC pMalloc;
  168.    CoGetMalloc(1, &pMalloc);
  169.    if(pMalloc)
  170.       {
  171.       pMalloc->Free(pwsz);
  172.       pMalloc->Release();
  173.       }
  174.    }
  175.  
  176. //get this DLL's path and file name
  177. GetModuleFileName(g_hInst, szModule, ARRAYSIZE(szModule));
  178.  
  179. //register the CLSID entries
  180. REGSTRUCT ClsidEntries[] = {  HKEY_CLASSES_ROOT,   TEXT("CLSID\\%s"),                  NULL,                   TEXT("Registry View"),
  181.                               HKEY_CLASSES_ROOT,   TEXT("CLSID\\%s\\InprocServer32"),  NULL,                   TEXT("%s"),
  182.                               HKEY_CLASSES_ROOT,   TEXT("CLSID\\%s\\InprocServer32"),  TEXT("ThreadingModel"), TEXT("Apartment"),
  183.                               HKEY_CLASSES_ROOT,   TEXT("CLSID\\%s\\DefaultIcon"),     NULL,                   TEXT("%s,0"),
  184.                               NULL,                NULL,                               NULL,                   NULL};
  185.  
  186. for(i = 0; ClsidEntries[i].hRootKey; i++)
  187.    {
  188.    //Create the sub key string.
  189.    wsprintf(szSubKey, ClsidEntries[i].lpszSubKey, szCLSID);
  190.  
  191.    lResult = RegCreateKeyEx(  ClsidEntries[i].hRootKey,
  192.                               szSubKey,
  193.                               0,
  194.                               NULL,
  195.                               REG_OPTION_NON_VOLATILE,
  196.                               KEY_WRITE,
  197.                               NULL,
  198.                               &hKey,
  199.                               &dwDisp);
  200.    
  201.    if(NOERROR == lResult)
  202.       {
  203.       TCHAR szData[MAX_PATH];
  204.  
  205.       //if necessary, create the value string
  206.       wsprintf(szData, ClsidEntries[i].lpszData, szModule);
  207.    
  208.       lResult = RegSetValueEx(   hKey,
  209.                                  ClsidEntries[i].lpszValueName,
  210.                                  0,
  211.                                  REG_SZ,
  212.                                  (LPBYTE)szData,
  213.                                  lstrlen(szData) + 1);
  214.       
  215.       RegCloseKey(hKey);
  216.       }
  217.    else
  218.       return SELFREG_E_CLASS;
  219.    }
  220.  
  221. //Register the default flags for the folder.
  222.  
  223. wsprintf(   szSubKey, 
  224.             TEXT("CLSID\\%s\\ShellFolder"), 
  225.             szCLSID);
  226.  
  227. lResult = RegCreateKeyEx(  HKEY_CLASSES_ROOT,
  228.                            szSubKey,
  229.                            0,
  230.                            NULL,
  231.                            REG_OPTION_NON_VOLATILE,
  232.                            KEY_WRITE,
  233.                            NULL,
  234.                            &hKey,
  235.                            &dwDisp);
  236.  
  237. if(NOERROR == lResult)
  238.    {
  239.    DWORD dwData = SFGAO_FOLDER | SFGAO_HASSUBFOLDER;
  240.  
  241.    lResult = RegSetValueEx(   hKey,
  242.                               TEXT("Attributes"),
  243.                               0,
  244.                               REG_BINARY,
  245.                               (LPBYTE)&dwData,
  246.                               sizeof(dwData));
  247.    
  248.    RegCloseKey(hKey);
  249.    }
  250. else
  251.    return SELFREG_E_CLASS;
  252.  
  253. //Register the name space extension
  254.  
  255. /*
  256. Create the sub key string. Change this from "...Desktop..." to 
  257. "...MyComputer..." if desired.
  258. */
  259. wsprintf(   szSubKey, 
  260.             TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Desktop\\NameSpace\\%s"), 
  261.             szCLSID);
  262.  
  263. lResult = RegCreateKeyEx(  HKEY_LOCAL_MACHINE,
  264.                            szSubKey,
  265.                            0,
  266.                            NULL,
  267.                            REG_OPTION_NON_VOLATILE,
  268.                            KEY_WRITE,
  269.                            NULL,
  270.                            &hKey,
  271.                            &dwDisp);
  272.  
  273. if(NOERROR == lResult)
  274.    {
  275.    TCHAR szData[MAX_PATH];
  276.  
  277.    //Create the value string.
  278.    lstrcpy(szData, TEXT("Registry View"));
  279.  
  280.    lResult = RegSetValueEx(   hKey,
  281.                               NULL,
  282.                               0,
  283.                               REG_SZ,
  284.                               (LPBYTE)szData,
  285.                               lstrlen(szData) + 1);
  286.    
  287.    RegCloseKey(hKey);
  288.    }
  289. else
  290.    return SELFREG_E_CLASS;
  291.  
  292. //If running on NT, register the extension as approved.
  293. OSVERSIONINFO  osvi;
  294.  
  295. osvi.dwOSVersionInfoSize = sizeof(osvi);
  296. GetVersionEx(&osvi);
  297.  
  298. if(VER_PLATFORM_WIN32_NT == osvi.dwPlatformId)
  299.    {
  300.    lstrcpy( szSubKey, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved"));
  301.  
  302.    lResult = RegCreateKeyEx(  HKEY_LOCAL_MACHINE,
  303.                               szSubKey,
  304.                               0,
  305.                               NULL,
  306.                               REG_OPTION_NON_VOLATILE,
  307.                               KEY_WRITE,
  308.                               NULL,
  309.                               &hKey,
  310.                               &dwDisp);
  311.  
  312.    if(NOERROR == lResult)
  313.       {
  314.       TCHAR szData[MAX_PATH];
  315.  
  316.       //Create the value string.
  317.       lstrcpy(szData, TEXT("Registry View"));
  318.  
  319.       lResult = RegSetValueEx(   hKey,
  320.                                  szCLSID,
  321.                                  0,
  322.                                  REG_SZ,
  323.                                  (LPBYTE)szData,
  324.                                  lstrlen(szData) + 1);
  325.       
  326.       RegCloseKey(hKey);
  327.       }
  328.    else
  329.       return SELFREG_E_CLASS;
  330.    }
  331.  
  332. return S_OK;
  333. }
  334.  
  335.