stdcpp - how to detect if run as Administrator?

Monkey Forums/Monkey Programming/stdcpp - how to detect if run as Administrator?

Shagwana(Posted 2013) [#1]
I have a little issue at the moment, consider the following code..

#If TARGET<>"stdcpp"
#Error "Compile Error: C++ target only supported"
#Endif

Strict

Import brl.filestream
Import os

Function Main:Int()
	Print "*** started ***"
	
	Local f:=FileStream.Open("C:\test_abc.txt","w")
	
	If f<>Null
		Print "File is open, writing data"
		f.WriteByte(78) 'N
		f.Close()
	Endif

	Print "*** finished ***"
    Return 0
End


Depending if the above is run as an Administrator or Not on Windows 7 depends on the location of the file that will be created.

File created in '%APPDATA%\..\Local\VirtualStore' when not run as administrator

File created in correct location when run as administrator!

This all comes down to UAC, and the rights that Monkey is run in. If I run Monkey as an Administrator it works however if I dont then the problem occurs (file generated in wrong location).

What I am after is a way to detect if the program is being run as an Administrator so I may throw a prompt up stating that its not!, Any ideas?


ziggy(Posted 2013) [#2]
It will work the same with UAC on or OFF. UAC only prevents to write INTO the program files folder (or the windows folder). If you write directly to the disk root folder, it won't affect you.
However, it is a good idea to use system folders for what they're intended to contain. that's the easiest way to get rid of this usability issues on any OS.


Shagwana(Posted 2013) [#3]
Perhaps I got a little mixed up with UAC & Administrator rights however what I am trying to say is the above code only works as expected when its run as an Administrator.

It is not writing to 'Program Files' or 'Windows' so I dont know what is going on.

As this is intended for personnel use (on windows only), I was just trying to whip up a little tool to generate a text file on my own C: drive.


DruggedBunny(Posted 2013) [#4]
I might have an answer to this but will have to fiddle later -- I can't remember if we can just use Win32 stuff direct from Monkey and have to go out right now!


Shagwana(Posted 2013) [#5]
Yes, out in the sunshine a very rare thing in the UK


Skn3(Posted 2013) [#6]
Just a hint: you can use win32 stuff as long as you import the correct winapi libs/headers in your native code.

If only computer monitors worked in the sun!


DruggedBunny(Posted 2013) [#7]
I'm not particularly handy with native code! Anyone fancy native-izing this PB code? It's only three functions and a struct.

I've tested it on my normal account (admin) and a non-admin account and it worked. (Win7.) I don't use UAC but I believe this should do the job anyway...


; Required Structure...

Structure TOKEN_ELEVATION
	TokenIsElevated.l
EndStructure

#TOKENELEVATION = 20 ; Constant

; #TOKEN_READ = 131080 ; Already defined in PB!
; Note: in PB the @ symbol means pointer to variable,
; also, some variables are declared on-the-fly here:

If OpenProcessToken_ (GetCurrentProcess_ (), #TOKEN_READ, @token)

	tokeninfo.TOKEN_ELEVATION ; Type declaration
	
	If GetTokenInformation_ (token, #TOKENELEVATION, @tokeninfo, SizeOf (tokeninfo), @buffersize)
		Debug "Running with elevated permissions"
	Else
		Debug "Not running with elevated permissions"
	EndIf

EndIf



Also, I didn't go out in the sun by choice! It burns!


Skn3(Posted 2013) [#8]
This should do it?

checkaccess.monkey


checkaccess.cpp



DruggedBunny(Posted 2013) [#9]
No worky here! I get:


main.cpp: In function 'bool CheckHasAdminAccess()':
main.cpp:1504:13: error: 'TOKEN_ELEVATION' was not declared in this scope
main.cpp:1504:29: error: expected ';' before 'elevation'
main.cpp:1506:42: error: 'TokenElevation' was not declared in this scope
main.cpp:1506:58: error: 'elevation' was not declared in this scope



If I hack out the actual elevation stuff it builds, but I can't actually tell what's wrong in there...

        if(OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,&token)) {
            //TOKEN_ELEVATION elevation;
            //DWORD size = sizeof(TOKEN_ELEVATION);
            //if(GetTokenInformation(token,TokenElevation,&elevation,sizeof(elevation),&size)) {
            //    result = elevation.TokenIsElevated;
            //}
        }



Skn3(Posted 2013) [#10]
Oh whoops I only tested it with glfw which is where windows.h is included.

I'm not at pc at mo but will update the snippet tomorrow to make it work on stdcpp target.


Skn3(Posted 2013) [#11]
ok so I updated it to work with cpptool.

Not sure why it wasn't picking up the structures defined in windows.h (for cpp tool target), perhaps it was using the incorrect version of the windows headers?

I just redefined them in the code so there is no messing about.


Shagwana(Posted 2013) [#12]
Ta, that looks like what little old me needs


DruggedBunny(Posted 2013) [#13]

Not sure why it wasn't picking up the structures defined in windows.h


Yeah, I checked and found they were defined so that was me stuck... handy to have a simple template for interfacing with native code now though!