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 / utility.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-27  |  11.6 KB  |  470 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:          Utility.cpp
  13.    
  14.    Description:   
  15.  
  16. **************************************************************************/
  17.  
  18. /**************************************************************************
  19.    #include statements
  20. **************************************************************************/
  21.  
  22. #include "Utility.h"
  23. #include "ShlFldr.h"
  24. #include "resource.h"
  25.  
  26. /**************************************************************************
  27.    private function prototypes
  28. **************************************************************************/
  29.  
  30. /**************************************************************************
  31.    global variables
  32. **************************************************************************/
  33.  
  34. extern HIMAGELIST g_himlLarge;
  35. extern HIMAGELIST g_himlSmall;
  36.  
  37. #define MAIN_KEY_STRING       (TEXT("Software\\RegView"))
  38. #define VALUE_STRING          (TEXT("Display Settings"))
  39. #define INITIAL_COLUMN_SIZE   100
  40.  
  41. /**************************************************************************
  42.  
  43.    GetKeyName()
  44.    
  45. **************************************************************************/
  46.  
  47. DWORD GetKeyName( HKEY hKeyRoot, 
  48.                   LPCTSTR pszSubKey, 
  49.                   DWORD dwIndex, 
  50.                   LPTSTR pszOut, 
  51.                   DWORD dwSize)
  52. {
  53. HKEY     hKey;
  54. LONG     lResult;
  55. FILETIME ft;
  56.  
  57. if(!pszOut)
  58.    return 0;
  59.  
  60. if(!pszSubKey)
  61.    pszSubKey = TEXT("");
  62.  
  63. //open the specified key
  64. lResult = RegOpenKeyEx( hKeyRoot,
  65.                         pszSubKey,
  66.                         0,
  67.                         KEY_ENUMERATE_SUB_KEYS,
  68.                         &hKey);
  69.  
  70. if(ERROR_SUCCESS != lResult)
  71.    return 0;
  72.  
  73. //try to get the specified subkey
  74. lResult = RegEnumKeyEx( hKey,
  75.                         dwIndex,
  76.                         pszOut,
  77.                         &dwSize,
  78.                         NULL,
  79.                         NULL,
  80.                         NULL,
  81.                         &ft);
  82.  
  83. RegCloseKey(hKey);
  84.  
  85. if(ERROR_SUCCESS == lResult)
  86.    return dwSize;
  87.  
  88. return 0;
  89. }
  90.  
  91. /**************************************************************************
  92.  
  93.    GetValueName()
  94.    
  95. **************************************************************************/
  96.  
  97. BOOL GetValueName(   HKEY hKeyRoot, 
  98.                      LPCTSTR pszSubKey, 
  99.                      DWORD dwIndex, 
  100.                      LPTSTR pszOut, 
  101.                      DWORD dwSize)
  102. {
  103. HKEY     hKey;
  104. LONG     lResult;
  105. DWORD    dwType;
  106.  
  107. if(!pszOut)
  108.    return 0;
  109.  
  110. if(!pszSubKey)
  111.    pszSubKey = TEXT("");
  112.  
  113. //open the specified key
  114. lResult = RegOpenKeyEx( hKeyRoot,
  115.                         pszSubKey,
  116.                         0,
  117.                         KEY_QUERY_VALUE,
  118.                         &hKey);
  119.  
  120. if(ERROR_SUCCESS != lResult)
  121.    return 0;
  122.  
  123. //try to get the specified subkey
  124. lResult = RegEnumValue( hKey,
  125.                         dwIndex,
  126.                         pszOut,
  127.                         &dwSize,
  128.                         NULL,
  129.                         &dwType,
  130.                         NULL,
  131.                         NULL);
  132.  
  133. RegCloseKey(hKey);
  134.  
  135. return BOOL(ERROR_SUCCESS == lResult);
  136.  
  137. if(ERROR_SUCCESS == lResult)
  138.    return dwSize;
  139.  
  140. return 0;
  141. }
  142.  
  143.  
  144. /**************************************************************************
  145.  
  146.    GetRootKeyText()
  147.    
  148. **************************************************************************/
  149.  
  150. DWORD GetRootKeyText(HKEY hKeyRoot, LPTSTR lpszOut, DWORD dwOutSize)
  151. {
  152. *lpszOut = 0;
  153.  
  154. if(hKeyRoot == HKEY_CLASSES_ROOT)
  155.    {
  156.    lstrcpyn(lpszOut, TEXT("HKEY_CLASSES_ROOT"), dwOutSize);
  157.    }
  158.  
  159. else if(hKeyRoot == HKEY_CURRENT_USER)
  160.    {
  161.    lstrcpyn(lpszOut, TEXT("HKEY_CURRENT_USER"), dwOutSize);
  162.    }
  163.  
  164. else if(hKeyRoot == HKEY_LOCAL_MACHINE)
  165.    {
  166.    lstrcpyn(lpszOut, TEXT("HKEY_LOCAL_MACHINE"), dwOutSize);
  167.    }
  168.  
  169. else if(hKeyRoot == HKEY_USERS)
  170.    {
  171.    lstrcpyn(lpszOut, TEXT("HKEY_USERS"), dwOutSize);
  172.    }
  173.  
  174. else if(hKeyRoot == HKEY_PERFORMANCE_DATA)
  175.    {
  176.    lstrcpyn(lpszOut, TEXT("HKEY_PERFORMANCE_DATA"), dwOutSize);
  177.    }
  178.  
  179. else if(hKeyRoot == HKEY_CURRENT_CONFIG)
  180.    {
  181.    lstrcpyn(lpszOut, TEXT("HKEY_CURRENT_CONFIG"), dwOutSize);
  182.    }
  183.  
  184. else if(hKeyRoot == HKEY_DYN_DATA)
  185.    {
  186.    lstrcpyn(lpszOut, TEXT("HKEY_DYN_DATA"), dwOutSize);
  187.    }
  188.    
  189. return lstrlen(lpszOut) + 1;
  190. }
  191.  
  192. /**************************************************************************
  193.  
  194.    RootKeyExists()
  195.    
  196. **************************************************************************/
  197.  
  198. BOOL RootKeyExists(HKEY hKeyRoot)
  199. {
  200. LONG  lResult;
  201. HKEY  hKey;
  202.  
  203. //open the specified key
  204. lResult = RegOpenKeyEx( hKeyRoot,
  205.                         NULL,
  206.                         0,
  207.                         KEY_ENUMERATE_SUB_KEYS,
  208.                         &hKey);
  209.  
  210. if(ERROR_SUCCESS != lResult)
  211.    return FALSE;
  212.  
  213. RegCloseKey(hKey);
  214.  
  215. return TRUE;
  216. }
  217.  
  218. /**************************************************************************
  219.  
  220.    SaveGlobalSettings()
  221.    
  222. **************************************************************************/
  223.  
  224. BOOL SaveGlobalSettings(void)
  225. {
  226. HKEY  hKey;
  227. LONG  lResult;
  228. DWORD dwDisp;
  229.  
  230. lResult = RegCreateKeyEx(  HKEY_CURRENT_USER,
  231.                            MAIN_KEY_STRING,
  232.                            0,
  233.                            NULL,
  234.                            REG_OPTION_NON_VOLATILE, 
  235.                            KEY_ALL_ACCESS,
  236.                            NULL, 
  237.                            &hKey,
  238.                            &dwDisp);
  239.  
  240. if(lResult != ERROR_SUCCESS)
  241.    return FALSE;
  242.  
  243. //create an array to put our data in
  244. DWORD dwArray[4];
  245. dwArray[0] = g_nColumn1;
  246. dwArray[1] = g_nColumn2;
  247. dwArray[2] = g_bViewKeys;
  248. dwArray[3] = g_bShowIDW;
  249.  
  250. //save the last printer selected
  251. lResult = RegSetValueEx(   hKey,
  252.                            VALUE_STRING,
  253.                            0,
  254.                            REG_BINARY,
  255.                            (LPBYTE)dwArray,
  256.                            sizeof(dwArray));
  257.  
  258. RegCloseKey(hKey);
  259.  
  260. if(lResult != ERROR_SUCCESS)
  261.    return FALSE;
  262.  
  263. return TRUE;
  264. }
  265.  
  266. /**************************************************************************
  267.  
  268.    GetGlobalSettings()
  269.    
  270. **************************************************************************/
  271.  
  272. BOOL GetGlobalSettings(void)
  273. {
  274. HKEY  hKey;
  275. LONG  lResult;
  276.  
  277. //set up the default data
  278. g_nColumn1 = INITIAL_COLUMN_SIZE;
  279. g_nColumn2 = INITIAL_COLUMN_SIZE;
  280. g_bViewKeys = TRUE;
  281. g_bShowIDW = FALSE;
  282.  
  283. lResult = RegOpenKeyEx( HKEY_CURRENT_USER,
  284.                         MAIN_KEY_STRING,
  285.                         0,
  286.                         KEY_ALL_ACCESS,
  287.                         &hKey);
  288.  
  289. if(lResult != ERROR_SUCCESS)
  290.    return FALSE;
  291.  
  292. //create an array to put our data in
  293. DWORD dwArray[4];
  294. DWORD dwType;
  295. DWORD dwSize = sizeof(dwArray);
  296.  
  297. //save the last printer selected
  298. lResult = RegQueryValueEx( hKey,
  299.                            VALUE_STRING,
  300.                            NULL,
  301.                            &dwType,
  302.                            (LPBYTE)dwArray,
  303.                            &dwSize);
  304.  
  305. RegCloseKey(hKey);
  306.  
  307. if(lResult != ERROR_SUCCESS)
  308.    return FALSE;
  309.  
  310. g_nColumn1 = dwArray[0];
  311. g_nColumn2 = dwArray[1];
  312. g_bViewKeys = dwArray[2];
  313. g_bShowIDW = dwArray[3];
  314.  
  315. return TRUE;
  316. }
  317.  
  318. /**************************************************************************
  319.  
  320.    CompareItems()
  321.    
  322. **************************************************************************/
  323.  
  324. int CALLBACK CompareItems(LPARAM lParam1, LPARAM lParam2, LPARAM lpData)
  325. {
  326. CShellFolder  *pFolder = (CShellFolder*)lpData;
  327.  
  328. if(!pFolder)
  329.    return 0;
  330.  
  331. return (int)pFolder->CompareIDs(0, (LPITEMIDLIST)lParam1, (LPITEMIDLIST)lParam2);
  332. }
  333.  
  334. /**************************************************************************
  335.  
  336.    CreateImageLists()
  337.    
  338. **************************************************************************/
  339.  
  340. VOID CreateImageLists(VOID)
  341. {
  342. int   cx;
  343. int   cy;
  344.  
  345. cx = GetSystemMetrics(SM_CXSMICON);
  346. cy = GetSystemMetrics(SM_CYSMICON);
  347.  
  348. cx = 16;
  349. cy = 16;
  350.  
  351. if(g_himlSmall)
  352.    ImageList_Destroy(g_himlSmall);
  353.  
  354. //set the small image list
  355. g_himlSmall = ImageList_Create(cx, cy, ILC_COLORDDB | ILC_MASK, 4, 0);
  356.  
  357. if(g_himlSmall)
  358.    {
  359.    HICON hIcon;
  360.    
  361.    hIcon = (HICON)LoadImage(g_hInst, MAKEINTRESOURCE(IDI_BINARY), IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
  362.    ImageList_AddIcon(g_himlSmall, hIcon);
  363.  
  364.    hIcon = (HICON)LoadImage(g_hInst, MAKEINTRESOURCE(IDI_STRING), IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
  365.    ImageList_AddIcon(g_himlSmall, hIcon);
  366.  
  367.    hIcon = (HICON)LoadImage(g_hInst, MAKEINTRESOURCE(IDI_FOLDER), IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
  368.    ImageList_AddIcon(g_himlSmall, hIcon);
  369.  
  370.    hIcon = (HICON)LoadImage(g_hInst, MAKEINTRESOURCE(IDI_FOLDEROPEN), IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
  371.    ImageList_AddIcon(g_himlSmall, hIcon);
  372.    }
  373.  
  374. if(g_himlLarge)
  375.    ImageList_Destroy(g_himlLarge);
  376.  
  377. cx = GetSystemMetrics(SM_CXICON);
  378. cy = GetSystemMetrics(SM_CYICON);
  379.  
  380. cx = 32;
  381. cy = 32;
  382.  
  383. //set the large image list
  384. g_himlLarge = ImageList_Create(cx, cy, ILC_COLORDDB | ILC_MASK, 4, 0);
  385.  
  386. if(g_himlSmall)
  387.    {
  388.    HICON hIcon;
  389.    
  390.    hIcon = (HICON)LoadImage(g_hInst, MAKEINTRESOURCE(IDI_BINARY), IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
  391.    ImageList_AddIcon(g_himlLarge, hIcon);
  392.  
  393.    hIcon = (HICON)LoadImage(g_hInst, MAKEINTRESOURCE(IDI_STRING), IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
  394.    ImageList_AddIcon(g_himlLarge, hIcon);
  395.  
  396.    hIcon = (HICON)LoadImage(g_hInst, MAKEINTRESOURCE(IDI_FOLDER), IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
  397.    ImageList_AddIcon(g_himlLarge, hIcon);
  398.  
  399.    hIcon = (HICON)LoadImage(g_hInst, MAKEINTRESOURCE(IDI_FOLDEROPEN), IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
  400.    ImageList_AddIcon(g_himlLarge, hIcon);
  401.    }
  402.  
  403. }
  404.  
  405. /**************************************************************************
  406.  
  407.    DestroyImageLists()
  408.    
  409. **************************************************************************/
  410.  
  411. VOID DestroyImageLists(VOID)
  412. {
  413. if(g_himlSmall)
  414.    ImageList_Destroy(g_himlSmall);
  415.  
  416. if(g_himlLarge)
  417.    ImageList_Destroy(g_himlLarge);
  418. }
  419.  
  420. /**************************************************************************
  421.  
  422.    WideCharToLocal()
  423.    
  424. **************************************************************************/
  425.  
  426. int WideCharToLocal(LPTSTR pLocal, LPWSTR pWide, DWORD dwChars)
  427. {
  428. *pLocal = 0;
  429.  
  430. #ifdef UNICODE
  431. lstrcpyn(pLocal, pWide, dwChars);
  432. #else
  433. WideCharToMultiByte( CP_ACP, 
  434.                      0, 
  435.                      pWide, 
  436.                      -1, 
  437.                      pLocal, 
  438.                             dwChars, 
  439.                      NULL, 
  440.                      NULL);
  441. #endif
  442.  
  443. return lstrlen(pLocal);
  444. }
  445.  
  446. /**************************************************************************
  447.  
  448.    LocalToWideChar()
  449.    
  450. **************************************************************************/
  451.  
  452. int LocalToWideChar(LPWSTR pWide, LPTSTR pLocal, DWORD dwChars)
  453. {
  454. *pWide = 0;
  455.  
  456. #ifdef UNICODE
  457. lstrcpyn(pWide, pLocal, dwChars);
  458. #else
  459. MultiByteToWideChar( CP_ACP, 
  460.                      0, 
  461.                      pLocal, 
  462.                      -1, 
  463.                      pWide, 
  464.                      dwChars); 
  465. #endif
  466.  
  467. return lstrlenW(pWide);
  468. }
  469.  
  470.