Visual Basic Network Services
NetShareCheck: Determine Remote Folder or Device Share Status
     
Posted:   Wednesday August 04, 2004
Updated:   Monday December 26, 2011
     
Applies to:   VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   Windows NT4, Windows 2000, Windows XP, Windows 2003
Author:   VBnet - Randy Birch
     

Related:  

WNetAddConnection2: Transparently Connect to Network Shares
NetShareCheck: Determine Remote Folder or Device Share Status
WNetEnumResource: Enumerating Local Network Resources
NetConnectionEnum: Enumerating Share Connection Information
NetShareEnum: Enumerating Shared Resources on Other Machines  
NetShareAdd: Create a Local or Remote Share
WNetGetUser: User, Share and Share User for Network Resources
WNetGetConnection: Get UNC Path for Mapped Drive

     
 Prerequisites
For this demo, one of the operating systems listed under OS Restrictions above.

Here's a simple call you can make to determine whether a remote server (or the local machine for that matter) is sharing a specific device. A device can be a file system folder, communications device, printer, IPC or special admin share (eg. C$)

Specific information for a confirmed shared device can be obtained using NetShareGetInfo, whose calling syntax is handled in a manner similar to NetShareEnum (NetShareEnum: Enumerating Shared Resources on Other Machines).

 BAS Module Code
None.

 Form Code
To a form add a command button (Command1) along with the following code. Change the values in the command routine per your requirements:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const ERROR_SUCCESS As Long = 0
Private Const STYPE_DISKTREE As Long = 0
Private Const STYPE_PRINTQ As Long = 1
Private Const STYPE_DEVICE As Long = 2
Private Const STYPE_IPC As Long = 3
Private Const STYPE_TEMPORARY As Long = &H40000000
Private Const STYPE_SPECIAL As Long = &H80000000

Private Declare Function NetShareCheck Lib "netapi32.dll" _
  (servername As Byte, _
   device As Byte, _
   sharetype As Long) As Long


Private Sub Command1_Click()

   Dim sharetype As Long
   Dim success As Long
   Dim bServer() As Byte
   Dim bShare() As Byte
   
  'machine name or vbNullChar (not vbNullString) 
  'for the local machine
   bServer() = "\\laptopxp"
   
  'device of interest - here a file system folder
   bShare() = UCase$("c:\my documents")
   
   If NetShareCheck(bServer(0), bShare(0), sharetype) = ERROR_SUCCESS Then
   
      Debug.Print "device is "; GetSharedDeviceType(sharetype) & " (share type = " & sharetype & ")"
    
   End If

End Sub


Private Function GetSharedDeviceType(sharetype As Long) As String

   Dim buff As String
   
   Select Case sharetype
      Case STYPE_DISKTREE:  buff = "Disk drive"
      Case STYPE_PRINTQ:    buff = "Print queue"
      Case STYPE_DEVICE:    buff = "Communication device"
      Case STYPE_IPC:       buff = "Interprocess communication (IPC)"
      Case STYPE_SPECIAL:   buff = "Special share"
      Case STYPE_TEMPORARY: buff = "Temporary share"
      Case Else
   End Select
      
   GetSharedDeviceType = buff
   
End Function
 Comments

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter