FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour User rights on a Vista OS
Posts: 858
Joined: Fri Oct 07, 2005 12:00 PM

User rights on a Vista OS

Posted: 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
Posts: 824
Joined: Thu Oct 13, 2005 07:39 AM

User rights on a Vista OS

Posted: Mon Jan 07, 2008 12:25 PM
Marco,

here is small function from Enrico. (not tested)

#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
Posts: 858
Joined: Fri Oct 07, 2005 12:00 PM

User rights on a Vista OS

Posted: Mon Jan 07, 2008 04: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
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM

User rights on a Vista OS

Posted: Mon Jan 07, 2008 06:10 PM
StefanHaupt wrote:Marco,

here is small function from Enrico. (not tested)


It's not from me.

EMG
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM

User rights on a Vista OS

Posted: Mon Jan 07, 2008 09:00 PM

Marco,

Try to change:

HARBOUR HB_FUN_ISADMIN( PARAMS )

into:

HB_FUNC( ISADMIN )

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 824
Joined: Thu Oct 13, 2005 07:39 AM

User rights on a Vista OS

Posted: Tue Jan 08, 2008 08:44 AM
EnricoMaria wrote:
It's not from me.

EMG


Hmm, so we have an unknown author
kind regards

Stefan
Posts: 824
Joined: Thu Oct 13, 2005 07:39 AM

User rights on a Vista OS

Posted: 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
Posts: 858
Joined: Fri Oct 07, 2005 12:00 PM

User rights on a Vista OS

Posted: 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
Posts: 1303
Joined: Tue Jul 21, 2009 08:12 AM

Re: User rights on a Vista OS

Posted: Sun Sep 30, 2012 08:53 AM
Hi,

Is there any newest function?.

This is my code:
Code (fw): Select all Collapse
#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.
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM

Re: User rights on a Vista OS

Posted: Sun Sep 30, 2012 08:57 AM

You can safely ignore those warnings.

EMG

Posts: 1303
Joined: Tue Jul 21, 2009 08:12 AM

Re: User rights on a Vista OS

Posted: Sun Sep 30, 2012 08: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.
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM

Re: User rights on a Vista OS

Posted: Sun Sep 30, 2012 09: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

Posts: 1303
Joined: Tue Jul 21, 2009 08:12 AM

Re: User rights on a Vista OS

Posted: Mon Oct 01, 2012 03: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.
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM

Re: User rights on a Vista OS

Posted: Mon Oct 01, 2012 03:40 PM

Lucas, why are you ignoring my advice?

EMG

Posts: 337
Joined: Fri Oct 07, 2005 02:44 PM

Re: User rights on a Vista OS

Posted: Mon Oct 01, 2012 06:19 PM

Hello,

Harbour have WAPI_ISUSERANADMIN() funcion in hbwin.

Best regards,

Lautaro

Hola,

Soy un Contador que por necesidad aprendio a programar y se quedo programando.