473,396 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

specifying password using NetShareEnum

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;
}

Nov 16 '05 #1
1 7230
1. You can't specify user credentials when using the Net API's, please read
the WIN32 API description carefully (especially the remarks and the
Security Requirements for the Network Management Functions. )
You have to impersonate or you need to create a session with the server
using an administrative share (net use \\server\IPC$ ....)

2. Your NetShareEnum declaration is incorrect, the last 3 arguments should
be

out int entriesread,
out int totalentries,
ref IntPtr resume_handle

3. You better stay away from WIN32 API calls if managed solutions are
available, System.Management or System.DirectoryServices are built to allow
you to perform network management tasks using managed code.

Here is a sample using System.DirectoryServices and ADSI (through the
imported activeds.tlb)

using System;
using System.DirectoryServices;
using activeds; // this refers to the namespace of the imported activeds.tlb
(set reference to %windir%\system32\activeds.tlb)
// List all File shares on a system only for W2K and higher
class Tester {
public static void Main() {
// SERVER: name of the server you want to enumerate
// userName: valid user on server
// pwd: his password
using(DirectoryEntry container = new
DirectoryEntry("WinNT://SERVER/LanmanServer", "userName", "pwd",
AuthenticationTypes.ServerBind))
{
foreach(IADsFileShare share in container.NativeObject as IADsContainer)
{
Console.WriteLine("{0} \t{1} \t{2}", share.Name, share.Path,
share.Description);
}
}
}
}

And here is the same using System.Management (and WMI). Note that for this
to work properly you need to grant remote access privileges the the account
connecting to WMI o the remote system.

....
ManagementPath path = new ManagementPath();
path.Server = "SERVER"; // name of the remote system
path.NamespacePath = @"root\CIMV2";
//Connect to the remote computer
ConnectionOptions co = new ConnectionOptions();
// Using valid credentials on SERVER
co.Username = "user";
co.Password = "pwd";
ManagementScope ms = new ManagementScope(path, co);
path.RelativePath ="Win32_Share";
ManagementClass shares = null;
using(shares = new ManagementClass(ms,path,null))
{
ManagementObjectCollection moc = shares.GetInstances();
foreach(ManagementObject mo in moc)
// dump local drive and UNC path
Console.WriteLine("{0} - {1} - {2}",mo["Name"],mo["Path"],
mo["Description"]);
}
Willy.

"Siddharth Jain" <si**************@rediffmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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;
}

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Marc | last post by:
Hello, Newbie here..... Searching and working this for a week now. We too are having the same problems. Using MySql 4.0.14 and there are "no problems" at all.
10
by: Fabrizio | last post by:
(Sorry for the crosspost, but I really don't know which is the right newsgroup!) Hi all, I try to change the password to a user that as to change the password at first logon: try {
0
by: siddharth_jain_1 | last post by:
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...
0
by: Siddharth Jain | last post by:
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...
1
by: David Crawford | last post by:
I have an application that needs to copy a file to a server which my user may or may not have rights to. In the case where the user does NOT have rights, I want to map a drive to the specified...
9
by: KSC | last post by:
Hello, I have been looking through the docs on System.IO and cannot find a way to return a share name on a mapped drive. I have done this using FileSystemObject.Drive.ShareName, but I don't...
1
by: Jean-Marc St-Hilaire | last post by:
I need to access Windows API NetShareEnum. It is working fine in the post Win98 version but the Win98 function is returning me error number 87 (Invalid parameters) Whats I am doing wrong ? ...
6
by: hotani | last post by:
I am attempting to pull info from an LDAP server (Active Directory), but cannot specify an OU. In other words, I need to search users in all OU's, not a specific one. Here is what works: con...
1
by: taghi | last post by:
I want to call NetShareEnum, a function from netapi32.dll NetShareEnum has this definition: NET_API_STATUS NetShareEnum( __in LPWSTR servername, __in DWORD level, __out LPBYTE...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.