UTILITY.CPP
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:12k
源码类别:

Windows编程

开发平台:

Visual C++

  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.    Copyright 1997 Microsoft Corporation.  All Rights Reserved.
  7. **************************************************************************/
  8. /**************************************************************************
  9.    File:          Utility.cpp
  10.    
  11.    Description:   
  12. **************************************************************************/
  13. /**************************************************************************
  14.    #include statements
  15. **************************************************************************/
  16. #include "Utility.h"
  17. #include "ShlFldr.h"
  18. #include "resource.h"
  19. /**************************************************************************
  20.    private function prototypes
  21. **************************************************************************/
  22. /**************************************************************************
  23.    global variables
  24. **************************************************************************/
  25. extern HIMAGELIST g_himlLarge;
  26. extern HIMAGELIST g_himlSmall;
  27. #define MAIN_KEY_STRING       (TEXT("Software\RegView"))
  28. #define VALUE_STRING          (TEXT("Display Settings"))
  29. #define INITIAL_COLUMN_SIZE   100
  30. /**************************************************************************
  31.    GetKeyName()
  32.    
  33. **************************************************************************/
  34. DWORD GetKeyName( HKEY hKeyRoot, 
  35.                   LPCTSTR pszSubKey, 
  36.                   DWORD dwIndex, 
  37.                   LPTSTR pszOut, 
  38.                   DWORD dwSize)
  39. {
  40. HKEY     hKey;
  41. LONG     lResult;
  42. FILETIME ft;
  43. if(!pszOut)
  44.    return 0;
  45. if(!pszSubKey)
  46.    pszSubKey = TEXT("");
  47. //open the specified key
  48. lResult = RegOpenKeyEx( hKeyRoot,
  49.                         pszSubKey,
  50.                         0,
  51.                         KEY_ENUMERATE_SUB_KEYS,
  52.                         &hKey);
  53. if(ERROR_SUCCESS != lResult)
  54.    return 0;
  55. //try to get the specified subkey
  56. lResult = RegEnumKeyEx( hKey,
  57.                         dwIndex,
  58.                         pszOut,
  59.                         &dwSize,
  60.                         NULL,
  61.                         NULL,
  62.                         NULL,
  63.                         &ft);
  64. RegCloseKey(hKey);
  65. if(ERROR_SUCCESS == lResult)
  66.    return dwSize;
  67. return 0;
  68. }
  69. /**************************************************************************
  70.    GetValueName()
  71.    
  72. **************************************************************************/
  73. BOOL GetValueName(   HKEY hKeyRoot, 
  74.                      LPCTSTR pszSubKey, 
  75.                      DWORD dwIndex, 
  76.                      LPTSTR pszOut, 
  77.                      DWORD dwSize)
  78. {
  79. HKEY     hKey;
  80. LONG     lResult;
  81. DWORD    dwType;
  82. if(!pszOut)
  83.    return 0;
  84. if(!pszSubKey)
  85.    pszSubKey = TEXT("");
  86. //open the specified key
  87. lResult = RegOpenKeyEx( hKeyRoot,
  88.                         pszSubKey,
  89.                         0,
  90.                         KEY_QUERY_VALUE,
  91.                         &hKey);
  92. if(ERROR_SUCCESS != lResult)
  93.    return 0;
  94. //try to get the specified subkey
  95. lResult = RegEnumValue( hKey,
  96.                         dwIndex,
  97.                         pszOut,
  98.                         &dwSize,
  99.                         NULL,
  100.                         &dwType,
  101.                         NULL,
  102.                         NULL);
  103. RegCloseKey(hKey);
  104. return BOOL(ERROR_SUCCESS == lResult);
  105. if(ERROR_SUCCESS == lResult)
  106.    return dwSize;
  107. return 0;
  108. }
  109. /**************************************************************************
  110.    GetRootKeyText()
  111.    
  112. **************************************************************************/
  113. DWORD GetRootKeyText(HKEY hKeyRoot, LPTSTR lpszOut, DWORD dwOutSize)
  114. {
  115. *lpszOut = 0;
  116. if(hKeyRoot == HKEY_CLASSES_ROOT)
  117.    {
  118.    lstrcpyn(lpszOut, TEXT("HKEY_CLASSES_ROOT"), dwOutSize);
  119.    }
  120. else if(hKeyRoot == HKEY_CURRENT_USER)
  121.    {
  122.    lstrcpyn(lpszOut, TEXT("HKEY_CURRENT_USER"), dwOutSize);
  123.    }
  124. else if(hKeyRoot == HKEY_LOCAL_MACHINE)
  125.    {
  126.    lstrcpyn(lpszOut, TEXT("HKEY_LOCAL_MACHINE"), dwOutSize);
  127.    }
  128. else if(hKeyRoot == HKEY_USERS)
  129.    {
  130.    lstrcpyn(lpszOut, TEXT("HKEY_USERS"), dwOutSize);
  131.    }
  132. else if(hKeyRoot == HKEY_PERFORMANCE_DATA)
  133.    {
  134.    lstrcpyn(lpszOut, TEXT("HKEY_PERFORMANCE_DATA"), dwOutSize);
  135.    }
  136. else if(hKeyRoot == HKEY_CURRENT_CONFIG)
  137.    {
  138.    lstrcpyn(lpszOut, TEXT("HKEY_CURRENT_CONFIG"), dwOutSize);
  139.    }
  140. else if(hKeyRoot == HKEY_DYN_DATA)
  141.    {
  142.    lstrcpyn(lpszOut, TEXT("HKEY_DYN_DATA"), dwOutSize);
  143.    }
  144.    
  145. return lstrlen(lpszOut) + 1;
  146. }
  147. /**************************************************************************
  148.    RootKeyExists()
  149.    
  150. **************************************************************************/
  151. BOOL RootKeyExists(HKEY hKeyRoot)
  152. {
  153. LONG  lResult;
  154. HKEY  hKey;
  155. //open the specified key
  156. lResult = RegOpenKeyEx( hKeyRoot,
  157.                         NULL,
  158.                         0,
  159.                         KEY_ENUMERATE_SUB_KEYS,
  160.                         &hKey);
  161. if(ERROR_SUCCESS != lResult)
  162.    return FALSE;
  163. RegCloseKey(hKey);
  164. return TRUE;
  165. }
  166. /**************************************************************************
  167.    SaveGlobalSettings()
  168.    
  169. **************************************************************************/
  170. BOOL SaveGlobalSettings(void)
  171. {
  172. HKEY  hKey;
  173. LONG  lResult;
  174. DWORD dwDisp;
  175. lResult = RegCreateKeyEx(  HKEY_CURRENT_USER,
  176.                            MAIN_KEY_STRING,
  177.                            0,
  178.                            NULL,
  179.                            REG_OPTION_NON_VOLATILE, 
  180.                            KEY_ALL_ACCESS,
  181.                            NULL, 
  182.                            &hKey,
  183.                            &dwDisp);
  184. if(lResult != ERROR_SUCCESS)
  185.    return FALSE;
  186. //create an array to put our data in
  187. DWORD dwArray[4];
  188. dwArray[0] = g_nColumn1;
  189. dwArray[1] = g_nColumn2;
  190. dwArray[2] = g_bViewKeys;
  191. dwArray[3] = g_bShowIDW;
  192. //save the last printer selected
  193. lResult = RegSetValueEx(   hKey,
  194.                            VALUE_STRING,
  195.                            0,
  196.                            REG_BINARY,
  197.                            (LPBYTE)dwArray,
  198.                            sizeof(dwArray));
  199. RegCloseKey(hKey);
  200. if(lResult != ERROR_SUCCESS)
  201.    return FALSE;
  202. return TRUE;
  203. }
  204. /**************************************************************************
  205.    GetGlobalSettings()
  206.    
  207. **************************************************************************/
  208. BOOL GetGlobalSettings(void)
  209. {
  210. HKEY  hKey;
  211. LONG  lResult;
  212. //set up the default data
  213. g_nColumn1 = INITIAL_COLUMN_SIZE;
  214. g_nColumn2 = INITIAL_COLUMN_SIZE;
  215. g_bViewKeys = TRUE;
  216. g_bShowIDW = FALSE;
  217. lResult = RegOpenKeyEx( HKEY_CURRENT_USER,
  218.                         MAIN_KEY_STRING,
  219.                         0,
  220.                         KEY_ALL_ACCESS,
  221.                         &hKey);
  222. if(lResult != ERROR_SUCCESS)
  223.    return FALSE;
  224. //create an array to put our data in
  225. DWORD dwArray[4];
  226. DWORD dwType;
  227. DWORD dwSize = sizeof(dwArray);
  228. //save the last printer selected
  229. lResult = RegQueryValueEx( hKey,
  230.                            VALUE_STRING,
  231.                            NULL,
  232.                            &dwType,
  233.                            (LPBYTE)dwArray,
  234.                            &dwSize);
  235. RegCloseKey(hKey);
  236. if(lResult != ERROR_SUCCESS)
  237.    return FALSE;
  238. g_nColumn1 = dwArray[0];
  239. g_nColumn2 = dwArray[1];
  240. g_bViewKeys = dwArray[2];
  241. g_bShowIDW = dwArray[3];
  242. return TRUE;
  243. }
  244. /**************************************************************************
  245.    CompareItems()
  246.    
  247. **************************************************************************/
  248. int CALLBACK CompareItems(LPARAM lParam1, LPARAM lParam2, LPARAM lpData)
  249. {
  250. CShellFolder  *pFolder = (CShellFolder*)lpData;
  251. if(!pFolder)
  252.    return 0;
  253. return (int)pFolder->CompareIDs(0, (LPITEMIDLIST)lParam1, (LPITEMIDLIST)lParam2);
  254. }
  255. /**************************************************************************
  256.    CreateImageLists()
  257.    
  258. **************************************************************************/
  259. VOID CreateImageLists(VOID)
  260. {
  261. int   cx;
  262. int   cy;
  263. cx = GetSystemMetrics(SM_CXSMICON);
  264. cy = GetSystemMetrics(SM_CYSMICON);
  265. cx = 16;
  266. cy = 16;
  267. if(g_himlSmall)
  268.    ImageList_Destroy(g_himlSmall);
  269. //set the small image list
  270. g_himlSmall = ImageList_Create(cx, cy, ILC_COLORDDB | ILC_MASK, 4, 0);
  271. if(g_himlSmall)
  272.    {
  273.    HICON hIcon;
  274.    
  275.    hIcon = (HICON)LoadImage(g_hInst, MAKEINTRESOURCE(IDI_BINARY), IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
  276.    ImageList_AddIcon(g_himlSmall, hIcon);
  277.    hIcon = (HICON)LoadImage(g_hInst, MAKEINTRESOURCE(IDI_STRING), IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
  278.    ImageList_AddIcon(g_himlSmall, hIcon);
  279.    hIcon = (HICON)LoadImage(g_hInst, MAKEINTRESOURCE(IDI_FOLDER), IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
  280.    ImageList_AddIcon(g_himlSmall, hIcon);
  281.    hIcon = (HICON)LoadImage(g_hInst, MAKEINTRESOURCE(IDI_FOLDEROPEN), IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
  282.    ImageList_AddIcon(g_himlSmall, hIcon);
  283.    }
  284. if(g_himlLarge)
  285.    ImageList_Destroy(g_himlLarge);
  286. cx = GetSystemMetrics(SM_CXICON);
  287. cy = GetSystemMetrics(SM_CYICON);
  288. cx = 32;
  289. cy = 32;
  290. //set the large image list
  291. g_himlLarge = ImageList_Create(cx, cy, ILC_COLORDDB | ILC_MASK, 4, 0);
  292. if(g_himlSmall)
  293.    {
  294.    HICON hIcon;
  295.    
  296.    hIcon = (HICON)LoadImage(g_hInst, MAKEINTRESOURCE(IDI_BINARY), IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
  297.    ImageList_AddIcon(g_himlLarge, hIcon);
  298.    hIcon = (HICON)LoadImage(g_hInst, MAKEINTRESOURCE(IDI_STRING), IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
  299.    ImageList_AddIcon(g_himlLarge, hIcon);
  300.    hIcon = (HICON)LoadImage(g_hInst, MAKEINTRESOURCE(IDI_FOLDER), IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
  301.    ImageList_AddIcon(g_himlLarge, hIcon);
  302.    hIcon = (HICON)LoadImage(g_hInst, MAKEINTRESOURCE(IDI_FOLDEROPEN), IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
  303.    ImageList_AddIcon(g_himlLarge, hIcon);
  304.    }
  305. }
  306. /**************************************************************************
  307.    DestroyImageLists()
  308.    
  309. **************************************************************************/
  310. VOID DestroyImageLists(VOID)
  311. {
  312. if(g_himlSmall)
  313.    ImageList_Destroy(g_himlSmall);
  314. if(g_himlLarge)
  315.    ImageList_Destroy(g_himlLarge);
  316. }
  317. /**************************************************************************
  318.    WideCharToLocal()
  319.    
  320. **************************************************************************/
  321. int WideCharToLocal(LPTSTR pLocal, LPWSTR pWide, DWORD dwChars)
  322. {
  323. *pLocal = 0;
  324. #ifdef UNICODE
  325. lstrcpyn(pLocal, pWide, dwChars);
  326. #else
  327. WideCharToMultiByte( CP_ACP, 
  328.                      0, 
  329.                      pWide, 
  330.                      -1, 
  331.                      pLocal, 
  332. dwChars, 
  333.                      NULL, 
  334.                      NULL);
  335. #endif
  336. return lstrlen(pLocal);
  337. }
  338. /**************************************************************************
  339.    LocalToWideChar()
  340.    
  341. **************************************************************************/
  342. int LocalToWideChar(LPWSTR pWide, LPTSTR pLocal, DWORD dwChars)
  343. {
  344. *pWide = 0;
  345. #ifdef UNICODE
  346. lstrcpyn(pWide, pLocal, dwChars);
  347. #else
  348. MultiByteToWideChar( CP_ACP, 
  349.                      0, 
  350.                      pLocal, 
  351.                      -1, 
  352.                      pWide, 
  353.                      dwChars); 
  354. #endif
  355. return lstrlenW(pWide);
  356. }