Recent

Author Topic: TRegistry.CreateKey() does not work - neither with administratation-rights!  (Read 14874 times)

Lazarus

  • New Member
  • *
  • Posts: 26
Hello everybody :),

I like to create a new key in the Windows Registry and I'm using for this TRegistry.
The RootKey is HKEY_LOCAL_MACHINE.

When I use
Code: [Select]
Registry := TRegistry.Create;
Registry.RootKey := HKEY_LOCAL_MACHINE;
Registry.OpenKey('CurrentControlSet\Control\StorageDevicePolicies', true);
the program does not create the "StorageDevicePolicies"-Key, because I doesn't exists but I set the second paramter to "true"  %)

When I use
Code: [Select]
Registry.OpenKey('CurrentControlSet\Control', true);
if not Registry.KeyExists('StorageDevicePolicies') then Registry.CreateKey('StorageDevicePolicies');
Registry.OpenKey('StorageDevicePolicies', true);
(which should do the same) I just get an error that the Registry-Key could not be created, although I runned the program with admin-rights  :o!

Do you have an idea how to solve this problem?
This code is executed in the Form's OnCreate-Event, maybe this helps.

Thanks in advance and Greetings,
bastla
« Last Edit: September 18, 2012, 03:47:15 pm by bastla »

IPguy

  • Sr. Member
  • ****
  • Posts: 385
Re: TRegistry.CreateKey() does not work - neither with administratation-rights!
« Reply #1 on: September 18, 2012, 04:08:53 pm »
suggest you search the forum.  There have been prior discussions on this topic.

here are links to a few of them.
http://lazarus.freepascal.org/index.php/topic,18023.msg101017.html#msg101017

http://lazarus.freepascal.org/index.php/topic,12086.msg81434.html#msg81434

Lazarus

  • New Member
  • *
  • Posts: 26
Re: TRegistry.CreateKey() does not work - neither with administratation-rights!
« Reply #2 on: September 18, 2012, 04:25:04 pm »
I've even searched using Google and it doesn't gives me useable results...
And your linked threads do not handle with my described problem... I'm on 32bit, if this helps.

Greetings,
bastla

Knipfty

  • Full Member
  • ***
  • Posts: 232
Re: TRegistry.CreateKey() does not work - neither with administratation-rights!
« Reply #3 on: September 18, 2012, 04:42:05 pm »
Try this search in google

stite:http://lazarus.freepascal.org registry

A number of links appear.
64-bit Lazarus 2.2.0 FPC 3.2.2, 64-bit Win 11

Lazarus

  • New Member
  • *
  • Posts: 26
Re: TRegistry.CreateKey() does not work - neither with administratation-rights!
« Reply #4 on: September 18, 2012, 05:09:11 pm »
Even this Google-Search doesn't give me results concerning my problem  %) :o
I can't create a new key in HKEY_LOCAL_MACHINE using TRegistry.CreateKey() neither with admin-rights  >:(

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: TRegistry.CreateKey() does not work - neither with administratation-rights!
« Reply #5 on: September 18, 2012, 06:25:46 pm »
Check the return value of OpenKey, if false, try displaying SysErrorMessage(GetLastOSError).

Lazarus

  • New Member
  • *
  • Posts: 26
Re: TRegistry.CreateKey() does not work - neither with administratation-rights!
« Reply #6 on: September 18, 2012, 06:42:13 pm »
Thank you for the tip, but this displays no message.
The error is defently in this line:
Code: [Select]
if not Registry.KeyExists('StorageDevicePolicies') then Registry.CreateKey('StorageDevicePolicies');
Greetings,
bastla

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: TRegistry.CreateKey() does not work - neither with administratation-rights!
« Reply #7 on: September 18, 2012, 06:47:45 pm »
Some ideas:
- provide a sample project that demonstrates your problem. If it is a bug, upload it to the bugtracker for the developers to look at.
- if you haven't already, mention Lazarus+FPC version+operating system+version
- run an FPC debug build and step through the code to see where the problem occurs
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Lazarus

  • New Member
  • *
  • Posts: 26
Re: TRegistry.CreateKey() does not work - neither with administratation-rights!
« Reply #8 on: September 18, 2012, 07:26:56 pm »
Hi BigChimp,

thank you for you tips. Here's what've done:

I've attachted a zip-File wich contains a DemoProject of my project:
In the Form's OnCreate-Event, the programs checks wether exists the given Registry-Key, if not it should create it.
That are these lines:
Code: [Select]
Registry.OpenKey('\CurrentControlSet\Control', true);
// the error occurs in this line:
if not Registry.KeyExists('StorageDevicePolicies') then Registry.CreateKey('StorageDevicePolicies');
Registry.OpenKey('\StorageDevicePolicies', true);

As far as I know, this one line should do the same, but it doesn't:
Code: [Select]
Registry.OpenKey('\CurrentControlSet\Control\StorageDevicePolicies', true);
The error occurs in the marked line.

Important: To edit the Windows Registry, administration-rights are required! So you've to start the compiled project in name of administrator!

My Lazarus Version is 0.9.30 with FPC Version 2.4.2 inside.
I'm using Windows 7 32bit

Greetings,
bastla
« Last Edit: September 18, 2012, 07:29:47 pm by bastla »

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: TRegistry.CreateKey() does not work - neither with administratation-rights!
« Reply #9 on: September 18, 2012, 07:28:39 pm »
I would suggest you upgrade to Lazarus 1.0 (with FPC 2.6.0). It has many bug fixes.
Edit: I don't see any attachment??
« Last Edit: September 18, 2012, 07:30:24 pm by BigChimp »
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Lazarus

  • New Member
  • *
  • Posts: 26
Re: TRegistry.CreateKey() does not work - neither with administratation-rights!
« Reply #10 on: September 18, 2012, 07:31:55 pm »
OK, thank you!
I'll do that :)!

I'll post the result :D

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: TRegistry.CreateKey() does not work - neither with administratation-rights!
« Reply #11 on: September 18, 2012, 07:34:55 pm »
Why would this:
Code: [Select]
OpenKey('\StorageDevicePolicies', true);work? Shouldn't you give the path including CurrentControlSet as in the first example?
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: TRegistry.CreateKey() does not work - neither with administratation-rights!
« Reply #12 on: September 18, 2012, 07:36:11 pm »
Going to test with this adapted test code:
Code: [Select]
 
  if (Registry.OpenKey('\CurrentControlSet\Control', true)) then showmessage('First openkey worked.');
  // the error occurs in this line:
  if not Registry.KeyExists('StorageDevicePolicies') then
    if (Registry.CreateKey('StorageDevicePolicies')) then showmessage('Createkey worked.');
  if (Registry.OpenKey('\StorageDevicePolicies', true)) then showmessage('Second openkey worked.');
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Lazarus

  • New Member
  • *
  • Posts: 26
Re: TRegistry.CreateKey() does not work - neither with administratation-rights!
« Reply #13 on: September 18, 2012, 07:37:29 pm »
I think the key to create has just to be relative to already opended one...

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: TRegistry.CreateKey() does not work - neither with administratation-rights!
« Reply #14 on: September 18, 2012, 07:39:42 pm »
Well.. I don't know. Have never used this.

Also:
Code: [Select]
  Registry.RootKey := HKEY_LOCAL_MACHINE;

  if (Registry.OpenKey('\CurrentControlSet\Control', true)) then showmessage('First openkey worked.');
On both windows 2000 and Windows vista, the registry goes HKLM\SYSTEM\CurrentControlSet
Are you missing system in your path?
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

 

TinyPortal © 2005-2018