User rights on a Vista OS

User rights on a Vista OS

Postby Marco Turco » Mon Jan 07, 2008 12:04 pm

Hi,
is there any way to know if the current user has the administrator rights or if it has only limited rights on a Vista OS ?

I need to know it because my app create a virtual printer at runtime so I have to check if the user has the administrator rights before create the printer to avoid a system erron if the user is not the administrator. Unfortunately on a Windows Vista OS only the administrator can add a new printer.

Thanks in advance

Best Regards,

Marco Turco
Best Regards,

Marco Turco
SOFTWARE XP LLP
User avatar
Marco Turco
 
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London

Postby StefanHaupt » Mon Jan 07, 2008 12:25 pm

Marco,

here is small function from Enrico. (not tested)

Code: Select all  Expand view
#include <WinTen.h>
#include <Windows.h>
#include <ClipApi.h>
#include <CommDlg.h>

extern LPSTR LToStr( long w );

HARBOUR HB_FUN_ISADMIN( PARAMS )

{

  HANDLE hToken;
  PTOKEN_GROUPS pGroupInfo;
  DWORD dwSize = 0, dwResult;
  DWORD nError = 0, i;
  BOOL lError, lAdMin = FALSE;
  LPSTR cFunc = "";
  PSID   psidAdmin;
  CHAR cMess[200];
  SID_IDENTIFIER_AUTHORITY SystemSidAuthority= SECURITY_NT_AUTHORITY;

  if ( lError = (! OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,&hToken) ))
    {
     cFunc = "OpenProcessToken";
     nError = GetLastError();
     if (nError == ERROR_CALL_NOT_IMPLEMENTED)
       {
        _retl( TRUE );
        return;
       }
    }

  if ( ! lError &&  ! GetTokenInformation(hToken, TokenGroups, NULL, dwSize, &dwSize))
    {
      dwResult = GetLastError();
      if( lError=(dwResult != ERROR_INSUFFICIENT_BUFFER ))
       {
        nError = dwResult;
        cFunc = "GetTokenInformation";
       }
    }

  if ( ! lError )
   {
    pGroupInfo = (PTOKEN_GROUPS) GlobalAlloc( GPTR, dwSize );
    if( lError = (! GetTokenInformation(hToken, TokenGroups, pGroupInfo, dwSize, &dwSize ) ))
      {
        nError = GetLastError();
        cFunc = "GetTokenInformation";
      }
   }

  if ( ! lError )
    if ( lError = (! AllocateAndInitializeSid ( &SystemSidAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &psidAdmin) ))
     {
      nError = GetLastError();
      cFunc = "AllocateAndInitializeSid";
     }

  if ( ! lError )
    {
      for( i=0; i<pGroupInfo->GroupCount; i++)
       {
          if ( EqualSid(psidAdmin, pGroupInfo->Groups[i].Sid) )
           {
               lAdMin = TRUE;
               break;
           }
       }
    }
  else
    {
      cMess[0]=0;
      lstrcat(cMess,"Error calling ");
      lstrcat(cMess,cFunc);
      lstrcat(cMess,": ");
      lstrcat(cMess,LToStr(nError));
      MessageBox(GetActiveWindow(),cMess,"Attention", MB_OK);
    }

  if (psidAdmin)
      FreeSid(psidAdmin);

  if ( pGroupInfo )
      GlobalFree( pGroupInfo );

  CloseHandle( hToken );
  _retl( lAdMin );

}

kind regards
Stefan
StefanHaupt
 
Posts: 824
Joined: Thu Oct 13, 2005 7:39 am
Location: Germany

Postby Marco Turco » Mon Jan 07, 2008 4:46 pm

Thank you Stefan,
but I have a "Declaration sintax error" in
HARBOUR HB_FUN_ISADMIN( PARAMS )

Do you know how I have to declare this function with xHarbour (BCC) ?

Thanks

Best Regards,

Marco
Best Regards,

