You need admin privileges (elevated) to write to HKLM. There're three ways to run elevated in Windows:
1) Be launched by an elevated process.
2) Be an executable with a manifest that says that it must run elevated, in which case Windows will prompt for elevation first before launching the process.
3) Run a command requesting elevation, which is what happens when you do Run as Administrator on something.
You can see that you cannot elevate a process in the middle of execution.
There're other registry locations, such as HKEY_CURRENT_USER that don't require elevation, they are per-user.
If this doesn't answer your question, give us some more detail on what you're trying to accomplish.