[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.sdk

finding groups a user belongs to

Richard

11/14/2002 10:02:00 PM

I have an app that needs to determine what NT groups a
user belongs to. I can find nothing in the .Net framework
that lets me do this - only a way to check against a
specific group. We are not an ADS shop yet, so I think
those options are out.

So, I have been trying to use a Win32 api call to the
NetUserGetGroups function in netapi32.dll out of vb.net
code. I have a couple of questions regarding this:


First: I can't get the function to return a 0. Instead I
get back 843842105133957243. Also, none of the referenced
values that I'm passing in are getting changed as they are
supposed to do.

Second: this function modifies one of the reference values
passed in (lBuffer) as location. Once I do get it working,
in .Net how do I obtain the values from this location?
Will I need to use another Win32 api call such as
RtlMoveMemory do get the values I'm after?


A note about the code below:

The lpServer parameter will only work if I pass it as
Object. Otherwise, I get an exception regarding Invalid
Managed/Unmanaged type combination.

This is from a windows forms app I'm using for testing,
thus the assignment of values to all the labels.

I'm no expert on WIN32 calls, so go easy on me :)


===================================================
Imports System.Runtime.InteropServices


Public Class Form1
Inherits System.Windows.Forms.Form

'api declaration
Declare Function NetUserGetGroups Lib "NETAPI32.DLL"
(<MarshalAs(UnmanagedType.Struct)> _
ByRef lpServer As Object, _
ByRef UserName As Byte, _
ByRef Level As Object, _
ByRef lpBuffer As Long, _
ByRef PrefMaxLen As Long, _
ByRef lpEntriesRead As Long, _
ByRef lpTotalEntries As Long) As Long



Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button1.Click

Dim bytUser() As Byte
Dim bytServer() As Byte
Dim stServer As String = "\\SONOCOHUB"
Dim stUser As String = "hillrich"
Dim lLevel As Long = 0
Dim lBuffer As Long
Dim lMaxPrefLen As Long = 1024
Dim lEntriesRead As Long
Dim lTotalEntries As Long


Dim oE As New System.Text.UnicodeEncoding()
bytServer = oE.GetBytes(stServer)
bytUser = oE.GetBytes(stUser)


Try
'api call
Label8.Text = NetUserGetGroups(bytServer(0),
bytUser(0), lLevel, lBuffer, lMaxPrefLen, lEntriesRead,
lTotalEntries).ToString

Label1.Text = stServer
Label2.Text = stUser
Label3.Text = lLevel.ToString
Label4.Text = lBuffer.ToString
Label5.Text = lMaxPrefLen.ToString
Label6.Text = lEntriesRead.ToString
Label7.Text = lTotalEntries.ToString

Catch ex As Exception

Label1.Text = ex.Message

End Try

End Sub









2 Answers

Willy Denoyette [MVP]

11/14/2002 10:52:00 PM

0

Don't use PInvoke, use the DirectoryServices namespace classes.
Here's a sample....

Imports System.DirectoryServices
Imports System

Module Module1

Sub Main()
Dim objUser As New DirectoryEntry("WinNT://machineNme/someUser")

Dim MembersCollection as Object
MembersCollection = objUser.Invoke("Groups")
Dim user As Object
for each user in MembersCollection
Console.WriteLine(" Name: " + user.Name )
next
End Sub
End Module
' Compile from the command line using:
'vbc /r:system.directoryservices.dll,System.dll usergr.vb

Willy.

"richard hill" <richard.hill@sonoco.com> wrote in message news:019101c28c21$168c2d50$8af82ecf@TK2MSFTNGXA03...
> I have an app that needs to determine what NT groups a
> user belongs to. I can find nothing in the .Net framework
> that lets me do this - only a way to check against a
> specific group. We are not an ADS shop yet, so I think
> those options are out.
>
> So, I have been trying to use a Win32 api call to the
> NetUserGetGroups function in netapi32.dll out of vb.net
> code. I have a couple of questions regarding this:
>
>
> First: I can't get the function to return a 0. Instead I
> get back 843842105133957243. Also, none of the referenced
> values that I'm passing in are getting changed as they are
> supposed to do.
>
> Second: this function modifies one of the reference values
> passed in (lBuffer) as location. Once I do get it working,
> in .Net how do I obtain the values from this location?
> Will I need to use another Win32 api call such as
> RtlMoveMemory do get the values I'm after?
>
>
> A note about the code below:
>
> The lpServer parameter will only work if I pass it as
> Object. Otherwise, I get an exception regarding Invalid
> Managed/Unmanaged type combination.
>
> This is from a windows forms app I'm using for testing,
> thus the assignment of values to all the labels.
>
> I'm no expert on WIN32 calls, so go easy on me :)
>
>
> ===================================================
> Imports System.Runtime.InteropServices
>
>
> Public Class Form1
> Inherits System.Windows.Forms.Form
>
> 'api declaration
> Declare Function NetUserGetGroups Lib "NETAPI32.DLL"
> (<MarshalAs(UnmanagedType.Struct)> _
> ByRef lpServer As Object, _
> ByRef UserName As Byte, _
> ByRef Level As Object, _
> ByRef lpBuffer As Long, _
> ByRef PrefMaxLen As Long, _
> ByRef lpEntriesRead As Long, _
> ByRef lpTotalEntries As Long) As Long
>
>
>
> Private Sub Button1_Click(ByVal sender As
> System.Object, ByVal e As System.EventArgs) Handles
> Button1.Click
>
> Dim bytUser() As Byte
> Dim bytServer() As Byte
> Dim stServer As String = "\\SONOCOHUB"
> Dim stUser As String = "hillrich"
> Dim lLevel As Long = 0
> Dim lBuffer As Long
> Dim lMaxPrefLen As Long = 1024
> Dim lEntriesRead As Long
> Dim lTotalEntries As Long
>
>
> Dim oE As New System.Text.UnicodeEncoding()
> bytServer = oE.GetBytes(stServer)
> bytUser = oE.GetBytes(stUser)
>
>
> Try
> 'api call
> Label8.Text = NetUserGetGroups(bytServer(0),
> bytUser(0), lLevel, lBuffer, lMaxPrefLen, lEntriesRead,
> lTotalEntries).ToString
>
> Label1.Text = stServer
> Label2.Text = stUser
> Label3.Text = lLevel.ToString
> Label4.Text = lBuffer.ToString
> Label5.Text = lMaxPrefLen.ToString
> Label6.Text = lEntriesRead.ToString
> Label7.Text = lTotalEntries.ToString
>
> Catch ex As Exception
>
> Label1.Text = ex.Message
>
> End Try
>
> End Sub
>
>
>
>
>
>
>
>
>


Richard

11/15/2002 12:11:00 AM

0

That's exactly what I need to do. Thanks for your help!

Richard


>-----Original Message-----
>Don't use PInvoke, use the DirectoryServices namespace
classes.
>Here's a sample....
>
>Imports System.DirectoryServices
>Imports System
>
>Module Module1
>
>Sub Main()
> Dim objUser As New DirectoryEntry
("WinNT://machineNme/someUser")
>
> Dim MembersCollection as Object
> MembersCollection = objUser.Invoke("Groups")
> Dim user As Object
> for each user in MembersCollection
> Console.WriteLine(" Name: " + user.Name )
> next
> End Sub
>End Module
>' Compile from the command line using:
>'vbc /r:system.directoryservices.dll,System.dll usergr.vb
>
>Willy.
>
>"richard hill" <richard.hill@sonoco.com> wrote in message
news:019101c28c21$168c2d50$8af82ecf@TK2MSFTNGXA03...
>> I have an app that needs to determine what NT groups a
...