Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. NetServerEnum

NetServerEnum

Scheduled Pinned Locked Moved Visual Basic
helpquestion
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vipul Bhatt
    wrote on last edited by
    #1

    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.

    Richard DeemingR 2 Replies Last reply
    0
    • V Vipul Bhatt

      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.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      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 Integer

      You then call it as:

      ...
      Dim EntriesRead As Integer = 0
      Dim TotalEntries As Integer = 0
      Dim Buffer As IntPtr = IntPtr.Zero

      Dim nRes As Integer = NetServerEnum( _
      IntPtr.Ze

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      V 1 Reply Last reply
      0
      • V Vipul Bhatt

        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.

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        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 where TempBufPtr As IntPtr AND Public 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[^]

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        V 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          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 where TempBufPtr As IntPtr AND Public 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[^]

          V Offline
          V Offline
          Vipul Bhatt
          wrote on last edited by
          #4

          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:

          Richard DeemingR 1 Reply Last reply
          0
          • V Vipul Bhatt

            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:

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            Vipul Bhatt wrote: tell me how can i use the RtlMoveMemory in VB.NET Don't! For most cases, using the MarshalAs and StructLayout attributes will be sufficient. In cases when these don't work, the Marshal class provides functions to convert between managed types and IntPtrs. 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 Structure

            In this case, fields of type DWORD map to Integers, or Enums based on Integer (the default). Fields of type LPWSTR map to Strings with the MarshalAs(UnmanagedType.LPWStr) attribute. Then, assuming TempBufPtr is an IntPtr 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

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            V 2 Replies Last reply
            0
            • Richard DeemingR Richard Deeming

              Vipul Bhatt wrote: tell me how can i use the RtlMoveMemory in VB.NET Don't! For most cases, using the MarshalAs and StructLayout attributes will be sufficient. In cases when these don't work, the Marshal class provides functions to convert between managed types and IntPtrs. 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 Structure

              In this case, fields of type DWORD map to Integers, or Enums based on Integer (the default). Fields of type LPWSTR map to Strings with the MarshalAs(UnmanagedType.LPWStr) attribute. Then, assuming TempBufPtr is an IntPtr 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

              V Offline
              V Offline
              Vipul Bhatt
              wrote on last edited by
              #6

              It worked A BIG THANKS RICHARDS. Great help. God bless you.

              1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                Vipul Bhatt wrote: tell me how can i use the RtlMoveMemory in VB.NET Don't! For most cases, using the MarshalAs and StructLayout attributes will be sufficient. In cases when these don't work, the Marshal class provides functions to convert between managed types and IntPtrs. 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 Structure

                In this case, fields of type DWORD map to Integers, or Enums based on Integer (the default). Fields of type LPWSTR map to Strings with the MarshalAs(UnmanagedType.LPWStr) attribute. Then, assuming TempBufPtr is an IntPtr 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

                V Offline
                V Offline
                Vipul Bhatt
                wrote on last edited by
                #7

                Thanks 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:

                1 Reply Last reply
                0
                • Richard DeemingR Richard Deeming

                  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 Integer

                  You then call it as:

                  ...
                  Dim EntriesRead As Integer = 0
                  Dim TotalEntries As Integer = 0
                  Dim Buffer As IntPtr = IntPtr.Zero

                  Dim nRes As Integer = NetServerEnum( _
                  IntPtr.Ze

                  V Offline
                  V Offline
                  Vipul Bhatt
                  wrote on last edited by
                  #8

                  Thanks it works fine. what do i need to do to use netapi32.dll in win9x/NT. More help please. Thanks.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups