home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sdktools / image / drwatson / registry.c < prev    next >
C/C++ Source or Header  |  1997-08-29  |  21KB  |  885 lines

  1. /*++
  2.  
  3. Copyright (c) 1993  Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     registry.c
  8.  
  9. Abstract:
  10.  
  11.     This file implements the apis for DRWTSN32 to access the registry.
  12.     All access to the registry are done in this file.  If additional
  13.     registry control is needed then a function should be added in this file
  14.     and exposed to the other files in DRWTSN32.
  15.  
  16. Author:
  17.  
  18.     Wesley Witt (wesw) 1-May-1993
  19.  
  20. Environment:
  21.  
  22.     User Mode
  23.  
  24. --*/
  25.  
  26. #include <windows.h>
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30.  
  31. #include "drwatson.h"
  32. #include "proto.h"
  33. #include "messages.h"
  34.  
  35.  
  36. //
  37. // string constants for accessing the registry
  38. // there is a string constant here for each key and each value
  39. // that is accessed in the registry.
  40. //
  41. #define DRWATSON_EXE_NAME           "drwtsn32.exe"
  42. #define REGKEY_SOFTWARE             "software\\microsoft"
  43. #define REGKEY_MESSAGEFILE          "EventMessageFile"
  44. #define REGKEY_TYPESSUPP            "TypesSupported"
  45. #define REGKEY_SYSTEMROOT           "%SystemRoot%\\System32\\"
  46. #define REGKEY_EVENTLOG             "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\"
  47. #define REGKEY_APPNAME              "ApplicationName"
  48. #define REGKEY_FUNCTION             "FunctionName"
  49. #define REGKEY_EXCEPTIONCODE        "ExceptionCode"
  50. #define REGKEY_ADDRESS              "Address"
  51. #define REGKEY_LOG_PATH             "LogFilePath"
  52. #define REGKEY_DUMPSYMBOLS          "DumpSymbols"
  53. #define REGKEY_DUMPALLTHREADS       "DumpAllThreads"
  54. #define REGKEY_APPENDTOLOGFILE      "AppendToLogFile"
  55. #define REGKEY_INSTRUCTIONS         "Instructions"
  56. #define REGKEY_VISUAL               "VisualNotification"
  57. #define REGKEY_SOUND                "SoundNotification"
  58. #define REGKEY_CRASH_DUMP           "CreateCrashDump"
  59. #define REGKEY_CRASH_FILE           "CrashDumpFile"
  60. #define REGKEY_WAVE_FILE            "WaveFile"
  61. #define REGKEY_NUM_CRASHES          "NumberOfCrashes"
  62. #define REGKEY_MAX_CRASHES          "MaximumCrashes"
  63. #define REGKEY_CURRENTVERSION       "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"
  64. #define REGKEY_CONTROLWINDOWS       "SYSTEM\\CurrentControlSet\\Control\\Windows"
  65. #define REGKEY_CSD_VERSION          "CSDVersion"
  66. #define REGKEY_CURRENT_BUILD        "CurrentBuildNumber"
  67. #define REGKEY_CURRENT_TYPE         "CurrentType"
  68. #define REGKEY_REG_ORGANIZATION     "RegisteredOrganization"
  69. #define REGKEY_REG_OWNER            "RegisteredOwner"
  70. #define REGKEY_AEDEBUG              "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug"
  71. #define REGKEY_AUTO                 "Auto"
  72. #define REGKEY_DEBUGGER             "Debugger"
  73. #define REGKEY_PROCESSOR            "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"
  74. #define REGKEY_PROCESSOR_ID         "Identifier"
  75.  
  76.  
  77. //
  78. // local prototypes
  79. //
  80. void  RegSetDWORD( HKEY hkey, char *szSubKey, DWORD dwValue );
  81. void  RegSetBOOL( HKEY hkey, char *szSubKey, BOOL dwValue );
  82. void  RegSetSZ( HKEY hkey, char *szSubKey, char *szValue );
  83. void  RegSetEXPANDSZ( HKEY hkey, char *szSubKey, char *szValue );
  84. BOOL  RegQueryBOOL( HKEY hkey, char *szSubKey );
  85. DWORD RegQueryDWORD( HKEY hkey, char *szSubKey );
  86. void  RegQuerySZ( HKEY hkey, char *szSubKey, char *szValue );
  87. BOOL  RegSaveAllValues( HKEY hKeyDrWatson, POPTIONS o );
  88. BOOL  RegGetAllValues( POPTIONS o, HKEY hKeyDrWatson );
  89. BOOL  RegInitializeDefaults( HKEY hKeyDrWatson );
  90. HKEY  RegGetAppKey( void );
  91. BOOL  RegCreateEventSource( void );
  92.  
  93.  
  94.  
  95. BOOL
  96. RegGetAllValues( POPTIONS o, HKEY hKeyDrWatson )
  97.  
  98. /*++
  99.  
  100. Routine Description:
  101.  
  102.     This functions retrieves all registry data for DRWTSN32 and puts
  103.     the data in the OPTIONS structure passed in.
  104.  
  105. Arguments:
  106.  
  107.     o              - pointer to an OPTIONS structure
  108.     hKeyDrWatson   - handle to a registry key for DRWTSN32 registry data
  109.  
  110. Return Value:
  111.  
  112.     TRUE       - retrieved all data without error
  113.     FALSE      - errors occurred and did not get all data
  114.  
  115. --*/
  116.  
  117. {
  118.     RegQuerySZ( hKeyDrWatson, REGKEY_LOG_PATH, o->szLogPath );
  119.     RegQuerySZ( hKeyDrWatson, REGKEY_WAVE_FILE, o->szWaveFile );
  120.     RegQuerySZ( hKeyDrWatson, REGKEY_CRASH_FILE, o->szCrashDump );
  121.     o->fDumpSymbols = RegQueryBOOL( hKeyDrWatson, REGKEY_DUMPSYMBOLS );
  122.     o->fDumpAllThreads = RegQueryBOOL( hKeyDrWatson, REGKEY_DUMPALLTHREADS );
  123.     o->fAppendToLogFile = RegQueryBOOL( hKeyDrWatson, REGKEY_APPENDTOLOGFILE );
  124.     o->fVisual = RegQueryBOOL( hKeyDrWatson, REGKEY_VISUAL );
  125.     o->fSound = RegQueryBOOL( hKeyDrWatson, REGKEY_SOUND );
  126.     o->fCrash = RegQueryBOOL( hKeyDrWatson, REGKEY_CRASH_DUMP );
  127.     o->dwInstructions = RegQueryDWORD( hKeyDrWatson, REGKEY_INSTRUCTIONS );
  128.     o->dwMaxCrashes = RegQueryDWORD( hKeyDrWatson, REGKEY_MAX_CRASHES );
  129.  
  130.     return TRUE;
  131. }
  132.  
  133. BOOL
  134. RegSaveAllValues( HKEY hKeyDrWatson, POPTIONS o )
  135.  
  136. /*++
  137.  
  138. Routine Description:
  139.  
  140.     This functions saves all registry data for DRWTSN32 that is passed
  141.     in via the OPTIONS structure.
  142.  
  143. Arguments:
  144.  
  145.     hKeyDrWatson   - handle to a registry key for DRWTSN32 registry data
  146.     o              - pointer to an OPTIONS structure
  147.  
  148. Return Value:
  149.  
  150.     TRUE       - saved all data without error
  151.     FALSE      - errors occurred and did not save all data
  152.  
  153. --*/
  154.  
  155. {
  156.     RegSetSZ( hKeyDrWatson, REGKEY_LOG_PATH, o->szLogPath );
  157.     RegSetSZ( hKeyDrWatson, REGKEY_WAVE_FILE, o->szWaveFile );
  158.     RegSetSZ( hKeyDrWatson, REGKEY_CRASH_FILE, o->szCrashDump );
  159.     RegSetBOOL( hKeyDrWatson, REGKEY_DUMPSYMBOLS, o->fDumpSymbols );
  160.     RegSetBOOL( hKeyDrWatson, REGKEY_DUMPALLTHREADS, o->fDumpAllThreads );
  161.     RegSetBOOL( hKeyDrWatson, REGKEY_APPENDTOLOGFILE, o->fAppendToLogFile );
  162.     RegSetBOOL( hKeyDrWatson, REGKEY_VISUAL, o->fVisual );
  163.     RegSetBOOL( hKeyDrWatson, REGKEY_SOUND, o->fSound );
  164.     RegSetBOOL( hKeyDrWatson, REGKEY_CRASH_DUMP, o->fCrash );
  165.     RegSetDWORD( hKeyDrWatson, REGKEY_INSTRUCTIONS, o->dwInstructions );
  166.     RegSetDWORD( hKeyDrWatson, REGKEY_MAX_CRASHES, o->dwMaxCrashes );
  167.  
  168.     return TRUE;
  169. }
  170.  
  171. BOOL
  172. RegInitializeDefaults( HKEY hKeyDrWatson )
  173.  
  174. /*++
  175.  
  176. Routine Description:
  177.  
  178.     This functions initializes the registry with the default values.
  179.  
  180. Arguments:
  181.  
  182.     hKeyDrWatson   - handle to a registry key for DRWTSN32 registry data
  183.  
  184. Return Value:
  185.  
  186.     TRUE       - saved all data without error
  187.     FALSE      - errors occurred and did not save all data
  188.  
  189. --*/
  190.  
  191. {
  192.     OPTIONS o;
  193.  
  194.     strcpy( o.szLogPath, "%windir%" );
  195.     strcpy( o.szCrashDump, "%windir%\\user.dmp" );
  196.     o.szWaveFile[0] = '\0';
  197.     o.fDumpSymbols = FALSE;
  198.     o.fDumpAllThreads = TRUE;
  199.     o.fAppendToLogFile = TRUE;
  200.     o.fVisual = TRUE;
  201.     o.fSound = FALSE;
  202.     o.fCrash = TRUE;
  203.     o.dwInstructions = 10;
  204.     o.dwMaxCrashes = 10;
  205.  
  206.     RegSetNumCrashes( 0 );
  207.  
  208.     RegSaveAllValues( hKeyDrWatson, &o );
  209.  
  210.     RegCreateEventSource();
  211.  
  212.     return TRUE;
  213. }
  214.  
  215. BOOL
  216. RegCreateEventSource( void )
  217.  
  218. /*++
  219.  
  220. Routine Description:
  221.  
  222.     This function creates an event source in the registry.  The event
  223.     source is used by the event viewer to display the data in a
  224.     presentable manner.
  225.  
  226. Arguments:
  227.  
  228.     None.
  229.  
  230. Return Value:
  231.  
  232.     TRUE       - saved all data without error
  233.     FALSE      - errors occurred and did not save all data
  234.  
  235. --*/
  236.  
  237. {
  238.     HKEY   hk;
  239.     char   szBuf[1024];
  240.     DWORD  dwDisp;
  241.     char   szAppName[MAX_PATH];
  242.  
  243.     GetAppName( szAppName, sizeof(szAppName) );
  244.     strcpy( szBuf, REGKEY_EVENTLOG );
  245.     strcat( szBuf, szAppName );
  246.     if (RegCreateKeyEx( HKEY_LOCAL_MACHINE,
  247.                         szBuf,
  248.                         0,
  249.                         NULL,
  250.                         REG_OPTION_NON_VOLATILE,
  251.                         KEY_QUERY_VALUE | KEY_SET_VALUE,
  252.                         NULL,
  253.                         &hk,
  254.                         &dwDisp
  255.                       )) {
  256.         return FALSE;
  257.     }
  258.  
  259.     if (dwDisp == REG_OPENED_EXISTING_KEY) {
  260.         RegCloseKey(hk);
  261.         return TRUE;
  262.     }
  263.  
  264.     strcpy( szBuf, REGKEY_SYSTEMROOT );
  265.     strcat( szBuf, DRWATSON_EXE_NAME );
  266.     RegSetEXPANDSZ( hk, REGKEY_MESSAGEFILE, szBuf );
  267.     RegSetDWORD( hk, REGKEY_TYPESSUPP, EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE );
  268.  
  269.     RegCloseKey(hk);
  270.  
  271.     return TRUE;
  272. }
  273.  
  274. HKEY
  275. RegGetAppKey( void )
  276.  
  277. /*++
  278.  
  279. Routine Description:
  280.  
  281.     This function gets a handle to the DRWTSN32 registry key.
  282.  
  283. Arguments:
  284.  
  285.     None.
  286.  
  287. Return Value:
  288.  
  289.     Valid handle   - handle opened ok
  290.     NULL           - could not open the handle
  291.  
  292. --*/
  293.  
  294. {
  295.     DWORD   rc;
  296.     DWORD   dwDisp;
  297.     HKEY    hKeyDrWatson;
  298.     HKEY    hKeyMicrosoft;
  299.     char    szAppName[MAX_PATH];
  300.  
  301.     rc = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  302.                        REGKEY_SOFTWARE,
  303.                        0,
  304.                        KEY_QUERY_VALUE | KEY_SET_VALUE |
  305.                        KEY_CREATE_SUB_KEY | KEY_ENUMERATE_SUB_KEYS,
  306.                        &hKeyMicrosoft
  307.                      );
  308.  
  309.     if (rc != ERROR_SUCCESS) {
  310.         return NULL;
  311.     }
  312.  
  313.     GetAppName( szAppName, sizeof(szAppName) );
  314.  
  315.     rc = RegCreateKeyEx( hKeyMicrosoft,
  316.                          szAppName,
  317.                          0,
  318.                          NULL,
  319.                          REG_OPTION_NON_VOLATILE,
  320.                          KEY_READ | KEY_WRITE,
  321.                          NULL,
  322.                          &hKeyDrWatson,
  323.                          &dwDisp
  324.                        );
  325.  
  326.     if (rc != ERROR_SUCCESS) {
  327.         return NULL;
  328.     }
  329.  
  330.     if (dwDisp == REG_CREATED_NEW_KEY) {
  331.         RegInitializeDefaults( hKeyDrWatson );
  332.     }
  333.  
  334.     return hKeyDrWatson;
  335. }
  336.  
  337. BOOL
  338. RegInitialize( POPTIONS o )
  339.  
  340. /*++
  341.  
  342. Routine Description:
  343.  
  344.     This function is used to initialize the OPTIONS structure passed in
  345.     with the current values in the registry.  Note that if the registry
  346.     is empty then the defaults are stored in the registry and also
  347.     returned in the OPTIONS structure.
  348.  
  349. Arguments:
  350.  
  351.     None.
  352.  
  353. Return Value:
  354.  
  355.     TRUE           - all data was retrieved ok
  356.     NULL           - could not get all data
  357.  
  358. --*/
  359.  
  360. {
  361.     HKEY    hKeyDrWatson;
  362.  
  363.     hKeyDrWatson = RegGetAppKey();
  364.     Assert( hKeyDrWatson != NULL );
  365.  
  366.     if (!RegGetAllValues( o, hKeyDrWatson )) {
  367.         return FALSE;
  368.     }
  369.  
  370.     RegCloseKey( hKeyDrWatson );
  371.  
  372.     return TRUE;
  373. }
  374.  
  375. BOOL
  376. RegSave( POPTIONS o )
  377.  
  378. /*++
  379.  
  380. Routine Description:
  381.  
  382.     This function is used to save the data in the OPTIONS structure
  383.     to the registry.
  384.  
  385. Arguments:
  386.  
  387.     o              - pointer to an OPTIONS structure
  388.  
  389. Return Value:
  390.  
  391.     TRUE           - all data was saved ok
  392.     NULL           - could not save all data
  393.  
  394. --*/
  395.  
  396. {
  397.     HKEY    hKeyDrWatson;
  398.  
  399.     hKeyDrWatson = RegGetAppKey();
  400.     Assert( hKeyDrWatson != NULL );
  401.     RegSaveAllValues( hKeyDrWatson, o );
  402.     RegCloseKey( hKeyDrWatson );
  403.  
  404.     return TRUE;
  405. }
  406.  
  407. void
  408. RegSetNumCrashes( DWORD dwNumCrashes )
  409.  
  410. /*++
  411.  
  412. Routine Description:
  413.  
  414.     This function changes the value in the registry that contains the
  415.     number of crashes that have occurred.
  416.  
  417. Arguments:
  418.  
  419.     dwNumCrashes   - the number of craches to save
  420.  
  421. Return Value:
  422.  
  423.     None.
  424.  
  425. --*/
  426.  
  427. {
  428.     HKEY    hKeyDrWatson;
  429.  
  430.     hKeyDrWatson = RegGetAppKey();
  431.     Assert( hKeyDrWatson != NULL );
  432.     RegSetDWORD( hKeyDrWatson, REGKEY_NUM_CRASHES, dwNumCrashes );
  433.     RegCloseKey( hKeyDrWatson );
  434.  
  435.     return;
  436. }
  437.  
  438. DWORD
  439. RegGetNumCrashes( void )
  440.  
  441. /*++
  442.  
  443. Routine Description:
  444.  
  445.     This function get the value in the registry that contains the
  446.     number of crashes that have occurred.
  447.  
  448. Arguments:
  449.  
  450.     None.
  451.  
  452. Return Value:
  453.  
  454.     the number of craches that have occurred
  455.  
  456. --*/
  457.  
  458. {
  459.     HKEY    hKeyDrWatson;
  460.     DWORD   dwNumCrashes;
  461.  
  462.     hKeyDrWatson = RegGetAppKey();
  463.     Assert( hKeyDrWatson != NULL );
  464.     dwNumCrashes = RegQueryDWORD( hKeyDrWatson, REGKEY_NUM_CRASHES );
  465.     RegCloseKey( hKeyDrWatson );
  466.  
  467.     return dwNumCrashes;
  468. }
  469.  
  470. BOOLEAN
  471. RegInstallDrWatson( BOOL fQuiet )
  472.  
  473. /*++
  474.  
  475. Routine Description:
  476.  
  477.     This function gets a handle to the DRWTSN32 registry key.
  478.  
  479. Arguments:
  480.  
  481.     None.
  482.  
  483. Return Value:
  484.  
  485.     Valid handle   - handle opened ok
  486.     NULL           - could not open the handle
  487.  
  488. --*/
  489.  
  490. {
  491.     DWORD     rc;
  492.     HKEY      hKeyMicrosoft;
  493.     OPTIONS   o;
  494.  
  495.     rc = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  496.                        REGKEY_AEDEBUG,
  497.                        0,
  498.                        KEY_QUERY_VALUE | KEY_SET_VALUE,
  499.                        &hKeyMicrosoft
  500.                      );
  501.  
  502.     if (rc != ERROR_SUCCESS) {
  503.         return FALSE;
  504.     }
  505.  
  506.     RegSetSZ( hKeyMicrosoft, REGKEY_AUTO, "1" );
  507.     RegSetSZ( hKeyMicrosoft, REGKEY_DEBUGGER, "drwtsn32 -p %ld -e %ld -g" );
  508.  
  509.     RegCloseKey( hKeyMicrosoft );
  510.  
  511.     RegInitialize( &o );
  512.     if (fQuiet) {
  513.         o.fVisual = FALSE;
  514.         o.fSound = FALSE;
  515.         RegSave( &o );
  516.     }
  517.  
  518.     return TRUE;
  519. }
  520.  
  521. void
  522. RegSetDWORD( HKEY hkey, char *szSubKey, DWORD dwValue )
  523.  
  524. /*++
  525.  
  526. Routine Description:
  527.  
  528.     This function changes a DWORD value in the registry using the
  529.     hkey and szSubKey as the registry key info.
  530.  
  531. Arguments:
  532.  
  533.     hkey          - handle to a registry key
  534.     szSubKey      - pointer to a subkey string
  535.     dwValue       - new registry value
  536.  
  537. Return Value:
  538.  
  539.     None.
  540.  
  541. --*/
  542.  
  543. {
  544.     DWORD rc;
  545.  
  546.     rc = RegSetValueEx( hkey, szSubKey, 0, REG_DWORD, (LPBYTE)&dwValue, 4 );
  547.     Assert( rc == ERROR_SUCCESS );
  548. }
  549.  
  550. void
  551. RegSetBOOL( HKEY hkey, char *szSubKey, BOOL dwValue )
  552.  
  553. /*++
  554.  
  555. Routine Description:
  556.  
  557.     This function changes a BOOL value in the registry using the
  558.     hkey and szSubKey as the registry key info.
  559.  
  560. Arguments:
  561.  
  562.     hkey          - handle to a registry key
  563.     szSubKey      - pointer to a subkey string
  564.     dwValue       - new registry value
  565.  
  566. Return Value:
  567.  
  568.     None.
  569.  
  570. --*/
  571.  
  572. {
  573.     DWORD rc;
  574.  
  575.     rc = RegSetValueEx( hkey, szSubKey, 0, REG_DWORD, (LPBYTE)&dwValue, 4 );
  576.     Assert( rc == ERROR_SUCCESS );
  577. }
  578.  
  579. void
  580. RegSetSZ( HKEY hkey, char *szSubKey, char *szValue )
  581.  
  582. /*++
  583.  
  584. Routine Description:
  585.  
  586.     This function changes a SZ value in the registry using the
  587.     hkey and szSubKey as the registry key info.
  588.  
  589. Arguments:
  590.  
  591.     hkey          - handle to a registry key
  592.     szSubKey      - pointer to a subkey string
  593.     szValue       - new registry value
  594.  
  595. Return Value:
  596.  
  597.     None.
  598.  
  599. --*/
  600.  
  601. {
  602.     DWORD rc;
  603.  
  604.     rc = RegSetValueEx( hkey, szSubKey, 0, REG_SZ, szValue, strlen(szValue)+1 );
  605.     Assert( rc == ERROR_SUCCESS );
  606. }
  607.  
  608. void
  609. RegSetEXPANDSZ( HKEY hkey, char *szSubKey, char *szValue )
  610.  
  611. /*++
  612.  
  613. Routine Description:
  614.  
  615.     This function changes a SZ value in the registry using the
  616.     hkey and szSubKey as the registry key info.
  617.  
  618. Arguments:
  619.  
  620.     hkey          - handle to a registry key
  621.     szSubKey      - pointer to a subkey string
  622.     szValue       - new registry value
  623.  
  624. Return Value:
  625.  
  626.     None.
  627.  
  628. --*/
  629.  
  630. {
  631.     DWORD rc;
  632.  
  633.     rc = RegSetValueEx( hkey, szSubKey, 0, REG_EXPAND_SZ, szValue, strlen(szValue)+1 );
  634.     Assert( rc == ERROR_SUCCESS );
  635. }
  636.  
  637. BOOL
  638. RegQueryBOOL( HKEY hkey, char *szSubKey )
  639.  
  640. /*++
  641.  
  642. Routine Description:
  643.  
  644.     This function queries BOOL value in the registry using the
  645.     hkey and szSubKey as the registry key info.  If the value is not
  646.     found in the registry, it is added with a FALSE value.
  647.  
  648. Arguments:
  649.  
  650.     hkey          - handle to a registry key
  651.     szSubKey      - pointer to a subkey string
  652.  
  653. Return Value:
  654.  
  655.     TRUE or FALSE.
  656.  
  657. --*/
  658.  
  659. {
  660.     DWORD   rc;
  661.     DWORD   len;
  662.     DWORD   dwType;
  663.     BOOL    fValue;
  664.  
  665.     len = 4;
  666.     rc = RegQueryValueEx( hkey, szSubKey, 0, &dwType, (LPBYTE)&fValue, &len );
  667.     if (rc != ERROR_SUCCESS) {
  668.         if (rc == ERROR_FILE_NOT_FOUND) {
  669.             fValue = FALSE;
  670.             RegSetBOOL( hkey, szSubKey, fValue );
  671.         }
  672.         else {
  673.             Assert( rc == ERROR_SUCCESS );
  674.         }
  675.     }
  676.     else {
  677.         Assert( dwType == REG_DWORD );
  678.     }
  679.  
  680.     return fValue;
  681. }
  682.  
  683. DWORD
  684. RegQueryDWORD( HKEY hkey, char *szSubKey )
  685.  
  686. /*++
  687.  
  688. Routine Description:
  689.  
  690.     This function queries BOOL value in the registry using the
  691.     hkey and szSubKey as the registry key info.  If the value is not
  692.     found in the registry, it is added with a zero value.
  693.  
  694. Arguments:
  695.  
  696.     hkey          - handle to a registry key
  697.     szSubKey      - pointer to a subkey string
  698.  
  699. Return Value:
  700.  
  701.     registry value
  702.  
  703. --*/
  704.  
  705. {
  706.     DWORD   rc;
  707.     DWORD   len;
  708.     DWORD   dwType;
  709.     DWORD   fValue;
  710.  
  711.     len = 4;
  712.     rc = RegQueryValueEx( hkey, szSubKey, 0, &dwType, (LPBYTE)&fValue, &len );
  713.     if (rc != ERROR_SUCCESS) {
  714.         if (rc == ERROR_FILE_NOT_FOUND) {
  715.             fValue = 0;
  716.             RegSetDWORD( hkey, szSubKey, fValue );
  717.         }
  718.         else {
  719.             Assert( rc == ERROR_SUCCESS );
  720.         }
  721.     }
  722.     else {
  723.         Assert( dwType == REG_DWORD );
  724.     }
  725.  
  726.     return fValue;
  727. }
  728.  
  729. void
  730. RegQuerySZ( HKEY hkey, char *szSubKey, char *szValue )
  731.  
  732. /*++
  733.  
  734. Routine Description:
  735.  
  736.     This function queries BOOL value in the registry using the
  737.     hkey and szSubKey as the registry key info.  If the value is not
  738.     found in the registry, it is added with a zero value.
  739.  
  740. Arguments:
  741.  
  742.     hkey          - handle to a registry key
  743.     szSubKey      - pointer to a subkey string
  744.  
  745. Return Value:
  746.  
  747.     registry value
  748.  
  749. --*/
  750.  
  751. {
  752.     DWORD   rc;
  753.     DWORD   len;
  754.     DWORD   dwType;
  755.     char    buf[1024];
  756.  
  757.     len = sizeof(buf);
  758.     rc = RegQueryValueEx( hkey, szSubKey, 0, &dwType, (LPBYTE)buf, &len );
  759.     if (rc != ERROR_SUCCESS) {
  760.         if (rc == ERROR_FILE_NOT_FOUND) {
  761.             buf[0] = 0;
  762.             RegSetSZ( hkey, szSubKey, buf );
  763.         }
  764.         else {
  765.             Assert( rc == ERROR_SUCCESS );
  766.         }
  767.     }
  768.     else {
  769.         Assert( dwType == REG_SZ );
  770.     }
  771.  
  772.     strcpy( szValue, buf );
  773. }
  774.  
  775. /*++
  776.  
  777. Routine Description:
  778.  
  779.     This function writes system and user info. to the log file
  780.  
  781. Arguments:
  782.  
  783.     None
  784.  
  785. Return Value:
  786.  
  787.     registry value
  788.  
  789. History:
  790.  
  791.     8/21/97 a-paulbr fixed bug 658
  792. --*/
  793.  
  794. void
  795. RegLogCurrentVersion( void )
  796. {
  797.     char    buf[1024];
  798.     DWORD   rc;
  799.     HKEY    hKeyCurrentVersion = NULL;
  800.     HKEY    hKeyControlWindows = NULL;
  801.     DWORD   dwSPNum = 0;
  802.     DWORD   dwType = REG_DWORD;
  803.     DWORD   dwSize = sizeof(DWORD);
  804.  
  805.     rc = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  806.                        REGKEY_CURRENTVERSION,
  807.                        0,
  808.                        KEY_QUERY_VALUE,
  809.                        &hKeyCurrentVersion
  810.                      );
  811.  
  812.     if (rc != ERROR_SUCCESS) {
  813.         return;
  814.     }
  815.     rc = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  816.                        REGKEY_CONTROLWINDOWS,
  817.                        0,
  818.                        KEY_QUERY_VALUE,
  819.                        &hKeyControlWindows);
  820.     if (hKeyControlWindows) {
  821.         //
  822.         // I'm using RegQueryValueEx() because there is an assertion in
  823.         // RegQueryDWORD() if the key does not exist.
  824.         //
  825.         RegQueryValueEx(hKeyControlWindows,
  826.                         REGKEY_CSD_VERSION,
  827.                         NULL,
  828.                         &dwType,
  829.                         (BYTE*)&dwSPNum,
  830.                         &dwSize);
  831.     }
  832.  
  833.     RegQuerySZ( hKeyCurrentVersion,REGKEY_CURRENT_BUILD,     buf );
  834.     lprintf( MSG_CURRENT_BUILD, buf );
  835.  
  836.     if ((hKeyControlWindows) &&
  837.         (dwType == REG_DWORD) &&
  838.         (HIBYTE(LOWORD(dwSPNum)) != 0)) {
  839.         sprintf(buf, "%hu", HIBYTE(LOWORD(dwSPNum)));
  840.         lprintf( MSG_CSD_VERSION, buf );
  841.     } else {
  842.         sprintf(buf, "None");
  843.         lprintf( MSG_CSD_VERSION, buf );
  844.     }
  845.  
  846.     RegQuerySZ( hKeyCurrentVersion,REGKEY_CURRENT_TYPE,      buf );
  847.     lprintf( MSG_CURRENT_TYPE, buf );
  848.     RegQuerySZ( hKeyCurrentVersion,REGKEY_REG_ORGANIZATION,  buf );
  849.     lprintf( MSG_REG_ORGANIZATION, buf );
  850.     RegQuerySZ( hKeyCurrentVersion,REGKEY_REG_OWNER,         buf );
  851.     lprintf( MSG_REG_OWNER, buf );
  852.  
  853.     //
  854.     // Close the keys that we opened
  855.     //
  856.     RegCloseKey(hKeyCurrentVersion);
  857.     RegCloseKey(hKeyControlWindows);
  858.  
  859.     return;
  860. }
  861.  
  862. void
  863. RegLogProcessorType( void )
  864. {
  865.     char    buf[1024];
  866.     DWORD   rc;
  867.     HKEY    hKey;
  868.  
  869.     rc = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  870.                        REGKEY_PROCESSOR,
  871.                        0,
  872.                        KEY_QUERY_VALUE,
  873.                        &hKey
  874.                      );
  875.  
  876.     if (rc != ERROR_SUCCESS) {
  877.         return;
  878.     }
  879.  
  880.     RegQuerySZ( hKey, REGKEY_PROCESSOR_ID, buf );
  881.     lprintf( MSG_SYSINFO_PROC_TYPE, buf );
  882.  
  883.     return;
  884. }
  885.