Announcement

Collapse
No announcement yet.

How to find out if user runs as administrator

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to find out if user runs as administrator

    Wrote a liitle application which works great with XP.
    I use a checkbox to install the program to c:\programs\..., just a copy-installation.
    As a normal user with VISTA you are not allowed to write anything to c:\programs.
    Of course i can include a manifest to make sure the program runs only with administrative rights, but for normal operation a normal user has enough rights.

    Now my thought is: i would disable the installation checkbox, if the program runs with normal user rights, when started as administrator the checkbox will be enabled. But how can i determine at runtime if the current user has administrative rights?
    I'am still confused...but on a higher level.

  • #2
    What's about this one, posted by Semen some years ago?

    User to user discussions about the PB/Win (formerly PB/DLL) product line. Discussion topics include PowerBASIC Forms, PowerGEN and PowerTree for Windows.


    Regards, Hanns.

    Comment


    • #3
      Yes it works with XP, but only partially with VISTA. Even if a user is member of the local administrators Semens code only determines a normal user.
      I have googled this issue and found Junfeng Zhang's notes:
      In Vista, even if the user is in administrators group, the OS will create a filtered user token. The full token is linked to the filtered token, and can be retrieved with API GetTokenInformation with the new TokenLinkedToken information type.
      This is a beginning, thanks. Will try with this code and tell you results later...
      I'am still confused...but on a higher level.

      Comment


      • #4
        Possible code to test registry access

        Hello Michael,

        What I always do to test if a user has allround registry access, is the following. Try to open with %KEY_ALL_ACCESS an "administrator key", for instances in %HKEY_CLASSES_ROOT. Or try one from inside your own proggie if you are 100 percent sure that admin-rights are necessary. Intercept %ERROR_ACCESS_DENIED and display a messagebox.

        If the result of the function is %ERROR_SUCCES (= 0; Microsoft logic?) either close the key immediately or, if it is the "real" key, process the key according to your code. Here follows a piece of code that maybe useful (not tested under Vista!). It's granted to the public domain on February 6, 2008.
        Code:
        #COMPILE EXE
        #DIM ALL
        
        #INCLUDE "WIN32API.INC"                                        ' Last Update: 27 January 2005
        
        '********************************************************************************************
        '* FUNCTION TestRegistryAccess                                                              *
        '* ===========================                                                              *
        '* This function tests whether a user has sufficient registry access or not.                *
        '* In order to get access to %HKEY_CLASSES_ROOT, one must be logged on as an administrator. *
        '* The class ID used here is the one for the recycle bin.                                   *
        '* Alternately you may want to do this test using one of the keys your program              *
        '*                                                               really needs to open.      *
        '********************************************************************************************
        FUNCTION TestRegistryAccess() AS LONG
          LOCAL hKey AS DWORD, iRet AS LONG
          LOCAL szKey AS ASCIIZ * %MAX_PATH
        
          szKey = "CLSID\{645FF040-5081-101B-9F08-00AA002F954E}"
          iRet = RegOpenKeyEx(%HKEY_CLASSES_ROOT, szKey, %NULL, %KEY_ALL_ACCESS, hKey)
          IF ISTRUE(iRet) THEN
            FUNCTION = iRet
          ELSE
            RegCloseKey hKey
            FUNCTION = %ERROR_SUCCESS
          END IF
        END FUNCTION
        
        FUNCTION PBMAIN () AS LONG
          LOCAL iRet AS LONG, sMessage AS STRING
          iRet = TestRegistryAccess
          SELECT CASE AS LONG iRet
            CASE %ERROR_ACCESS_DENIED
              sMessage = " No registry access!" & $CRLF & _
                         " In order to proceed, reboot your system" & $CRLF & _
                         " and log on as an administrator."
              MSGBOX sMessage, %MB_OK OR %MB_ICONEXCLAMATION OR %MB_TASKMODAL, " ERROR!"
              EXIT FUNCTION
        
            CASE %ERROR_SUCCESS
              ' no error, so no exit
        
            CASE ELSE
              MSGBOX "An unknown error occurred. Try again later.", %MB_OK OR %MB_ICONEXCLAMATION OR %MB_TASKMODAL, " ERROR!"
              EXIT FUNCTION
          END SELECT
        
          ' proceed your code here
        END FUNCTION
        Last edited by Egbert Zijlema; 6 Feb 2008, 06:21 AM.

        Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
        http://zijlema.basicguru.eu
        *** Opinions expressed here are not necessarily untrue ***

        Comment


        • #5
          Neils code with Semen's addition is working properly for me (within NT-domain) even with Vista as client and more than one user logged on.
          Egberts idea plain simple, but i prefer the first solution.
          Thanks.
          I'am still confused...but on a higher level.

          Comment


          • #6
            > use a checkbox to install the program to c:\programs\..., just a copy-installation.

            Or, you could use an installer program (eg Inno setup http://www.jrsoftware.org) .

            All the decent installer software handles all that elevation stuff, PLUS provide a clean "UN-install".

            It's a thought...

            MCM
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              Of course i could use Jordan's Innosetup or NSIS or InstallShield, but the the point is:
              Now i have a program, round about 200 kB, which i can run from USB stick and if i like install to the PC. All with one file, no separate installer neccessary.
              => that's really simple
              I'am still confused...but on a higher level.

              Comment


              • #8
                I'm afraid it does not work for me (Egbert's code). I'm running as administrator in Windows 7 Ultimate 32-bit.
                Even with elevation by means of a manifest, it does not work either.
                Francisco J Castanedo
                Software Developer
                Distribuidora 3HP, C.A.
                [URL]http://www.distribuidora3hp.com[/URL]

                Comment


                • #9
                  Semen's solution works like a charm in XP Sp2, Vista y 7!!!
                  Francisco J Castanedo
                  Software Developer
                  Distribuidora 3HP, C.A.
                  [URL]http://www.distribuidora3hp.com[/URL]

                  Comment

                  Working...
                  X