Link to home
Start Free TrialLog in
Avatar of Marco Gasi
Marco GasiFlag for Spain

asked on

RegOPenKeyEx not working

Hi all.

Another problem is driving me crazy! Below you find the code of a function which, do you guess? fails!

procedure TForm1.Stringvalue1Click(Sender: TObject);
var
  hOpenKey: HKEY;
  iI, iSize, iType: Integer;
  pcTemp: PByte;
  pdI: PDWORD;
  response: DWORD;
  error: string;
begin
  if (tvr.Selected = nil) or (tvr.Selected.Text = 'My Computer') then
    Exit
  else
  begin
    pdI := nil;
    response := RegOpenKeyEx(tvr.CurrentKey, PChar(tvr.ShortPath), 0, KEY_WRITE, hOpenKey);
//    response := RegOpenKeyEx(HKEY_CURRENT_USER, '000\CORBA Events\', 0, KEY_WRITE, hOpenKey);
    if response = ERROR_SUCCESS then
    begin
      ShowMessage('KEY OPEN');
      iType := REG_SZ;       // Type of data that is going to be read
      iSize := SizeOf(Integer); // Buffer for the value to read
      if RegQueryValueEx(hOpenKey, 'String Value', nil, @iType, pcTemp, @iSize)
        = ERROR_SUCCESS then
        ShowMessage('SET SUCCESS')
      else
        ShowMessage('SET FAILED');
      RegCloseKey(hOpenKey);
    end
    else
    begin
      ShowMessage('KEY NOT OPEN: '+ IntToStr(response));
    end;
  end;
end;

Open in new window


What happens here? In the commented line 17 you see the exact value passed with line 16. Let me explain variables used in line 16:

- tvr is TJvRegistryTreeView
- tvr.CurrentKey is a HKEY
- tvr.ShortPath is a string holding the current path without the HKEY value

Well: line 17 works, line 16 doesn't: please, I would have not only the solution but overall the explanation of waht is involving here.

As a side note, I have to admit that testing line 17 I saw that setting value doesn't work but this will be another question.

Thanks in advance to all

Cheers
Marco
ASKER CERTIFIED SOLUTION
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Marco Gasi

ASKER

Hi, sinisav: thanks for helping me.
Well, you're right (obviously): ShortPath returns a string with a leading backslash: once I removed it the key is correctly open.

About the rest, I simply copied and pasted the code from the web and I didn't analyze its core so I didn't notice it used RegQueryValueEx instead of RegSetValueEx: I want to create a new value. Now I have changed a bit the code but... it doesn't work. I open a new question about and I'll post here the link so you'll can help me in it too :-)

Cheers
Thank you
@sinissav, don't worry, I'm not dead yet :-) But I have to go away the next week, so it's not intelligent to post a new question knowing that I can't follow its answers. I'll cme back in 20-01
and I'll ask why I can't create new key in the damned Windows registry (do you guess? Right, my code doesn't work: what's a strang fact, uh?).

Cheers
Marco