TRegistry.CreateKey() does not work - neither with administratation- rights! lazarus / 31-LCL ---------------------------------------------------------------------- Lazarus · 2012-09-18 13:43 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  ! 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 ---------------------------------------------------------------------- IPguy · 2012-09-18 14:08 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#msg 101017 http://lazarus.freepascal.org/index.php/topic,12086.msg81434.html#msg8 1434 ---------------------------------------------------------------------- Lazarus · 2012-09-18 14:25 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 · 2012-09-18 14:42 Try this search in google stite:http://lazarus.freepascal.org registry A number of links appear. ---------------------------------------------------------------------- Lazarus · 2012-09-18 15:09 Even this Google-Search doesn't give me results concerning my problem  I can't create a new key in HKEY_LOCAL_MACHINE using TRegistry.CreateKey() neither with admin-rights ---------------------------------------------------------------------- Leledumbo · 2012-09-18 16:25 Check the return value of OpenKey, if false, try displaying SysErrorMessage(GetLastOSError). ---------------------------------------------------------------------- Lazarus · 2012-09-18 16:42 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 · 2012-09-18 16:47 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 ---------------------------------------------------------------------- Lazarus · 2012-09-18 17:26 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\StorageDevi cePolicies', 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 ---------------------------------------------------------------------- BigChimp · 2012-09-18 17:28 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?? ---------------------------------------------------------------------- Lazarus · 2012-09-18 17:31 OK, thank you! I'll do that ! I'll post the result ---------------------------------------------------------------------- BigChimp · 2012-09-18 17:34 Why would this: Code: [Select]OpenKey('\StorageDevicePolicies', true);work? Shouldn't you give the path including CurrentControlSet as in the first example? ---------------------------------------------------------------------- BigChimp · 2012-09-18 17:36 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.'); ---------------------------------------------------------------------- Lazarus · 2012-09-18 17:37 I think the key to create has just to be relative to already opended one... ---------------------------------------------------------------------- BigChimp · 2012-09-18 17:39 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? ---------------------------------------------------------------------- BigChimp · 2012-09-18 17:42 Testing gives QuoteFailed to create key "StorageDevicePolicies", no further error messages.. In my test, this code is before the createkey part: Code: [Select]if (Registry.OpenKey('\CurrentControlSet\Control', true)) then showmessage('First openkey worked.');so OpenKey fails as well. I'd look into the required syntax and paths for openkey first. Perhaps a forum search here will give some results. ---------------------------------------------------------------------- Lazarus · 2012-09-18 17:43 Oh yes, you're right! A simple mistake  I forgot the "system" in my path  so embarrasing! And I thought, Lazarus failed.... Thank you very much, BigChimp! Greetings, bastla ---------------------------------------------------------------------- BigChimp · 2012-09-18 17:44 No problem, I'm also very good at making simple mistakes (However, showing more source code or a small test project can work wonders in these cases as you may be making errors in parts you don't expect...) ---------------------------------------------------------------------- Lazarus · 2012-09-18 17:52 Four eyes see defenitely more than only two And you're right: The mistakes are located where nobody thought of it   ...