home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / video / vidcap / profile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-05  |  8.5 KB  |  400 lines

  1. /**************************************************************************
  2.  *
  3.  *  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4.  *  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5.  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6.  *  PURPOSE.
  7.  *
  8.  *  Copyright (C) 1992 - 1997 Microsoft Corporation.  All Rights Reserved.
  9.  *
  10.  **************************************************************************/
  11. /****************************************************************************
  12.  *
  13.  *   profile.c: Stores profile info in the Registry 
  14.  *
  15.  *   Vidcap32 Source code
  16.  *
  17.  ***************************************************************************/
  18.  
  19. /*
  20.  * win32/win16 utility functions to read and write profile items
  21.  * for multimedia tools
  22.  */
  23.  
  24. #include <windows.h>
  25. #include <windowsx.h>
  26.  
  27. #ifdef _WIN32
  28. #define KEYNAME     "Software\\Microsoft\\Multimedia Tools\\"
  29. #define ROOTKEY     HKEY_CURRENT_USER
  30. #else
  31. #define INIFILE    "mmtools.ini"
  32. #endif
  33.  
  34.  
  35. /*
  36.  * read a BOOL flag from the profile, or return default if
  37.  * not found.
  38.  */
  39. BOOL
  40. mmGetProfileFlag(LPSTR appname, LPSTR valuename, BOOL bDefault)
  41. {
  42. #ifdef _WIN32
  43.     char achName[MAX_PATH];
  44.     HKEY hkey;
  45.     DWORD dwType;
  46.     BOOL bValue = bDefault;
  47.     DWORD dwData;
  48.     int cbData;
  49.  
  50.  
  51.     lstrcpy(achName, KEYNAME);
  52.     lstrcat(achName, appname);
  53.     if (RegOpenKey(ROOTKEY, achName, &hkey) != ERROR_SUCCESS) {
  54.         return(bDefault);
  55.     }
  56.  
  57.     cbData = sizeof(dwData);
  58.     if (RegQueryValueEx(
  59.         hkey,
  60.         valuename,
  61.         NULL,
  62.         &dwType,
  63.         (PBYTE) &dwData,
  64.         &cbData) == ERROR_SUCCESS) {
  65.             if (dwType == REG_DWORD) {
  66.                 if (dwData) {
  67.                     bValue = TRUE;
  68.                 } else {
  69.                     bValue = FALSE;
  70.                 }
  71.             }
  72.     }
  73.  
  74.     RegCloseKey(hkey);
  75.  
  76.     return(bValue);
  77. #else
  78.     char ach[10];
  79.  
  80.     GetPrivateProfileString(appname, valuename, "X", ach, sizeof(ach),
  81.             INIFILE);
  82.  
  83.     switch(ach[0]) {
  84.     case 'N':
  85.     case 'n':
  86.     case '0':
  87.         return(FALSE);
  88.  
  89.     case 'Y':
  90.     case 'y':
  91.     case '1':
  92.         return(TRUE);
  93.  
  94.     default:
  95.         return(bDefault);
  96.     }
  97. #endif
  98. }
  99.  
  100.  
  101. /*
  102.  * write a boolean value to the registry, if it is not the
  103.  * same as the default or the value already there
  104.  */
  105. VOID
  106. mmWriteProfileFlag(LPSTR appname, LPSTR valuename, BOOL bValue, BOOL bDefault)
  107. {
  108.     if (mmGetProfileFlag(appname, valuename, bDefault) == bValue) {
  109.         return;
  110.     }
  111.  
  112. #ifdef _WIN32
  113.     {
  114.         char achName[MAX_PATH];
  115.         HKEY hkey;
  116.  
  117.         lstrcpy(achName, KEYNAME);
  118.         lstrcat(achName, appname);
  119.         if (RegCreateKey(ROOTKEY, achName, &hkey) == ERROR_SUCCESS) {
  120.             RegSetValueEx(
  121.                 hkey,
  122.                 valuename,
  123.                 0,
  124.                 REG_DWORD,
  125.                 (PBYTE) &bValue,
  126.                 sizeof(bValue)
  127.             );
  128.  
  129.             RegCloseKey(hkey);
  130.         }
  131.     }
  132.  
  133. #else
  134.     WritePrivateProfileString(
  135.         appname,
  136.         valuename,
  137.         bValue ? "1" : "0",
  138.         INIFILE);
  139. #endif
  140. }
  141.  
  142. /*
  143.  * read a UINT from the profile, or return default if
  144.  * not found.
  145.  */
  146. UINT
  147. mmGetProfileInt(LPSTR appname, LPSTR valuename, UINT uDefault)
  148. {
  149. #ifdef _WIN32
  150.     char achName[MAX_PATH];
  151.     HKEY hkey;
  152.     DWORD dwType;
  153.     UINT value = uDefault;
  154.     DWORD dwData;
  155.     int cbData;
  156.  
  157.  
  158.     lstrcpy(achName, KEYNAME);
  159.     lstrcat(achName, appname);
  160.     if (RegOpenKey(ROOTKEY, achName, &hkey) != ERROR_SUCCESS) {
  161.         return(uDefault);
  162.     }
  163.  
  164.     cbData = sizeof(dwData);
  165.     if (RegQueryValueEx(
  166.         hkey,
  167.         valuename,
  168.         NULL,
  169.         &dwType,
  170.         (PBYTE) &dwData,
  171.         &cbData) == ERROR_SUCCESS) {
  172.             if (dwType == REG_DWORD) {
  173.                 value = (UINT)dwData;
  174.             }
  175.     }
  176.  
  177.     RegCloseKey(hkey);
  178.  
  179.     return(value);
  180. #else
  181.     return(GetPrivateProfileInt(appname, valuename, uDefault, INIFILE);
  182. #endif
  183. }
  184.  
  185.  
  186. /*
  187.  * write a UINT to the profile, if it is not the
  188.  * same as the default or the value already there
  189.  */
  190. VOID
  191. mmWriteProfileInt(LPSTR appname, LPSTR valuename, UINT uValue, UINT uDefault)
  192. {
  193.     if (mmGetProfileInt(appname, valuename, uDefault) == uValue) {
  194.         return;
  195.     }
  196.  
  197. #ifdef _WIN32
  198.     {
  199.         char achName[MAX_PATH];
  200.         HKEY hkey;
  201.         DWORD dwData = uValue;
  202.  
  203.         lstrcpy(achName, KEYNAME);
  204.         lstrcat(achName, appname);
  205.         if (RegCreateKey(ROOTKEY, achName, &hkey) == ERROR_SUCCESS) {
  206.             RegSetValueEx(
  207.                 hkey,
  208.                 valuename,
  209.                 0,
  210.                 REG_DWORD,
  211.                 (PBYTE) &dwData,
  212.                 sizeof(dwData)
  213.             );
  214.  
  215.             RegCloseKey(hkey);
  216.         }
  217.     }
  218.  
  219. #else
  220.     char ach[12];
  221.  
  222.     wsprintf(ach, "%d", uValue);
  223.  
  224.     WritePrivateProfileString(
  225.         appname,
  226.         valuename,
  227.         ach,
  228.         INIFILE);
  229. #endif
  230. }
  231.  
  232.  
  233. /*
  234.  * read a string from the profile into pResult.
  235.  * result is number of bytes written into pResult
  236.  */
  237. DWORD
  238. mmGetProfileString(
  239.     LPSTR appname,
  240.     LPSTR valuename,
  241.     LPSTR pDefault,
  242.     LPSTR pResult,
  243.     int cbResult
  244. )
  245. {
  246. #ifdef _WIN32
  247.     char achName[MAX_PATH];
  248.     HKEY hkey;
  249.     DWORD dwType;
  250.  
  251.  
  252.     lstrcpy(achName, KEYNAME);
  253.     lstrcat(achName, appname);
  254.     if (RegOpenKey(ROOTKEY, achName, &hkey) == ERROR_SUCCESS) {
  255.  
  256.         if (RegQueryValueEx(
  257.             hkey,
  258.             valuename,
  259.             NULL,
  260.             &dwType,
  261.             pResult,
  262.             &cbResult) == ERROR_SUCCESS) {
  263.  
  264.                 if (dwType == REG_SZ) {
  265.                     // cbResult is set to the size including null
  266.                     RegCloseKey(hkey);
  267.                     return(cbResult - 1);
  268.                 }
  269.         }
  270.  
  271.  
  272.         RegCloseKey(hkey);
  273.     }
  274.  
  275.     // if we got here, we didn't find it, or it was the wrong type - return
  276.     // the default string
  277.     lstrcpy(pResult, pDefault);
  278.     return(lstrlen(pDefault));
  279.  
  280. #else
  281.     return GetPrivateProfileString(
  282.                 appname,
  283.                 valuename,
  284.                 pDefault,
  285.                 pResult,
  286.                 cbResult
  287.                 INIFILE);
  288. #endif
  289. }
  290.  
  291.  
  292. /*
  293.  * write a string to the profile
  294.  */
  295. VOID
  296. mmWriteProfileString(LPSTR appname, LPSTR valuename, LPSTR pData)
  297. {
  298. #ifdef _WIN32
  299.     char achName[MAX_PATH];
  300.     HKEY hkey;
  301.  
  302.     lstrcpy(achName, KEYNAME);
  303.     lstrcat(achName, appname);
  304.     if (RegCreateKey(ROOTKEY, achName, &hkey) == ERROR_SUCCESS) {
  305.         RegSetValueEx(
  306.             hkey,
  307.             valuename,
  308.             0,
  309.             REG_SZ,
  310.             pData,
  311.             lstrlen(pData) + 1
  312.         );
  313.  
  314.         RegCloseKey(hkey);
  315.     }
  316.  
  317. #else
  318.     WritePrivateProfileString(
  319.         appname,
  320.         valuename,
  321.         pData,
  322.         INIFILE);
  323. #endif
  324. }
  325.  
  326. /*
  327.  * read binary values from the profile into pResult.
  328.  * result is number of bytes written into pResult
  329.  */
  330. DWORD
  331. mmGetProfileBinary(
  332.     LPSTR appname,
  333.     LPSTR valuename,
  334.     LPVOID pDefault,  
  335.     LPVOID pResult,   // if NULL, return the required size
  336.     int cbSize
  337. )
  338. {
  339.     char achName[MAX_PATH];
  340.     HKEY hkey;
  341.     DWORD dwType;
  342.     int cbResult = cbSize;
  343.  
  344.     lstrcpy(achName, KEYNAME);
  345.     lstrcat(achName, appname);
  346.     if (RegOpenKey(ROOTKEY, achName, &hkey) == ERROR_SUCCESS) {
  347.  
  348.         if (RegQueryValueEx(
  349.             hkey,
  350.             valuename,
  351.             NULL,
  352.             &dwType,
  353.             pResult,
  354.             &cbResult) == ERROR_SUCCESS) {
  355.  
  356.                 if (dwType == REG_BINARY) {
  357.                     // cbResult is the size
  358.                     RegCloseKey(hkey);
  359.                     return(cbResult);
  360.                 }
  361.         }
  362.  
  363.  
  364.         RegCloseKey(hkey);
  365.     }
  366.  
  367.     // if we got here, we didn't find it, or it was the wrong type - return
  368.     // the default values (use MoveMemory, since src could equal dst)
  369.     MoveMemory (pResult, pDefault, cbSize);
  370.     return cbSize;
  371.  
  372. }
  373.  
  374.  
  375. /*
  376.  * write binary data to the profile
  377.  */
  378. VOID
  379. mmWriteProfileBinary(LPSTR appname, LPSTR valuename, LPVOID pData, int cbData)
  380. {
  381.     char achName[MAX_PATH];
  382.     HKEY hkey;
  383.  
  384.     lstrcpy(achName, KEYNAME);
  385.     lstrcat(achName, appname);
  386.     if (RegCreateKey(ROOTKEY, achName, &hkey) == ERROR_SUCCESS) {
  387.         RegSetValueEx(
  388.             hkey,
  389.             valuename,
  390.             0,
  391.             REG_BINARY,
  392.             pData,
  393.             cbData
  394.         );
  395.  
  396.         RegCloseKey(hkey);
  397.     }
  398. }
  399.  
  400.