Solved

RegOPenKeyEx not working

Posted on 2013-01-10
4
658 Views
Last Modified: 2013-01-11
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
Comment
Question by:Marco Gasi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 27

Accepted Solution

by:
Sinisa Vuk earned 500 total points
ID: 38763253
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
 
LVL 31

Author Comment

by:Marco Gasi
ID: 38763797
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
 
LVL 31

Author Closing Comment

by:Marco Gasi
ID: 38763799
Thank you
0
 
LVL 31

Author Comment

by:Marco Gasi
ID: 38768290
@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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to send memory stream from ics Client To ics server ? 11 203
Multiple image collision 13 102
How to define IfThen functions in one common unit? 4 71
Base1 Encode/Decode 3 96
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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