Page 1 of 2
User rights on a Vista OS
Posted: Mon Jan 07, 2008 12:04 pm
by Marco Turco
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
Posted: Mon Jan 07, 2008 12:25 pm
by StefanHaupt
Marco,
here is small function from Enrico. (not tested)
Code: Select all | Expand
#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 );
}
Posted: Mon Jan 07, 2008 4:46 pm
by Marco Turco
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
Posted: Mon Jan 07, 2008 6:10 pm
by Enrico Maria Giordano
StefanHaupt wrote:Marco,
here is small function from Enrico. (not tested)
It's not from me.
EMG
Posted: Mon Jan 07, 2008 9:00 pm
by Antonio Linares
Marco,
Try to change:
HARBOUR HB_FUN_ISADMIN( PARAMS )
into:
HB_FUNC( ISADMIN )
Posted: Tue Jan 08, 2008 8:44 am
by StefanHaupt
EnricoMaria wrote:It's not from me.
EMG
Hmm, so we have an unknown author
Posted: Tue Jan 08, 2008 12:18 pm
by StefanHaupt
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
Posted: Wed Jan 09, 2008 10:04 pm
by Marco Turco
Yes, runs fine.
Thanks a lot Stefan.
Best Regards,
Marco Turco
Re: User rights on a Vista OS
Posted: Sun Sep 30, 2012 8:53 am
by lucasdebeltran
Hi,
Is there any newest function?.
This is my code:
Code: Select all | Expand
#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.
Re: User rights on a Vista OS
Posted: Sun Sep 30, 2012 8:57 am
by Enrico Maria Giordano
You can safely ignore those warnings.
EMG
Re: User rights on a Vista OS
Posted: Sun Sep 30, 2012 8:59 am
by lucasdebeltran
Hi,
I would prefer not.
Maybe ANtonio could help please to indicate how to fix it.
Re: User rights on a Vista OS
Posted: Sun Sep 30, 2012 9:21 am
by Enrico Maria Giordano
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
Re: User rights on a Vista OS
Posted: Mon Oct 01, 2012 3:35 pm
by lucasdebeltran
Antonio,
Please can you help me with C code?.
Also, it would be very interesting to include this function into FWH!!!.
Re: User rights on a Vista OS
Posted: Mon Oct 01, 2012 3:40 pm
by Enrico Maria Giordano
Lucas, why are you ignoring my advice?
EMG
Re: User rights on a Vista OS
Posted: Mon Oct 01, 2012 6:19 pm
by Lautaro
Hello,
Harbour have WAPI_ISUSERANADMIN() funcion in hbwin.
Best regards,
Lautaro