TRegistry problem with Embarcadero XE2

I have ported some code from  a C++ builder 5 application. the code simply reads a registry key but does not work with embarcadero XE2.
The issue is not due to permissions because the same registry key can be read by the application compiled with BCB 5 on the same PC where the XE2 compiler is used.
The only difference being UnicodeStrings.
I am using Windows 7 Pro 64 bit. I attach a screen shot of the registry key in question.
When the program is run, the LastErrorMsg which I display in a Memo control is "The system cannot find the file specified"

The code is as follows:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
      TRegistry *reg;
    UnicodeString u,v,key;
    bool ok;

    key = L"SOFTWARE\\Alcindor\\CRVMMI";
    v = L"defaultdbs";
    reg = new TRegistry();
    reg->RootKey = HKEY_LOCAL_MACHINE;
    ok = reg->OpenKey(key,false);
    Memo1->Lines->Add(reg->LastErrorMsg);
    if(ok)
    {
        u = reg->ReadString(v);
        Label1->Caption = u;
    }
    delete reg;
Registry Key
LVL 2
Roger AlcindorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
It seems the issue is W7/64 and you are a victim of registry redirection. If your application is 32bit, try placing the registry entry under "HKLM\Wow6432Node" instead, so 32bit applications will seamlessly be able to read it. See also https://msdn.microsoft.com/en-us/library/windows/desktop/ms724072(v=vs.85).aspx ("32-bit and 64-bit Application Data in the Registry"). BTW, if your installer writes the entry, don't bother to specify Wow6432Node, the data will automatically be placed there.
0
jimyXCommented:
> "The system cannot find the file specified"

This error means you have supplied wrong key. I see it's a match to the screen shot but that error means the key is not valid. make sure there is no trailing spaces.

Another possibility being that the key is looked up under the 32 keys while it is under 64.

And after opening the key you are going to hit "Access Denied", unless you use "Run as Admin." or "OpenKeyReadOnly":

I do not think it is about the Unicode, because in Delphi your code runs OK (with KeyReadOnly):
void __fastcall TForm1::Button1Click(TObject *Sender)
{
      TRegistry *reg;
    UnicodeString u,v,key;
    bool ok;

    key = L"SOFTWARE\\Alcindor\\CRVMMI";
    v = L"defaultdbs";
    reg = new TRegistry();
    reg->RootKey = HKEY_LOCAL_MACHINE;
    ok = reg->OpenKeyReadOnly(key);
    Memo1->Lines->Add(reg->LastErrorMsg);
    if(ok)
    {
        u = reg->ReadString(v);
        Label1->Caption = u;
    }
    delete reg;

Open in new window

0
jkrCommented:
I'm sorry, but didn't I mention the 64bit issue a while ago?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Sinisa VukCommented:
Can u post Delphi code here? Your key path in XE2 should be:

key := 'SOFTWARE\Alcindor\CRVMMI';
...

Open in new window

0
jimyXCommented:
> I'm sorry, but didn't I mention the 64bit issue a while ago?

You did. I apologize, probably I should have mentioned "..., as jkr commented".

But I assure you, no answer hijacking intended here. I intended to stress the error meaning, that's all. Even no code or further explanation was provided for tackling 32/64 registry keys!
0
Roger AlcindorAuthor Commented:
I have tried using OpenKeyReadOnly and this makes no difference. the application is being compiled as a 32 bit application and the compiler is also installed as a 32 bit application.
There is no HKLM\Wow6432Node to place the registry entry under (see second regedit screen shot). In addition to this, I am confused by the statement under the Accessing an Alternate Registry View section at the link
 
https://msdn.microsoft.com/en-us/library/windows/desktop/aa384129(v=vs.85).aspx


"Note  The Wow6432Node key is reserved. For compatibility, applications should not use this key directly.

For information about accessing the alternate registry view through WMI, see Requesting WMI Data on a 64-bit Platform.
"
Regisry2.JPG
0
Roger AlcindorAuthor Commented:
By the way, I get the same problem when running as Administrator.
0
Sinisa VukCommented:
try to set access mode for 64 bit nodes:
reg->Access = KEY_READ | KEY_WOW64_64KEY; //for 
...

Open in new window

or for 32 bit:
reg->Access = KEY_READ | KEY_WOW64_32KEY; //for 
...

Open in new window

... before open key... where is:
KEY_WOW64_64KEY = 0x0100; 
KEY_WOW64_32KEY = 0x0200;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jimyXCommented:
> I have tried using OpenKeyReadOnly and this makes no difference.

Are you able to open the RootKey?
Probably you need to check from the root and move down each level until you find what's going on.
0
Roger AlcindorAuthor Commented:
The reg->Access =  KEY_READ | KEY_WOW64_64KEY fixed the issue without the need to add a key
It was also fixed worked by adding the SOFTWARE\\Wow6432Node\\SOFTWARE\\Alcindor\\CRVMMI key to the registry but since this involves more work and associated documentation for maintenance of the application, in this instance Sinsa Vuk's solution is the better fix. However jkr was first to identify the root cause so higher points were therefore assigned.

Many thanks,

Roger
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.