User Groups

Everything else that doesn't fall into one of the other PB categories.
BinoX
User
User
Posts: 46
Joined: Mon Jan 31, 2005 11:57 am

User Groups

Post 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.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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.
I may look like a mule, but I'm not a complete ass.
maw

Post 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.
Post Reply