Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/ttpmenu/registry.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9048 - (show annotations) (download) (as text)
Wed Dec 16 12:24:13 2020 UTC (3 years, 5 months ago) by nmaya
File MIME type: text/x-c++src
File size: 13474 byte(s)
ソースファイルの著作権表記の "最後の発行の年" を削除

ticket #40996
1 /*
2 * Copyright (C) S.Hayakawa NTT-IT 1998-2002
3 * (C) 2002- TeraTerm Project
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 //��������ini�t�@�C�����g�p�������������A0�o�C�g���t�@�C������������ttpmenu.exe�������t�H���_��ttpmenu.ini���p������
31 #define STRICT
32 #include "registry.h"
33 #include "tchar.h"
34 #include "stdio.h"
35
36 BOOL bUseINI = FALSE; // ������(TRUE=INI, FALSE=���W�X�g��)
37 TCHAR szSectionName[MAX_PATH]; // INI���Z�N�V������
38 TCHAR szSectionNames[1024*10]={0}; // INI���Z�N�V����������
39 TCHAR szApplicationName[MAX_PATH]={0}; // INI�t�@�C�����t���p�X
40
41 BOOL getSection(LPCTSTR str)
42 {
43 szSectionNames[0] = 0;
44 LPCTSTR t = _tcsrchr(str, _T('\\'));
45 if(t){
46 t++;
47 }else{
48 t = str;
49 }
50 _tcscpy(szSectionName, t);
51 return TRUE;
52 }
53
54 LPCTSTR getModuleName()
55 {
56 if(*szApplicationName == 0){
57 GetModuleFileName(NULL, szApplicationName, sizeof(TCHAR)*sizeof(szApplicationName));
58 LPTSTR t = szApplicationName + _tcslen(szApplicationName) - 3;
59 _tcscpy(t, _T("ini"));
60 }
61 return szApplicationName;
62 }
63
64 //exe�������t�H���_��ini�t�@�C��������������ini���g�p�A�����������������W�X�g�����g�p
65 void checkIniFile()
66 {
67 DWORD dwAttr = ::GetFileAttributes(getModuleName());
68 bUseINI = dwAttr != INVALID_FILE_ATTRIBUTES;
69 }
70
71 /* ==========================================================================
72 Function Name : (HKEY) RegCreate()
73 Outline : �w���������W�X�g���L�[�������i�������I�[�v���j����
74 Arguments : HKEY hCurrentKey (in) �������I�[�v���L�[
75 : LPCTSTR lpszKeyName (in) �I�[�v�������T�u�L�[��
76 : ���O
77 Return Value : ���� �I�[�v�������������������L�[���n���h��
78 : ���s NULL
79 Reference :
80 Renewal :
81 Notes :
82 Attention :
83 Up Date :
84 ======1=========2=========3=========4=========5=========6=========7======= */
85 HKEY RegCreate(HKEY hCurrentKey, LPCTSTR lpszKeyName)
86 {
87 if(bUseINI){
88 getSection(lpszKeyName);
89 return ERROR_SUCCESS;
90 }else{
91 long lError;
92 HKEY hkResult;
93 DWORD dwDisposition;
94
95 lError = ::RegCreateKeyEx(hCurrentKey,
96 lpszKeyName,
97 0,
98 NULL,
99 REG_OPTION_NON_VOLATILE,
100 KEY_ALL_ACCESS,
101 NULL,
102 &hkResult,
103 &dwDisposition);
104 if (lError != ERROR_SUCCESS) {
105 ::SetLastError(lError);
106 return (HKEY) INVALID_HANDLE_VALUE;
107 }
108
109 return hkResult;
110 }
111 }
112
113 /* ==========================================================================
114 Function Name : (HKEY) RegOpen()
115 Outline : �w���������W�X�g���L�[���I�[�v������
116 Arguments : HKEY hCurrentKey (in) �������I�[�v���L�[
117 : LPCTSTR lpszKeyName (in) �I�[�v�������T�u�L�[��
118 : ���O
119 Return Value : ���� �I�[�v�������������������L�[���n���h��
120 : ���s NULL
121 Reference :
122 Renewal :
123 Notes :
124 Attention :
125 Up Date :
126 ======1=========2=========3=========4=========5=========6=========7======= */
127 HKEY RegOpen(HKEY hCurrentKey, LPCTSTR lpszKeyName)
128 {
129 if(bUseINI){
130 getSection(lpszKeyName);
131 return ERROR_SUCCESS;
132 }else{
133 long lError;
134 HKEY hkResult;
135
136 lError = ::RegOpenKeyEx(hCurrentKey,
137 lpszKeyName,
138 0,
139 KEY_ALL_ACCESS,
140 &hkResult);
141 if (lError != ERROR_SUCCESS) {
142 ::SetLastError(lError);
143 return (HKEY) INVALID_HANDLE_VALUE;
144 }
145
146 return hkResult;
147 }
148 }
149
150 /* ==========================================================================
151 Function Name : (BOOL) RegClose()
152 Outline : �w���������W�X�g���L�[���N���[�Y����
153 Arguments : HKEY hKey (in) �N���[�Y�����L�[���n���h��
154 Return Value : ���� TRUE
155 : ���s FALSE
156 Reference :
157 Renewal :
158 Notes :
159 Attention :
160 Up Date :
161 ======1=========2=========3=========4=========5=========6=========7======= */
162 BOOL RegClose(HKEY hKey)
163 {
164 if(bUseINI){
165
166 }else{
167 long lError;
168
169 lError = ::RegCloseKey(hKey);
170 if (lError != ERROR_SUCCESS) {
171 ::SetLastError(lError);
172 return FALSE;
173 }
174 }
175
176 return TRUE;
177 }
178
179 /* ==========================================================================
180 Function Name : (BOOL) RegSetStr()
181 Outline : ���W�X�g���L�[���l������������������
182 Arguments : HKEY hKey (in) �l�����������L�[���n���h��
183 : LPCTSTR lpszValueName (in) ���������l
184 : char *buf (in) �l�f�[�^
185 Return Value : ���� TRUE
186 : ���s FALSE
187 Reference :
188 Renewal :
189 Notes :
190 Attention :
191 Up Date :
192 ======1=========2=========3=========4=========5=========6=========7======= */
193 BOOL RegSetStr(HKEY hKey, LPCTSTR lpszValueName, TCHAR *buf)
194 {
195 if(bUseINI){
196 return WritePrivateProfileString(szSectionName, lpszValueName, buf, getModuleName());
197 }else{
198 long lError;
199
200 lError = ::RegSetValueEx(hKey,
201 lpszValueName,
202 0,
203 REG_SZ,
204 (CONST BYTE *) buf,
205 (::lstrlen(buf) + 1) * sizeof(TCHAR));
206 if (lError != ERROR_SUCCESS) {
207 ::SetLastError(lError);
208 return FALSE;
209 }
210 }
211
212 return TRUE;
213 }
214
215 /* ==========================================================================
216 Function Name : (BOOL) RegGetStr()
217 Outline : ���W�X�g���L�[���l��������������������
218 Arguments : HKEY hKey (in) �l�����������L�[��
219 : �n���h��
220 : LPCTSTR lpszValueName (in) ���������l
221 : char *buf (out) �l�f�[�^���i�[����
222 : �o�b�t�@
223 : DWORD dwSize (in/out) ������
224 Return Value : ���� TRUE
225 : ���s FALSE
226 Reference :
227 Renewal :
228 Notes :
229 Attention :
230 Up Date :
231 ======1=========2=========3=========4=========5=========6=========7======= */
232 BOOL RegGetStr(HKEY hKey, LPCTSTR lpszValueName, TCHAR *buf, DWORD dwSize)
233 {
234 if(bUseINI){
235 return GetPrivateProfileString(szSectionName, lpszValueName, _T(""), buf, dwSize, getModuleName());
236 }else{
237 LONG lError;
238 DWORD dwWriteSize;
239 DWORD dwType = REG_SZ;
240
241 dwWriteSize = dwSize * sizeof(TCHAR);
242
243 lError = ::RegQueryValueEx(hKey, lpszValueName, 0, &dwType, (LPBYTE) buf, &dwWriteSize);
244 if (lError != ERROR_SUCCESS) {
245 ::SetLastError(lError);
246 return FALSE;
247 }
248
249 buf[dwSize - 1] = '\0';
250 }
251
252 return TRUE;
253 }
254
255 /* ==========================================================================
256 Function Name : (BOOL) RegSetDword()
257 Outline : ���W�X�g���L�[���l�� DWORD����������
258 Arguments : HKEY hKey (in) �l�����������L�[���n���h��
259 : LPCTSTR lpszValueName (in) ���������l
260 : DWORD dwValue (in) �l�f�[�^
261 Return Value : ���� TRUE
262 : ���s FALSE
263 Reference :
264 Renewal :
265 Notes :
266 Attention :
267 Up Date :
268 ======1=========2=========3=========4=========5=========6=========7======= */
269 BOOL RegSetDword(HKEY hKey, LPCTSTR lpszValueName, DWORD dwValue)
270 {
271 if(bUseINI){
272 TCHAR t[64];
273 _stprintf(t, _T("%d"), dwValue);
274 return WritePrivateProfileString(szSectionName, lpszValueName, t, getModuleName());
275 }else{
276 long lError;
277
278 lError = ::RegSetValueEx(hKey,
279 lpszValueName,
280 0,
281 REG_DWORD,
282 (CONST BYTE *) &dwValue,
283 sizeof(DWORD));
284 if (lError != ERROR_SUCCESS) {
285 ::SetLastError(lError);
286 return FALSE;
287 }
288 }
289
290 return TRUE;
291 }
292
293 /* ==========================================================================
294 Function Name : (BOOL) RegGetDword()
295 Outline : ���W�X�g���L�[���l���� DWORD����������
296 Arguments : HKEY hKey (in) �l�����������L�[���n���h��
297 : LPCTSTR lpszValueName (in) ���������l
298 : DWORD *dwValue (out) �l�f�[�^
299 Return Value : ���� TRUE
300 : ���s FALSE
301 Reference :
302 Renewal :
303 Notes :
304 Attention :
305 Up Date :
306 ======1=========2=========3=========4=========5=========6=========7======= */
307 BOOL RegGetDword(HKEY hKey, LPCTSTR lpszValueName, DWORD *dwValue)
308 {
309 int defmark = 0xdeadbeef;
310
311 if(bUseINI){
312 // �������������s���������� false ������ (2007.11.14 yutaka)
313 *dwValue = GetPrivateProfileInt(szSectionName, lpszValueName, defmark, getModuleName());
314 if (*dwValue == defmark) {
315 *dwValue = 0;
316 return FALSE;
317 } else {
318 return TRUE;
319 }
320 }else{
321 long lError;
322 DWORD dwType = REG_DWORD;
323 DWORD dwSize = sizeof(DWORD);
324
325 lError = ::RegQueryValueEx(hKey,
326 lpszValueName,
327 0,
328 &dwType,
329 (LPBYTE) dwValue,
330 &dwSize);
331 if (lError != ERROR_SUCCESS) {
332 ::SetLastError(lError);
333 return FALSE;
334 }
335 }
336
337 return TRUE;
338 }
339
340 /* ==========================================================================
341 Function Name : (BOOL) RegSetBinary()
342 Outline : ���W�X�g���L�[���l���� BINARY����������
343 Arguments : HKEY hKey (in) �l�����������L�[���n���h��
344 : LPCTSTR lpszValueName (in) ���������l
345 : void *buf (out) �l�f�[�^
346 Return Value : ���� TRUE
347 : ���s FALSE
348 Reference :
349 Renewal :
350 Notes :
351 Attention :
352 Up Date :
353 ======1=========2=========3=========4=========5=========6=========7======= */
354 BOOL RegSetBinary(HKEY hKey, LPCTSTR lpszValueName, void *buf, DWORD dwSize)
355 {
356 if(bUseINI){
357 TCHAR t[1024] = {0};
358 LPBYTE s = (LPBYTE)buf;
359 for(DWORD i=0; i<dwSize; i++){
360 TCHAR c[4];
361 _stprintf(c, _T("%02X "), s[i]);
362 _tcscat(t, c);
363 }
364 BOOL ret = WritePrivateProfileString(szSectionName, lpszValueName, t, getModuleName());
365 return ret;
366 }else{
367 long lError;
368 DWORD dwWriteSize;
369
370 dwWriteSize = dwSize * sizeof(TCHAR);
371
372 if ((lError = ::RegSetValueEx(hKey,
373 lpszValueName,
374 0,
375 REG_BINARY,
376 (CONST BYTE *) buf,
377 dwWriteSize)) != ERROR_SUCCESS) {
378 ::SetLastError(lError);
379 return FALSE;
380 }
381 }
382
383 return TRUE;
384 }
385
386 /* ==========================================================================
387 Function Name : (BOOL) RegGetBinary()
388 Outline : ���W�X�g���L�[���l���� BINARY����������
389 Arguments : HKEY hKey (in) �l�����������L�[���n���h��
390 : LPCTSTR lpszValueName (in) ���������l
391 : int *buf (out) �l�f�[�^
392 Return Value : ���� TRUE
393 : ���s FALSE
394 Reference :
395 Renewal :
396 Notes :
397 Attention :
398 Up Date :
399 ======1=========2=========3=========4=========5=========6=========7======= */
400 // ���������l���^������ (2006.2.18 yutaka)
401 int RegGetBinary(HKEY hKey, LPCTSTR lpszValueName, void *buf, LPDWORD lpdwSize)
402 {
403 if(bUseINI){
404 TCHAR t[1024] = {0};
405 BOOL ret = GetPrivateProfileString(szSectionName, lpszValueName, _T(""), t, sizeof(t), getModuleName());
406 if(ret){
407 int size = _tcslen(t);
408 while(t[size-1] == ' '){
409 size--;
410 t[size] = 0;
411 }
412 LPCTSTR s = t;
413 LPBYTE p = (LPBYTE)buf;
414 DWORD cnt = 0;
415 *p = 0;
416 for(int i=0; i<(size+1)/3; i++){
417 *p++ = (BYTE)_tcstol(s, NULL, 16);
418 s += 3;
419 cnt ++;
420 }
421 *lpdwSize = cnt;
422 }
423 return ret;
424 }else{
425 long lError;
426 DWORD dwType = REG_BINARY;
427 DWORD dwWriteSize;
428
429 dwWriteSize = *lpdwSize * sizeof(TCHAR);
430
431 if ((lError = ::RegQueryValueEx(hKey,
432 lpszValueName,
433 NULL,
434 &dwType,
435 (LPBYTE) buf,
436 &dwWriteSize)) != ERROR_SUCCESS) {
437 ::SetLastError(lError);
438 return FALSE;
439 }
440 }
441
442 return TRUE;
443 }
444
445
446 LONG RegEnumEx(HKEY hKey, DWORD dwIndex, LPTSTR lpName, LPDWORD lpcName, LPDWORD lpReserved, LPTSTR lpClass, LPDWORD lpcClass, PFILETIME lpftLastWriteTime)
447 {
448 static LPCTSTR ptr = szSectionNames;
449 if(bUseINI){
450 if(*szSectionNames == 0){
451 GetPrivateProfileSectionNames(szSectionNames, sizeof(szSectionNames), getModuleName());
452 ptr = szSectionNames;
453 }
454 if(_tcscmp(ptr, _T("TTermMenu")) == 0){
455 //skip
456 while(*ptr++);
457 // ptr++;
458 }
459 if(*ptr == 0){
460 return ERROR_NO_MORE_ITEMS;
461 }
462 _tcscpy(lpName, ptr);
463 while(*ptr++);
464 // ptr++;
465 return ERROR_SUCCESS;
466 }else{
467 return ::RegEnumKeyEx(hKey, dwIndex, lpName, lpcName, lpReserved, lpClass, lpcClass, lpftLastWriteTime);
468 }
469 }
470
471 LONG RegDelete(HKEY hKey, LPCTSTR lpSubKey)
472 {
473 if(bUseINI){
474 return WritePrivateProfileString(szSectionName, NULL, NULL, getModuleName()) ? ERROR_SUCCESS : ERROR_ACCESS_DENIED;
475 }else{
476 return ::RegDeleteKey(hKey, lpSubKey);
477 }
478 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26