0

How do I check if my C++ program is running with admin privileges?

I did it in this manner for batch

set fold=%random%

mkdir "C:\Windows\%fold%"

if errorlevel 1 (

goto Tag1

)

goto Tag2

But I cannot use the same thing for C++ because I do not know how to transfer value of %random% that is variable 1 to variable 2 that is %fold% and also, I do not know if there is error level for C++.

Can anyone help me in this case or is there any way to check if my program is running with admin privileges?

Yurii
  • 4,811
  • 7
  • 32
  • 41
Dragon121
  • 25
  • 1
  • 1
  • 3
  • Have a look at this. C# I know but the principles are the same:http://stackoverflow.com/questions/509292/how-can-i-tell-if-my-process-is-running-as-administrator – Bathsheba Oct 07 '14 at 07:16
  • Your batch file is awful. Spraying folders into the system directories. Yuch! Don't do it that way!! – David Heffernan Oct 07 '14 at 08:53
  • We also have http://stackoverflow.com/questions/8046097/how-to-check-if-a-process-has-the-administrative-rights and http://stackoverflow.com/questions/3546643/how-do-i-check-if-my-program-is-ran-by-user-as-administrator-vista-win7-c and http://stackoverflow.com/questions/981171/check-if-process-user-is-an-administrator-c and hundreds more. Please don't treat Stack Overflow as a replacement for web search. Always search first. – David Heffernan Oct 07 '14 at 08:55
  • It's just for testing. If not elevated, no folder will be created. If elevated, later the created folder will be deleted. @DavidHeffernan – Dragon121 Oct 07 '14 at 09:51

2 Answers2

5

You can use the OpenProcessToken / GetTokenInformation pair: https://stackoverflow.com/a/8196291/3235496

An alternative is the AccessCheck function.

Last the IsUserAnAdmin function: it's simple but deprecated (available from Windows XP/Windows Server 2003).

Anyway why are you checking? Trying could be a good strategy: if it works, you have sufficient rights (possibly a subset of Admin rights).

PS

Just out of curiosity... the C++ translation of your batch file should be based on the CreateDirectory function. If it fails check the extended error information via GetLastError (return code ERROR_ACCESS_DENIED). But, as David Heffernan says, spraying folders into the system directories isn't a great idea.

Community
  • 1
  • 1
manlio
  • 18,345
  • 14
  • 76
  • 126
  • It's just for testing. If not elevated, no folder will be created. If elevated, later the created folder will be deleted. – Dragon121 Oct 07 '14 at 09:48
  • Why are you saying that **IsUserAnAdmin()** is deprecated ? I didn't find a confirmation of such deprecation. The doc describe it as a "wrapper for CheckTokenMembership." – Bemipefe Aug 02 '19 at 10:25
  • @Bemipefe It's in the Deprecated Shell APIs section of https://learn.microsoft.com/en-us/windows/win32/shell/deprecated-api. It could be superseded by newer APIs (*`IsUserAnAdmin` is available for use in the operating systems specified in the Requirements section. **It may be altered or unavailable in subsequent versions**.*). – manlio Aug 02 '19 at 12:38
-1

You find the answer in the MSDN:

https://support.microsoft.com/kb/118626

Also your application should have a manifest.

xMRi
  • 14,982
  • 3
  • 26
  • 59