NetServerEnum
-
I have declared 'NetServerEnum' function in module as follows; Public Declare Function NetServerEnum Lib "netapi32.dll" ( _ ByVal servername As String, _ ByVal level As Integer, _ ByRef buffer As Integer, _ ByRef prefmaxlen As Integer, _ ByVal entriesread As Integer, _ ByVal totalentries As Integer, _ ByVal servertype As Integer, _ ByVal domain As String, _ ByVal resumehandle As Integer) As Integer when i call it, i get an error saying "Object reference not set to an instance of an object." Can anyone help me out? Thanks in advance.
-
I have declared 'NetServerEnum' function in module as follows; Public Declare Function NetServerEnum Lib "netapi32.dll" ( _ ByVal servername As String, _ ByVal level As Integer, _ ByRef buffer As Integer, _ ByRef prefmaxlen As Integer, _ ByVal entriesread As Integer, _ ByVal totalentries As Integer, _ ByVal servertype As Integer, _ ByVal domain As String, _ ByVal resumehandle As Integer) As Integer when i call it, i get an error saying "Object reference not set to an instance of an object." Can anyone help me out? Thanks in advance.
The error message usually means that the declaration is wrong, i.e. passing arguments ByVal instead of ByRef. The C++ declaration is:
NET_API_STATUS NetServerEnum(
LPCWSTR servername, // [in] Reserved - must be 0
//
DWORD level, // [in] Level - 100 or 101
//
LPBYTE* bufptr, // [out] Pointer to the buffer that receives the data.
// This buffer is allocated by the system and must be
// freed using the NetApiBufferFree function. Note
// that you must free the buffer even if the function
// fails with ERROR_MORE_DATA.
//
DWORD prefmaxlen, // [in] Prefered max length of returned data (bytes)
//
LPDWORD entriesread, // [out] Pointer to a value that receives the
// count of elements actually enumerated.
//
LPDWORD totalentries, // [out] Pointer to a value that receives the total
// number of visible servers and workstations on
// the network.
//
DWORD servertype, // [in] Specifies a value that filters the server
// entries to return from the enumeration.
//
LPCWSTR domain, // [in] Pointer to a constant string that specifies
// the name of the domain for which a list of
// servers is to be returned.
//
LPDWORD resume_handle // [out] Reserved - must be 0
);In VB.NET, this translates to:
Public Declare Function NetServerEnum Lib "netapi32" ( _
ByVal ServerName As IntPtr, _
ByVal Level As Integer, _
ByRef Buffer As IntPtr, _
ByVal PrefMaxLen As Integer, _
ByRef EntriesRead As Integer, _
ByRef TotalEntries As Integer, _
ByVal ServerType As Integer, _
<MarshalAs(UnmanagedType.LPWStr)> _
ByVal Domain As String, _
ByVal ResumeHandle As IntPtr _
) As IntegerYou then call it as:
...
Dim EntriesRead As Integer = 0
Dim TotalEntries As Integer = 0
Dim Buffer As IntPtr = IntPtr.ZeroDim nRes As Integer = NetServerEnum( _
IntPtr.Ze -
I have declared 'NetServerEnum' function in module as follows; Public Declare Function NetServerEnum Lib "netapi32.dll" ( _ ByVal servername As String, _ ByVal level As Integer, _ ByRef buffer As Integer, _ ByRef prefmaxlen As Integer, _ ByVal entriesread As Integer, _ ByVal totalentries As Integer, _ ByVal servertype As Integer, _ ByVal domain As String, _ ByVal resumehandle As Integer) As Integer when i call it, i get an error saying "Object reference not set to an instance of an object." Can anyone help me out? Thanks in advance.
NB: Please post any responses to the forum, so that other users get the chance to answer them! Vipul Bhatt wrote:
Thanks a lot. It worked. Can you please tell me how i point to next memory location as
TempBufPtr = TempBufPtr + SIZE_SI_101
gives me an compile time error saying datatype conversion error whereTempBufPtr As IntPtr
ANDPublic Const SIZE_SI_101 = 24
Thanks in advance. Also where can i find info about these API functions as i was unable to find the correct declaration info. THANKS ONCE AGAIN.To move an IntPtr in VB.NET, you need to re-create it:
TempBufPtr = New IntPtr(TempBufPtr.ToInt32() + SI_SIZE_101)
The best place to find information about any API is MSDN: http://msdn.microsoft.com/[^] Also, take a look at this article, which makes extensive use of the NetServerEnum function: http://www.codeproject.com/csharp/ServerComboBox.asp[^]
-
NB: Please post any responses to the forum, so that other users get the chance to answer them! Vipul Bhatt wrote:
Thanks a lot. It worked. Can you please tell me how i point to next memory location as
TempBufPtr = TempBufPtr + SIZE_SI_101
gives me an compile time error saying datatype conversion error whereTempBufPtr As IntPtr
ANDPublic Const SIZE_SI_101 = 24
Thanks in advance. Also where can i find info about these API functions as i was unable to find the correct declaration info. THANKS ONCE AGAIN.To move an IntPtr in VB.NET, you need to re-create it:
TempBufPtr = New IntPtr(TempBufPtr.ToInt32() + SI_SIZE_101)
The best place to find information about any API is MSDN: http://msdn.microsoft.com/[^] Also, take a look at this article, which makes extensive use of the NetServerEnum function: http://www.codeproject.com/csharp/ServerComboBox.asp[^]
Thanks Richard. Great help. Can U pls. tell me how can i use the RtlMoveMemory in VB.NET and also do i need to convert pointer to string later. If yes please tell how do i do that. I tried using marshal.PtrToStringUni but in vain. hope i will get similar response as earlier. Thanks in advance. :cool:
-
Thanks Richard. Great help. Can U pls. tell me how can i use the RtlMoveMemory in VB.NET and also do i need to convert pointer to string later. If yes please tell how do i do that. I tried using marshal.PtrToStringUni but in vain. hope i will get similar response as earlier. Thanks in advance. :cool:
Vipul Bhatt wrote: tell me how can i use the RtlMoveMemory in VB.NET Don't! For most cases, using the
MarshalAs
andStructLayout
attributes will be sufficient. In cases when these don't work, the Marshal class provides functions to convert between managed types andIntPtr
s. For example:Imports System.Runtime.InteropServices
'
Public Enum PlatformID
DOS = 300
OS2 = 400
NT = 500
OSF = 600
VMS = 700
End Enum
'
<Flags> _
Public Enum ServerType
None = &H00000000
Workstation = &H00000001
Server = &H00000002
SQLServer = &H00000004
DomainController = &H00000008
DomainBackupController = &H00000010
TimeSource = &H00000020
AFP = &H00000040
Novell = &H00000080
DomainMember = &H00000100
PrintQueue = &H00000200
Dialin = &H00000400
Xenix = &H00000800
Unix = Xenix
NT = &H00001000
WFW = &H00002000
MFPN = &H00004000
NTServer = &H00008000
PotentialBrowser = &H00010000
BackupBrowser = 0x00020000
MasterBrowser = &H00040000
DomainMaster = &H00080000
OSF = &H00100000
VMS = &H00200000
Windows = &H00400000
DFS = &H00800000
ClusterNT = &H01000000
TerminalServer = &H02000000
DCE = &H10000000
AlternateXPort = &H20000000
ListOnly = &H40000000
DomainEnum = &H80000000
All = &HFFFFFFFF
End Enum
'
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure SERVER_INFO_101
'
Public dwPlatformID As PlatformID
'
<MarshalAs(UnmanagedType.LPWStr)> _
Public lpszServerName As String
'
Public dwVersionMajor As Integer
Public dwVersionMinor As Integer
'
Public dwType As ServerType
'
<MarshalAs(UnmanagedType.LPWStr)> _
Public lpszComment As String
End StructureIn this case, fields of type
DWORD
map toInteger
s, orEnum
s based onInteger
(the default). Fields of typeLPWSTR
map toString
s with theMarshalAs(UnmanagedType.LPWStr)
attribute. Then, assumingTempBufPtr
is anIntPtr
pointing to the current item, you can use:Imports System.Runtime.InteropServices
...
Dim tS As Type = GetType(SERVER_INFO_101)
Dim currentItem As SERVER_INFO_101
currentItem = DirectCast(Marshal.PtrToStructure(TempBufPtr, tS), tS)
Dim ServerName As String = cu -
Vipul Bhatt wrote: tell me how can i use the RtlMoveMemory in VB.NET Don't! For most cases, using the
MarshalAs
andStructLayout
attributes will be sufficient. In cases when these don't work, the Marshal class provides functions to convert between managed types andIntPtr
s. For example:Imports System.Runtime.InteropServices
'
Public Enum PlatformID
DOS = 300
OS2 = 400
NT = 500
OSF = 600
VMS = 700
End Enum
'
<Flags> _
Public Enum ServerType
None = &H00000000
Workstation = &H00000001
Server = &H00000002
SQLServer = &H00000004
DomainController = &H00000008
DomainBackupController = &H00000010
TimeSource = &H00000020
AFP = &H00000040
Novell = &H00000080
DomainMember = &H00000100
PrintQueue = &H00000200
Dialin = &H00000400
Xenix = &H00000800
Unix = Xenix
NT = &H00001000
WFW = &H00002000
MFPN = &H00004000
NTServer = &H00008000
PotentialBrowser = &H00010000
BackupBrowser = 0x00020000
MasterBrowser = &H00040000
DomainMaster = &H00080000
OSF = &H00100000
VMS = &H00200000
Windows = &H00400000
DFS = &H00800000
ClusterNT = &H01000000
TerminalServer = &H02000000
DCE = &H10000000
AlternateXPort = &H20000000
ListOnly = &H40000000
DomainEnum = &H80000000
All = &HFFFFFFFF
End Enum
'
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure SERVER_INFO_101
'
Public dwPlatformID As PlatformID
'
<MarshalAs(UnmanagedType.LPWStr)> _
Public lpszServerName As String
'
Public dwVersionMajor As Integer
Public dwVersionMinor As Integer
'
Public dwType As ServerType
'
<MarshalAs(UnmanagedType.LPWStr)> _
Public lpszComment As String
End StructureIn this case, fields of type
DWORD
map toInteger
s, orEnum
s based onInteger
(the default). Fields of typeLPWSTR
map toString
s with theMarshalAs(UnmanagedType.LPWStr)
attribute. Then, assumingTempBufPtr
is anIntPtr
pointing to the current item, you can use:Imports System.Runtime.InteropServices
...
Dim tS As Type = GetType(SERVER_INFO_101)
Dim currentItem As SERVER_INFO_101
currentItem = DirectCast(Marshal.PtrToStructure(TempBufPtr, tS), tS)
Dim ServerName As String = cuIt worked A BIG THANKS RICHARDS. Great help. God bless you.
-
Vipul Bhatt wrote: tell me how can i use the RtlMoveMemory in VB.NET Don't! For most cases, using the
MarshalAs
andStructLayout
attributes will be sufficient. In cases when these don't work, the Marshal class provides functions to convert between managed types andIntPtr
s. For example:Imports System.Runtime.InteropServices
'
Public Enum PlatformID
DOS = 300
OS2 = 400
NT = 500
OSF = 600
VMS = 700
End Enum
'
<Flags> _
Public Enum ServerType
None = &H00000000
Workstation = &H00000001
Server = &H00000002
SQLServer = &H00000004
DomainController = &H00000008
DomainBackupController = &H00000010
TimeSource = &H00000020
AFP = &H00000040
Novell = &H00000080
DomainMember = &H00000100
PrintQueue = &H00000200
Dialin = &H00000400
Xenix = &H00000800
Unix = Xenix
NT = &H00001000
WFW = &H00002000
MFPN = &H00004000
NTServer = &H00008000
PotentialBrowser = &H00010000
BackupBrowser = 0x00020000
MasterBrowser = &H00040000
DomainMaster = &H00080000
OSF = &H00100000
VMS = &H00200000
Windows = &H00400000
DFS = &H00800000
ClusterNT = &H01000000
TerminalServer = &H02000000
DCE = &H10000000
AlternateXPort = &H20000000
ListOnly = &H40000000
DomainEnum = &H80000000
All = &HFFFFFFFF
End Enum
'
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure SERVER_INFO_101
'
Public dwPlatformID As PlatformID
'
<MarshalAs(UnmanagedType.LPWStr)> _
Public lpszServerName As String
'
Public dwVersionMajor As Integer
Public dwVersionMinor As Integer
'
Public dwType As ServerType
'
<MarshalAs(UnmanagedType.LPWStr)> _
Public lpszComment As String
End StructureIn this case, fields of type
DWORD
map toInteger
s, orEnum
s based onInteger
(the default). Fields of typeLPWSTR
map toString
s with theMarshalAs(UnmanagedType.LPWStr)
attribute. Then, assumingTempBufPtr
is anIntPtr
pointing to the current item, you can use:Imports System.Runtime.InteropServices
...
Dim tS As Type = GetType(SERVER_INFO_101)
Dim currentItem As SERVER_INFO_101
currentItem = DirectCast(Marshal.PtrToStructure(TempBufPtr, tS), tS)
Dim ServerName As String = cuThanks Richard, only one change required: currentItem = DirectCast(Marshal.PtrToStructure(TempBufPtr, tS), tS) Just replace tS with Object as below currentItem = DirectCast(Marshal.PtrToStructure(TempBufPtr, tS), Object) A big thanks to you. :cool:
-
The error message usually means that the declaration is wrong, i.e. passing arguments ByVal instead of ByRef. The C++ declaration is:
NET_API_STATUS NetServerEnum(
LPCWSTR servername, // [in] Reserved - must be 0
//
DWORD level, // [in] Level - 100 or 101
//
LPBYTE* bufptr, // [out] Pointer to the buffer that receives the data.
// This buffer is allocated by the system and must be
// freed using the NetApiBufferFree function. Note
// that you must free the buffer even if the function
// fails with ERROR_MORE_DATA.
//
DWORD prefmaxlen, // [in] Prefered max length of returned data (bytes)
//
LPDWORD entriesread, // [out] Pointer to a value that receives the
// count of elements actually enumerated.
//
LPDWORD totalentries, // [out] Pointer to a value that receives the total
// number of visible servers and workstations on
// the network.
//
DWORD servertype, // [in] Specifies a value that filters the server
// entries to return from the enumeration.
//
LPCWSTR domain, // [in] Pointer to a constant string that specifies
// the name of the domain for which a list of
// servers is to be returned.
//
LPDWORD resume_handle // [out] Reserved - must be 0
);In VB.NET, this translates to:
Public Declare Function NetServerEnum Lib "netapi32" ( _
ByVal ServerName As IntPtr, _
ByVal Level As Integer, _
ByRef Buffer As IntPtr, _
ByVal PrefMaxLen As Integer, _
ByRef EntriesRead As Integer, _
ByRef TotalEntries As Integer, _
ByVal ServerType As Integer, _
<MarshalAs(UnmanagedType.LPWStr)> _
ByVal Domain As String, _
ByVal ResumeHandle As IntPtr _
) As IntegerYou then call it as:
...
Dim EntriesRead As Integer = 0
Dim TotalEntries As Integer = 0
Dim Buffer As IntPtr = IntPtr.ZeroDim nRes As Integer = NetServerEnum( _
IntPtr.ZeThanks it works fine. what do i need to do to use netapi32.dll in win9x/NT. More help please. Thanks.