Windowsレジストリの基本的な使用例
18973 ワード
インターネットを利用して多くの資料を探して、1つの問題が年を取っていることを発見しました.例えばRegCreateKeysだけでRegCreateKeyExの使い方が詳しくなく、自分で何時間も模索してやっと基本的に使ったいくつかの関数を試してみました.
次のコードはコンパイルテストに合格しました
時間の関係でまだ多くの関数が使われていないので、後で機会に置いておきましょう(例えば:RegQueryInfoKeyなど)
次のコードはコンパイルテストに合格しました
1 // test__regedit.cpp : 。
2 //
3
4 #include "stdafx.h"
5
6 #include <windows.h>
7 #include <iostream>
8
9 #include <vld.h>
10
11 void test__create_regedit()
12 {
13 LPCTSTR lpSubKeyGOCOM = TEXT("SOFTWARE\\GoCom5.5");
14 HKEY hKeyGOCOM = NULL;
15 DWORD dwDisposition;//
16 if ( ERROR_SUCCESS==::RegCreateKeyEx(HKEY_LOCAL_MACHINE, lpSubKeyGOCOM, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKeyGOCOM, &dwDisposition) )
17 {
18 std::cout << "RegCreateKeyEx successful" << std::endl;
19 if (REG_CREATED_NEW_KEY==dwDisposition)
20 {
21 std::cout << "key create" << std::endl;
22 }
23 else
24 {
25 std::cout << "key open" << std::endl;
26 }
27
28 LPCTSTR lpAutoRun = TEXT("auto_run");
29 LPCTSTR lpAutoRunVal = TEXT("TRUE");
30 if ( ERROR_SUCCESS==::RegSetValueEx(hKeyGOCOM, lpAutoRun, 0, REG_SZ, (BYTE*)lpAutoRunVal, (lstrlen(lpAutoRunVal)+1)*sizeof(TCHAR)) )
31 {
32 std::cout << "value successful" << std::endl;
33 }
34 else
35 {
36 std::cout << "value fail" << std::endl;
37 }
38
39 LPCTSTR lpAutoRun2 = TEXT("auto_run2");
40 DWORD dw32AutoRunVal2 = 12;
41 if ( ERROR_SUCCESS==::RegSetValueEx(hKeyGOCOM, lpAutoRun2, 0, REG_DWORD, (BYTE*)&dw32AutoRunVal2, sizeof(DWORD)) )
42 {
43 std::cout << "value2 successful" << std::endl;
44 }
45 else
46 {
47 std::cout << "value2 fail" << std::endl;
48 }
49
50
51 LPTSTR lpAutoRunValret = (LPTSTR)malloc(20*sizeof(TCHAR));
52 DWORD cbAutoRunValret = 20*sizeof(TCHAR);
53 ZeroMemory(lpAutoRunValret, 20);
54 if ( ERROR_SUCCESS==::RegGetValue(HKEY_LOCAL_MACHINE, lpSubKeyGOCOM, lpAutoRun, RRF_RT_REG_SZ, NULL, lpAutoRunValret, &cbAutoRunValret) )
55 {
56 std::cout << "value get successful" << std::endl;
57 }
58 else
59 {
60 std::cout << "value get fail" << std::endl;
61 }
62 free(lpAutoRunValret);
63
64
65 DWORD dwAutoRunValret2;
66 DWORD cbAutoRunValret2 = sizeof(DWORD);
67 if ( ERROR_SUCCESS==::RegGetValue(HKEY_LOCAL_MACHINE, lpSubKeyGOCOM, lpAutoRun2, RRF_RT_DWORD, NULL, (PVOID)&dwAutoRunValret2, &cbAutoRunValret2) )
68 {
69 std::cout << "value2 get successful" << dwAutoRunValret2 << std::endl;
70 }
71 else
72 {
73 std::cout << "value2 get fail" << std::endl;
74 }
75
76
77 LPTSTR lpAutoRunValret3 = (LPTSTR)malloc(20*sizeof(TCHAR));
78 DWORD cbAutoRunValret3 = 20*sizeof(TCHAR);
79 ZeroMemory(lpAutoRunValret, 20);
80 if ( ERROR_SUCCESS==::RegSetValueEx(hKeyGOCOM, lpAutoRun, 0, RRF_RT_REG_SZ, (BYTE*)lpAutoRunValret3, cbAutoRunValret3) )
81 {
82 std::cout << "value get successful" << std::endl;
83 }
84 else
85 {
86 std::cout << "value get successful" << std::endl;
87 }
88 free(lpAutoRunValret);
89
90
91 DWORD dwAutoRunValret4 = -1;
92 DWORD cbAutoRunValret4 = sizeof(DWORD);
93 if ( ERROR_SUCCESS==::RegSetValueEx(hKeyGOCOM, lpAutoRun, 0, RRF_RT_DWORD, (BYTE*)dwAutoRunValret4, cbAutoRunValret4) )
94 {
95 std::cout << "value2 get successful" << std::endl;
96 }
97 else
98 {
99 std::cout << "value2 get successful" << std::endl;
100 }
101
102 }
103 else
104 {
105 std::cout << "RegCreateKeyEx fail" << std::endl;
106 }
107 if (hKeyGOCOM!=NULL)
108 {
109 ::RegCloseKey(hKeyGOCOM);
110 }
111 }
112 void test__open_regedit()
113 {
114 LPCTSTR lpSubKeyGOCOM = TEXT("SOFTWARE\\GoCom5.5");
115 HKEY hKeyGOCOM = NULL;
116 if ( ERROR_SUCCESS==::RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpSubKeyGOCOM, 0, KEY_ALL_ACCESS|KEY_READ|KEY_WRITE, &hKeyGOCOM) )
117 {
118 std::cout << "RegOpenKeyEx successful" << std::endl;
119 }
120 else
121 {
122 std::cout << "RegOpenKeyEx fail" << std::endl;
123 }
124 if (hKeyGOCOM!=NULL)
125 {
126 ::RegCloseKey(hKeyGOCOM);
127 }
128 }
129
130 int _tmain(int argc, _TCHAR* argv[])
131 {
132 test__open_regedit();
133 test__create_regedit();
134 return 0;
135 }
時間の関係でまだ多くの関数が使われていないので、後で機会に置いておきましょう(例えば:RegQueryInfoKeyなど)