Results 1 to 5 of 5

Thread: CODE: NetShareAdd

  1. #1

    Thread Starter
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032

    CODE: NetShareAdd

    Here's some code to add shares to a computer. I'm sure somebody could use it, and I'll bet lotsa people could improve it.

    Code:
    'THIS CODE IS FOR NT/2000
    '5-3-2001 by JoshT
    
    Option Explicit
    
    Private Const STYPE_DISKTREE = 0 'disk drive
    Private Const STYPE_PRINTQ = 1  'printer
    Private Const STYPE_DEVICE = 2
    Private Const STYPE_IPC = 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
    
    'NetShareAdd returns 0 if no error
    '
    'lpwstrServerName As Any, --the computer to create the share on,
    '                           NULL for local computer,
    '                           otherwise must begin with "\\"
    'ByVal dwordLevel As Long, -- specifies the type struct that contains the data
    '       2 for SHARE_INFO_2 Struct
    '       502 for SHARE_INFO_502 Struct
    'ByVal lpbyteBuf As Any, -- pointer to the struct who type was specified above
    'lpdwordParmErr As Long, -- if an error in the struct, the number of the struct's parameter
    '                            that caused the error (0 is no error)
    
    Private Declare Function NetShareAdd Lib "netapi32.dll" _
        (lpwstrServerName As String, _
        ByVal dwordLevel As Long, _
        ByVal lpbyteBuf As Any, _
        lpdwordParmErr As Long) As Long
        
    
    
    Private Sub TestIt()
        Dim si2 As SHARE_INFO_2
        Dim retval As Long
        Dim parmerr As Long
        parmerr = 0
        
        si2.shi2_netname = "DownloadE" & vbNullChar 'the name of the share
        si2.shi2_type = STYPE_DISKTREE 'the share is on the disk
        si2.shi2_remark = "Testing Out NetShareAdd VB API" & 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 = "E:\Download" & vbNullChar 'the path to the share
        si2.shi2_passwd = vbNullString 'NULL, the password 'this should be ignored
        retval = NetShareAdd(vbNullString, 2, VarPtr(si2), parmerr)
        
    End Sub
    
    Private Sub Form_Load()
        TestIt
    End Sub
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  2. #2
    Fanatic Member crispin's Avatar
    Join Date
    Aug 2000
    Location
    2 clicks west of a Quirkafleeg...Cornwall, England
    Posts
    754
    Cool - thanks JoshT, I can make good use of that
    Crispin
    VB6 ENT SP5
    VB.NET
    W2K ADV SVR SP3
    WWW.BLOCKSOFT.CO.UK

    [Microsoft Basic: 1976-2001, RIP]

  3. #3

    Thread Starter
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    You're welcome. Here's a better version of the code. It seems you have to use a byte array to pass the name of the computer instead of a string. Anyone know why that is?

    Code:
    'THIS CODE IS FOR NT/2000
    '5-3-2001 by JoshT
    
    Option Explicit
    Option Base 0
    
    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
    
    'NetShareAdd returns 0 if no error
    '
    'lpwstrServerName As Any, --the computer to create the share on,
    '                           NULL for local computer,
    '                           otherwise must begin with "\\"
    'ByVal dwordLevel As Long, -- specifies the type struct that contains the data
    '       2 for SHARE_INFO_2 Struct
    '       502 for SHARE_INFO_502 Struct
    'ByVal lpbyteBuf As Any, -- pointer to the struct who type was specified above
    'lpdwordParmErr As Long, -- if an error in the struct, the number of the struct's parameter
    '                            that caused the error (0 is no error)
    
    Private Declare Function NetShareAdd Lib "netapi32.dll" _
        (lpwstrServerName As Byte, _
        ByVal dwordLevel As Long, _
        ByVal lpbyteBuf As Long, _
        lpdwordParmErr As Long) As Long
        
    
    
    Private Sub TestIt()
        Dim si2 As SHARE_INFO_2
        Dim retval As Long
        Dim parmerr As Long
        Dim compname() As Byte
        
        parmerr = 0
        'compname = vbNullString
        compname() = "\\COMPUTER7" & vbNullChar
        
        si2.shi2_netname = "Test2C" & vbNullChar 'the name of the share
        si2.shi2_type = STYPE_DISKTREE 'the share is on the disk
        si2.shi2_remark = "Testing Out NetShareAdd VB API" & 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 = "C:\TEMP" & vbNullChar 'the path to the share
        si2.shi2_passwd = vbNullString 'NULL, the password 'this should be ignored
        retval = NetShareAdd(compname(0), 2, VarPtr(si2), parmerr)
        
    End Sub
    
    Private Sub Form_Load()
        TestIt
    End Sub
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  4. #4
    jim mcnamara
    Guest
    I believe it's to force straight 7 bit ASCII, rather than any other form of text, eg., unicode, etc.

  5. #5

    Thread Starter
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    I believe it's to force straight 7 bit ASCII, rather than any other form of text, eg., unicode, etc.
    The Platform SDK says its supposed to be a pointer to a unicode string for NT. But it works, so I'm not going to worry about it.
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width