Link to home
Start Free TrialLog in
Avatar of damonc090698
damonc090698

asked on

How to programmatically share/unshare a folder or drive

Does anyone know how to share and unshare a drive or folder programmatically? Also, how can you tell all the drives/folders that are shared on a system?
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Does damonc want to have example on NetShareAdd and NetShareDel?

Try this

MODULE code

Option Explicit

Private Const STYPE_DISKTREE As Long = 0&
Private Const ShareLevel2 As Long = 2&

Private Const ACCESS_NONE As Long = &H0&
Private Const ACCESS_READ As Long = &H1&
Private Const ACCESS_WRITE As Long = &H2&
Private Const ACCESS_CREATE As Long = &H4&
Private Const ACCESS_EXEC As Long = &H8&
Private Const ACCESS_DELETE As Long = &H10&
Private Const ACCESS_ATRIB As Long = &H20&
Private Const ACCESS_PERM As Long = &H40&
Private Const ACCESS_ALL As Long = ACCESS_READ Or ACCESS_WRITE Or ACCESS_CREATE Or ACCESS_EXEC Or ACCESS_DELETE Or ACCESS_ATRIB Or ACCESS_PERM

Private Type SHARE_INFO_2
   shi2_netname As Long
   shi2_type  As Long
   shi2_remark As Long
   shi2_permissions As Long
   shi2_max_uses As Long
   shi2_current_uses As Long
   shi2_path As Long
   shi2_passwd As Long
End Type

Private Declare Function NetShareAdd Lib "netapi32.dll" (ByRef servername As Byte, ByVal level As Long, ByVal Buffer As Any, ByVal parm_err As Long) As Long
Private Declare Function NetShareDel Lib "netapi32.dll" (ByRef servername As Byte, ByRef netname As Byte, ByVal reserved As Long) As Long

Public Function AddShare(ByVal xi_strServerName As String, ByVal xi_strShareName As String, ByVal xi_strPath As String, ByVal xi_strRemark As String) As Boolean
   Dim p_lngRtn As Long
   Dim p_abytServerName() As Byte
   Dim p_strErr As String
   Dim p_typShareInfo2 As SHARE_INFO_2
   Dim p_lngPtrToType As Long
   Dim p_strPassword As String
   Dim p_lngParmErr As Long
   
   ' ------------------------------
   ' Convert strings to byte arrays
   ' ------------------------------
   p_abytServerName = xi_strServerName & vbNullChar
   p_strPassword = vbNullString
   p_typShareInfo2.shi2_netname = StrPtr(xi_strShareName)
   p_typShareInfo2.shi2_type = STYPE_DISKTREE
   p_typShareInfo2.shi2_remark = StrPtr(xi_strRemark)
   p_typShareInfo2.shi2_permissions = ACCESS_ALL
   p_typShareInfo2.shi2_max_uses = -1
   p_typShareInfo2.shi2_current_uses = 0
   p_typShareInfo2.shi2_path = StrPtr(xi_strPath)
   p_typShareInfo2.shi2_passwd = StrPtr(p_strPassword)
   p_lngPtrToType = VarPtr(p_typShareInfo2)
   
   p_lngRtn = NetShareAdd(servername:=p_abytServerName(0), level:=ShareLevel2, Buffer:=p_lngPtrToType, parm_err:=p_lngParmErr)
End Function

Public Function DelShare(ByVal servername As String, ByVal ShareName As String)
    Dim p_abytServerName() As Byte, shi2_netname As Long
    Dim p_abytServerName1() As Byte
   
    p_abytServerName = servername & vbNullChar
    p_abytServerName1 = ShareName & vbNullChar
    'shi2_netname = StrPtr(ShareName)
    NetShareDel p_abytServerName(0), p_abytServerName1(0), 0
End Function

FORM code
To use

AddShare "", "TEMP", "c:\TEMP", "TEMP DIRECTORY"
DelShare "", "TEMP"

Avatar of damonc090698
damonc090698

ASKER

emoreau you're not supposed to lock a question. The normal procedure is to post as a comment. Besides my question was not how to map a drive, but how to add a share. EDDYKT, your answer is what I was looking for. Do you know how to determine all the shares on a computer? Also, in your example above, how can you tell whether the process was succesful or not?
emoreau you're not supposed to lock a question. The normal procedure is to post as a comment. Besides my question was not how to map a drive, but how to add a share. EDDYKT, your answer is what I was looking for. Do you know how to determine all the shares on a computer? Also, in your example above, how can you tell whether the process was succesful or not?
emoreau you're not supposed to lock a question. The normal procedure is to post as a comment. Besides my question was not how to map a drive, but how to add a share. EDDYKT, your answer is what I was looking for. Do you know how to determine all the shares on a computer? Also, in your example above, how can you tell whether the process was succesful or not?
Sorry for the three posts. There is a problem with the Expert's Exchange site and it posted it 3 times instead of one.
You will have to use the NetShareEnum api, see here for an example

http://www.killervb.com/grabbag.html see the NetFileInfo example
i might have missed something but that was just an network share enumeration package...

search the msdn for NetShareAdd...which is the api call you're looking for in order to share resources...then there's NetShareDel, NetShareGetInfo, NetShareSetInfo, and NetShareCheck...

okay...i've been up tooooo long...my appologies to everyone...i really _read_ WNetAddConnection from EDDYKT's post and completely overlooked the question for an enumeration...again sorry to all.

ASKER CERTIFIED SOLUTION
Avatar of EDDYKT
EDDYKT
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
where "kwane" is your server name
Thank you EDDYKT. The code samples worked great and were easy to implement. I am curious about one thing though. What is the IPC$ share? If you don't know it off the top of your head don't worry. I was just curious. Once again, thanks for your responses.
(interprocess communication)

the ipc share is basically used to bind to other rpc services and provides lanmanager functions...