home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2006 April / DPPRO0406DVD.ISO / Essentials / Programming / Notepad2 / Source / NP2SRC.ZIP / Appreg.c next >
Encoding:
C/C++ Source or Header  |  2004-06-25  |  6.0 KB  |  311 lines

  1. /******************************************************************************
  2. *
  3. *
  4. * Notepad2
  5. *
  6. * Appreg.c
  7. *   Registry helper functions
  8. *
  9. * See Readme.txt for more information about this source code.
  10. * Please send me your comments to this work.
  11. *
  12. * Distributed under the terms of the GNU General Public License,
  13. * see License.txt for details.
  14. *
  15. *                                              (c) Florian Balmer 1996-2004
  16. *                                                       textview@bluewin.ch
  17. *                                               http://www.flos-freeware.ch
  18. *
  19. *
  20. ******************************************************************************/
  21. #include <windows.h>
  22. #include "helpers.h"
  23. #include "appreg.h"
  24. #include "resource.h"
  25.  
  26.  
  27. HKEY RegGetAppKey(LPCSTR lpSubKey)
  28. {
  29.  
  30.   HKEY  hKey;
  31.   char  szKey[256];
  32.   DWORD dwDisp;
  33.   LONG  nRet;
  34.  
  35.   lstrcpy(szKey,"Software\\Notepad2");
  36.   if (lpSubKey)
  37.   {
  38.     lstrcat(szKey,"\\");
  39.     lstrcat(szKey,lpSubKey);
  40.   }
  41.  
  42.   nRet = RegCreateKeyEx(
  43.            HKEY_CURRENT_USER,
  44.            szKey,
  45.            0,
  46.            NULL,
  47.            0,
  48.            KEY_ALL_ACCESS,
  49.            NULL,
  50.            &hKey,
  51.            &dwDisp);
  52.  
  53.   if (nRet == ERROR_SUCCESS)
  54.     return(hKey);
  55.  
  56.   else
  57.     return(NULL);
  58.  
  59. }
  60.  
  61.  
  62. HKEY RegGetSubKey(HKEY hKeyRoot,LPCSTR lpSubKey)
  63. {
  64.  
  65.   HKEY  hKey;
  66.   DWORD dwDisp;
  67.   LONG  nRet;
  68.  
  69.   nRet = RegCreateKeyEx(
  70.            hKeyRoot,
  71.            lpSubKey,
  72.            0,
  73.            NULL,
  74.            0,
  75.            KEY_ALL_ACCESS,
  76.            NULL,
  77.            &hKey,
  78.            &dwDisp);
  79.  
  80.   if (nRet == ERROR_SUCCESS)
  81.     return(hKey);
  82.  
  83.   else
  84.     return(NULL);
  85.  
  86. }
  87.  
  88.  
  89. HKEY RegGetKey(LPCSTR lpKey)
  90. {
  91.  
  92.   HKEY  hKey;
  93.   DWORD dwDisp;
  94.   LONG  nRet;
  95.  
  96.   nRet = RegCreateKeyEx(
  97.            HKEY_CURRENT_USER,
  98.            lpKey,
  99.            0,
  100.            NULL,
  101.            0,
  102.            KEY_ALL_ACCESS,
  103.            NULL,
  104.            &hKey,
  105.            &dwDisp);
  106.  
  107.   if (nRet == ERROR_SUCCESS)
  108.     return(hKey);
  109.  
  110.   else
  111.     return(NULL);
  112.  
  113. }
  114.  
  115.  
  116. void RegDeleteKeyEx(HKEY hKey,LPCSTR pszSubKey,BOOL *pbError)
  117. {
  118.  
  119.   HKEY  hSubKey;
  120.   char  szSubSubKey[64];
  121.   DWORD dwSubSubKey = 64;
  122.  
  123.   if (ERROR_SUCCESS == RegOpenKeyEx(hKey,
  124.                                     pszSubKey,
  125.                                     0,
  126.                                     KEY_ALL_ACCESS,
  127.                                     &hSubKey))
  128.   {
  129.  
  130.     while (ERROR_SUCCESS == RegEnumKeyEx(hSubKey,
  131.                                          0,
  132.                                          szSubSubKey,
  133.                                          &dwSubSubKey,
  134.                                          NULL,
  135.                                          NULL,
  136.                                          0,
  137.                                          NULL))
  138.     {
  139.  
  140.       RegDeleteKeyEx(hSubKey,szSubSubKey,pbError);
  141.       dwSubSubKey = 64;
  142.  
  143.     }
  144.  
  145.     RegCloseKey(hSubKey);
  146.  
  147.     if (ERROR_SUCCESS != RegDeleteKey(hKey,pszSubKey))
  148.       *pbError = TRUE;
  149.  
  150.   }
  151.  
  152.   else
  153.     *pbError = TRUE;
  154.  
  155. }
  156.  
  157.  
  158. int RegGetInt(HKEY hKey,LPCSTR lpName,int nDefault)
  159. {
  160.  
  161.   LONG  nRet;
  162.   DWORD dwValue;
  163.   DWORD cbData = sizeof(DWORD);
  164.   DWORD dwType = REG_DWORD;
  165.  
  166.   nRet = RegQueryValueEx(
  167.            hKey,
  168.            lpName,
  169.            NULL,
  170.            &dwType,
  171.            (LPBYTE)&dwValue,
  172.            &cbData);
  173.  
  174.   if (nRet == ERROR_SUCCESS)
  175.     return(int)dwValue;
  176.  
  177.   else
  178.     return(nDefault);
  179.  
  180. }
  181.  
  182.  
  183. BOOL RegGetString(HKEY hKey,LPCSTR lpName,LPCSTR lpDefault,LPSTR lpBuf,int nBuf)
  184. {
  185.  
  186.   LONG  nRet;
  187.   DWORD cbData = nBuf;
  188.   DWORD dwType = REG_SZ;
  189.  
  190.   nRet = RegQueryValueEx(
  191.            hKey,
  192.            lpName,
  193.            NULL,
  194.            &dwType,
  195.            (LPBYTE)lpBuf,
  196.            &cbData);
  197.  
  198.   if (nRet == ERROR_SUCCESS)
  199.     return (lstrlen(lpBuf));
  200.  
  201.   else
  202.   {
  203.     lstrcpy(lpBuf,lpDefault);
  204.     return FALSE;
  205.   }
  206.  
  207. }
  208.  
  209.  
  210. void RegSetInt(HKEY hKey,LPCSTR lpName,int nValue)
  211. {
  212.  
  213.   RegSetValueEx(hKey,
  214.                 lpName,
  215.                 0,
  216.                 REG_DWORD,
  217.                 (LPBYTE)&nValue,
  218.                 sizeof(int));
  219.  
  220. }
  221.  
  222.  
  223. void RegSetString(HKEY hKey,LPCSTR lpName,LPCSTR lpValue)
  224. {
  225.  
  226.   if (lpValue && *lpValue) {
  227.  
  228.     RegSetValueEx(hKey,
  229.                   lpName,
  230.                   0,
  231.                   REG_SZ,
  232.                   (LPBYTE)lpValue,
  233.                   lstrlen(lpValue)+1); }
  234.  
  235.   else {
  236.  
  237.     RegSetValueEx(hKey,
  238.                   lpName,
  239.                   0,
  240.                   REG_SZ,
  241.                   (LPBYTE)"\0",
  242.                   1); }
  243.  
  244. }
  245.  
  246.  
  247. int RegGetAppIntEx(LPCSTR lpKey, LPCSTR lpName, int nDefault)
  248. {
  249.   HKEY hKey = RegGetAppKey(lpKey);
  250.   int  nValue = RegGetInt(hKey,lpName,nDefault);
  251.   RegCloseKey(hKey);
  252.   return(nValue);
  253. }
  254.  
  255.  
  256. void RegSetAppIntEx(LPCSTR lpKey, LPCSTR lpName, int nValue)
  257. {
  258.   HKEY hKey = RegGetAppKey(lpKey);
  259.   RegSetInt(hKey,lpName,nValue);
  260.   RegCloseKey(hKey);
  261. }
  262.  
  263.  
  264.  
  265. /******************************************************************************
  266. *
  267. *
  268. * UnregisterApp()
  269. *
  270. * Removes the Textview registry entry
  271. *
  272. *
  273. ******************************************************************************/
  274. void UnregisterApp(HINSTANCE hInstance)
  275. {
  276.  
  277.   MSGBOXPARAMS mbp;
  278.  
  279.   char szTitle[32];
  280.   char szText[256];
  281.  
  282.   GetString(IDS_APPTITLE,szTitle,COUNTOF(szTitle));
  283.   GetString(IDS_UNREG1,szText,COUNTOF(szText));
  284.  
  285.   mbp.cbSize = sizeof(MSGBOXPARAMS);
  286.   mbp.hwndOwner = NULL;
  287.   mbp.hInstance = hInstance;
  288.   mbp.lpszText = szText;
  289.   mbp.lpszCaption = szTitle;
  290.   mbp.dwStyle = MB_YESNO | MB_USERICON | MB_SETFOREGROUND;
  291.   mbp.lpszIcon = MAKEINTRESOURCE(IDR_MAINWND);
  292.   mbp.dwContextHelpId = 0;
  293.   mbp.lpfnMsgBoxCallback = NULL;
  294.   mbp.dwLanguageId = MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL);
  295.  
  296.   if (MessageBoxIndirect(&mbp) == IDYES)
  297.   {
  298.     BOOL bError;
  299.     RegDeleteKeyEx(HKEY_CURRENT_USER,"Software\\Notepad2",&bError);
  300.  
  301.     GetString(IDS_UNREG2,szText,COUNTOF(szText));
  302.     mbp.dwStyle = MB_OK | MB_USERICON | MB_SETFOREGROUND;
  303.  
  304.     MessageBoxIndirect(&mbp);
  305.   }
  306.  
  307. }
  308.  
  309.  
  310. // End of Appreg.h
  311.