I'm not sure if there is a easier way or not. This will work for Win2k and I'm pretty sure WinXP. Not sure about earlier versions of windows. Place this in a module - Option Explicit Private Const STYPE_DISKTREE As Long = 0 'disk drive Private Const STYPE_PRINTQ As Long = 1 'printer Private Const STYPE_DEVICE As Long = 2 Private Const STYPE_IPC As Long = 3 Private Type SHARE_INFO_2 shi2_netname As String 'LPWSTR shi2_type As Long 'DWORD shi2_remark As String 'LPWSTR shi2_permissions As Long 'DWORD shi2_max_uses As Long 'DWORD shi2_current_uses As Long 'DWORD shi2_path As String 'LPWSTR shi2_passwd As String 'LPWSTR End Type Private Declare Function NetShareAdd Lib "netapi32.dll" ( _ lpwstrServerName As Byte, _ ByVal dwordLevel As Long, _ ByVal lpbyteBuf As Long, _ lpdwordParmErr As Long) As Long Public Function ShareIt(ByVal CompterName As String, _ ByVal SharePath As String, _ Optional ShareName As String = " ", _ Optional ByVal ShareRemarks As String = " ") As Long ' This function returns 0 if successful and ' a none zero number if the function fails. Dim si2 As SHARE_INFO_2 Dim parmerr As Long Dim compname() As Byte parmerr = 0 compname() = CompterName & vbNullChar si2.shi2_netname = ShareName 'the name of the share si2.shi2_type = STYPE_DISKTREE 'the share is on the disk si2.shi2_remark = ShareRemarks & vbNullChar 'the comment si2.shi2_permissions = 0 'this should be ignored si2.shi2_max_uses = -1 'unlimited connections si2.shi2_current_uses = 0 'I don't think this is applicable si2.shi2_path = SharePath 'the path to the share si2.shi2_passwd = vbNullString ' the password 'this should be ignored ShareIt = NetShareAdd(compname(0), 2, VarPtr(si2), parmerr) End Function
And to use it, call it from your form like this - Private Sub Command1_Click() If ShareIt("CC687927-b", "c:\temp") = 0 Then MsgBox "The share was created" Else MsgBox "The share could not be created with the information you supplied" End If ' 'to share a drive ' If ShareIt("CC687927-b", "c:\") = 0 Then End Sub
|