How to Share a Folder using NetShareAdd
解决方案 »
- 还是乱码问题:VB6(en)+SQL2000,C/S,在繁体XP里浏览SQL里的简体资料时乱码
- Combobox设计时在List里输入一位数字,一回关就变乱码,我的VB出问题了?
- 小弟求助!在VB+SQL编程中,如何将数据库中某一张表的具体信息添加入下拉列表框?
- 请教:VB中导出EXCEL时,怎样把单元格的格式设置成百分比格式,并且保留小数位3位,在线等~~急!!
- 如何放大datagrid控件的滚动条按钮
- 怎样是文本框获取焦点的时候,是里面的内容处于全选的状态
- 我的程序使用了API REGCREATEVALUE函数实现自动加载,但在卸载后发现键值依然还存在,我该怎么办?
- C/S结构的数据库程序,VB作前端,如何连接??
- VPN终端及隧道安全
- 怎样是一个窗体显示2秒后显示另外一个窗体?
- 关于vb中调用API函数获取系统信息的代码
- MSFlexGrid、MSHFlexGrid和DataGrid的问题
Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2002 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 can not publish
' or reproduce this code on any web site,
' on any online service, or distribute on
' any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const NERR_SUCCESS As Long = 0&'share types
Private Const STYPE_ALL As Long = -1 'note: my const
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_SPECIAL As Long = &H80000000'permissions
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_PERMPrivate Type SHARE_INFO_2
shi2_netname As Long
shi2_type As Long
shi2_re 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" _
(ByVal servername As Long, _
ByVal level As Long, _
buf As Any, _
parmerr As Long) As Long Private Sub Form_Load()
Text1.Text = "\\" & Environ$("COMPUTERNAME")
Text2.Text = "c:\program files\adobe"
Text3.Text = "vbnetdemo"
Text4.Text = "VBnet demo test share"
Text5.Text = ""
End Sub
Private Sub Command1_Click() Dim success As Long
success = ShareAdd(Text1.Text, _
Text2.Text, _
Text3.Text, _
Text4.Text, _
Text5.Text)
Select Case success
Case 0: MsgBox "share created successfully!"
Case 2118: MsgBox "share name already exists"
Case Else: MsgBox "create error " & success
End SelectEnd Sub
Private Function ShareAdd(sServer As String, _
sSharePath As String, _
sShareName As String, _
sShareRe As String, _
sSharePw As String) As Long
Dim dwServer As Long
Dim dwNetname As Long
Dim dwPath As Long
Dim dwRe As Long
Dim dwPw As Long
Dim parmerr As Long
Dim si2 As SHARE_INFO_2
'obtain pointers to the server, share and path
dwServer = StrPtr(sServer)
dwNetname = StrPtr(sShareName)
dwPath = StrPtr(sSharePath)
'if the re or password specified,
'obtain pointer to those as well
If Len(sShareRe) > 0 Then
dwRe = StrPtr(sShareRe)
End If
If Len(sSharePw) > 0 Then
dwPw = StrPtr(sSharePw)
End If
'prepare the SHARE_INFO_2 structure
With si2
.shi2_netname = dwNetname
.shi2_path = dwPath
.shi2_re = dwRe
.shi2_type = STYPE_DISKTREE
.shi2_permissions = ACCESS_ALL
.shi2_max_uses = -1
.shi2_passwd = dwPw
End With
'add the share
ShareAdd = NetShareAdd(dwServer, _
2, _
si2, _
parmerr)
End Function
Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2002 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 can not publish
' or reproduce this code on any web site,
' on any online service, or distribute on
' any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const NERR_SUCCESS As Long = 0&'share types
Private Const STYPE_ALL As Long = -1 'note: my const
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_SPECIAL As Long = &H80000000'permissions
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_PERMPrivate Type SHARE_INFO_2
shi2_netname As Long
shi2_type As Long
shi2_re 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" _
(ByVal servername As Long, _
ByVal level As Long, _
buf As Any, _
parmerr As Long) As Long Private Sub Form_Load()
Text1.Text = "\\" & Environ$("COMPUTERNAME")
Text2.Text = "c:\program files\adobe"
Text3.Text = "vbnetdemo"
Text4.Text = "VBnet demo test share"
Text5.Text = ""
End Sub
Private Sub Command1_Click() Dim success As Long
success = ShareAdd(Text1.Text, _
Text2.Text, _
Text3.Text, _
Text4.Text, _
Text5.Text)
Select Case success
Case 0: MsgBox "share created successfully!"
Case 2118: MsgBox "share name already exists"
Case Else: MsgBox "create error " & success
End SelectEnd Sub
Private Function ShareAdd(sServer As String, _
sSharePath As String, _
sShareName As String, _
sShareRe As String, _
sSharePw As String) As Long
Dim dwServer As Long
Dim dwNetname As Long
Dim dwPath As Long
Dim dwRe As Long
Dim dwPw As Long
Dim parmerr As Long
Dim si2 As SHARE_INFO_2
'obtain pointers to the server, share and path
dwServer = StrPtr(sServer)
dwNetname = StrPtr(sShareName)
dwPath = StrPtr(sSharePath)
'if the re or password specified,
'obtain pointer to those as well
If Len(sShareRe) > 0 Then
dwRe = StrPtr(sShareRe)
End If
If Len(sSharePw) > 0 Then
dwPw = StrPtr(sSharePw)
End If
'prepare the SHARE_INFO_2 structure
With si2
.shi2_netname = dwNetname
.shi2_path = dwPath
.shi2_re = dwRe
.shi2_type = STYPE_DISKTREE
.shi2_permissions = ACCESS_ALL
.shi2_max_uses = -1
.shi2_passwd = dwPw
End With
'add the share
ShareAdd = NetShareAdd(dwServer, _
2, _
si2, _
parmerr)
End Function