Announcement

Collapse
No announcement yet.

C++ to PowerBASIC conversion - A little help.

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

  • C++ to PowerBASIC conversion - A little help.

    I have a C++ routine that I was wondering if someone has converted to PowerBASIC. It detects if the user has run your app "As Administrator". Its a good check to perform and exit your app if "Run As Administrator" is required.

    Anyone have a PB equivalent, or want to convert this one?

    Code:
    bool IsUserAdmin ()
    {
        struct Data
        {
            PACL   pACL;
            PSID   psidAdmin;
            HANDLE hToken;
            HANDLE hImpersonationToken;
            PSECURITY_DESCRIPTOR     psdAdmin;
            Data() : pACL(NULL), psidAdmin(NULL), hToken(NULL),
                hImpersonationToken(NULL), psdAdmin(NULL)
            {}
            ~Data()
            {
                if (pACL) 
                    LocalFree(pACL);
                if (psdAdmin) 
                    LocalFree(psdAdmin);
                if (psidAdmin) 
                    FreeSid(psidAdmin);
                if (hImpersonationToken) 
                    CloseHandle (hImpersonationToken);
                if (hToken) 
                    CloseHandle (hToken);
            }
        } data;
    
    	BOOL   fReturn = FALSE;
    	DWORD  dwStatus;
    	DWORD  dwAccessMask;
    	DWORD  dwAccessDesired;
    	DWORD  dwACLSize;
    	DWORD  dwStructureSize = sizeof(PRIVILEGE_SET);
    
    	PRIVILEGE_SET   ps;
    	GENERIC_MAPPING GenericMapping;
    	SID_IDENTIFIER_AUTHORITY SystemSidAuthority = SECURITY_NT_AUTHORITY;
    
    	const DWORD ACCESS_READ  = 1;
    	const DWORD ACCESS_WRITE = 2;
    
    	if (!OpenThreadToken (GetCurrentThread(), TOKEN_DUPLICATE|TOKEN_QUERY, TRUE, &data.hToken))
    	{
    		if (GetLastError() != ERROR_NO_TOKEN)
    			return false;
    
    		if (!OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &data.hToken))
    			return false;
    	}
    
    	if (!DuplicateToken (data.hToken, SecurityImpersonation, &data.hImpersonationToken))
    		return false;
    
    	if (!AllocateAndInitializeSid(&SystemSidAuthority, 2,
    								SECURITY_BUILTIN_DOMAIN_RID,
    								DOMAIN_ALIAS_RID_ADMINS,
    								0, 0, 0, 0, 0, 0, &data.psidAdmin))
    		return false;
    
    	data.psdAdmin = LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
    	if (data.psdAdmin == NULL)
    		return false;
    
    	if (!InitializeSecurityDescriptor(data.psdAdmin, SECURITY_DESCRIPTOR_REVISION))
    		return false;
    
    	// Compute size needed for the ACL.
    	dwACLSize = sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(data.psidAdmin) - sizeof(DWORD);
    
    	data.pACL = (PACL)LocalAlloc(LPTR, dwACLSize);
    	if (data.pACL == NULL)
    		return false;
    
    	if (!InitializeAcl(data.pACL, dwACLSize, ACL_REVISION2))
    		return false;
    
    	dwAccessMask = ACCESS_READ | ACCESS_WRITE;
    
    	if (!AddAccessAllowedAce(data.pACL, ACL_REVISION2, dwAccessMask, data.psidAdmin))
    		return false;
    
    	if (!SetSecurityDescriptorDacl(data.psdAdmin, TRUE, data.pACL, FALSE))
    		return false;
    
    	// AccessCheck validates a security descriptor somewhat; set the group
    	// and owner so that enough of the security descriptor is filled out 
    	// to make AccessCheck happy.
    	SetSecurityDescriptorGroup(data.psdAdmin, data.psidAdmin, FALSE);
    	SetSecurityDescriptorOwner(data.psdAdmin, data.psidAdmin, FALSE);
    
    	if (!IsValidSecurityDescriptor(data.psdAdmin))
    		return false;
    
    	dwAccessDesired = ACCESS_READ;
    
    	GenericMapping.GenericRead    = ACCESS_READ;
    	GenericMapping.GenericWrite   = ACCESS_WRITE;
    	GenericMapping.GenericExecute = 0;
    	GenericMapping.GenericAll     = ACCESS_READ | ACCESS_WRITE;
    
    	if (!AccessCheck(data.psdAdmin, data.hImpersonationToken, dwAccessDesired,
    					&GenericMapping, &ps, &dwStructureSize, &dwStatus,
    					&fReturn))
    	{
    		return false;
    	}
    
    return fReturn;
    }
    Explorations RPG System
    http://www.explore-rpg.com

  • #2
    I don't understand all of that well enough to translate it - but I did come across this on the forum search that is similar (see last post): https://www.powerbasic.com/support/p...ad.php?p=51006
    Adam Drake
    Drake Software

    Comment


    • #3
      Originally posted by Adam J. Drake View Post
      I did come across this on the forum search that is similar (see last post): https://www.powerbasic.com/support/p...ad.php?p=51006

      There are posts on doing conversions dating back to 2000.

      Mostly it is rearranging the items.

      Comment


      • #4
        Thanks,..

        You guys are great!

        I will check them out, but there is a Kernel IsUserAdmin() functions that does what I needed.
        Explorations RPG System
        http://www.explore-rpg.com

        Comment


        • #5
          Originally posted by Tyrone Lee View Post
          You guys are great!

          I will check them out, but there is a Kernel IsUserAdmin() functions that does what I needed.
          I'm sorting the information into categories, there is about 20 separate post on conversions since 2000.

          Comment


          • #6
            C to PB

            Here is a list from the first 7K post on the forum:

            Porting questions.. (http://www.powerbasic.com/support/pb...ead.php?t=2097)
            Help converting C to PB for a DLL (http://www.powerbasic.com/support/pb...ead.php?t=2225)
            C to PB Conversion (http://www.powerbasic.com/support/pb...ead.php?t=2696)
            HANDLE HWND and others... (http://www.powerbasic.com/support/pb...ead.php?t=2764)
            PB conversion of C/C++ SDK (http://www.powerbasic.com/support/pb...ead.php?t=2927)
            Specific C-porting questions (http://www.powerbasic.com/support/pb...ead.php?t=3489)
            C port of Type structure (http://www.powerbasic.com/support/pb...ead.php?t=3585)
            C to PB conversion problem (http://www.powerbasic.com/support/pb...ead.php?t=3793)
            Help with some C code to PB (http://www.powerbasic.com/support/pb...ead.php?t=3971)
            Converting a C++ statement (http://www.powerbasic.com/support/pb...ead.php?t=4321)
            Accessing C array type from PB-looks easy enough? (http://www.powerbasic.com/support/pb...ead.php?t=4424)
            data conversion from a "C" struct (http://www.powerbasic.com/support/pb...ead.php?t=4675)
            translate C enum to PowerBasic (http://www.powerbasic.com/support/pb...ead.php?t=5001)
            C to PowerBasic translation (http://www.powerbasic.com/support/pb...ead.php?t=5017)
            C Conversion (http://www.powerbasic.com/support/pb...ead.php?t=5371)
            Translation from C++ to PB (http://www.powerbasic.com/support/pb...ead.php?t=5403)
            Would anyone know how to convert this (http://www.powerbasic.com/support/pb...ead.php?t=5416)
            C-2-PB Question Please! (http://www.powerbasic.com/support/pb...ead.php?t=5549)
            C construct help please (http://www.powerbasic.com/support/pb...ead.php?t=6302)

            Comment


            • #7
              There should be about 49 more post about conversion, based upon .3% of 7K post looked at.

              So ya should find what ya need!!!!!

              Comment


              • #8
                Probably too late now, but I think a SIG (special interest group) for "converting from other languages" might have been a nice subforum, huh?
                Michael Mattias
                Tal Systems (retired)
                Port Washington WI USA
                [email protected]
                http://www.talsystems.com

                Comment


                • #9
                  Originally posted by Michael Mattias View Post
                  Probably too late now, but I think a SIG (special interest group) for "converting from other languages" might have been a nice subforum, huh?

                  sure, I've found a lot of links on PowerBasic under addons and in the forums not to be working. After 14 years I can see why some are not working any more.

                  Comment


                  • #10
                    Tyrone,
                    Here is a translation of the posted C code...
                    Hth...

                    Pierre

                    Code:
                     
                    #COMPILE EXE '#Win 8.04#
                    #DIM ALL
                    #INCLUDE "Win32Api.inc"
                     
                    %SECURITY_DESCRIPTOR_MIN_LENGTH = 20
                    %SECURITY_NT_AUTHORITY          = 5
                    %ACCESS_READ                    = 1
                    %ACCESS_WRITE                   = 2
                     
                    TYPE LUID
                      LowPart  AS DWORD
                      HighPart AS LONG
                    END TYPE
                     
                    TYPE LUID_AND_ATTRIBUTES_2
                      pLuid      AS LUID
                      Attributes AS DWORD
                    END TYPE
                     
                    TYPE PRIVILEGE_SET_2
                      PrivilegeCount AS DWORD
                      nControl       AS DWORD
                      Privilege(0)   AS LUID_AND_ATTRIBUTES_2
                    END TYPE
                    '______________________________________________________________________________
                     
                    FUNCTION IsUserAdmin AS LONG
                     LOCAL psdAdmin            AS SECURITY_DESCRIPTOR POINTER
                     LOCAL SystemSidAuthority  AS SID_IDENTIFIER_AUTHORITY
                     LOCAL GenericMapping      AS GENERIC_MAPPING
                     LOCAL ps                  AS PRIVILEGE_SET
                     LOCAL psidAdmin           AS SID POINTER
                     LOCAL pACL                AS ACL POINTER
                     LOCAL hToken              AS DWORD
                     LOCAL hImpersonationToken AS DWORD
                     LOCAL dwStatus            AS DWORD
                     LOCAL dwAccessMask        AS DWORD
                     LOCAL dwAccessDesired     AS DWORD
                     LOCAL dwACLSize           AS DWORD
                     LOCAL dwStructureSize     AS DWORD
                     LOCAL fReturn             AS LONG
                     
                     SystemSidAuthority.Value(5) = %SECURITY_NT_AUTHORITY
                     dwStructureSize             = SIZEOF(PRIVILEGE_SET_2)
                     
                     'Get a token
                     IF OpenThreadToken(GetCurrentThread(), %TOKEN_DUPLICATE OR %TOKEN_QUERY, %TRUE, hToken) THEN 'Try from current thread
                       IF GetLastError() = %ERROR_NO_TOKEN THEN 'Make sur token is valid
                         hToken = 0
                       END IF
                     ELSE 'Try from current process
                       IF OpenProcessToken(GetCurrentProcess(), %TOKEN_DUPLICATE OR %TOKEN_QUERY, hToken) = %FALSE THEN
                         hToken = 0
                       END IF
                     END IF
                     
                     IF hToken THEN
                       IF DuplicateToken(hToken, BYVAL %SecurityImpersonation, hImpersonationToken) THEN
                         IF AllocateAndInitializeSid(SystemSidAuthority, 2, %SECURITY_BUILTIN_DOMAIN_RID, _
                                                    %DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, psidAdmin) THEN
                           psdAdmin = LocalAlloc(%LPTR, %SECURITY_DESCRIPTOR_MIN_LENGTH)
                           IF psdAdmin THEN
                             IF InitializeSecurityDescriptor(@psdAdmin, %SECURITY_DESCRIPTOR_REVISION) THEN
                               dwACLSize = SIZEOF(ACL) + SIZEOF(ACCESS_ALLOWED_ACE) + GetLengthSid(BYVAL psidAdmin) - 4 '4 = SIZEOF(DWORD)
                               pACL = LocalAlloc(%LPTR, dwACLSize)
                               IF pACL THEN
                                 IF InitializeAcl(@pACL, dwACLSize, %ACL_REVISION2) THEN
                                   dwAccessMask = %ACCESS_READ OR %ACCESS_WRITE
                                   IF AddAccessAllowedAce(@pACL, %ACL_REVISION2, dwAccessMask, BYVAL psidAdmin) THEN
                                     IF SetSecurityDescriptorDacl(@psdAdmin, %TRUE, @pACL, %FALSE) THEN
                                       'AccessCheck validates a security descriptor somewhat; set the group
                                       'and owner so that enough of the security descriptor is filled out
                                       'to make AccessCheck happy.
                                       SetSecurityDescriptorGroup(@psdAdmin, @psidAdmin, %FALSE)
                                       SetSecurityDescriptorOwner(@psdAdmin, @psidAdmin, %FALSE)
                                       IF IsValidSecurityDescriptor(@psdAdmin) THEN
                                         dwAccessDesired               = %ACCESS_READ
                                         GenericMapping.GenericRead    = %ACCESS_READ
                                         GenericMapping.GenericWrite   = %ACCESS_WRITE
                                         GenericMapping.GenericExecute = 0
                                         GenericMapping.GenericAll     = %ACCESS_READ OR %ACCESS_WRITE
                                         IF AccessCheck(BYREF @psdAdmin, BYVAL hImpersonationToken, BYVAL dwAccessDesired, _
                                                        BYREF GenericMapping, BYREF ps, BYREF dwStructureSize, _
                                                        BYREF dwStatus, BYREF fReturn) THEN
                                           FUNCTION = fReturn
                                         END IF
                                       END IF
                                     END IF
                                   END IF
                                 END IF
                                 LocalFree(pACL)
                               END IF
                             END IF
                             LocalFree(psdAdmin)
                           END IF
                           FreeSid(BYVAL psidAdmin)
                         END IF
                         CloseHandle(hImpersonationToken)
                       END IF
                       CloseHandle(hToken)
                     END IF
                     
                    END FUNCTION
                    '______________________________________________________________________________
                     
                    FUNCTION PBMAIN() AS LONG
                     
                     MessageBox(%HWND_DESKTOP, _
                                "IsUserAdmin: " & IIF$(IsUserAdmin <> %FALSE, "Yes", "No"), _]
                                "IsUserAdmin", %MB_OK OR %MB_APPLMODAL)
                     
                    END FUNCTION
                    '______________________________________________________________________________
                    '
                    Last edited by Pierre Bellisle; 10 Apr 2014, 10:58 AM.

                    Comment

                    Working...
                    X