How to write a registry key in Windows with elevated privilegies?

90 views
Skip to first unread message

Davide Perini

unread,
Jun 21, 2023, 12:10:08 PM6/21/23
to Java Native Access
Hi,
I would like a registry key in Windows.

This commands
Advapi32Util.registryCreateKey(WinReg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\MyApp.exe\\PerfOptions");

works well if I run my Java app elevated,
if I run the app non elevated I get an access denied error.

Is there a way to elevate my App using JNA just for that specific command at runtime?

If not, is there a way to "restart the app" elevated?

Thanks
Davide


Tres Finocchiaro

unread,
Jun 21, 2023, 2:03:21 PM6/21/23
to jna-...@googlegroups.com
The only way I've been able to elevate reliability in Java is by using Shell.execute() against PowerShell.   Of course, at that point, you might as well use PowerShell for the registry commands as well.

{"Start-Process", "powershell.exe", "-ArgumentList", args, "-Wait", "-Verb", "RunAs"};

... where "args" is your "reg write", etc.

This would mean that the final solution doesn't use JNA at all.

If instead, you'd like to use Win32 APIs to relaunch the Java application as administrator, I'm sure there's a way to do that too.

Tres Finocchiaro

unread,
Jun 21, 2023, 2:05:17 PM6/21/23
to jna-...@googlegroups.com

... Oh, and this solution is starting a PowerShell process, from a PowerShell process.   It's redundant, but it's the quickest way I know how.

Tres Finocchiaro

unread,
Jun 21, 2023, 2:05:52 PM6/21/23
to jna-...@googlegroups.com

Davide Perini

unread,
Jun 21, 2023, 2:14:10 PM6/21/23
to jna-...@googlegroups.com
I really, really appreciate your help Tres, you are very kind and you always have an answer :)
It did the trick!

Thanks!



Il 21/06/2023 20:05, Tres Finocchiaro ha scritto:
--
You received this message because you are subscribed to the Google Groups "Java Native Access" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jna-users/CANQs7dApjywQXtrsLGGHyW%3DqT1_gnL_HufP0%3Dj5ZCubEKtL91Q%40mail.gmail.com.

Reply all
Reply to author
Forward
0 new messages