[DllImport("Netapi32.dll", CharSet=CharSet.Unicode)]
private static extern int NetShareEnum(
StringBuilder ServerName,
int level,
ref IntPtr bufPtr,
uint prefmaxlen,
ref int entriesread,
ref int totalentries,
ref int resume_handle
);
Declare Unicode Function NetShareEnum Lib "netapi32.dll" _
(ByVal ServerName As StringBuilder, _
ByVal level As Integer, _
ByRef BufPtr As IntPtr, _
ByVal prefmaxbufferlen As Integer, _
ByRef entriesread As Integer, _
ByRef totalentries As Integer, _
ByRef resume_handle As Integer) As Integer
<DllImport("Netapi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Shared Function NetShareEnum( _
ByVal ServerName As StringBuilder, _
ByVal level As Integer, _
ByRef BufPtr As IntPtr, _
ByVal prefmaxbufferlen As Integer, _
ByRef entriesread As Integer, _
ByRef totalentries As Integer, _
ByRef resume_handle As Integer) As Integer
End Function
if you use C# Sample code;
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct SHARE_INFO_0
{
public string shi0_netname;
}
const uint MAX_PREFERRED_LENGTH = 0xFFFFFFFF;
const int NERR_Success = 0;
[2006-07-05]
VB Alternate Signature code added by KRONOS
[2004-06-11]
VB Def and Sample code added by RACKLEY
[2004-08-31]
C# Def and Sample code added by K.MORONO
Please add some!
Dim currentPtr As IntPtr
Dim BufPtr As IntPtr 'in
Dim dwEntriesread As Integer 'out
Dim dwTotalentries As Integer 'out
Dim dwResumehandle As Integer 'out
Dim nStructSize As Integer
Dim shi2 As SHARE_INFO_2
Dim cnt As Long 'share enumeration counter
Dim SvrNameBldr As New StringBuilder(ServerName)
retval = NetShareEnum(SvrNameBldr, 2, BufPtr, MAX_PREFERRED_LENGTH, dwEntriesread, dwTotalentries, dwResumehandle)
If retval = NERR_Success And retval <> ERROR_MORE_DATA Then
currentPtr = BufPtr
nStructSize = Marshal.SizeOf(GetType(SHARE_INFO_2))
' Enumerate over all shares on the server and find the any/all shares that point
' to c:\deletemeplease and delete those shares.
For cnt = 0 To dwEntriesread - 1
shi2 = Marshal.PtrToStructure(currentPtr, GetType(SHARE_INFO_2))
If System.String.Compare(shi2.shi2_path, "c:\deletemeplease" True) = 0 Then
DelRetVal = NetShareDel(ServerName, shi2.shi2_netname, 0)
End If
currentPtr = New IntPtr(currentPtr.ToInt32 + Marshal.SizeOf(GetType(SHARE_INFO_2)))
Next
End If
Call NetApiBufferFree(BufPtr)
[C#]
PUT TextBox control,ListBox control and Button control on your Form.
private void button1_Click(object sender, System.EventArgs e)
{
listBox1.Items.Clear();
int entriesread = 0;
int totalentries = 0;
int resume_handle = 0;
int nStructSize = Marshal.SizeOf(typeof(SHARE_INFO_0));
IntPtr bufPtr = IntPtr.Zero;
StringBuilder server = new StringBuilder(textBox1.Text);
int ret = NetShareEnum(server,0,ref bufPtr,MAX_PREFERRED_LENGTH,ref entriesread,ref totalentries,ref resume_handle);
if (ret == NERR_Success)
{
IntPtr currentPtr = bufPtr;
for (int i = 0;i<entriesread;i++)
{
SHARE_INFO_0 shi0 = (SHARE_INFO_0)Marshal.PtrToStructure(currentPtr,typeof(SHARE_INFO_0));
listBox1.Items.Add(shi0.shi0_netname);
currentPtr = new IntPtr(currentPtr.ToInt32() + nStructSize);
}
NetApiBufferFree(bufPtr);
}
else
{
listBox1.Items.Add("失敗 = " + ret.ToString());
}
}
[C#]
Sample Class for SHARE_INFO_1 (sharename, sharetype, remark)
public class GetNetShares
{
#region External Calls
[DllImport("Netapi32.dll", SetLastError = true)]
static extern int NetApiBufferFree(IntPtr Buffer);
[DllImport("Netapi32.dll", CharSet = CharSet.Unicode)]
private static extern int NetShareEnum(
StringBuilder ServerName,
int level,
ref IntPtr bufPtr,
uint prefmaxlen,
ref int entriesread,
ref int totalentries,
ref int resume_handle
);
#endregion
#region External Structures
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct SHARE_INFO_1
{
public string shi1_netname;
public uint shi1_type;
public string shi1_remark;
public SHARE_INFO_1(string sharename, uint sharetype, string remark)
{
this.shi1_netname = sharename;
this.shi1_type = sharetype;
this.shi1_remark = remark;
}
public override string ToString()
{
return shi1_netname;
}
}
#endregion
const uint MAX_PREFERRED_LENGTH = 0xFFFFFFFF;
const int NERR_Success = 0;
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_SPECIAL = 0x80000000,
}
public SHARE_INFO_1[] EnumNetShares(string Server)
{
List<SHARE_INFO_1> ShareInfos = new List<SHARE_INFO_1>();
int entriesread = 0;
int totalentries = 0;
int resume_handle = 0;
int nStructSize = Marshal.SizeOf(typeof(SHARE_INFO_1));
IntPtr bufPtr = IntPtr.Zero;
StringBuilder server = new StringBuilder(Server);
int ret = NetShareEnum(server, 1, ref bufPtr, MAX_PREFERRED_LENGTH, ref entriesread, ref totalentries, ref resume_handle);
if (ret == NERR_Success)
{
IntPtr currentPtr = bufPtr;
for (int i = 0; i < entriesread; i++)
{
SHARE_INFO_1 shi1 = (SHARE_INFO_1)Marshal.PtrToStructure(currentPtr, typeof(SHARE_INFO_1));
ShareInfos.Add(shi1);
currentPtr = new IntPtr(currentPtr.ToInt32() + nStructSize);
}
NetApiBufferFree(bufPtr);
return ShareInfos.ToArray();
}
else
{
ShareInfos.Add(new SHARE_INFO_1("ERROR=" + ret.ToString(),10,string.Empty));
return ShareInfos.ToArray();
}
}
}
Do you know one? Please contribute it!
See also NetApiBufferFree API.