HELP!在VB中设置共享目录的API谁知道?并且会用?!请指教!在线等待您的回答!
最好给个事例,就比如说将 C:\A 目录设置为共享!
还有最好能够告诉我,怎么设置为只读,怎么设置为,完全,或者密码,并且怎么通过程序进入有密码的目录!
请各位多给我指教指教!!!!
最好给个事例,就比如说将 C:\A 目录设置为共享!
还有最好能够告诉我,怎么设置为只读,怎么设置为,完全,或者密码,并且怎么通过程序进入有密码的目录!
请各位多给我指教指教!!!!
解决方案 »
- 100分求解:"怎样锁定IE窗口的大小,设置为不可调整大小?并且显示在屏幕正中?"[ 急啊>_<b ]
- 如何判断SQL 中某个数据库是否存在?
- 1000分求网络中设置主页,UP有分,解决了立刻开帖结分。
- 使用VB6的安装打包向导,可以让卸载图标出现,但是桌面图标没有出现?~?~~在重新编译Setup1.Exe时出错~!!!提示gstrDIR_DEST变量未定
- VB:如何修改命令按钮Caption属性的文字颜色?
- 关于Access数据库用SELECT查询四舍五入的问题?
- 请问在网页上的超连接的那只小手的.co在哪有的?
- 如何用VB把一个XML文件变为ZIP文件
- 简单问题:如何得知据库中有几个表,表名分别是什末?,即可回分!!
- 突破950软件怎么 下载不了
- RadioButton的问题?
- 给点儿思路或建议,来者有分,不够再加...
最好给个事例,就比如说将 C:\A 目录设置为共享!
还有最好能够告诉我,怎么设置为只读,怎么设置为,完全,或者密码,并且怎么通过程序进入有密码的目录!
请各位多给我指教指教!!!!
Public Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" _
(lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUsername As String, _
ByVal dwFlags As Long) As LongPublic Declare Function WNetCancelConnection2 Lib "mpr.dll" _
Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long, _
ByVal fForce As Long) As LongPublic ErrorNum As Long
Public ErrorMsg As String
Public rc As Long 'Holds the Return Code from a called function
Public RemoteName As String'Consts for return codes errors
Public Const ERROR_BAD_DEV_TYPE = 66&
Public Const ERROR_ALREADY_ASSIGNED = 85&
Public Const ERROR_ACCESS_DENIED = 5&
Public Const ERROR_BAD_NET_NAME = 67&
Public Const ERROR_BAD_PROFILE = 1206&
Public Const ERROR_BAD_PROVIDER = 1204&
Public Const ERROR_BUSY = 170&
Public Const ERROR_CANCEL_VIOLATION = 173&
Public Const ERROR_CANNOT_OPEN_PROFILE = 1205&
Public Const ERROR_DEVICE_ALREADY_REMEMBERED = 1202&
Public Const ERROR_EXTENDED_ERROR = 1208&
Public Const ERROR_INVALID_PASSWORD = 86&
Public Const ERROR_NO_NET_OR_BAD_PATH = 1203&
Public Const ERROR_NO_NETWORK = 1222&
Public Const ERROR_NO_CONNECTION = 8
Public Const ERROR_NO_DISCONNECT = 9
Public Const ERROR_DEVICE_IN_USE = 2404&
Public Const ERROR_NOT_CONNECTED = 2250&
Public Const ERROR_OPEN_FILES = 2401&
Public Const ERROR_MORE_DATA = 234Public Const CONNECT_UPDATE_PROFILE = &H1
Public Const RESOURCETYPE_DISK = &H1Public Type NETRESOURCE
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
lpLocalName As String
lpRemoteName As String
lpComment As String
lpProvider As String
End TypePublic lpNetResourse As NETRESOURCEPublic Declare Function FormatMessage Lib "kernel32" _
Alias "FormatMessageA" (ByVal dwFlags As Long, _
lpSource As Any, ByVal dwMessageId As Long, _
ByVal dwLanguageId As Long, ByVal lpBuffer As String, _
ByVal nSize As Long, Arguments As Long) As LongPublic Const FORMAT_MESSAGE_FROM_SYSTEM As Long = &H1000Public Sub Connect(ByVal HostName As String, ByVal RemoteName As String, ByVal UserName As String, ByVal Password As String)Dim lpUsername As String
Dim lpPassword As StringOn Error GoTo Err_Connect
ErrorNum = 0
ErrorMsg = vbNullString
lpNetResourse.dwType = RESOURCETYPE_DISK
lpNetResourse.lpLocalName = RemoteName & ChrW$(0)
'Drive Letter to use
lpNetResourse.lpRemoteName = "\\" & HostName & ChrW$(0)
'Network Path to share
lpNetResourse.lpProvider = ChrW$(0)
lpPassword = Password & ChrW$(0)
'password on share pass "" if none
lpUsername = UserName & ChrW$(0)
'username to connect as if applicable
rc = WNetAddConnection2(lpNetResourse, lpPassword, lpUsername, CONNECT_UPDATE_PROFILE)
If rc <> 0 Then GoTo Err_ConnectExit SubErr_Connect:
ErrorNum = rc
ErrorMsg = WnetError(rc)MsgBox ApiErrorText(ErrorNum)
End SubPublic Sub DisConnect(ByVal Name As String, ByVal ForceOff As Boolean)On Error GoTo Err_DisConnect
ErrorNum = 0
ErrorMsg = vbNullString
rc = WNetCancelConnection2(Name & ChrW$(0), CONNECT_UPDATE_PROFILE, ForceOff)
If rc <> 0 Then GoTo Err_DisConnectExit SubErr_DisConnect:
ErrorNum = rc
ErrorMsg = WnetError(rc)
MsgBox ErrorMsg
End SubPrivate Function WnetError(Errcode As Long) As StringSelect Case Errcode
Case ERROR_BAD_DEV_TYPE
WnetError = "Bad device."
Case ERROR_ALREADY_ASSIGNED
WnetError = "Already Assigned."
Case ERROR_ACCESS_DENIED
WnetError = "Access Denied."
Case ERROR_BAD_NET_NAME
WnetError = "Bad net name"
Case ERROR_BAD_PROFILE
WnetError = "Bad Profile"
Case ERROR_BAD_PROVIDER
WnetError = "Bad Provider"
Case ERROR_BUSY
WnetError = "Busy"
Case ERROR_CANCEL_VIOLATION
WnetError = "Cancel Violation"
Case ERROR_CANNOT_OPEN_PROFILE
WnetError = "Cannot Open Profile"
Case ERROR_DEVICE_ALREADY_REMEMBERED
WnetError = "Device already remembered"
Case ERROR_EXTENDED_ERROR
WnetError = "Device already remembered"
Case ERROR_INVALID_PASSWORD
WnetError = "Invalid Password"
Case ERROR_NO_NET_OR_BAD_PATH
WnetError = "Could not find the specified device"
Case ERROR_NO_NETWORK
WnetError = "No Network Present"
Case ERROR_DEVICE_IN_USE
WnetError = "Connection Currently in use "
Case ERROR_NOT_CONNECTED
WnetError = "No Connection Present"
Case ERROR_OPEN_FILES
WnetError = "Files open and the force parameter is false"
Case ERROR_MORE_DATA
WnetError = "Buffer to small to hold network name, make lpnLength bigger"
Case Else:
WnetError = "Unrecognized Error " & Str$(Errcode) & "."
End SelectEnd FunctionPublic Function ApiErrorText(ByVal ErrNum As Long) As String
Dim msg As String
Dim nRet As Longmsg = Space$(1024)
nRet = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, _
ErrNum, 0&, msg, Len(msg), ByVal 0&)
If nRet Then
ApiErrorText = Left$(msg, nRet)
Else
ApiErrorText = "Error (" & ErrNum & ") not defined."
End If
End Function
你这个代码是Win98的还是Win2000的???
设的共享是只读的还是完全的呢??
真想知道这个API吗?我可不想你去犯罪啊》
'Code by Roy Strickland, submitted by Jarret Peterson
'This code can share and unshare the directory 'c:\dos''===================================
'start a new project and add three command buttons
'set forms AutoRedraw property to true
'ADD TO A MODULE IN YOUR PROJECT:
'====================================
Option Explicit
Public Platform As Long 'Platform ID of OS. 1 or 2'Structure for Getversion
Public Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128 ' Maintenance string for PSS usage
End TypePublic Const STYPE_DISKTREE As Long = 0
Public Const STYPE_PRINTQ As Long = 1
Public Const STYPE_DEVICE As Long = 2
Public Const STYPE_IPC As Long = 3'Access types
Public Const ACCESS_READ As Long = &H1
Public Const ACCESS_WRITE As Long = &H2
Public Const ACCESS_CREATE As Long = &H4
Public Const ACCESS_EXEC As Long = &H8
Public Const ACCESS_DELETE As Long = &H10
Public Const ACCESS_ATRIB As Long = &H20
Public Const ACCESS_PERM As Long = &H40
Public Const ACCESS_ALL As Long = &H7F
Public Const WNTYPE_DRIVE As Long = 1
Public Const SHI_USES_UNLIMITED As Long = -1'Info structures for NetShareAdd
Type SHARE_INFO_2
shi2_netname As String * 14
shi2_type As Long
shi2_re As String 'Far pointer to string
shi2_permissions As Long
shi2_max_uses As Long
shi2_current_uses As Long
shi2_path As String 'Far pointer to string
shi2_passwd As String * 10
End TypeType SHARE_INFO_50
shi50_netname As String
shi50_type As String
shi50_flags As Long
shi50_re As String
shi50_path As String
shi50_rw_password As String
shi50_ro_password As String
End Type'ACL for Security Descriptor
Public Type ACL
AclRevision As Byte
Sbz1 As Byte
AclSize As Integer
AceCount As Integer
Sbz2 As Integer
End Type'Security Descriptor for SHARE_INFO_502
Public Type SECURITY_DESCRIPTOR
Revision As Byte
Sbz1 As Byte
Control As Long
Owner As Long
Group As Long
Sacl As ACL
Dacl As ACL
End TypeType SHARE_INFO_502
shi502_netname As String
shi502_type As Long
shi502_re As String
shi502_permissions As Long
shi502_max_uses As Long
shi502_current_uses As Long
shi502_path As String
shi502_passwd As String
shi502_reserved As Long
shi502_security_descriptor As SECURITY_DESCRIPTOR
End TypePublic Security As SECURITY_DESCRIPTORPublic Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" _
(lpVersionInformation As OSVERSIONINFO) As Long
Public Declare Function lstrcpy Lib "kernel32" _
(ByVal lpString1 As Any, ByVal lpString2 As Any) As Long
'NT
Public Declare Function NetShareDelNT Lib "netapi32.dll" Alias "NetShareDel" _
(ByVal servername As Any, ByVal netname As String, ByVal reserved As Long) As Long
Public Declare Function NetShareAddNT Lib "netapi32.dll" Alias "NetShareAdd" _
(ByVal servername As Any, ByVal slevel As Long, _
buf As SHARE_INFO_502, ByVal cbbuf As Long) As Long
'9x
Public Declare Function NetShareDel9x Lib "svrapi.dll" Alias "NetShareDel" _
(ByVal servername As Any, ByVal netname As String, ByVal reserved As Long) As Long
Public Declare Function NetShareAdd9x Lib "svrapi.dll" Alias "NetShareAdd" _
(ByVal servername As Any, ByVal slevel As Long, buf As SHARE_INFO_50, ByVal cbbuf As Long) As Long窗体:
'====================
'ADD CODE TO FORM:
'====================
Option Explicit
Dim SI2 As SHARE_INFO_2
Dim SI502 As SHARE_INFO_502
Dim SI50 As SHARE_INFO_50
Dim OSVERInfo As OSVERSIONINFO
Dim ShareRe As String
Dim SharePath As String
Dim nerr As Long
Dim nPath As String
Dim pwd As String
Dim ret As Long
Dim OS As Long
Private Sub Form_Load()
OSVERInfo.dwOSVersionInfoSize = Len(OSVERInfo)
OS = GetVersionEx(OSVERInfo)
Command1.Caption = "Create Share NT"
Command2.Caption = "Create Share Win9x"
Command3.Caption = "Delete Share"
End Sub
Private Sub Command1_Click()
'NT
On Error Resume Next
SetStrings
nerr = NetShareAddNT(0&, 2, SI502, ret)
Print nerr
End Sub
Private Sub Command2_Click()
'9x
On Error Resume Next
SetStrings
nerr = NetShareAdd9x(0&, 50, SI50, ret)
Print nerr
End Sub
Private Sub Command3_Click()
'Delete
On Error Resume Next
If OSVERInfo.dwPlatformId = 1 Then
nerr = NetShareDel9x(0&, nPath, 0&)
Else
nerr = NetShareDelNT(0&, nPath, 0&)
Print nerr
End If
End Sub
Public Sub SetStrings()
If OSVERInfo.dwPlatformId = 1 Then
'9x OS
nPath = "NewShare"
ShareRe = "Re for new share"
SharePath = "C:\windows.0"
pwd = "Share"
SI50.shi50_netname = nPath
SI50.shi50_path = SharePath SI50.shi50_re = ShareRe
SI50.shi50_type = STYPE_DISKTREE
SI50.shi50_ro_password = vbNullChar
SI50.shi50_rw_password = vbNullChar
Else
'NT OS
nPath = StrConv("NewShare", vbUnicode)
ShareRe = StrConv("Re for new share", vbUnicode)
SharePath = StrConv("C:\windows.0", vbUnicode)
pwd = StrConv("Share", vbUnicode)
SI502.shi502_current_uses = 0
SI502.shi502_max_uses = 10
SI502.shi502_netname = nPath
SI502.shi502_passwd = pwd
SI502.shi502_path = SharePath
SI502.shi502_permissions = ACCESS_ALL
SI502.shi502_re = ShareRe
SI502.shi502_reserved = 0
SI502.shi502_security_descriptor = Security
SI502.shi502_type = STYPE_DISKTREE
End If
End Sub