Page 1 of 1

User Groups

Posted: Sun Apr 26, 2009 11:59 am
by BinoX
I'm hoping someone here can help me with this, otherwise I'm going to have to resort to visual basic!!!!!

I want to write an application that lists the "groups" that a user has assigned to it under windows XP (and maybe vista in the future).
The user will be the one running the program, so hopefully permissions won't be an issue.

I was wondering how to do it (with a simple example if possible).
I don't need the IDs of the groups, just the names.

If it matters, all the users log onto a domain. I'd rather do it all client side if possible as I don't want to install unneccesary apps on the servers.

Thanks in advance.

Posted: Sun Apr 26, 2009 12:02 pm
by srod
If you have some VB code to do this then post it here and chances are that someone can convert it - probably using COMatePLUS.

Posted: Sun Apr 26, 2009 12:53 pm
by maw
This is a snippet I ripped from an old program I made some time ago, can't test it now as I'm not on a domain, but it should work. If not, I'll have a look at it tomorrow at work and fix it :)

Code: Select all

EnableExplicit

Define *buffer, size, *dcname, dcname$, username$, *groups, entries, i, result, void

NewList groups$()

#MAX_PREFERRED_LENGTH = -1
#ps = SizeOf(Integer)

*buffer = AllocateMemory(1026)

size = 1024

If GetUserName_(*buffer, @size)
	username$ = PeekS(*buffer)
	PokeS(*buffer, username$, -1, #PB_Unicode)

	If NetGetDCName_(0, 0, @*dcname) = 0
		dcname$ = PeekS(*dcname, -1, #PB_Unicode)
		If Left(dcname$, 2) = "\\"
			dcname$ = Mid(dcname$, 3)
		EndIf

		If NetUserGetGroups_(*dcname, *buffer, 0, @*groups, #MAX_PREFERRED_LENGTH, @entries, @void) = 0
			entries = (entries * #ps) - #ps
			For i = 0 To entries Step #ps
				AddElement(groups$())
				groups$() = PeekS(PeekI(*groups + i), -1, #PB_Unicode)
			Next
			NetApiBufferFree_(*groups)
		Else
			Debug "Error getting user groups"
		EndIf

		NetApiBufferFree_(*dcname)
	Else
		Debug "Error getting domain controller name"
	EndIf
Else
	Debug "Error getting username"
EndIf

FreeMemory(*buffer)

Debug "User " + username$ + " on domain controller " + dcname$ + " is a member of the following groups:"

ForEach groups$()
	Debug groups$()
Next
EDIT: The program simply looks up which groups the currently logged on user is a member of on the first available domain controller. If you have more than one domain controller and/or an entire forest then you have to rewrite the code that looks up the domain controller name.