@@ -702,6 +702,91 @@ BOOL ModifyValue(HWND hwnd, HKEY hKey, LPCTSTR valueName, BOOL EditBin)
702
702
return result ;
703
703
}
704
704
705
+ static LONG CopyKey (HKEY hDestKey , LPCTSTR lpDestSubKey , HKEY hSrcKey , LPCTSTR lpSrcSubKey )
706
+ {
707
+ LONG lResult ;
708
+ DWORD dwDisposition ;
709
+ HKEY hDestSubKey = NULL ;
710
+ HKEY hSrcSubKey = NULL ;
711
+ DWORD dwIndex , dwType , cbName , cbData ;
712
+ TCHAR szSubKey [256 ];
713
+ TCHAR szValueName [256 ];
714
+ BYTE szValueData [512 ];
715
+
716
+ FILETIME ft ;
717
+
718
+ /* open the source subkey, if specified */
719
+ if (lpSrcSubKey )
720
+ {
721
+ lResult = RegOpenKeyEx (hSrcKey , lpSrcSubKey , 0 , KEY_ALL_ACCESS , & hSrcSubKey );
722
+ if (lResult )
723
+ goto done ;
724
+ hSrcKey = hSrcSubKey ;
725
+ }
726
+
727
+ /* create the destination subkey */
728
+ lResult = RegCreateKeyEx (hDestKey , lpDestSubKey , 0 , NULL , 0 , KEY_WRITE , NULL ,
729
+ & hDestSubKey , & dwDisposition );
730
+ if (lResult )
731
+ goto done ;
732
+
733
+ /* copy all subkeys */
734
+ dwIndex = 0 ;
735
+ do
736
+ {
737
+ cbName = sizeof (szSubKey ) / sizeof (szSubKey [0 ]);
738
+ lResult = RegEnumKeyEx (hSrcKey , dwIndex ++ , szSubKey , & cbName , NULL , NULL , NULL , & ft );
739
+ if (lResult == ERROR_SUCCESS )
740
+ {
741
+ lResult = CopyKey (hDestSubKey , szSubKey , hSrcKey , szSubKey );
742
+ if (lResult )
743
+ goto done ;
744
+ }
745
+ }
746
+ while (lResult == ERROR_SUCCESS );
747
+
748
+ /* copy all subvalues */
749
+ dwIndex = 0 ;
750
+ do
751
+ {
752
+ cbName = sizeof (szValueName ) / sizeof (szValueName [0 ]);
753
+ cbData = sizeof (szValueData ) / sizeof (szValueData [0 ]);
754
+ lResult = RegEnumValue (hSrcKey , dwIndex ++ , szValueName , & cbName , NULL , & dwType , szValueData , & cbData );
755
+ if (lResult == ERROR_SUCCESS )
756
+ {
757
+ lResult = RegSetValueEx (hDestSubKey , szValueName , 0 , dwType , szValueData , cbData );
758
+ if (lResult )
759
+ goto done ;
760
+ }
761
+ }
762
+ while (lResult == ERROR_SUCCESS );
763
+
764
+ lResult = ERROR_SUCCESS ;
765
+
766
+ done :
767
+ if (hSrcSubKey )
768
+ RegCloseKey (hSrcSubKey );
769
+ if (hDestSubKey )
770
+ RegCloseKey (hDestSubKey );
771
+ if (lResult != ERROR_SUCCESS )
772
+ SHDeleteKey (hDestKey , lpDestSubKey );
773
+ return lResult ;
774
+ }
775
+
776
+ static LONG MoveKey (HKEY hDestKey , LPCTSTR lpDestSubKey , HKEY hSrcKey , LPCTSTR lpSrcSubKey )
777
+ {
778
+ LONG lResult ;
779
+
780
+ if (!lpSrcSubKey )
781
+ return ERROR_INVALID_FUNCTION ;
782
+
783
+ lResult = CopyKey (hDestKey , lpDestSubKey , hSrcKey , lpSrcSubKey );
784
+ if (lResult == ERROR_SUCCESS )
785
+ SHDeleteKey (hSrcKey , lpSrcSubKey );
786
+
787
+ return lResult ;
788
+ }
789
+
705
790
BOOL DeleteKey (HWND hwnd , HKEY hKeyRoot , LPCTSTR keyPath )
706
791
{
707
792
TCHAR msg [128 ], caption [128 ];
@@ -732,3 +817,128 @@ BOOL DeleteKey(HWND hwnd, HKEY hKeyRoot, LPCTSTR keyPath)
732
817
RegCloseKey (hKey );
733
818
return result ;
734
819
}
820
+
821
+ LONG RenameKey (HKEY hKey , LPCTSTR lpSubKey , LPCTSTR lpNewName )
822
+ {
823
+ LPCTSTR s ;
824
+ LPTSTR lpNewSubKey = NULL ;
825
+ LONG Ret = 0 ;
826
+
827
+ if (!lpSubKey )
828
+ return Ret ;
829
+
830
+ s = _tcsrchr (lpSubKey , _T ('\\' ));
831
+ if (s )
832
+ {
833
+ s ++ ;
834
+ lpNewSubKey = (LPTSTR ) HeapAlloc (GetProcessHeap (), 0 , (s - lpSubKey + _tcslen (lpNewName ) + 1 ) * sizeof (TCHAR ));
835
+ if (lpNewSubKey != NULL )
836
+ {
837
+ memcpy (lpNewSubKey , lpSubKey , (s - lpSubKey ) * sizeof (TCHAR ));
838
+ lstrcpy (lpNewSubKey + (s - lpSubKey ), lpNewName );
839
+ lpNewName = lpNewSubKey ;
840
+ }
841
+ else
842
+ return ERROR_NOT_ENOUGH_MEMORY ;
843
+ }
844
+
845
+ Ret = MoveKey (hKey , lpNewName , hKey , lpSubKey );
846
+
847
+ if (lpNewSubKey )
848
+ {
849
+ HeapFree (GetProcessHeap (), 0 , lpNewSubKey );
850
+ }
851
+ return Ret ;
852
+ }
853
+
854
+ LONG RenameValue (HKEY hKey , LPCTSTR lpSubKey , LPCTSTR lpDestValue , LPCTSTR lpSrcValue )
855
+ {
856
+ LONG lResult ;
857
+ HKEY hSubKey = NULL ;
858
+ DWORD dwType , cbData ;
859
+ BYTE data [512 ];
860
+
861
+ if (lpSubKey )
862
+ {
863
+ lResult = RegOpenKey (hKey , lpSubKey , & hSubKey );
864
+ if (lResult != ERROR_SUCCESS )
865
+ goto done ;
866
+ hKey = hSubKey ;
867
+ }
868
+
869
+ cbData = sizeof (data );
870
+ lResult = RegQueryValueEx (hKey , lpSrcValue , NULL , & dwType , data , & cbData );
871
+ if (lResult != ERROR_SUCCESS )
872
+ goto done ;
873
+
874
+ lResult = RegSetValueEx (hKey , lpDestValue , 0 , dwType , data , cbData );
875
+ if (lResult != ERROR_SUCCESS )
876
+ goto done ;
877
+
878
+ RegDeleteValue (hKey , lpSrcValue );
879
+
880
+ done :
881
+ if (hSubKey )
882
+ RegCloseKey (hSubKey );
883
+ return lResult ;
884
+ }
885
+
886
+ LONG QueryStringValue (HKEY hKey , LPCTSTR lpSubKey , LPCTSTR lpValueName , LPTSTR pszBuffer , DWORD dwBufferLen )
887
+ {
888
+ LONG lResult ;
889
+ HKEY hSubKey = NULL ;
890
+ DWORD cbData , dwType ;
891
+
892
+ if (lpSubKey )
893
+ {
894
+ lResult = RegOpenKey (hKey , lpSubKey , & hSubKey );
895
+ if (lResult != ERROR_SUCCESS )
896
+ goto done ;
897
+ hKey = hSubKey ;
898
+ }
899
+
900
+ cbData = (dwBufferLen - 1 ) * sizeof (* pszBuffer );
901
+ lResult = RegQueryValueEx (hKey , lpValueName , NULL , & dwType , (LPBYTE ) pszBuffer , & cbData );
902
+ if (lResult != ERROR_SUCCESS )
903
+ goto done ;
904
+ if (dwType != REG_SZ )
905
+ {
906
+ lResult = -1 ;
907
+ goto done ;
908
+ }
909
+
910
+ pszBuffer [cbData / sizeof (* pszBuffer )] = _T ('\0' );
911
+
912
+ done :
913
+ if (lResult != ERROR_SUCCESS )
914
+ pszBuffer [0 ] = _T ('\0' );
915
+ if (hSubKey )
916
+ RegCloseKey (hSubKey );
917
+ return lResult ;
918
+ }
919
+
920
+ BOOL GetKeyName (LPTSTR pszDest , size_t iDestLength , HKEY hRootKey , LPCTSTR lpSubKey )
921
+ {
922
+ LPCTSTR pszRootKey ;
923
+
924
+ if (hRootKey == HKEY_CLASSES_ROOT )
925
+ pszRootKey = TEXT ("HKEY_CLASSES_ROOT" );
926
+ else if (hRootKey == HKEY_CURRENT_USER )
927
+ pszRootKey = TEXT ("HKEY_CURRENT_USER" );
928
+ else if (hRootKey == HKEY_LOCAL_MACHINE )
929
+ pszRootKey = TEXT ("HKEY_LOCAL_MACHINE" );
930
+ else if (hRootKey == HKEY_USERS )
931
+ pszRootKey = TEXT ("HKEY_USERS" );
932
+ else if (hRootKey == HKEY_CURRENT_CONFIG )
933
+ pszRootKey = TEXT ("HKEY_CURRENT_CONFIG" );
934
+ else if (hRootKey == HKEY_DYN_DATA )
935
+ pszRootKey = TEXT ("HKEY_DYN_DATA" );
936
+ else
937
+ return FALSE;
938
+
939
+ if (lpSubKey [0 ])
940
+ _sntprintf (pszDest , iDestLength , TEXT ("%s\\%s" ), pszRootKey , lpSubKey );
941
+ else
942
+ _sntprintf (pszDest , iDestLength , TEXT ("%s" ), pszRootKey );
943
+ return TRUE;
944
+ }
0 commit comments