Marco Turco
SOFTWARE XP LLP
User avatar
Marco Turco
 
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London

Postby Enrico Maria Giordano » Mon Jan 07, 2008 6:10 pm

StefanHaupt wrote:Marco,

here is small function from Enrico. (not tested)


It's not from me.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8710
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Postby Antonio Linares » Mon Jan 07, 2008 9:00 pm

Marco,

Try to change:

HARBOUR HB_FUN_ISADMIN( PARAMS )

into:

HB_FUNC( ISADMIN )
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42081
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby StefanHaupt » Tue Jan 08, 2008 8:44 am

EnricoMaria wrote:It's not from me.

EMG


Hmm, so we have an unknown author
kind regards
Stefan
StefanHaupt
 
Posts: 824
Joined: Thu Oct 13, 2005 7:39 am
Location: Germany

Postby StefanHaupt » Tue Jan 08, 2008 12:18 pm

Marco Turco wrote:but I have a "Declaration sintax error" in
HARBOUR HB_FUN_ISADMIN( PARAMS )



Marco,

you have to add -D__HARBOUR__ to the compiler options, then it is working
kind regards
Stefan
StefanHaupt
 
Posts: 824
Joined: Thu Oct 13, 2005 7:39 am
Location: Germany

Postby Marco Turco » Wed Jan 09, 2008 10:04 pm

Yes, runs fine.
Thanks a lot Stefan.

Best Regards,

Marco Turco
Best Regards,

Marco Turco
SOFTWARE XP LLP
User avatar
Marco Turco
 
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London

Re: User rights on a Vista OS

Postby lucasdebeltran » Sun Sep 30, 2012 8:53 am

Hi,

Is there any newest function?.

This is my code:
Code: Select all  Expand view
#pragma BEGINDUMP

#include <hbapi.h>
#include <Windows.h>
#include <CommDlg.h>

extern LPSTR LToStr( long w );

HB_FUNC( ISADMIN )

{

  HANDLE hToken;
  PTOKEN_GROUPS pGroupInfo;
  DWORD dwSize = 0, dwResult;
  DWORD nError = 0, i;
  BOOL lError, lAdMin = FALSE;
  LPSTR cFunc = "";
  PSID   psidAdmin;
  CHAR cMess[200];
  SID_IDENTIFIER_AUTHORITY SystemSidAuthority= SECURITY_NT_AUTHORITY;

  if ( lError = (! OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,&hToken) ))
    {
     cFunc = "OpenProcessToken";
     nError = GetLastError();
     if (nError == ERROR_CALL_NOT_IMPLEMENTED)
       {
        hb_retl( TRUE );
        return;
       }
    }

  if ( ! lError &&  ! GetTokenInformation(hToken, TokenGroups, NULL, dwSize, &dwSize))
    {
      dwResult = GetLastError();
      if( lError=(dwResult != ERROR_INSUFFICIENT_BUFFER ))
       {
        nError = dwResult;
        cFunc = "GetTokenInformation";
       }
    }

  if ( ! lError )
   {
    pGroupInfo = (PTOKEN_GROUPS) GlobalAlloc( GPTR, dwSize );
    if( lError = (! GetTokenInformation(hToken, TokenGroups, pGroupInfo, dwSize, &dwSize ) ))
      {
        nError = GetLastError();
        cFunc = "GetTokenInformation";
      }
   }

  if ( ! lError )
    if ( lError = (! AllocateAndInitializeSid ( &SystemSidAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &psidAdmin) ))
     {
      nError = GetLastError();
      cFunc = "AllocateAndInitializeSid";
     }

  if ( ! lError )
    {
      for( i=0; i<pGroupInfo->GroupCount; i++)
       {
          if ( EqualSid(psidAdmin, pGroupInfo->Groups[i].Sid) )
           {
               lAdMin = TRUE;
               break;
           }
       }
    }
  else
    {
      cMess[0]=0;
      lstrcat(cMess,"Error calling ");
      lstrcat(cMess,cFunc);
      lstrcat(cMess,": ");
      lstrcat(cMess,LToStr(nError));
      MessageBox(GetActiveWindow(),cMess,"Attention", MB_OK);
    }

  if (psidAdmin)
      FreeSid(psidAdmin);

  if ( pGroupInfo )
      GlobalFree( pGroupInfo );

  CloseHandle( hToken );
  hb_retl( lAdMin );

}



