Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
netshareadd (netapi32)
 
.
Summary
Adds a new network share to either a local or remote server

C# Signature:

    [DllImport("Netapi32.dll")]
    private static extern uint NetShareAdd(
        [MarshalAs(UnmanagedType.LPWStr)] string strServer,
        Int32 dwLevel,
        ref SHARE_INFO_502 buf,
        out uint parm_err
    );

VB Signature:

Declare Unicode Function NetShareAdd Lib "netapi32.dll" ( _
            <MarshalAs(UnmanagedType.LPWStr)> _
            ByVal servername As String, _
            ByVal level As Integer, _
            ByRef buf As SHARE_INFO_2, _
            ByRef parm_err As Integer) As Integer

C# User-Defined Types:

        private enum NetError : uint
        {
            NERR_Success = 0,
            NERR_BASE = 2100,
            NERR_UnknownDevDir = (NERR_BASE + 16),
            NERR_DuplicateShare = (NERR_BASE + 18),
            NERR_BufTooSmall = (NERR_BASE + 23),
        }

        private enum SHARE_TYPE : uint
        {
            STYPE_DISKTREE = 0,
            STYPE_PRINTQ = 1,
            STYPE_DEVICE = 2,
            STYPE_IPC = 3,
            STYPE_TEMPORARY = 0x40000000,
            STYPE_SPECIAL = 0x80000000,
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct SHARE_INFO_502
        {
            [MarshalAs(UnmanagedType.LPWStr)] public string shi502_netname;
            public SHARE_TYPE shi502_type;
            [MarshalAs(UnmanagedType.LPWStr)] public string shi502_remark;
            public Int32 shi502_permissions;
            public Int32 shi502_max_uses;
            public Int32 shi502_current_uses;
            [MarshalAs(UnmanagedType.LPWStr)] public string shi502_path;
            [MarshalAs(UnmanagedType.LPWStr)] public string shi502_passwd;
            public Int32 shi502_reserved;
            public IntPtr shi502_security_descriptor;
        }

VB User-Defined Types:

    Public Enum NetError
        NERR_Success = 0
        ERROR_ACCESS_DENIED = 5
        ERROR_INVALID_PARAMETER = 87
        ERROR_INVALID_NAME = 123
        ERROR_INVALID_LEVEL = 124
        NERR_BASE = 2100
        NERR_UnknownDevDir = (NERR_BASE + 16)
        NERR_RedirectedPath = (NERR_BASE + 17)
        NERR_DuplicateShare = (NERR_BASE + 18)
        NERR_BufTooSmall = (NERR_BASE + 23)
    End Enum

    #Region "SHARE_TYPE Enumeration Definition"
    ' <summary>Type of share</summary>
    Public Enum ShareType
        ' <summary>Disk Share</summary>
        Disk = 0
        ' <summary>Printer Share</summary>
        Printer = 1
        ' <summary>Device Share</summary>
        Device = 2
        ' <summary>IPC Share</summary>
        IPC = 3
        ' <summary>Special Share</summary>
        ' <remarks>
        ' Add this value to one of the above values
        ' to get an Administrative Share of that type
        ' </remarks>
        Temporary = &H40000000
        Special = &H80000000
    End Enum
    #End Region

    #Region "SharePermissions Enum Definition"
    Public Enum SharePermissions
        ACCESS_NONE =    0
        ACCESS_READ =    1
        ACCESS_WRITE =    2
        ACCESS_CREATE =    4
        ACCESS_EXEC =    8
        ACCESS_DELETE =    &H10
        ACCESS_ATRIB =    &H20
        ACCESS_PERM =    &H40
        ACCESS_ALL =    ACCESS_READ + _
                ACCESS_WRITE + _
                ACCESS_CREATE + _
                ACCESS_EXEC + _
                ACCESS_DELETE + _
                ACCESS_ATRIB + _
                ACCESS_PERM
        ACCESS_GROUP = &H8000
    End Enum
    #End Region

    #Region "SHARE_INFO_2 Structure Definition"
    <StructLayout(LayoutKind.Sequential)> _
    Public Structure SHARE_INFO_2
        <MarshalAs(UnmanagedType.LPWStr)> _
        Public shi2_netname As String
        <MarshalAs(UnmanagedType.U4)> _
        Public shi2_type As ShareType
        <MarshalAs(UnmanagedType.LPWStr)> _
        Public shi2_remark As String
        <MarshalAs(UnmanagedType.U4)> _
        Public shi2_permissions As SharePermissions
        <MarshalAs(UnmanagedType.U4)> _
        Public shi2_max_uses As Integer
        <MarshalAs(UnmanagedType.U4)> _
        Public shi2_current_uses As Integer
        <MarshalAs(UnmanagedType.LPWStr)> _
        Public shi2_path As String
        <MarshalAs(UnmanagedType.LPWStr)> _
        Public shi2_passwd As String

    Public Sub New( _
        ByVal netName As String, _
        ByVal type As ShareType, _
        ByVal remark As String, _
        ByVal maxUses As Integer, _
        ByVal path As String, _
        ByVal permissions As SharePermissions, _
        ByVal passwd As String)

        shi2_netname = netName
        shi2_type = type
        shi2_remark = remark
        shi2_permissions = permissions
        shi2_max_uses = maxUses
        shi2_current_uses = 0
        shi2_path = path
        shi2_passwd = passwd
    End Sub

    End Structure
    #End Region

Notes:

[2004-06-11]
VB Def and Sample code added by RACKLEY
VB Def and enums updated by SPASCOE

Tips & Tricks:

This function will take as it's third parameter, a number of structures. The second parameter defines which level the third parameter structure is. Acceptable structures are SHARE_INFO_2, SHARE_INFO_502, and under Windows 9x/ME, SHARE_INFO_50.

C# Sample Code:

    string shareName = "testshare";
    string shareDesc = "This is a test share kekelar2000";
    string path = @"C:\MyShareDirectory"; // do not append comma, it'll fail

    SHARE_INFO_502 info = new SHARE_INFO_502();
    info.shi502_netname = shareName;
    info.shi502_type = SHARE_TYPE.STYPE_DISKTREE;
    info.shi502_remark = shareDesc;
    info.shi502_permissions = 0;    // ignored for user-level security
    info.shi502_max_uses = -1;
    info.shi502_current_uses = 0;    // ignored for set
    info.shi502_path = path;
    info.shi502_passwd = null;        // ignored for user-level security
    info.shi502_reserved = 0;
    info.shi502_security_descriptor = IntPtr.Zero;

    uint error = 0;
    uint result = NetShareAdd(server, 502, ref info, out error);

VB.NET Sample Code:

Dim shi2 As SHARE_INFO_2

' Populate the structure with information
shi2.shi2_netname = sharename"      ' share name
shi2.shi2_type = ShareType.Disk     ' disk drive
shi2.shi2_remark = ""           ' share comment
shi2.shi2_permissions = SharePermissions.ACCESS_NONE
shi2.shi2_max_uses = -1        ' unlimited    
shi2.shi2_current_uses = 0
shi2.shi2_path = "c:\myshare"
shi2.shi2_passwd = Nothing      ' no password

retval = NetShareAdd(ServerName, 2, shi2, Nothing)

Alternative Managed API:

Do you know one? Please contribute it!

Documentation
NetShareAdd on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions