hello
I am trying to enumerate the shared folders on a server using the
NetShareEnum function.
Now, when the server has a password set to access the shared folders,
the function returns system error code 5 (access denied).
However, if I know the password to access the shares, how can I specify
that in my program?
The basic skeleton of my prog is as follows:
StructLayout( LayoutKind.Sequential )]
public struct SHARE_INFO_0
{
[MarshalAs( UnmanagedType.LPWStr )] public String shi0_netname;
}
[DllImport("Netapi32.dll")]
public static extern int NetShareEnum([MarshalAs(UnmanagedType.LPWStr)]
string servername,
Int32 level,
out IntPtr bufptr,
Int32 prefmaxlen,
[MarshalAs(UnmanagedType.LPArray)] Int32[] entriesread,
[MarshalAs(UnmanagedType.LPArray)] Int32[] totalentries,
[MarshalAs(UnmanagedType.LPArray)] Int32[] resume_handle
);
[DllImport("Netapi32.dll")]
public static extern int NetApiBufferFree(long lpBuffer);
public static string[] GetShares(string server)
{
IntPtr buf = new IntPtr(0);
Int32[] dwEntriesRead = new Int32[1];
dwEntriesRead[0] = 0;
Int32[] dwTotalEntries = new Int32[1];
dwTotalEntries[0] = 0;
Int32[] dwResumeHandle = new Int32[1];
dwResumeHandle[0] = 0;
Int32 success = 0;
string[] shares = new string[0];
success = NetShareEnum(server, 0, out buf, -1, dwEntriesRead,
dwTotalEntries, dwResumeHandle);
if(dwEntriesRead[0] > 0)
{
SHARE_INFO_0[] s0 = new SHARE_INFO_0[dwEntriesRead[0]];
shares = new string[dwEntriesRead[0]];
for(int i = 0; i < dwEntriesRead[0]; i++)
{
s0[i] = (SHARE_INFO_0) Marshal.PtrToStructure(buf,
typeof(SHARE_INFO_0));
shares[i] = s0[i].shi0_netname;
buf = (IntPtr)((long) buf + Marshal.SizeOf(s0[0]));
}
NetApiBufferFree((long) buf);
}
return shares;
}