home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / icm20 / icmview / regutil.c < prev    next >
C/C++ Source or Header  |  1997-09-07  |  3KB  |  112 lines

  1. //THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. //ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. //THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright  1994-1997  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  FILE:
  9. //    REGUTIL.C
  10. //
  11. //  PURPOSE:
  12. //    Registry access functions.
  13. //
  14. //  PLATFORMS:
  15. //    Windows 95, Windows NT
  16. //
  17. //  SPECIAL INSTRUCTIONS: N/A
  18. //
  19.  
  20. // Windows Header Files:
  21. #pragma warning(disable:4001)   // Single-line comment warnings
  22. #pragma warning(disable:4115)   // Named type definition in parentheses
  23. #pragma warning(disable:4201)   // Nameless struct/union warning
  24. #pragma warning(disable:4214)   // Bit field types other than int warnings
  25. #pragma warning(disable:4514)   // Unreferenced inline function has been removed
  26.  
  27. // Windows Header Files:
  28. #include <Windows.h>
  29. #include <WindowsX.h>
  30.  
  31. // Restore the warnings--leave the single-line comment warning OFF
  32. #pragma warning(default:4115)   // Named type definition in parentheses
  33. #pragma warning(default:4201)   // Nameless struct/union warning
  34. #pragma warning(default:4214)   // Bit field types other than int warnings
  35.  
  36. // C RunTime Header Files
  37.  
  38. // Local Header Files
  39.  
  40. // local definitions
  41.  
  42. // default settings
  43.  
  44. // external functions
  45.  
  46. // external data
  47.  
  48. // public data
  49.  
  50. // private data
  51.  
  52. // public functions
  53.  
  54. //////////////////////////////////////////////////////////////////////////
  55. //  Function:  GetRegistryString
  56. //
  57. //  Description:
  58. //    Retrieves the string associated with the specified key in the registry.
  59. //
  60. //  Parameters:
  61. //    @@@
  62. //
  63. //  Returns:
  64. //    LPTSTR   Pointer to registry string.  NULL upon failure.
  65. //
  66. //  Comments:
  67. //
  68. //
  69. //////////////////////////////////////////////////////////////////////////
  70. LPTSTR GetRegistryString(HKEY hKeyClass, LPTSTR lpszSubKey, LPTSTR lpszValueName)
  71. {
  72.     // Local variables
  73.     HKEY          hKey;                 // Registry key
  74.     LPTSTR         lpszKeyValue;         // Buffer for key name
  75.     DWORD         dwKeySize;            // Size of key value
  76.     DWORD         dwKeyDataType;        // Type of data stored in key
  77.     LONG          lRC;                  // Return code
  78.  
  79.     //  Initialize variables
  80.     dwKeyDataType = 0;
  81.     dwKeySize = 0;
  82.     hKey = NULL;
  83.  
  84.     lRC = RegOpenKey(hKeyClass, lpszSubKey, &hKey);
  85.     if (lRC != ERROR_SUCCESS)
  86.     {
  87.         return(NULL);
  88.     }
  89.  
  90.     // Got key, get value.  First, get the size of the key.
  91.     lRC = RegQueryValueEx(hKey, lpszValueName, NULL, NULL, NULL, &dwKeySize);
  92.     if (lRC != ERROR_SUCCESS)
  93.     {
  94.         return(NULL);
  95.     }
  96.     if (dwKeySize <= 1)  // Registry will return "" if no printers installed
  97.     {
  98.         return(NULL);
  99.     }
  100.  
  101.     lpszKeyValue = GlobalAlloc(GPTR, (++dwKeySize));
  102.     if (lpszKeyValue == NULL)
  103.     {
  104.         return(NULL);
  105.     }
  106.  
  107.     lRC = RegQueryValueEx(hKey, lpszValueName, NULL, &dwKeyDataType, (LPBYTE)lpszKeyValue, &dwKeySize);
  108.     return(lpszKeyValue);
  109. }   // End of function GetRegistryString
  110.  
  111.  
  112.