基于Visual C++之Windows核心编程代码分析(5)操作注册表与系统时间

来源:互联网 发布:淘宝网怎么装修店面 编辑:程序博客网 时间:2024/06/04 19:24

我们进行Windows编程的时候,经常需要进行注册表操作,操作注册表请见下列实现代码与说明注释。

 

 

[cpp] view plaincopyprint?
  1. /* 头文件 */  
  2. #include <windows.h>  
  3. #include <stdio.h>  
  4. #include <tchar.h>  
  5. /* 预定义 */  
  6. #define MAX_KEY_LENGTH 255  
  7. #define MAX_VALUE_NAME 16383  
  8. /* ************************************ 
  9. * void QueryKey(HKEY hKey)  
  10. * 功能    列举指定注册表项的子键 
  11. **************************************/  
  12. void QueryKey(HKEY hKey)   
  13. {   
  14.     TCHAR    achKey[MAX_KEY_LENGTH];     
  15.     DWORD    cbName;                   
  16.     TCHAR    achClass[MAX_PATH] = TEXT("");    
  17.     DWORD    cchClassName = MAX_PATH;   
  18.     DWORD    cSubKeys=0;          
  19.     DWORD    cbMaxSubKey;            
  20.     DWORD    cchMaxClass;             
  21.     DWORD    cValues;                
  22.     DWORD    cchMaxValue;          
  23.     DWORD    cbMaxValueData;        
  24.     DWORD    cbSecurityDescriptor;   
  25.     FILETIME ftLastWriteTime;      
  26.   
  27.     DWORD i, retCode;   
  28.   
  29.     TCHAR  achValue[MAX_VALUE_NAME];   
  30.     DWORD cchValue = MAX_VALUE_NAME;   
  31.   
  32.     // 获取类名和数量  
  33.     retCode = RegQueryInfoKey(  
  34.         hKey,                    // 键的句柄  
  35.         achClass,                //  类名   
  36.         &cchClassName,           // 类名长度   
  37.         NULL,                    // 保留  
  38.         &cSubKeys,               // 子键的数量  
  39.         &cbMaxSubKey,            // 子键长度  
  40.         &cchMaxClass,            // 类长度  
  41.         &cValues,                // 子键键值数量  
  42.         &cchMaxValue,            // 子键名长度  
  43.         &cbMaxValueData,         // 键值长度  
  44.         &cbSecurityDescriptor,   // 安全描述符  
  45.         &ftLastWriteTime);       // 最后写时间   
  46.   
  47.     // 列举子键      
  48.     if (cSubKeys)  
  49.     {  
  50.         printf( "\nNumber of subkeys: %d\n", cSubKeys);  
  51.   
  52.         for (i=0; i<cSubKeys; i++)   
  53.         {   
  54.             cbName = MAX_KEY_LENGTH;  
  55.             retCode = RegEnumKeyEx(hKey, i,  
  56.                 achKey,   
  57.                 &cbName,   
  58.                 NULL,   
  59.                 NULL,   
  60.                 NULL,   
  61.                 &ftLastWriteTime);   
  62.             if (retCode == ERROR_SUCCESS)   
  63.             {  
  64.                 printf(TEXT("(%d) %s\n"), i+1, achKey);  
  65.             }  
  66.         }  
  67.     }   
  68.   
  69.     // 列举键值  
  70.     if (cValues)   
  71.     {  
  72.         printf( "\nNumber of values: %d\n", cValues);  
  73.   
  74.         for (i=0, retCode=ERROR_SUCCESS; i<cValues; i++)   
  75.         {   
  76.             cchValue = MAX_VALUE_NAME;   
  77.             achValue[0] = '\0';   
  78.             retCode = RegEnumValue(hKey, i,   
  79.                 achValue,   
  80.                 &cchValue,   
  81.                 NULL,   
  82.                 NULL,  
  83.                 NULL,  
  84.                 NULL);  
  85.   
  86.             if (retCode == ERROR_SUCCESS )   
  87.             {   
  88.                 printf(TEXT("(%d) %s\n"), i+1, achValue);   
  89.             }   
  90.         }  
  91.     }  
  92. }  
  93. /* ************************************ 
  94. * void AddKey(HKEY hKey) 
  95. * 功能    增加一个子键,并设置键值 
  96. **************************************/  
  97. void AddKey(HKEY hKey)  
  98. {  
  99.     HKEY hSubKey;  
  100.     DWORD dwKeyValue = 100;  
  101.     // 创建键  
  102.     RegCreateKey(hKey,"MySoftware",&hSubKey);  
  103.     // 设置键值  
  104.     if( ERROR_SUCCESS != RegSetValueEx(  
  105.         hSubKey,  
  106.         "TEST",  
  107.         0,  
  108.         REG_DWORD,  
  109.         (LPBYTE)&dwKeyValue,  
  110.         sizeof(DWORD)))  
  111.     {  
  112.         printf("error\n");  
  113.     }  
  114. }  
  115. /* ************************************ 
  116. * void main(void) 
  117. * 功能    打开键,获得键句柄 
  118. **************************************/  
  119. void main(void)  
  120. {  
  121.     HKEY hTestKey;  
  122.   
  123.     if( RegOpenKeyEx( HKEY_CURRENT_USER,  
  124.         TEXT("SOFTWARE"),  
  125.         0,  
  126.         KEY_READ | KEY_WRITE,  
  127.         &hTestKey) == ERROR_SUCCESS  
  128.         )  
  129.     {  
  130.         // 增加键  
  131.         AddKey(hTestKey);  
  132.         // 列举子键  
  133.         QueryKey(hTestKey);  
  134.     }  
  135. }  

 

我们进行Windows编程的时候,经常需要获取时间与设置时间,操作时间请见下列实现代码与说明注释。


 

[cpp] view plaincopyprint?
  1. **************************************/  
  2. #include <Windows.h>  
  3. #include <stdio.h>  
  4. /* ************************************ 
  5. * int main() 
  6. * 功能    获取并显示系统当前时间,然后将时间提前一个小时 
  7. **************************************/  
  8. int main()  
  9. {  
  10.     SYSTEMTIME st;  
  11.     // 获取当前时间,以本时区时间格式  
  12.     GetLocalTime( &st );  
  13.     printf("Now: %d-%d-%d, %d:%d:%d",  
  14.         st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);  
  15.     // 提前一小时  
  16.     st.wHour --;  
  17.     // 设置当前系统时间  
  18.     SetLocalTime( &st );  
  19. }