Mark Williams 14 Posted January 13, 2021 Is there a simple way to check if my app is running elevated? I can see a few oldish posts on this on Google, but nothing recent. I was just wondering if newer Delphi has introduced a simple function for it, although I couldn't find anything in help or likely units. Or perhaps there is a new Windows API procedure? Share this post Link to post
Anders Melander 1784 Posted January 14, 2021 3 hours ago, Mark Williams said: I can see a few oldish posts on this on Google, but nothing recent. I was just wondering if newer Delphi has introduced a simple function for it, although I couldn't find anything in help or likely units. Or perhaps there is a new Windows API procedure? The ones you found probably does something like this, so I guess that's oldish too : // ----------------------------------------------------------------------------- // // RunningAsAdmin // // ----------------------------------------------------------------------------- // Detect if we're running at an elevated security level. // ----------------------------------------------------------------------------- function RunningAsAdmin: boolean; var hToken, hProcess: THandle; pTokenInformation: pointer; ReturnLength: DWord; TokenInformation: TTokenElevation; begin Result := False; hProcess := GetCurrentProcess; try if OpenProcessToken(hProcess, TOKEN_QUERY, hToken) then try FillChar(TokenInformation, SizeOf(TokenInformation), 0); pTokenInformation := @TokenInformation; GetTokenInformation(hToken, TokenElevation, pTokenInformation, SizeOf(TokenInformation), ReturnLength); Result := (TokenInformation.TokenIsElevated <> 0); finally CloseHandle(hToken); end; except // Ignore error - although none of the above should throw an exception... end; end; 1 Share this post Link to post
Guest Posted January 14, 2021 (edited) Here by Craig Chapman a code to Create Elevate Level as Admin https://chapmanworld.com/2015/06/08/elevated-privileges-for-delphi-applications/ Here, by Alex a code to create your verification https://stackoverflow.com/questions/923350/delphi-prompt-for-uac-elevation-when-needed Here, by Ruben Bartelink same question - try this https://stackoverflow.com/questions/1220213/detect-if-running-as-administrator-with-or-without-elevated-privileges/4497572#4497572 Edited January 14, 2021 by Guest Share this post Link to post
Lars Fosdal 1792 Posted January 14, 2021 If you want it to always run elevated, you can specify that in the manifest. 1 Share this post Link to post
Mark Williams 14 Posted January 14, 2021 6 minutes ago, Lars Fosdal said: If you want it to always run elevated, you can specify that in the manifest. No. I just need to know if user has run my app in elevated mode. My App launches Outlook and if one is running elevated and the other isn't then you get an OLE error. I just want to be able to advise user which is doing which so they can correct. Share this post Link to post
KodeZwerg 54 Posted March 4, 2021 On 1/14/2021 at 1:49 PM, Mark Williams said: No. I just need to know if user has run my app in elevated mode. My App launches Outlook and if one is running elevated and the other isn't then you get an OLE error. I just want to be able to advise user which is doing which so they can correct. RunElevatedSupport.pas is what I use. Call IsElevated (Boolean) thats all you need to do. Also offers to elevate code pieces. I love it. 2 Share this post Link to post
Mark Williams 14 Posted March 4, 2021 9 minutes ago, KodeZwerg said: RunElevatedSupport.pas is what I use. Thanks. I'll check it out Share this post Link to post
KodeZwerg 54 Posted March 4, 2021 49 minutes ago, Mark Williams said: Thanks. I'll check it out You might love the ease of use aswell. Never had any kind of problems so far. Only limitation is when you execute elevated code. That depend on your code. To write files or registry data it worked flawless for me so far. Enjoy it 🙂 Share this post Link to post