DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: once more: NetShareAdd --> save me from going mad!!

  1. #1
    Daniel Teufert Guest

    once more: NetShareAdd --> save me from going mad!!


    Hi folks,

    same ****, different day:

    I'm trying to share a directory on the server, but I just can't manage it.

    I have the following code which works fine as long as I stay on my local
    mashine.
    But if I try to share a directory on the server, ERROR 123 occures and tells
    me that "the syntax of filename, directoryname or drivename is wrong".

    I'm logged in on my pc (WinNT) as "Teufert", on the server I'm a member of
    the group "administrator".
    The directory I want to share is "\\AIS_SERVER_DO\Transfer\Teufert\shareTest".

    I get the foldername from a foldertreeview and it is exactly the string "\\AIS_SERVER_DO\Transfer\Teufert\shareTest".

    Sorry that I bother you with so much lines of code, but I didn't know what
    you need to find the bug, so I copied all I think it mażbe relevant.

    So here is the code:
    --------------------------------------------------------------------------
    '##################
    'code of the 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 ShareRemark 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

    Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
    Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
    Const LANG_NEUTRAL = &H0
    Const SUBLANG_DEFAULT = &H1

    Private Declare Function GetLastError Lib "kernel32" () As Long
    Private 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 Long


    Private Sub Form_Load()
    OSVERInfo.dwOSVersionInfoSize = Len(OSVERInfo)
    OS = GetVersionEx(OSVERInfo)
    End Sub


    Private Sub btnCreateShare_Click()

    Dim buffer As String

    On Error Resume Next
    SetStrings
    nerr = NetShareAddNT(0&, 2, SI502, ret)

    'Create a string buffer
    buffer = Space(2000)
    'Format the message string
    FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, nerr, _
    LANG_NEUTRAL, buffer, 200, ByVal 0&
    'Show the message
    MsgBox buffer
    End Sub

    Public Sub SetStrings()

    nPath = StrConv("NewShare", vbUnicode)
    ShareRemark = StrConv("Remark for new share", vbUnicode)
    SharePath = StrConv(foldertreeview1.selectedfolder, 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_remark = ShareRemark
    SI502.shi502_reserved = 0
    SI502.shi502_security_descriptor = Security
    SI502.shi502_type = STYPE_DISKTREE
    End Sub

    --------------------------------------------------------------------------
    '###################
    'code of the module#
    '###################

    Option Explicit

    Public Platform As Long

    '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
    End Type

    Public 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_remark 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 Type

    Type SHARE_INFO_50
    shi50_netname As String
    shi50_type As String
    shi50_flags As Long
    shi50_remark 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 Type

    Type SHARE_INFO_502
    shi502_netname As String
    shi502_type As Long
    shi502_remark 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 Type

    Public Security As SECURITY_DESCRIPTOR

    Public 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

    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

    --------------------------------------------------------------------------

    What the **** I'm doing wrong?

    Thank you all in advance!!

    Daniel

  2. #2
    L.J. Johnson Guest

    Re: once more: NetShareAdd --> save me from going mad!!

    Daniel,

    GetLastError -- don't use in VB -- use Err.LastDllError instead.

    The SI502.shi502_path is *NOT* the UNC path. It is the path as if you were
    setting at the console of that machine. So, if the path is actually
    C:\Transfer\Teufert\shareTest, then that is the path you need to give. You
    are giving the function the name of the server, and the path *from that
    server's perspective*.

    --
    L.J. Johnson, Slightly Tilted Software
    Microsoft MVP (Visual Basic)
    LJJohnson@SlightlyTiltedSoftware.com or LJJohnson@mvps.org
    <http://www.SlightlyTiltedSoftware.com>
    Ask The NT Pro at <http://www.devx.com/gethelp>



  3. #3
    Daniel Teufert Guest

    Re: once more: NetShareAdd --> save me from going mad!!


    "L.J. Johnson" <LJJohnson@SlightlyTiltedSoftware.com> wrote:
    >Daniel,
    >
    >GetLastError -- don't use in VB -- use Err.LastDllError instead.
    >
    >The SI502.shi502_path is *NOT* the UNC path. It is the path as if you were
    >setting at the console of that machine. So, if the path is actually
    >C:\Transfer\Teufert\shareTest, then that is the path you need to give. You
    >are giving the function the name of the server, and the path *from that
    >server's perspective*.
    >


    Hi L.J.,

    thanks for your support, but I'm still unable to create a share.
    Now I get the errormessage: "The network path can't be found".

    Pathnames: Local: on Server:
    \\AIS_SERVER_DO\testdt c:\testdt
    \\AIS_SERVER_DO\Transfer\Teufert\NewDir h:\Transfer\Teufert\NewDir

    So I thought it would be helpful to combine all servername-variantions and
    pathname-variations I could imagine. I created two arrays and combined them
    as follows:

    arrayServer(1) = "\\Ais_server_do"
    arrayServer(2) = "\\ais_server_do\"
    arrayServer(3) = "\\AIS_SERVER_DO"
    arrayServer(4) = "\\AIS_SERVER_DO\"
    arrayServer(5) = "AIS_SERVER_DO"
    arrayServer(6) = "ais_server_do"

    arrayPath(1) = "c:\testdt"
    arrayPath(2) = "c$\testdt"
    arrayPath(3) = "h:\transfer\teufert\newdir"
    arrayPath(4) = "h$\transfer\teufert\newdir"
    arrayPath(5) = "\transfer\teufert\newdir"
    arrayPath(6) = "transfer\teufert\newdir"
    arrayPath(7) = "\transfer\teufert\newdir\"
    arrayPath(8) = "transfer\teufert\newdir\"
    arrayPath(9) = "\c:\testdt"
    arrayPath(10) = "\c$\testdt"
    arrayPath(11) = "\c:\testdt\"
    arrayPath(12) = "\c$\testdt\"
    arrayPath(13) = "\\AIS_SERVER_DO\transfer\teufert\newdir"
    arrayPath(14) = "\\AIS_SERVER_DO/transfer/teufert/newdir"

    For i = 1 To UBound(arrayServer)
    For j = 1 To UBound(arrayPath)
    SetStrings (arrayPath(j)) 'here the pathname is put into
    'SI502.shi502_path value (as vbUnicode)
    nerr = NetShareAddNT(arrayServer(i), 2, SI502, ret)
    'Create a string buffer
    buffer = Space(200)
    'Set the error number
    'SetLastError ERROR_BAD_USERNAME
    'Format the message string
    FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, _
    nerr, LANG_NEUTRAL, buffer, 200, ByVal 0&
    'Show the message
    List1.AddItem (nerr & ": i=" & i & " j=" & j & "--> " & buffer)
    If nerr = 0 Then MsgBox "Hoooraayy! " & arrayServer(i)_
    & " " & arrayPath(j)
    If Err.LastDllError Then
    MsgBox "Err-No: " & Err.Number & ": " & Err.Description
    End If
    Next j
    Next i

    None of the above combinations succeeded :-(
    The only thing I always received was error 53: The network path couldn't
    be found.

    I'm on the verge of throwing my pc out of the window!!
    Why does it always do exactly what I tell him, but not what I want him to
    do?

    &h54 &h49 &h41

    Daniel

  4. #4
    L.J. Johnson Guest

    Re: once more: NetShareAdd --> save me from going mad!!

    Daniel,

    > thanks for your support, but I'm still unable to create a share.
    > Now I get the errormessage: "The network path can't be found".


    Well, it *does* work <g>. I have a working example at my website
    (http://mvps.org/st-software/Code/NetCalls_NT_9x.zip). The following code
    was extracted from there. I just set a share on a W2K machine on the
    network, passing in:

    xi_strServerName:="DualW2K" (name of server)
    xi_strShareName:="Utils"
    xi_strPath:="C:\Util"
    xi_strRemark:="Utilities"

    **********************************************************

    Option Explicit

    Private Const NERR_SUCCESS As Long = 0&

    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 Enum enmPermissions
    ACCESS_NONE = &H0
    ACCESS_READ = &H1&
    ACCESS_WRITE = &H2&
    ACCESS_CREATE = &H4&
    ACCESS_EXEC = &H8&
    ACCESS_DELETE = &H10&
    ACCESS_ATRIB = &H20&
    ACCESS_PERM = &H40&
    ACCESS_ALL = ACCESS_READ Or ACCESS_WRITE Or ACCESS_CREATE Or ACCESS_EXEC
    Or ACCESS_DELETE Or ACCESS_ATRIB Or ACCESS_PERM
    End Enum
    Private Enum enmShareType
    STYPE_DISKTREE = 0&
    STYPE_PRINTQ = 1&
    STYPE_DEVICE = 2&
    STYPE_IPC = 3&
    STYPE_SPECIAL = &H80000000
    End Enum

    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

    Public Sub AddShare(ByVal xi_strServerName As String, _
    ByVal xi_strShareName As String, _
    ByVal xi_strPath As String, _
    ByVal xi_strRemark As String)
    Dim p_lngRtn As Long
    Dim p_lngPtrToType As Long
    Dim p_lngParmErr As Long
    Dim p_abytServerName() As Byte
    Dim p_strErr As String
    Dim p_strPassword As String
    Dim p_typShareInfo_2 As SHARE_INFO_2
    Const cnstProcName As String = "AddShare"

    ' ------------------------------------------
    ' Initialize the variable(s)
    ' ------------------------------------------
    xi_strServerName = PrepareServerName(xi_strServerName)
    ' Won't work if remote machine is Win9x
    'If IsRemoteWin9x(xi_strServerName) = True Then
    ' Err.Raise Number:=ERR_WRONG_CALL_LEVEL + cnstErrBase, _
    ' Source:=cnstProcName, _
    ' Description:=CNST_CANNOT_CALL_WIN9X
    'End If

    p_abytServerName = xi_strServerName & vbNullChar
    p_strPassword = vbNullString

    With p_typShareInfo_2
    .shi2_netname = StrPtr(xi_strShareName)
    .shi2_type = enmShareType.STYPE_DISKTREE
    .shi2_remark = StrPtr(xi_strRemark)
    .shi2_permissions = enmPermissions.ACCESS_READ
    .shi2_max_uses = -1
    .shi2_current_uses = 0
    .shi2_path = StrPtr(xi_strPath)
    .shi2_passwd = StrPtr(p_strPassword)
    End With

    p_lngPtrToType = VarPtr(p_typShareInfo_2)

    ' ------------------------------------------
    ' Make appropriate API call and check for error
    ' ------------------------------------------
    p_lngRtn = NetShareAdd(ServerName:=p_abytServerName(0), _
    level:=2, _
    Buffer:=p_lngPtrToType, _
    parm_err:=p_lngParmErr)

    If p_lngRtn <> NERR_SUCCESS Then
    GoTo Err_Handler
    End If

    Exit Sub
    Err_Handler:
    ' Raise an error
    End Sub

    Private Function PrepareServerName(ByVal xi_strServerName As String) As
    String

    xi_strServerName = Trim$(xi_strServerName)

    If Len(Trim$(xi_strServerName)) = 0 Then
    PrepareServerName = vbNullString
    Else
    If Left$(xi_strServerName, 2) <> "\\" Then
    PrepareServerName = "\\" & xi_strServerName
    Else
    PrepareServerName = xi_strServerName
    End If
    End If

    End Function

    --
    L.J. Johnson, Slightly Tilted Software
    Microsoft MVP (Visual Basic)
    LJJohnson@SlightlyTiltedSoftware.com or LJJohnson@mvps.org
    <http://www.SlightlyTiltedSoftware.com>
    Ask The NT Pro at <http://www.devx.com/gethelp>




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center