[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 703
  • Last Modified:

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
0
Marco Gasi
Asked:
Marco Gasi
  • 3
1 Solution
 
Sinisa VukCommented:
What is value of response after RegOpen.. fails?
Make sure that value passing to RegOpen... for tvr.ShortPath is something like 'Software\AnalogX\' (without slash as first character). Is you tvr.CurrentKey set to HKEY_CURRENT_USER? Maybe you havnt write access to that part of registry.

Second question: You miss a lot. You must allocate enough storage for buffer
for reading. For reading you must open key with KEY_READ or KEY_WRITE. ... and
value 'String Value' must exist!

So, I propose:
procedure TForm1.Stringvalue1Click(Sender: TObject);
var
  hOpenKey: HKEY;
  iI, iSize, iType: Integer;
  pcTemp: array[0..MAX_PATH] of Byte;
  pdI: PDWORD;
  response: DWORD;
  error, sPath: string;
begin
  if (tvr.Selected = nil) or (tvr.Selected.Text = 'My Computer') then
    Exit
  else
  begin
    pdI := nil;
    sPath := tvr.ShortPath;
    if sPath[1]='\' then Delete(sPath, 1, 1);
    response := RegOpenKeyEx(tvr.CurrentKey, PChar(sPath), 0, KEY_READ or 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(pcTemp); // 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



Message 'SET SUCCESS' for reading !?!?!???
0
 
Marco GasiFreelancerAuthor Commented:
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
0
 
Marco GasiFreelancerAuthor Commented:
Thank you
0
 
Marco GasiFreelancerAuthor Commented:
@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
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now