WinAsm Studio, The Assembly IDE - Free Downloads, Source Code
Sponsors
Articles
Programming Quick Start
32-bit Assembler is Easy
Porting Iczelion tutorials
What is FASM
Hard Drive Recovery
Wiring your own LAN
Personal menu
Welcome Guest
User:
Pass:
Register!
Resend Validation Email
 
Forum
 

Detect Administrator under Vista, how to detect if the program has been or has not been run as administrator so that a message is displayed?

colinramsden
Quote Post


Active Member
***

Group: Members
Posts: 49
Member No.: 9469
Joined: 27-June 08


Hi all,

I have written a small asm program.

If this program is executed under Vista, it needs to be 'run as administrator'.

Does anyone know how to detect if the program has been or has not been run as administrator so that a message can be displayed to tell the user to run as an administrator?

I have thought about attemting to open a system file, if it fails this should indicate that the user is not running as administrator, but there must be an official way of doing it.

Thanks in advance,

Colin.
PMEmail Poster
Top
laledesi
Quote Post


Very Active Member
****

Group: Members
Posts: 78
Member No.: 787
Joined: 16-November 04


Hi,

Just wanting to help (so I am not 100% sure this will work), if you have the Windows SDK for Vista help files, look for the C code example for the CheckTokenMembership API function, which begins like this

QUOTE
BOOL IsUserAdmin(VOID)
/*++
Routine Description: This routine returns TRUE if the caller's
process is a member of the Administrators local group. Caller is NOT
expected to be impersonating anyone and is expected to be able to
open its own process and process token.
Arguments: None.
Return Value:
  TRUE - Caller has Administrators local group.
  FALSE - Caller does not have Administrators local group. --
*/
{
PMEmail Poster
Top
ragdog
Quote Post


Extremely Active Member
******

Group: Moderators
Posts: 873
Member No.: 5019
Joined: 13-May 07


Hi Colin

Under xp works this for check "have user admin privileges"
I´m not sure if this works under vista you can test it.

CODE

;--------------------------------------------------------------------
; IsAdmin
; Returns TRUE if calling process (you) have Admin privileges and
; FALSE if you don't or in case of error.
;
; Copy IsAdmin proc and variables (except those starting with Msg) to
; your own program.
;--------------------------------------------------------------------

.486
.MODEL  FLAT, STDCALL
option  casemap:none;case sensitive

include  windows.inc
include  kernel32.inc
include  user32.inc
include  advapi32.inc
includelib user32.lib
includelib kernel32.lib
includelib advapi32.lib

IsAdmin    PROTO

.data
MsgCaption   db "IsAdmin", 0
MsgAdmin   db "You have Admin privileges!", 0
MsgNoAdmin   db "You don't have Admin privileges!", 0
hCurrentThread  dd 0
hAccessToken  dd 0
hCurrentProcess  dd 0
dwInfoBufferSize dd 0
bSuccess   dd 0
pInfoBuffer   dd 0
siaNtAuthority  SID_IDENTIFIER_AUTHORITY <SECURITY_NT_AUTHORITY>
psidAdministrators dd 0

.data?

.const

.code
Start:
invoke IsAdmin
.if eax == TRUE
 invoke MessageBox, NULL, ADDR MsgAdmin, ADDR MsgCaption, MB_OK
.else
 invoke MessageBox, NULL, ADDR MsgNoAdmin, ADDR MsgCaption, MB_OK
.endif
invoke ExitProcess, 0

IsAdmin proc
invoke GetCurrentThread
mov  hCurrentThread, eax
invoke OpenThreadToken, hCurrentThread, TOKEN_QUERY, TRUE, ADDR hAccessToken
.if eax == 0
 invoke GetLastError
 .if eax != ERROR_NO_TOKEN
  mov eax, FALSE
  ret
 .endif
 invoke GetCurrentProcess
 mov  hCurrentProcess, eax
 invoke OpenProcessToken, hCurrentProcess, TOKEN_QUERY, ADDR hAccessToken
 .if eax == 0
  mov  eax, FALSE
  ret
 .endif
.endif
invoke GetTokenInformation, hAccessToken, TokenGroups, NULL, NULL, ADDR dwInfoBufferSize
.if dwInfoBufferSize > 0
 invoke GlobalAlloc, GMEM_FIXED, dwInfoBufferSize
 mov  pInfoBuffer, eax
 invoke GetTokenInformation, hAccessToken, TokenGroups, pInfoBuffer, dwInfoBufferSize, ADDR dwInfoBufferSize
.endif
mov  bSuccess, eax
invoke CloseHandle, hAccessToken

.if bSuccess == 0
 mov  eax, FALSE
 ret
.endif

invoke AllocateAndInitializeSid, ADDR siaNtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, ADDR psidAdministrators
.if eax == 0
 mov  eax, FALSE
 ret
.endif

mov  bSuccess, FALSE

mov  ebx, pInfoBuffer
mov  ecx, TOKEN_GROUPS.GroupCount[ebx]
xor  esi, esi
.while esi < ecx
 push esi
 push ecx
 mov  ecx, TOKEN_GROUPS.Groups.Sid[ebx]
 mov  eax, sizeof TOKEN_GROUPS.Groups
 xor  edx, edx
 mul  esi        ;eax * esi -> eax
 add  ecx, eax
 invoke EqualSid, psidAdministrators, ecx
 pop  ecx
 pop  esi
 .if eax != 0
  mov  bSuccess, TRUE
  .break
 .endif
 inc  esi
.endw
invoke FreeSid, psidAdministrators
invoke GlobalFree, pInfoBuffer
mov eax, bSuccess
ret
IsAdmin endp
end Start


Greets,
PMUsers Website
Top
colinramsden
Quote Post


Active Member
***

Group: Members
Posts: 49
Member No.: 9469
Joined: 27-June 08


Thank you Ragdog, you are one hell of a programmer!

I managed to get this problem solved by attempting to write a file to the root of system drive, it did work but your method is much more professional.

Thanks again,

Colin.
PMEmail Poster
Top
Slider
Quote Post


Extremely Active Member
******

Group: Moderators
Posts: 287
Member No.: 5004
Joined: 11-May 07


Thanks ragdog.

Andy
PMEmail Poster
Top
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

Topic Options Reply to this topicStart new topicStart Poll

 

Sponsors
Computer Science

Internet
C/C++
Hardware & PC maintenance

HiEditor

General Discussions
Suggestions/Bug Reports
WinAsm Studio

General Discussions
Suggestions/Bug Reports
WinAsm Studio FAQ
Multilingual User Interface
Add-Ins
Assembly Programming

Main
Newbies
Projects
Custom Controls
Snippets
Announcements & Rules

Announcements

General

Online Degrees - Distance Learning
The Heap
Russian
Google