Movekey method for Win 95

How I can COPY or RENAME registry key?
Delphi method MoveKey works correctly under Win98 and Win NT but does not copy subkeys under Win95.
I'm unable to use combination 'SaveKey/Loadkey' because 'Loadkey' does not write file to subkey but only to the root so I again need 'Movekey'
zaitsevAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MadshiCommented:
I guess you will have to enumerate through all the subkeys and subvalues. A lot of work, but I know no better way.

Regards, Madshi.
0
zaitsevAuthor Commented:
Edited text of question.
0
rwilson032697Commented:
Here's a routine that will recusively delete a key from the registry. Its a start anyway...

Cheers,

Raymond.

delRegKey(myreg,'A',myreg.currentpath);
 
procedure TMainFrm.delRegKey(r: TRegistry; key,parent: String);
begin
  screen.cursor := crhourglass;
  try
    _delRegKey(r,key,parent);
  finally
    screen.cursor := crdefault;
  end;
end;

procedure TMainFrm._delRegKey(r: TRegistry; key,parent: String);
var
  sk: TStringList;
  x: integer;
begin { access the key }
  if r.openkey(key,false) then
    try { remove any subkeys }
      if r.hassubkeys then
        begin
          sk := tstringlist.create;
          try
            r.getkeynames(sk);
            for x := 0 to sk.count-1 do
              _delRegKey(r,sk.strings[x],r.currentpath);
          finally
            sk.free;
          end;
        end;
    finally
      r.closekey
    end;
  r.openkey(parent,false);
  r.deletekey(key);
end;
 
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

rwilson032697Commented:
Zaitsev: Are you sure Movekey does not work for 95? I just had a look at the VCL code (D5) and its looks pretty simple - really just what you would write if you had to go about writing it again...

If you have the 'Pro' or better version of Delphi, look in registry.pas. The MoveKey routine source is there.

Cheers,

Raymond.
0
SteveWaiteCommented:
I've just checked it and it just does not work in Win95!.

(horrible kludges inc.)
You could take control of regedit and export the key in a hidden dos window and programatically hack the key name and import it back.
Then delete the original key.

No? I thought not!
0
LischkeCommented:
Hi all,

here is my version which works for all OS's:

procedure RenameRegistryItem(AKey: HKEY; Old, New: String);

var
  OldKey,
  NewKey: HKEY;
  Status: Integer;

begin
  // Open Source key
  Status := RegOpenKey(AKey, PChar(Old), OldKey);
  if Status = ERROR_SUCCESS then
  begin
    // Create Destination key
    Status := RegCreateKey(AKey, PChar(New), NewKey);
    if Status = ERROR_SUCCESS then CopyRegistryKey(OldKey, NewKey);
    RegCloseKey(OldKey);
    RegCloseKey(NewKey);
    // Delete last top-level key
    RegDeleteKey(AKey, PChar(Old));
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

procedure CopyRegistryKey(Source, Dest: HKEY);

const
  DefValueSize  = 512;
  DefBufferSize = 8192;

var
  Status: Integer;
  Key: Integer;
  ValueSize,
  BufferSize: Cardinal;
  KeyType: Integer;
  ValueName: String;
  Buffer: Pointer;
  NewTo,
  NewFrom: HKEY;

begin
  SetLength(ValueName, DefValueSize);
  Buffer := AllocMem(DefBufferSize);
  try
    Key := 0;
    repeat
      ValueSize := DefValueSize;
      BufferSize := DefBufferSize;
      //  enumerate data values at current key
      Status := RegEnumValue(Source, Key, PChar(ValueName), ValueSize, nil, @KeyType, Buffer, @BufferSize);
      if Status = ERROR_SUCCESS then
      begin
        // move each value to new place
        Status := RegSetValueEx(Dest, PChar(ValueName), 0, KeyType, Buffer, BufferSize);
         // delete old value
        RegDeleteValue(Source, PChar(ValueName));
      end;
    until Status <> ERROR_SUCCESS; // Loop until all values found

    // start over, looking for keys now instead of values
    Key := 0;
    repeat
      ValueSize := DefValueSize;
      BufferSize := DefBufferSize;
      Status := RegEnumKeyEx(Source, Key, PChar(ValueName), ValueSize, nil, Buffer, @BufferSize, nil);
      // was a valid key found?
      if Status = ERROR_SUCCESS then
      begin
        // open the key if found
        Status := RegCreateKey(Dest, PChar(ValueName), NewTo);
        if Status = ERROR_SUCCESS then
        begin
          //  Create new key of old name
          Status := RegCreateKey(Source, PChar(ValueName), NewFrom);
          if Status = ERROR_SUCCESS then
          begin
            // if that worked, recurse back here
            CopyRegistryKey(NewFrom, NewTo);
            RegCloseKey(NewFrom);
            RegDeleteKey(Source, PChar(ValueName));
          end;
          RegCloseKey(NewTo);
        end;
      end;
    until Status <> ERROR_SUCCESS; // loop until key enum fails
  finally
    FreeMem(Buffer);
  end;
end;

//----------------------------------------------------------------------------------------------------------------------

Ciao, Mike
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AttarSoftwareCommented:
Listening... =O)

Tim.
0
zaitsevAuthor Commented:
Lischke,
The posted code works fine on WinNt, but still does not produce anticipated result on Win95
Thanks,
Yuri
0
zaitsevAuthor Commented:
Lischke,
The posted code works fine on WinNt, but still does not produce anticipated result on Win95
Thanks,
Yuri
0
LischkeCommented:
Mmmh, this is weird. It *should* work with Win9x too, in particular because it does not use special APIs. Unfortunately, I'm running currently only WinNT4 and Win2K. So I cannot check what is wrong. Actually, there are only very few lines of code which could fail so please step through them and tell what line fails with which error.

Ciao, Mike
0
gandalf_the_whiteCommented:
listening...
0
LischkeCommented:
Yuri, can you tell me what actually happens on your system? Does at least a part of the code work?

Ciao, Mike
0
zaitsevAuthor Commented:
RegOpenKey returns error under Win95,but
RegCreateKey works. Also RegDeleteKey does not delete upper level key.
But it's OK, I just copy key. Thanks a lot.
Yuri
0
LischkeCommented:
Okay, thank you too...:-)

Caio, Mike
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.