title: Add explorer context menu item for PDF files from delphi
body: <p>I have my application written in Delphi XE that works with PDF files. Applicaiton is Win32. On start I would like to ensure that there is my item in explorer context menu for PDF files. I would like to be able to specify whether it should be added for active user only or for all users (with UAC I will need to restart with Admin privileges but thats ok). </p>
  
  <p>I started with <a href="https://stackoverflow.com/questions/6285791/how-to-associate-a-delphi-program-with-a-file-type-but-only-for-the-current-use/6285850#6285850">How to associate a Delphi program with a file type, but only for the current user?</a> and <a href="https://stackoverflow.com/questions/3797817/how-to-add-item-to-windows-explorer-content-menu-in-delphi">How to add item to windows explorer content menu in delphi?</a> . I tested it with manual registry editing via regedit and it worked fine for "new" extensions. But for .pdf it is more complicated as it will be most probably already present in the registry. </p>
  
  <p>On my PC the .pdf key is referencing AcroExch.Document . But adding shell/something subkey to the AcroExch.Document key is not working because it has CurVer subkey referencing to AcroExch.Document.7. However another PC with another verison of Acrobat had this names a little different. It is no problem for me to follow the CurVer reference but is that a correct approach? And what about situation where no PDF reader is installed, how should I name my keys so Acrobat won't overwrite them when installed?</p>
  
  <p>But more pressing matter is in which root should I put my keys? <a href="https://stackoverflow.com/questions/6285791/how-to-associate-a-delphi-program-with-a-file-type-but-only-for-the-current-use/6285850#6285850">How to associate a Delphi program with a file type, but only for the current user?</a> is mentioning HKLM (Local Machine) and HKCU (Current user). Its seems rather straightforward but I am unable to set values in HKLM from Delphi. Strangely I can create keys:</p>
  
  <pre><code>var reg:TRegistry;
  
  key := '\Software\Classes\'+keyname+'\shell\'+name+'\command';
  reg.CreateKey(key);
  </code></pre>
  
  <p>but I am getting Access Denied when trying to write the actual value:</p>
  
  <pre><code>reg.OpenKey(key,false);
  reg.WriteString('',command);
  </code></pre>
  
  <p>I am getting the same Access Denied exception even on WinXP, no matter if the applicaiton is running as Admin (Win7), I even tried to set permissions (Everyone full control) for the key via regedit (I can edit the value via regedit without problems). I tried creating the registry with different access modes, all with no luck:</p>
  
  <pre><code>reg := TRegistry.Create(KEY_WRITE or KEY_WOW64_64KEY);
  reg := TRegistry.Create(KEY_ALL_ACCESS or KEY_WOW64_64KEY);
  reg.Access := KEY_ALL_ACCESS;
  reg.Access := KEY_WRITE or KEY_WOW64_64KEY;
  reg.Access := KEY_ALL_ACCESS or KEY_WOW64_64KEY;
  </code></pre>
  
  <p>With HKCU everything works fine. </p>
  
  <p>So I tried writing into HKEY_CLASSES_ROOT and it works and actually puts the keys exactly where I want (into HKLM) if running as Admin. But according to <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms724475.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/windows/desktop/ms724475.aspx</a> </p>
  
  <blockquote>
    <p>The HKEY_CLASSES_ROOT (HKCR) key contains file name extension associations and COM class registration information such as ProgIDs, CLSIDs, and IIDs. It is primarily intended for compatibility with the registry in 16-bit Windows.</p>
  </blockquote>
  
  <p>I do not like the note about the primary purpose being compatibile with 16-bit Windows. And the actual conditions where the changes will be written is more complicated than I would like. </p>
  
  <p>So basically I have these questions:</p>
  
  <ul>
  <li><p>What is the advantage of using AcroExch.Document and CurVer instead of pointing directly to AcroExch.Document.7? And what are the "best manners" when adding my keys into this structure? What about the case when the .pdf is not yet associated with anything?</p></li>
  <li><p>Where should I put my keys and why I am not able to write into HKLM?</p></li>
  </ul>
  
  <p>Edit:
  The problem with Access Denied when writing to HKLM was caused by my error. I did use in previous code openKeyReadOnly and I did not notice that it will swtich the Access property to readonly for all subsequent calls.</p>