?
Solved

RegOPenKeyEx not working

Posted on 2013-01-10
4
Medium Priority
?
679 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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

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