[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Movekey method for Win 95

Posted on 2000-02-10
14
Medium Priority
?
296 Views
Last Modified: 2008-02-01
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'
0
Comment
Question by:zaitsev
  • 4
  • 4
  • 2
  • +4
14 Comments
 
LVL 20

Expert Comment

by:Madshi
ID: 2508723
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
 

Author Comment

by:zaitsev
ID: 2508883
Edited text of question.
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2509233
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
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
LVL 12

Expert Comment

by:rwilson032697
ID: 2509487
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
 
LVL 3

Expert Comment

by:SteveWaite
ID: 2510565
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
 
LVL 10

Accepted Solution

by:
Lischke earned 800 total points
ID: 2510714
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
 
LVL 1

Expert Comment

by:AttarSoftware
ID: 2511283
Listening... =O)

Tim.
0
 

Author Comment

by:zaitsev
ID: 2512470
Lischke,
The posted code works fine on WinNt, but still does not produce anticipated result on Win95
Thanks,
Yuri
0
 

Author Comment

by:zaitsev
ID: 2512674
Lischke,
The posted code works fine on WinNt, but still does not produce anticipated result on Win95
Thanks,
Yuri
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2514055
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
 
LVL 3

Expert Comment

by:gandalf_the_white
ID: 2519845
listening...
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2521739
Yuri, can you tell me what actually happens on your system? Does at least a part of the code work?

Ciao, Mike
0
 

Author Comment

by:zaitsev
ID: 2524413
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
 
LVL 10

Expert Comment

by:Lischke
ID: 2526184
Okay, thank you too...:-)

Caio, Mike
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
Suggested Courses

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question