创建键 RegCreateKeyEx
1 int SetRecordVideoSavedDays(int newSavedDays)
2 {
3 HKEY hSubKey = NULL;
4 LONG lRet = 0;
5 DWORD dwType = 0;
6 int iRet = 0;
7
8 do
9 {
10 if (newSavedDays < 0)
11 {
12 printf("error: input negative number\n");
13 break;
14 }
15 lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Cloudsoar 3C\\ServerInfo",
16 0, NULL, REG_OPTION_NON_VOLATILE,
17 KEY_ALL_ACCESS, NULL, &hSubKey, NULL);
18 if (ERROR_SUCCESS != lRet)
19 {
20 printf("Create Reg failed\n");
21 break;
22 }
23
24 lRet = RegSetValueEx(hSubKey, "VideoSavedDays", 0, REG_DWORD, (BYTE*)&newSavedDays, sizeof(newSavedDays));
25 if (ERROR_SUCCESS != lRet)
26 {
27 printf("Set reg value VideoSavedDays failed\n");
28 break;
29 }
30
31 iRet = (int)newSavedDays;
32 } while (0);
33
34 if(NULL != hSubKey)
35 {
36 RegCloseKey(hSubKey);
37 hSubKey = NULL;
38 }
39
40
41 return iRet;
42 }
43
44 // parameter1: out, save video saved path
45 // parameter2: in, save path length
46 BOOL GetRecordVideoSavedPath(char *pchPath,int pathBufLen)
47 {
48 HKEY hSubKey = NULL;
49 LONG lRet = 0;
50 DWORD dwType = 0;
51 DWORD dwPathLen = pathBufLen;
52 BOOL bRet = FALSE;
53
54 do
55 {
56 lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
57 "SOFTWARE\\Cloudsoar 3C\\ServerInfo",
58 0, KEY_READ, &hSubKey);
59 if (ERROR_SUCCESS != lRet)
60 {
61 printf("Open reg value VideoSavedPath failed\n");
62 break;
63 }
64
65 lRet = RegQueryValueEx(hSubKey, "VideoSavedPath",
66 0, &dwType, (LPBYTE)pchPath, &dwPathLen);
67 if (ERROR_SUCCESS != lRet)
68 {
69 printf("Query reg value VideoSavedPath failed\n");
70 break;
71 }
72
73 bRet = TRUE;
74
75 } while (0);
76
77 RegCloseKey(hSubKey);
78
79 return bRet;
80 }
81
82 BOOL SetRecordVideoSavePath(char *pchNewPath)
83 {
84 HKEY hSubKey = NULL;
85 LONG lRet = 0;
86 DWORD dwType = 0;
87 DWORD dwState = 0;
88 BOOL bRet = FALSE;
89
90 do
91 {
92 if (NULL == pchNewPath)
93 {
94 printf("error: input negative new path\n");
95 break;
96 }
97 lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Cloudsoar 3C\\ServerInfo",
98 0, NULL, REG_OPTION_NON_VOLATILE,
99 KEY_ALL_ACCESS, NULL, &hSubKey, &dwState);
100 if (ERROR_SUCCESS != lRet)
101 {
102 printf("Create Reg VideoSavedPath failed\n");
103 break;
104 }
105
106 lRet = RegSetValueEx(hSubKey, "VideoSavedPath", 0, REG_SZ, (PBYTE)pchNewPath, sizeof(pchNewPath));
107 if (ERROR_SUCCESS != lRet)
108 {
109 printf("Set reg value VideoSavedPath failed\n");
110 break;
111 }
112
113 bRet = TRUE;
114 } while (0);
115
116 RegCloseKey(hSubKey);
117
118 return bRet;
119 }
函数原型
1 LONG RegCreateKeyEx(
2 HKEY hKey, // handle to open key
3 LPCTSTR lpSubKey, // subkey name
4 DWORD Reserved, // reserved
5 LPTSTR lpClass, // class string
6 DWORD dwOptions, // special options
7 REGSAM samDesired, // desired security access
8 LPSECURITY_ATTRIBUTES lpSecurityAttributes, // inheritance
9 PHKEY phkResult, // key handle
10 LPDWORD lpdwDisposition // disposition value buffer
11 );
参数说明
1 hKey: 要打开键的句柄或以下预定义句柄
2 HKEY_CLASSES_ROOT
3 HKEY_CURRENT_USER
4 HKEY_LOCAL_MACHINE
5 HKEY_USERS
6 lpSubKey: 指向一个用于定义子键路径的字符串
7 Reserved,dwOptions,samDesired: 置0
8 lpClass,lpSecurityAttributes: 置NULL
9 phkResult: 用于接收键句柄
10 lpdwDisposition: 接收的相关信息,取值如下
11 REG_CREATED_NEW_KEY 创建成功
12 REG_OPENED_EXISTING_KEY 键已存在
打开键 RegOpenKeyEx
函数原型
1 LONG RegOpenKeyEx(
2 HKEY hKey, // handle to open key
3 LPCTSTR lpSubKey, // subkey name
4 DWORD ulOptions, // reserved
5 REGSAM samDesired, // security access mask
6 PHKEY phkResult // handle to open key
7 );
参数说明
1 hKey: 要打开键的句柄或以下预定义句柄
2 HKEY_CLASSES_ROOT
3 HKEY_CURRENT_USER
4 HKEY_LOCAL_MACHINE
5 HKEY_USERS
6 lpSubKey: 指向一个用于定义子键路径的字符串
7 ulOptions: 保留位,置0
8 samDesired: 打开键后键的操作权限
9 phResult: 接收打开的键的句柄
修改/添加键值 RegSetValueEx
函数原型
1 LONG RegSetValueEx(
2 HKEY hKey, // handle to key
3 LPCTSTR lpValueName, // value name
4 DWORD Reserved, // reserved
5 DWORD dwType, // value type
6 CONST BYTE *lpData, // value data
7 DWORD cbData // size of value data
8 );
参数说明
1 hKey: 打开键的句柄或以下预定义句柄
2 HKEY_CLASSES_ROOT
3 HKEY_CURRENT_USER
4 HKEY_LOCAL_MACHINE
5 HKEY_USERS
6 lpValueName: 键值的名称
7 Reserved: 保留位,置0
8 dwType: 键值的类型
9 lpData: 键值
10 cbData: 键值数据长度