specifying password while using NetShareEnum

S

siddharth_jain_1

Hello

I am using NetShareEnum to enumerate all the shared folders of a
server.
If there is password set on the server, I get system error code 5
(access denied) as the return value of NetShareEnum, as expected.

However, if I know the password required to access the shares on the
server, how do I specify that in my code?

the following is the basic skeleton of my code:

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 = (SHARE_INFO_0)
Marshal.PtrToStructure(buf, typeof(SHARE_INFO_0));
shares = s0.shi0_netname;
buf = (IntPtr)((long) buf +
Marshal.SizeOf(s0[0]));
}
NetApiBufferFree((long) buf);
}
return shares;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top