Solved

RegOPenKeyEx not working

Posted on 2013-01-10
4
623 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
  • 3
4 Comments
 
LVL 25

Accepted Solution

by:
Sinisa Vuk earned 500 total points
Comment Utility
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 30

Author Comment

by:Marco Gasi
Comment Utility
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 30

Author Closing Comment

by:Marco Gasi
Comment Utility
Thank you
0
 
LVL 30

Author Comment

by:Marco Gasi
Comment Utility
@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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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 this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now