#pragma ENDDUMP




But I get some warnings:

Warning W8060 test.prg 41: Possibly incorrect assignment in function HB_FUN_ISADMIN
Warning W8060 test.prg 55: Possibly incorrect assignment in function HB_FUN_ISADMIN
Warning W8060 test.prg 65: Possibly incorrect assignment in function HB_FUN_ISADMIN
Warning W8060 test.prg 73: Possibly incorrect assignment in function HB_FUN_ISADMIN


Line 41:
if ( lError = (! OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,&hToken) ))

Line 55:
if( lError=(dwResult != ERROR_INSUFFICIENT_BUFFER ))

Line 65:
if( lError = (! GetTokenInformation(hToken, TokenGroups, pGroupInfo, dwSize, &dwSize ) ))

Line 73:
if ( lError = (! AllocateAndInitializeSid ( &SystemSidAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &psidAdmin) ))


Thanks a lot.
Muchas gracias. Many thanks.

Un saludo, Best regards,

Harbour 3.2.0dev, Borland C++ 5.82 y FWH 13.06 [producción]

Implementando MSVC 2010, FWH64 y ADO.

Abandonando uso xHarbour y SQLRDD.
User avatar
lucasdebeltran
 
Posts: 1303
Joined: Tue Jul 21, 2009 8:12 am

Re: User rights on a Vista OS

Postby Enrico Maria Giordano » Sun Sep 30, 2012 8:57 am

You can safely ignore those warnings.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8710
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: User rights on a Vista OS

Postby lucasdebeltran » Sun Sep 30, 2012 8:59 am

Hi,

I would prefer not.

Maybe ANtonio could help please to indicate how to fix it.
Muchas gracias. Many thanks.

Un saludo, Best regards,

Harbour 3.2.0dev, Borland C++ 5.82 y FWH 13.06 [producción]

Implementando MSVC 2010, FWH64 y ADO.

Abandonando uso xHarbour y SQLRDD.
User avatar
lucasdebeltran
 
Posts: 1303
Joined: Tue Jul 21, 2009 8:12 am

Re: User rights on a Vista OS

Postby Enrico Maria Giordano » Sun Sep 30, 2012 9:21 am

I think you cannot avoid them. The compiler is warning you that you are assigning

lError = ...

while it thinks that you did mean

lError == ...

but it's not what you want to do as you're intentionally doing an assignment.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8710
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: User rights on a Vista OS

Postby lucasdebeltran » Mon Oct 01, 2012 3:35 pm

Antonio,

Please can you help me with C code?.

Also, it would be very interesting to include this function into FWH!!!.
Muchas gracias. Many thanks.

Un saludo, Best regards,

Harbour 3.2.0dev, Borland C++ 5.82 y FWH 13.06 [producción]

Implementando MSVC 2010, FWH64 y ADO.

Abandonando uso xHarbour y SQLRDD.
User avatar
lucasdebeltran
 
Posts: 1303
Joined: Tue Jul 21, 2009 8:12 am

Re: User rights on a Vista OS

Postby Enrico Maria Giordano » Mon Oct 01, 2012 3:40 pm

Lucas, why are you ignoring my advice?

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8710
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: User rights on a Vista OS

Postby Lautaro » Mon Oct 01, 2012 6:19 pm

Hello,

Harbour have WAPI_ISUSERANADMIN() funcion in hbwin.


Best regards,

Lautaro
User avatar
Lautaro
 
Posts: 322
Joined: Fri Oct 07, 2005 2:44 pm
Location: Osorno, Chile

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 31 guests