Skip to content

Instantly share code, notes, and snippets.

@EBNull
Created February 28, 2012 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EBNull/1935574 to your computer and use it in GitHub Desktop.
Save EBNull/1935574 to your computer and use it in GitHub Desktop.
Get current user groups (win32)
import ctypes
import win32net
def getUsername(name_format=8):
#Only NameUserSamCompatable supported if not on a domain.
#'' is returned in that case.
#NameUserSamCompatable = 2
#NameUserPrincipal = 8,
bufsz = ctypes.c_ulong()
ctypes.windll.secur32.GetUserNameExW(name_format, None, ctypes.byref(bufsz))
buf = (ctypes.c_wchar*bufsz.value)()
ctypes.windll.secur32.GetUserNameExW(name_format, ctypes.byref(buf), ctypes.byref(bufsz))
return buf.value
def getCurrentUserGroups():
NameUserSamCompatable = 2
NameUserPrincipal = 8
un, _, dom = getUsername(NameUserPrincipal).partition('@')
if un == dom == '':
dom = None
pc, _, un = getUsername(NameUserSamCompatable).partition('\\')
if dom:
groups = [i[0] for i in win32net.NetUserGetGroups(dom, un)]
else:
groups = win32net.NetUserGetLocalGroups(dom, un)
return groups
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment