473,385 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Win32API Function NetQueryDisplayInformation

I am trying to enumerate the users of a system from within my app (VB.NET) Not finding any .Net Framework classes to do this I turned to the Win32 API and NetQueryDisplayInformation. I can't get it to work from within VB.NET however. I found the following KB article from MS

http://support.microsoft.com/default...b;en-us;316318

When I try to convert to .Net I get an object reference not set error when calling the actual API function....Please help...

Here's my code:

Public Class UserList

'/* definitions and declarations

Private Const NET_USER = 1
Private Const NET_MACHINE = 2|
Private Const NET_GROUP = 3
Private Const ERROR_MORE_DATA = 234
Private Const REQUESTED_ENTRIES = 5
Private Const MAX_PREFERRED_LENGTH = -1

Private lResult As Int32

Private Structure NET_DISPLAY_USER
Dim usri1_name As Int32 'lpwstr
Dim usri1_comment As Int32 'lpwstr
Dim usri1_flags As Int32
Dim usri1_full_name As Int32 'lpwstr
Dim usri1_user_id As Int32
Dim usri1_next_index As Int32
End Structure

Private Structure NET_DISPLAY_GROUP
Dim grpi3_name As Int32 'lpwstr
Dim grpi3_comment As Int32 'lpwstr
Dim grpi3_group_id As Int32
Dim grpi3_attributes As Int32
Dim grpi3_next_index As Int32
End Structure

Private Structure NET_DISPLAY_MACHINE
Dim usri2_name As Int32 'lpwstr
Dim usri2_comment As Int32 'lpwstr
Dim usri2_flags As Int32
Dim usri2_user_id As Int32
Dim usri2_next_index As Int32
End Structure

Private Declare Function NetQueryDisplayInformation Lib "netapi32.dll" ( _
ByVal lpwServername As String, _
ByVal dwLevel As Int32, _
ByVal dwIndex As Int32, _
ByVal dwReqEntries As Int32, _
ByVal dwMaxLength As Int32, _
ByVal pdwNumEntries As Int32, _
ByVal pBuffer As Int32) As Int32

Private Declare Function NetApiBufferFree Lib "netapi32.dll" (ByVal BufPtr As Int32) As Int32

Private Declare Sub CopyMem Lib "KERNEL32" Alias "RtlMoveMemory" ( _
ByVal hpvDest As Int32, ByVal hpvSource As Int32, ByVal cbCopy As Int32)

Private Declare Function CopyString Lib "KERNEL32" Alias "lstrcpyW" ( _
ByVal NewString As String, ByVal OldString As Int32) As Int32

Public Sub GetUserNames()

Try

'/* This code enumerates domain users.
Dim pndu() As NET_DISPLAY_USER
Dim pszTemp As String, pszServer As String
Dim pBuffer As Int32
Dim lLevel As Int32
Dim lIndex As Int32
Dim lNumEntries As Int32
Dim lTotalEntries As Int32

pszServer = ""
lIndex = 0
lTotalEntries = 0
lNumEntries = 0

lResult = ERROR_MORE_DATA
'************************************************* ***************************************
'Here's where I get the object reference not set to an instance of an object error
lResult = NetQueryDisplayInformation(pszServer, NET_USER, lIndex, _
REQUESTED_ENTRIES, MAX_PREFERRED_LENGTH, lNumEntries, pBuffer)
'************************************************* ***************************************

Catch ex As Exception
Throw New ApplicationException(ex.Message, ex)

End Try

End Sub

End Class

Jul 21 '05 #1
2 2210

"Chris van den Heuvel" <va******@fireblade.ca> wrote in message news:ek**************@tk2msftngp13.phx.gbl...
I am trying to enumerate the users of a system from within my app (VB.NET) Not finding any .Net Framework classes to do this I turned to the Win32 API and NetQueryDisplayInformation. I can't get it to work from within VB.NET however. I found the following KB article from MS

http://support.microsoft.com/default...b;en-us;316318

When I try to convert to .Net I get an object reference not set error when calling the actual API function....Please help...

Here's my code:

Public Class UserList

'/* definitions and declarations

Private Const NET_USER = 1
Private Const NET_MACHINE = 2|
Private Const NET_GROUP = 3
Private Const ERROR_MORE_DATA = 234
Private Const REQUESTED_ENTRIES = 5
Private Const MAX_PREFERRED_LENGTH = -1

Private lResult As Int32

Private Structure NET_DISPLAY_USER
Dim usri1_name As Int32 'lpwstr
Dim usri1_comment As Int32 'lpwstr
Dim usri1_flags As Int32
Dim usri1_full_name As Int32 'lpwstr
Dim usri1_user_id As Int32
Dim usri1_next_index As Int32
End Structure

Private Structure NET_DISPLAY_GROUP
Dim grpi3_name As Int32 'lpwstr
Dim grpi3_comment As Int32 'lpwstr
Dim grpi3_group_id As Int32
Dim grpi3_attributes As Int32
Dim grpi3_next_index As Int32
End Structure

Private Structure NET_DISPLAY_MACHINE
Dim usri2_name As Int32 'lpwstr
Dim usri2_comment As Int32 'lpwstr
Dim usri2_flags As Int32
Dim usri2_user_id As Int32
Dim usri2_next_index As Int32
End Structure

Private Declare Function NetQueryDisplayInformation Lib "netapi32.dll" ( _
ByVal lpwServername As String, _
ByVal dwLevel As Int32, _
ByVal dwIndex As Int32, _
ByVal dwReqEntries As Int32, _
ByVal dwMaxLength As Int32, _
ByVal pdwNumEntries As Int32, _
ByVal pBuffer As Int32) As Int32

Private Declare Function NetApiBufferFree Lib "netapi32.dll" (ByVal BufPtr As Int32) As Int32

Private Declare Sub CopyMem Lib "KERNEL32" Alias "RtlMoveMemory" ( _
ByVal hpvDest As Int32, ByVal hpvSource As Int32, ByVal cbCopy As Int32)

Private Declare Function CopyString Lib "KERNEL32" Alias "lstrcpyW" ( _
ByVal NewString As String, ByVal OldString As Int32) As Int32

Public Sub GetUserNames()

Try

'/* This code enumerates domain users.
Dim pndu() As NET_DISPLAY_USER
Dim pszTemp As String, pszServer As String
Dim pBuffer As Int32
Dim lLevel As Int32
Dim lIndex As Int32
Dim lNumEntries As Int32
Dim lTotalEntries As Int32

pszServer = ""
lIndex = 0
lTotalEntries = 0
lNumEntries = 0

lResult = ERROR_MORE_DATA
'************************************************* ***************************************
'Here's where I get the object reference not set to an instance of an object error
lResult = NetQueryDisplayInformation(pszServer, NET_USER, lIndex, _
REQUESTED_ENTRIES, MAX_PREFERRED_LENGTH, lNumEntries, pBuffer)
'************************************************* ***************************************

Catch ex As Exception
Throw New ApplicationException(ex.Message, ex)

End Try

End Sub

End Class

The .NET classes to use for this are or in The System.DirectoryServices or System.Management namespace. The first are the ADSI wrapper classes while the latter use WMI.

No need to PInvoke here.

Willy.

Jul 21 '05 #2
Thanks exactly what I needed...
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...

"Chris van den Heuvel" <va******@fireblade.ca> wrote in message news:ek**************@tk2msftngp13.phx.gbl...
I am trying to enumerate the users of a system from within my app (VB.NET) Not finding any .Net Framework classes to do this I turned to the Win32 API and NetQueryDisplayInformation. I can't get it to work from within VB.NET however. I found the following KB article from MS

http://support.microsoft.com/default...b;en-us;316318

When I try to convert to .Net I get an object reference not set error when calling the actual API function....Please help...

Here's my code:

Public Class UserList

'/* definitions and declarations

Private Const NET_USER = 1
Private Const NET_MACHINE = 2|
Private Const NET_GROUP = 3
Private Const ERROR_MORE_DATA = 234
Private Const REQUESTED_ENTRIES = 5
Private Const MAX_PREFERRED_LENGTH = -1

Private lResult As Int32

Private Structure NET_DISPLAY_USER
Dim usri1_name As Int32 'lpwstr
Dim usri1_comment As Int32 'lpwstr
Dim usri1_flags As Int32
Dim usri1_full_name As Int32 'lpwstr
Dim usri1_user_id As Int32
Dim usri1_next_index As Int32
End Structure

Private Structure NET_DISPLAY_GROUP
Dim grpi3_name As Int32 'lpwstr
Dim grpi3_comment As Int32 'lpwstr
Dim grpi3_group_id As Int32
Dim grpi3_attributes As Int32
Dim grpi3_next_index As Int32
End Structure

Private Structure NET_DISPLAY_MACHINE
Dim usri2_name As Int32 'lpwstr
Dim usri2_comment As Int32 'lpwstr
Dim usri2_flags As Int32
Dim usri2_user_id As Int32
Dim usri2_next_index As Int32
End Structure

Private Declare Function NetQueryDisplayInformation Lib "netapi32.dll" ( _
ByVal lpwServername As String, _
ByVal dwLevel As Int32, _
ByVal dwIndex As Int32, _
ByVal dwReqEntries As Int32, _
ByVal dwMaxLength As Int32, _
ByVal pdwNumEntries As Int32, _
ByVal pBuffer As Int32) As Int32

Private Declare Function NetApiBufferFree Lib "netapi32.dll" (ByVal BufPtr As Int32) As Int32

Private Declare Sub CopyMem Lib "KERNEL32" Alias "RtlMoveMemory" ( _
ByVal hpvDest As Int32, ByVal hpvSource As Int32, ByVal cbCopy As Int32)

Private Declare Function CopyString Lib "KERNEL32" Alias "lstrcpyW" ( _
ByVal NewString As String, ByVal OldString As Int32) As Int32

Public Sub GetUserNames()

Try

'/* This code enumerates domain users.
Dim pndu() As NET_DISPLAY_USER
Dim pszTemp As String, pszServer As String
Dim pBuffer As Int32
Dim lLevel As Int32
Dim lIndex As Int32
Dim lNumEntries As Int32
Dim lTotalEntries As Int32

pszServer = ""
lIndex = 0
lTotalEntries = 0
lNumEntries = 0

lResult = ERROR_MORE_DATA
'************************************************* ***************************************
'Here's where I get the object reference not set to an instance of an object error
lResult = NetQueryDisplayInformation(pszServer, NET_USER, lIndex, _
REQUESTED_ENTRIES, MAX_PREFERRED_LENGTH, lNumEntries, pBuffer)
'************************************************* ***************************************

Catch ex As Exception
Throw New ApplicationException(ex.Message, ex)

End Try

End Sub

End Class

The .NET classes to use for this are or in The System.DirectoryServices or System.Management namespace. The first are the ADSI wrapper classes while the latter use WMI.

No need to PInvoke here.

Willy.

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Matt Smith | last post by:
hi all, again.... having another problem with python COMs, I run a python script on a win2000 machine, that uses win32com.client and it runs perfectly, whereas when I come to run it on a win98...
5
by: Gary Richardson | last post by:
I'm trying to use win32api.SetCursorPos() to position the cursor in a Tkinter canvas window. I.e.: from Tkinter import * import win32api root = Tk() canvas = Canvas(root, width=400,...
2
by: Nurchi BECHED | last post by:
Hello, my dearest respected brother, All! How do you make calls to Win32Api functions from C# I have an old example in Visual FoxPro... Kind of need to do the same in C# now... I need to get...
4
by: Sam Evans | last post by:
All: I am very new to C#, but not OO languages in general. I am working on a project where I need to enumerate the members of a local group on workstations. I was reading through the MSDN...
0
by: Chris van den Heuvel | last post by:
I am trying to enumerate the users of a system from within my app (VB.NET) Not finding any .Net Framework classes to do this I turned to the Win32 API and NetQueryDisplayInformation. I can't get it...
2
by: Jim | last post by:
I am trying to figure out how to embed Python in a little C++ Windows console app. Here's the code: // Py_Initialize(); Py_InitializeEx(0); PyRun_SimpleString("from win32com.client import *");...
0
by: Mike | last post by:
Hi. I have Python 2.4 installed on my local machine in c:\Python24. I have also downloaded the python for windows extensions installer pywin32-208.win32-py2.4.exe and installed this to...
1
by: reginpc | last post by:
Hi, Please anyone say how to find win32api.py file and where its present?? i am Getting the below error... from win32com.shell import shell File...
1
by: Michiel Overtoom | last post by:
On Saturday 19 July 2008 21:13:04 Lamonte Harris wrote: What are the actions you do and the commands you give, and what is the precise error you get? Greetings, -- "The ability of the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.