00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <stdio.h>
00020 #include <windows.h>
00021 #include "MyRegistry.h"
00022 #include "MyException.h"
00023 #include "mylist.h"
00024
00032 void MyRegistry::SetAutoCreate(BOOL autoCreate)
00033 {
00034 m_isAutoCreate = autoCreate;
00035 }
00036
00037 BOOL MyRegistry::IsAutoCreate()
00038 {
00039 return m_isAutoCreate;
00040 }
00041
00068 BOOL NextSubKey(MyString &key,MyString &subKey,MyString &suite)
00069 {
00070 for (int i=0;i<key.GetSize();i++)
00071 {
00072 if (key[i]=='\\')
00073 {
00074 key[i]='\0';
00075 subKey = key;
00076 key[i]='\\';
00077 suite=key.GetValue()+i+1;
00078 return TRUE;
00079 }
00080 }
00081 subKey=key;
00082 return FALSE;
00083 }
00084
00088 void MyRegistry::HKeyToString (MyString &result)
00089 {
00090 switch ((ULONG_PTR)m_hKey)
00091 {
00092 case HKEY_CLASSES_ROOT: result="HKEY_CLASSES_ROOT"; break;
00093 case HKEY_CURRENT_CONFIG: result="HKEY_CURRENT_CONFIG";break;
00094 case HKEY_CURRENT_USER: result="HKEY_CURRENT_USER"; break;
00095 case HKEY_LOCAL_MACHINE: result="HKEY_LOCAL_MACHINE"; break;
00096 case HKEY_USERS: result="HKEY_USERS";break;
00097 case HKEY_PERFORMANCE_DATA: result="HKEY_PERFORMANCE_DATA";break;
00098 case HKEY_DYN_DATA: result="HKEY_DYN_DATA"; break;
00099 default: result="";
00100 }
00101 }
00102
00108 MyRegistry::MyRegistry(BOOL autoCreate)
00109 {
00110 m_isAutoCreate = autoCreate;
00111 m_lastError = ERROR_SUCCESS;
00112 }
00113
00126 BOOL MyRegistry::Open(char *nameKey)
00127 {
00128 SetKeyName(nameKey);
00129
00130 HKEY hKey;
00131 m_lastError = RegOpenKeyExA( m_hKey, m_postFixKey, 0 , KEY_READ, &hKey);
00132 if (m_lastError!=ERROR_SUCCESS)
00133 {
00134 if (m_isAutoCreate) return Create(NULL);
00135 return FALSE;
00136 }
00137 RegCloseKey (hKey) ;
00138 return TRUE;
00139 }
00140
00141
00142
00143 BOOL MyRegistry::SetKeyName(char *nameKey)
00144 {
00145
00146 char *letter = nameKey;
00147 BOOL modeRet=FALSE;
00148 BOOL modeAdd=TRUE;
00149
00150 if (*letter=='.' || *letter=='\\')
00151 {
00152 while (*letter)
00153 {
00154 switch (*letter)
00155 {
00156 case '.':
00157 modeAdd=FALSE;
00158 if (modeRet)
00159 {
00160 m_postFixKey.Delete(-1,FALSE,1);
00161 if (!m_postFixKey.Delete(-1,FALSE,'\\')) m_postFixKey="";
00162 modeRet = FALSE;
00163 }
00164 else modeRet = TRUE;
00165 break;
00166
00167 case '\\':
00168 if (modeAdd&&m_postFixKey[-1]!='\\'&&nameKey-letter)
00169 m_postFixKey<<'\\';
00170 break;
00171
00172 default:
00173 if (!modeAdd&&m_postFixKey.GetSize()) m_postFixKey<<'\\';
00174 modeAdd=TRUE;
00175 m_postFixKey<<*letter;
00176 break;
00177 }
00178 letter++;
00179 }
00180 }
00181 else if (!SetPrefixAndPostfix(nameKey)) return FALSE;
00182
00183
00184 switch (m_postFixKey[-1])
00185 {
00186 case '\\': break;
00187 case '\0': break;
00188 default: m_postFixKey<<'\\';
00189 }
00190 return TRUE;
00191 }
00192
00193
00194
00195 void MyRegistry::GetKeyName(MyString &result)
00196 {
00197 HKeyToString(result);
00198 result<<"\\"<<m_postFixKey;
00199 }
00200
00206 BOOL MyRegistry::SetPrefixAndPostfix (char *nameKey)
00207 {
00208 MyString fullNameKey = nameKey;
00209 MyString subKey,nextKey;
00210
00211 m_hKey = NULL;
00212 NextSubKey(fullNameKey,subKey,nextKey);
00213 if (subKey=="HKEY_CLASSES_ROOT" || subKey=="HKCR")
00214 m_hKey=HKEY_CLASSES_ROOT;
00215 else if (subKey=="HKEY_CURRENT_CONFIG" || subKey=="HKCC")
00216 m_hKey=HKEY_CURRENT_CONFIG;
00217 else if (subKey=="HKEY_CURRENT_USER" || subKey=="HKCU")
00218 m_hKey=HKEY_CURRENT_USER;
00219 else if (subKey=="HKEY_LOCAL_MACHINE" || subKey=="HKLM")
00220 m_hKey=HKEY_LOCAL_MACHINE;
00221 else if (subKey=="HKEY_USERS" || subKey=="HKU")
00222 m_hKey=HKEY_USERS;
00223 else if (subKey=="HKEY_PERFORMANCE_DATA" || subKey=="HKPD")
00224 m_hKey=HKEY_PERFORMANCE_DATA;
00225 else if (subKey=="HKEY_DYN_DATA" || subKey=="HKDD")
00226 m_hKey=HKEY_DYN_DATA;
00227 else
00228 {
00229 m_hKey = NULL;
00230 return FALSE;
00231 }
00232 m_postFixKey = nextKey;
00233 return TRUE;
00234 }
00235
00245 BOOL MyRegistry::Create(char *nameKey)
00246 {
00247 if (nameKey) if (!SetKeyName(nameKey)) return FALSE;
00248
00249 HKEY hKey;
00250 m_lastError = RegCreateKeyA(m_hKey,m_postFixKey,&hKey);
00251 if (m_lastError!= ERROR_SUCCESS) return FALSE;
00252 RegCloseKey (hKey);
00253 return TRUE;
00254 }
00255
00256
00257 #include <shlwapi.h>
00258 #pragma comment(lib, "shlwapi.lib")
00259
00263 BOOL MyRegistry::DeleteKey(char *nameKey)
00264 {
00265 if (!nameKey) nameKey=m_postFixKey.GetValue();
00266 else if (!SetPrefixAndPostfix(nameKey)) return FALSE;
00267
00268 SHDeleteKey(m_hKey,nameKey);
00269 SetKeyName("..");
00270 TestKey();
00271 return TRUE;
00272 }
00273
00277 BOOL MyRegistry::DeleteValue(char *param)
00278 {
00279 HKEY hKey ;
00280 m_lastError = RegOpenKeyExA(m_hKey,m_postFixKey,0,KEY_SET_VALUE,&hKey);
00281 if (m_lastError!= ERROR_SUCCESS) return FALSE;
00282 m_lastError = RegDeleteValueA (hKey,param);
00283 RegCloseKey (hKey) ;
00284 return m_lastError== ERROR_SUCCESS?TRUE:FALSE;
00285 }
00286
00290 BOOL MyRegistry::TestKey()
00291 {
00292 HKEY hKey;
00293 m_lastError = RegOpenKeyExA(m_hKey,m_postFixKey,0,KEY_READ,&hKey);
00294 if (m_lastError==ERROR_SUCCESS)
00295 {
00296 RegCloseKey (hKey);
00297 return TRUE;
00298 }
00299 return FALSE;
00300 }
00301
00302
00303
00304 BOOL MyRegistry::TestValue(char *param)
00305 {
00306 HKEY hKey ;
00307 ULONG uType = REG_SZ ;
00308 DWORD dwSize=0;
00309
00310 m_lastError = RegOpenKeyExA(m_hKey,m_postFixKey,0,KEY_QUERY_VALUE,&hKey);
00311 if (m_lastError!= ERROR_SUCCESS) return FALSE;
00312 m_lastError = RegQueryValueExA (hKey,param,NULL,NULL,NULL,NULL);
00313 RegCloseKey (hKey) ;
00314 return m_lastError==ERROR_SUCCESS;
00315 }
00316
00320 int MyRegistry::GetValueSize(char *param)
00321 {
00322 HKEY hKey ;
00323 DWORD dwSize;
00324
00325 m_lastError = RegOpenKeyExA(m_hKey,m_postFixKey,0,KEY_QUERY_VALUE ,&hKey);
00326 if (m_lastError!= ERROR_SUCCESS) return FALSE;
00327 m_lastError = RegQueryValueExA (hKey,param,NULL,NULL,NULL,&dwSize);
00328 RegCloseKey (hKey) ;
00329
00330 return dwSize;
00331 }
00332
00333 int MyRegistry::GetValueType(char *param)
00334 {
00335 HKEY hKey ;
00336 ULONG uType;
00337
00338 m_lastError = RegOpenKeyExA(m_hKey,m_postFixKey,0,KEY_QUERY_VALUE ,&hKey);
00339 if (m_lastError!= ERROR_SUCCESS) return FALSE;
00340 m_lastError = RegQueryValueExA (hKey,param,NULL,&uType,NULL,NULL);
00341 RegCloseKey (hKey) ;
00342
00343 return uType;
00344 }
00345
00349 LPBYTE MyRegistry::GetValue(char *param ,DWORD uTypeResult)
00350 {
00351 HKEY hKey ;
00352 DWORD dwSize;
00353 ULONG uType;
00354 LPBYTE result;
00355
00356 m_lastError = RegOpenKeyExA(m_hKey,m_postFixKey,0,KEY_QUERY_VALUE,&hKey);
00357 if (m_lastError!=ERROR_SUCCESS) return FALSE;
00358 m_lastError = RegQueryValueExA (hKey,param,NULL,&uType,NULL,NULL);
00359 if (m_lastError!=ERROR_SUCCESS || uTypeResult!=uType) { RegCloseKey (hKey); return FALSE; }
00360 m_lastError = RegQueryValueExA (hKey,param,NULL,NULL,NULL,&dwSize);
00361 if (m_lastError!=ERROR_SUCCESS) { RegCloseKey (hKey); return FALSE; }
00362 result = new BYTE[dwSize];
00363 ZeroMemory(result,dwSize);
00364 m_lastError = RegQueryValueExA (hKey,param,NULL,&uType,result,&dwSize);
00365 RegCloseKey (hKey);
00366 return m_lastError==ERROR_SUCCESS?result:NULL;
00367 }
00368
00369 BOOL MyRegistry::GetValue(char *param , char *result , char *defValue)
00370 {
00371 if (result) result[0]='\0';
00372 else return FALSE;
00373 MyString temp;
00374 temp=result;
00375 if (GetValue(param,temp,defValue))
00376 {
00377 strncpy(result,temp.GetValue(),temp.GetSize()+1);
00378 return TRUE;
00379 }
00380 else return FALSE;
00381 }
00382
00383 BOOL MyRegistry::GetValue(char *param, MyString &result , char *defValue)
00384 {
00385 LPBYTE res=NULL;
00386 DWORD uType = GetValueType(param);
00387 result="";
00388
00389 switch (uType)
00390 {
00391 case REG_SZ:
00392 case REG_EXPAND_SZ:
00393 res = GetValue(param,uType);
00394 if (!res) break;
00395 result=(char *)res;
00396 delete res;
00397 return TRUE;
00398
00399 case REG_MULTI_SZ:
00400 res = GetValue(param,uType);
00401 if (!res) break;
00402 int size;
00403 size = GetValueSize(param);
00404 for (int i=0;i<size;i++)
00405 {
00406 if (res[i]=='\0') result<<' ';
00407 else result+=(char)res[i];
00408 }
00409 result.Delete(-1,FALSE,1);
00410 delete res;
00411 return TRUE;
00412 }
00413 if (defValue) result = defValue;
00414 else result ="";
00415 return FALSE;
00416 }
00417
00418 BOOL MyRegistry::GetValueMulti(char *param, MyList<MyString> &result , char *defValue)
00419 {
00420 result.i.SuprAll();
00421 LPBYTE res = GetValue(param,REG_MULTI_SZ);
00422 if (!res)
00423 {
00424 MyString temp;
00425 if (!defValue)
00426 {
00427 temp = defValue;
00428 result.i+=temp;
00429 }
00430 else result.i+=temp;
00431 return FALSE;
00432 }
00433
00434 MyString current;
00435 int size = GetValueSize(param);
00436 current=(char *)res;
00437 if (size!=1)result.i+=current;
00438 for (int i=0;i<size-1;i++)
00439 {
00440 if (res[i]=='\0')
00441 {
00442 current=(char *)res+i+1;
00443 result.i+=current;
00444 }
00445 }
00446 delete res;
00447 return TRUE;
00448 }
00449
00450 BOOL MyRegistry::GetValueExpand(char *param, char *result , char *defValue)
00451 {
00452 MyString temp;
00453 BOOL r = GetValueExpand(param,temp,defValue);
00454 strcpy(result,temp);
00455 return r;
00456 }
00457
00458 BOOL MyRegistry::GetValueExpand(char *param, MyString &result , char *defValue)
00459 {
00460 LPBYTE res = GetValue(param,REG_EXPAND_SZ);
00461 if (!res)
00462 {
00463 if (defValue) result = defValue;
00464 else result = "";
00465 return FALSE;
00466 }
00467 int newSize = ExpandEnvironmentStrings((LPCSTR)res,NULL,0);
00468 char *resExpand = new char[newSize];
00469 ExpandEnvironmentStrings((LPCSTR)res,resExpand,newSize);
00470 result=resExpand;
00471 delete resExpand;
00472 delete res;
00473 return TRUE;
00474 }
00475
00476 BOOL MyRegistry::GetValue(char *param,int *result,int defValue)
00477 {
00478 return GetValue(param,(DWORD *)result,defValue);
00479 }
00480
00481 BOOL MyRegistry::GetValue(char *param,DWORD *result,DWORD defValue)
00482 {
00483 DWORD uType = GetValueType(param);
00484 if (uType!=REG_DWORD && uType!=REG_DWORD_LITTLE_ENDIAN && uType!=REG_DWORD_BIG_ENDIAN)
00485 {
00486 *result = defValue;
00487 return FALSE;
00488 }
00489 LPBYTE res = GetValue(param,uType);
00490 if (res)
00491 {
00492 *result=*(DWORD *)res;
00493 delete res;
00494 return TRUE;
00495 }
00496 *result = defValue;
00497 return FALSE;
00498 }
00499
00500 BOOL MyRegistry::GetValue(char *param,LONGLONG *result, LONGLONG defValue)
00501 {
00502 DWORD uType = GetValueType(param);
00503 if (uType!=REG_QWORD && uType!=REG_QWORD_LITTLE_ENDIAN) return FALSE;
00504 LPBYTE res = GetValue(param,uType);
00505 if (res)
00506 {
00507 *result=*(LONGLONG *)res;
00508 delete res;
00509 return TRUE;
00510 }
00511 *result = defValue;
00512 return FALSE;
00513 }
00514
00515 BOOL MyRegistry::GetValue(char *param,float *result,float defValue)
00516 {
00517 MyString temp,defValueString;
00518 defValueString=defValue;
00519 BOOL res = GetValue(param,temp,defValueString);
00520 *result = temp.ToFloat();
00521 return res;
00522 }
00523
00524 BOOL MyRegistry::GetValue(char *param,double *result,double defValue)
00525 {
00526 MyString temp,defValueString;
00527 defValueString=defValue;
00528 BOOL res = GetValue(param,temp,defValueString);
00529 *result = temp.ToDouble();
00530 return res;
00531 }
00532
00545 LPBYTE MyRegistry::GetValue(char *param)
00546 {
00547 switch (GetValueType(param))
00548 {
00549 case REG_BINARY: return GetValue(param,REG_BINARY);
00550 case REG_NONE: return GetValue(param,(DWORD)REG_NONE)?(LPBYTE)TRUE:(LPBYTE)FALSE;
00551 default:return FALSE;
00552 }
00553 }
00554
00558 BOOL MyRegistry::SetValue(char *param,LPBYTE source,ULONG uTypeSource,ULONG sizeSource)
00559 {
00560 HKEY hKey ;
00561
00562 m_lastError = RegOpenKeyExA(m_hKey,m_postFixKey,0,KEY_SET_VALUE,&hKey);
00563 if (m_lastError!= ERROR_SUCCESS) return FALSE;
00564 m_lastError = RegSetValueExA (hKey, param, NULL, uTypeSource , source, sizeSource);
00565 RegCloseKey (hKey) ;
00566 return m_lastError== ERROR_SUCCESS?TRUE:FALSE;
00567 }
00568
00569 BOOL MyRegistry::SetValue(char *param)
00570 { return SetValue(param,NULL,REG_NONE,0); }
00571
00572 BOOL MyRegistry::SetValue(char *param,int value)
00573 { return SetValue(param,(DWORD)value); }
00574
00575 BOOL MyRegistry::SetValue(char *param,DWORD value)
00576 { return SetValue(param,(LPBYTE)&value,REG_DWORD,4); }
00577
00578 BOOL MyRegistry::SetValueLittleEndian(char *param,DWORD value)
00579 { return SetValue(param,(LPBYTE)&value,REG_DWORD_LITTLE_ENDIAN,4); }
00580
00581 BOOL MyRegistry::SetValueBigEndian(char *param,DWORD value)
00582 { return SetValue(param,(LPBYTE)&value,REG_DWORD_BIG_ENDIAN,4); }
00583
00584 BOOL MyRegistry::SetValue(char *param,LONGLONG value)
00585 { return SetValue(param,(LPBYTE)&value,REG_QWORD,8); }
00586
00587 BOOL MyRegistry::SetValueLittleEndian(char *param,LONGLONG value)
00588 { return SetValue(param,(LPBYTE)&value,REG_QWORD_LITTLE_ENDIAN,8); }
00589
00590 BOOL MyRegistry::SetValue(char *param,char *value)
00591 { return SetValue(param,(LPBYTE)value,REG_SZ,(int)strlen(value)+1); }
00592
00593 BOOL MyRegistry::SetValueExpand(char *param,char *value)
00594 { return SetValue(param,(LPBYTE)value,REG_EXPAND_SZ,(int)strlen(value)+1); }
00595
00596 BOOL MyRegistry::SetValueMulti(char *param,char *str)
00597 {
00598 BOOL isZero = FALSE;
00599 int size;
00600 for (size=0;;size++)
00601 if (str[size]=='\0')
00602 if (isZero) break;
00603 else isZero=TRUE;
00604 else isZero=FALSE;
00605
00606 return SetValue(param,(LPBYTE)str,REG_MULTI_SZ,size);
00607 }
00608
00609 BOOL MyRegistry::SetValueMulti(char *param,MyList<MyString> &lString)
00610 {
00611 MyString res;
00612 for (lString.i=0;lString.i.More();lString.i++)
00613 res<<lString.i.GetElem()<<'\1';
00614 for (int i=0;i<res.GetSize();i++) if (res[i]=='\1') res[i]='\0';
00615 return SetValueMulti(param,res);
00616 }
00617
00618 BOOL MyRegistry::SetValue(char *param,float value)
00619 {
00620 MyString temp;
00621 temp = value;
00622 return SetValue(param,temp);
00623 }
00624
00625 BOOL MyRegistry::SetValue(char *param,double value)
00626 {
00627 MyString temp;
00628 temp = value;
00629 return SetValue(param,temp);
00630 }
00631
00632 BOOL MyRegistry::SetValue(char *param,LPBYTE data,int size)
00633 { return SetValue(param,data,REG_BINARY,size); }
00634
00635
00636
00637
00638 BOOL MyRegistry::NextEnumKey (char *result)
00639 {
00640 MyString str;
00641 BOOL res = NextEnumKey(str);
00642 strcpy(result,str.GetValue());
00643 return res;
00644 }
00645
00646 BOOL MyRegistry::NextEnumKey(MyString &result)
00647 {
00648 HKEY hKey ;
00649 char res[261];
00650 DWORD size=261;
00651 result="";
00652
00653 m_lastError = RegOpenKeyExA(m_hKey,m_postFixKey,0,KEY_ENUMERATE_SUB_KEYS,&hKey);
00654 if (m_lastError!= ERROR_SUCCESS) return FALSE;
00655 m_lastError = RegEnumKeyA(hKey,m_numEnumKey++,res,261);
00656 RegCloseKey (hKey);
00657 if (m_lastError==ERROR_SUCCESS)
00658 {
00659 result = res;
00660 return TRUE;
00661 }
00662 else
00663 {
00664 if (m_lastError==ERROR_NO_MORE_ITEMS) m_lastError = ERROR_SUCCESS;
00665 result="";
00666 return FALSE;
00667 }
00668 }
00669
00670
00671
00672 BOOL MyRegistry::NextEnumValue (char *result)
00673 {
00674 MyString str;
00675 BOOL res = NextEnumValue(str);
00676 strcpy(result,str.GetValue());
00677 return res;
00678 }
00679
00680 BOOL MyRegistry::NextEnumValue (MyString &result)
00681 {
00682 HKEY hKey ;
00683 char res[261];
00684 DWORD size=261;
00685 result="";
00686
00687 m_lastError = RegOpenKeyExA(m_hKey,m_postFixKey,0,KEY_QUERY_VALUE,&hKey);
00688 if (m_lastError!=ERROR_SUCCESS) return FALSE;
00689 m_lastError = RegEnumValueA(hKey,m_numEnumValue++,res,&size,NULL,NULL,NULL,NULL);
00690 RegCloseKey (hKey);
00691 if (m_lastError==ERROR_SUCCESS)
00692 {
00693 result = res;
00694 return TRUE;
00695 }
00696 else
00697 {
00698 if (m_lastError==ERROR_NO_MORE_ITEMS) m_lastError = ERROR_SUCCESS;
00699 result="";
00700 return FALSE;
00701 }
00702 }
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733