Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Delphi Registry Functions

WizardyWizardy Member Posts: 24
Hi!


My code - Saving registry key into a separate file:

This code does not work in Win9xME. The problem is that the registry key is not saved into a file. What could be the problem?

[code]
...
var
phKey: hKey;
FileName: array [0..255] of Char;
ErrorMsg: Integer;
begin
// adjust registry privileges only if OS is WinNT2kXP
if (CheckOSVersion = VER_PLATFORM_WIN32_NT) then SetRegistryPrivilege ('SeBackupPrivilege');
// show save as dialog box
if (Dialog_Save_As.Execute) then
begin
RegOpenKeyEx (ROOT_KEY, PChar (SAVE_LOAD_KEY), 0, KEY_ALL_ACCESS, phKey);
StrPCopy (FileName, Dialog_Save_As.FileName);
ErrorMsg := RegSaveKey (phKey, FileName, nil);
if (ErrorMsg = 0) then
begin
ShowMessage ('Hive file named '''+ FileName +''' was successfully saved.');
end
else
begin
if (ErrorMsg = 1314) then MessageDlg ('You do not have the privileges to save a hive file.', mtError, [mbOk], 0)
else if (ErrorMsg = 1016) then MessageDlg ('An I/O operation initiated by the registry failed unrecoverably. File path might be too long or contains unusual characters.', mtError, [mbOk], 0)
else if (ErrorMsg = 183) then MessageDlg ('File already exists. Please, write a new name for a hive file.', mtError, [mbOk], 0)
else MessageDlg ('Error occured while saving a hive file. Error: '+ IntToStr (ErrorMsg) +'.', mtError, [mbOk], 0);
end;
RegCloseKey (phKey);
end;
...
[/code]

My code - Loading registry keys from a file into registry:


Problem is here in the RegRestoreKey which is not supported in Win9xME, but RegRestoreKey and RegLoadKey are not working too.
What is the problem, I do not know. Anyone knows the solution??

[code]
...
var
phKey: hKey;
FileName: array [0..255] of char;
ErrorMsg: Integer;
begin
// adjust registry privileges only if OS is WinNT2kXP
if (CheckOSVersion = VER_PLATFORM_WIN32_NT) then SetRegistryPrivilege ('SeRestorePrivilege');
// show load dialog box
if (Dialog_Load.Execute) then
begin
RegOpenKeyEx (ROOT_KEY, PChar (SAVE_LOAD_KEY), 0, KEY_ALL_ACCESS, phKey);
StrPCopy (FileName, Dialog_Load.FileName);
ErrorMsg := RegRestoreKey (phKey, FileName, 0);
if (ErrorMsg = 0) then
begin
ShowMessage ('Hive file named '''+ FileName +''' was successfully loaded.');
// update main form
Dialog_MKH.FormCreate (Sender);
end
else
begin
if (ErrorMsg = 1314) then MessageDlg ('You do not have the privileges to load a hive file into registry.', mtError, [mbOk], 0)
else if (ErrorMsg = 1017) then MessageDlg ('File '''+ FileName +''' is not a hive file.', mtError, [mbOk], 0)
else if (ErrorMsg = 1010) then MessageDlg ('Configuration registry key in a hive file is invalid.', mtError, [mbOk], 0)
else if (ErrorMsg = 120) then MessageDlg ('Function is not supported on this system.', mtError, [mbOk], 0)
else if (ErrorMsg = 5) then MessageDlg ('Access to the registry is denied. Close regedit or any other registry editing tool.', mtError, [mbOk], 0)
else MessageDlg ('Error occured while loading a hive file into registry. Error: '+ IntToStr (ErrorMsg) +'.', mtError, [mbOk], 0);
end;
RegCloseKey (phKey);
end;
...
[/code]
Please note, that this code is perfectly working on WinNT2kXP.

Thanks!

Comments

  • zibadianzibadian Member Posts: 6,349
    : Hi!
    :
    :
    : My code - Saving registry key into a separate file:
    :
    : This code does not work in Win9xME. The problem is that the registry key is not saved into a file. What could be the problem?
    :
    : [code]
    : ...
    : var
    : phKey: hKey;
    : FileName: array [0..255] of Char;
    : ErrorMsg: Integer;
    : begin
    : // adjust registry privileges only if OS is WinNT2kXP
    : if (CheckOSVersion = VER_PLATFORM_WIN32_NT) then SetRegistryPrivilege ('SeBackupPrivilege');
    : // show save as dialog box
    : if (Dialog_Save_As.Execute) then
    : begin
    : RegOpenKeyEx (ROOT_KEY, PChar (SAVE_LOAD_KEY), 0, KEY_ALL_ACCESS, phKey);
    : StrPCopy (FileName, Dialog_Save_As.FileName);
    : ErrorMsg := RegSaveKey (phKey, FileName, nil);
    : if (ErrorMsg = 0) then
    : begin
    : ShowMessage ('Hive file named '''+ FileName +''' was successfully saved.');
    : end
    : else
    : begin
    : if (ErrorMsg = 1314) then MessageDlg ('You do not have the privileges to save a hive file.', mtError, [mbOk], 0)
    : else if (ErrorMsg = 1016) then MessageDlg ('An I/O operation initiated by the registry failed unrecoverably. File path might be too long or contains unusual characters.', mtError, [mbOk], 0)
    : else if (ErrorMsg = 183) then MessageDlg ('File already exists. Please, write a new name for a hive file.', mtError, [mbOk], 0)
    : else MessageDlg ('Error occured while saving a hive file. Error: '+ IntToStr (ErrorMsg) +'.', mtError, [mbOk], 0);
    : end;
    : RegCloseKey (phKey);
    : end;
    : ...
    : [/code]
    :
    : My code - Loading registry keys from a file into registry:
    :
    :
    : Problem is here in the RegRestoreKey which is not supported in Win9xME, but RegRestoreKey and RegLoadKey are not working too.
    : What is the problem, I do not know. Anyone knows the solution??
    :
    : [code]
    : ...
    : var
    : phKey: hKey;
    : FileName: array [0..255] of char;
    : ErrorMsg: Integer;
    : begin
    : // adjust registry privileges only if OS is WinNT2kXP
    : if (CheckOSVersion = VER_PLATFORM_WIN32_NT) then SetRegistryPrivilege ('SeRestorePrivilege');
    : // show load dialog box
    : if (Dialog_Load.Execute) then
    : begin
    : RegOpenKeyEx (ROOT_KEY, PChar (SAVE_LOAD_KEY), 0, KEY_ALL_ACCESS, phKey);
    : StrPCopy (FileName, Dialog_Load.FileName);
    : ErrorMsg := RegRestoreKey (phKey, FileName, 0);
    : if (ErrorMsg = 0) then
    : begin
    : ShowMessage ('Hive file named '''+ FileName +''' was successfully loaded.');
    : // update main form
    : Dialog_MKH.FormCreate (Sender);
    : end
    : else
    : begin
    : if (ErrorMsg = 1314) then MessageDlg ('You do not have the privileges to load a hive file into registry.', mtError, [mbOk], 0)
    : else if (ErrorMsg = 1017) then MessageDlg ('File '''+ FileName +''' is not a hive file.', mtError, [mbOk], 0)
    : else if (ErrorMsg = 1010) then MessageDlg ('Configuration registry key in a hive file is invalid.', mtError, [mbOk], 0)
    : else if (ErrorMsg = 120) then MessageDlg ('Function is not supported on this system.', mtError, [mbOk], 0)
    : else if (ErrorMsg = 5) then MessageDlg ('Access to the registry is denied. Close regedit or any other registry editing tool.', mtError, [mbOk], 0)
    : else MessageDlg ('Error occured while loading a hive file into registry. Error: '+ IntToStr (ErrorMsg) +'.', mtError, [mbOk], 0);
    : end;
    : RegCloseKey (phKey);
    : end;
    : ...
    : [/code]
    : Please note, that this code is perfectly working on WinNT2kXP.
    :
    : Thanks!
    :
    Try using the TRegistry object. It's been designed to handle this kind of operations, and I've successfully used it so save registry keys to files under winME.
  • WizardyWizardy Member Posts: 24
    Thanks for you reply.

    I already tried TRegistry object (SaveKey and LoadKey) and it is not working. I really have no idea how did you make it working. Could you post a code?
    Check my programs!

  • zibadianzibadian Member Posts: 6,349
    : Thanks for you reply.
    :
    : I already tried TRegistry object (SaveKey and LoadKey) and it is not working. I really have no idea how did you make it working. Could you post a code?
    : Check my programs!
    :
    :
    I used something like this:
    [code]
    with Registry do
    begin
    RootKey := HKEY_LOCAL_MACHINE;
    OpenKey('Software');
    LoadKey('ICP-MS', 'icpms');
    end;
    [/code]
    The actual code had some string-handling in it, but it worked.
    Note: The last line in the help files on LoadKey() is especially important for win9x.
Sign In or Register to comment.