No announcement yet.

C++ to PowerBASIC conversion - A little help.

  • 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?

    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)
                if (pACL) 
                if (psdAdmin) 
                if (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;
    	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,
    								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,
    		return false;
    return fReturn;
    Explorations RPG System

  • #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):
    Adam Drake
    Drake Software


    • #3
      Originally posted by Adam J. Drake View Post
      I did come across this on the forum search that is similar (see last post):

      There are posts on doing conversions dating back to 2000.

      Mostly it is rearranging the items.


      • #4

        You guys are great!

        I will check them out, but there is a Kernel IsUserAdmin() functions that does what I needed.
        Explorations RPG System


        • #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.


          • #6
            C to PB

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

            Porting questions.. (
            Help converting C to PB for a DLL (
            C to PB Conversion (
            HANDLE HWND and others... (
            PB conversion of C/C++ SDK (
            Specific C-porting questions (
            C port of Type structure (
            C to PB conversion problem (
            Help with some C code to PB (
            Converting a C++ statement (
            Accessing C array type from PB-looks easy enough? (
            data conversion from a "C" struct (
            translate C enum to PowerBasic (
            C to PowerBasic translation (
            C Conversion (
            Translation from C++ to PB (
            Would anyone know how to convert this (
            C-2-PB Question Please! (
            C construct help please (


            • #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!!!!!


              • #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]


                • #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.


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


                    #COMPILE EXE '#Win 8.04#
                    #DIM ALL
                    #INCLUDE ""
                    %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
                               END IF
                             END IF
                           END IF
                           FreeSid(BYVAL psidAdmin)
                         END IF
                       END IF
                     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.