Discussion:
WNetAddConnection2
(too old to reply)
Steve Conner
2006-11-01 11:45:02 UTC
Permalink
I am having some problems with the WNetAddConnection2A api call. Here is a
summary of the code that I am using to make the call ...

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
private class NETRESOURCEA
{
public int dwScope;
public int dwType;
public int dwDisplayType;
public int dwUsage;
[MarshalAs(UnmanagedType.LPStr)] public string lpLocalName;
[MarshalAs(UnmanagedType.LPStr)] public string lpRemoteName;
[MarshalAs(UnmanagedType.LPStr)] public string lpComment;
[MarshalAs(UnmanagedType.LPStr)] public string lpProvider;
};

[DllImport("mpr.dll")]
private static extern int WNetAddConnection2A(NETRESOURCEA lpNetResource,
[MarshalAs(UnmanagedType.LPStr)] string lpPassword,
[MarshalAs(UnmanagedType.LPStr)] string lpUsername, int dwFlags);


NETRESOURCEA oNet = new NETRESOURCEA();
oNet.dwType = 1;
oNet.dwScope = 0;
oNet.dwDisplayType = 0;
oNet.dwUsage = 0;
oNet.lpLocalName = null;
oNet.lpRemoteName = @"\\server\share";
oNet.lpComment = null;
oNet.lpProvider = null;

int iFlags = 0;

string sPassword = @"password";
string sUsername = @"username";

int iStatus = WNetAddConnection2A(oNet, sPassword, sUsername, iFlags);


When running the code iStatus gets set to 1326 which is Logon failure:
unknown user name or bad password.

In the production code the username and password are being read from a
config xml file so I know they are correct.

Interestingly if I set the iFlags variable to 8, which is interactive mode,
then I get prompted for the password and the drive mapping works perfectly.

I'm not sure if it important but the server I am attempting to map to is not
part of our domain, it is a stand alone server which connects to our network
and an external information providers network.

I can map to the resource quite happily via windows explorer it just fails
every time with the api call in non interactive mode.

Any ideas ?

Cheers

Steve Conner
Steve Conner
2006-11-01 12:32:02 UTC
Permalink
As a follow up note, I have successfully run the code and mapped to an
administrative share on a dev server within our domain, so it would appear to
be an issue with the machine outside the domain. I still find it strange that
I can map to the share using explorer but not programmatically, clearly I am
missing something!
Post by Steve Conner
I am having some problems with the WNetAddConnection2A api call. Here is a
summary of the code that I am using to make the call ...
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
private class NETRESOURCEA
{
public int dwScope;
public int dwType;
public int dwDisplayType;
public int dwUsage;
[MarshalAs(UnmanagedType.LPStr)] public string lpLocalName;
[MarshalAs(UnmanagedType.LPStr)] public string lpRemoteName;
[MarshalAs(UnmanagedType.LPStr)] public string lpComment;
[MarshalAs(UnmanagedType.LPStr)] public string lpProvider;
};
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2A(NETRESOURCEA lpNetResource,
[MarshalAs(UnmanagedType.LPStr)] string lpPassword,
[MarshalAs(UnmanagedType.LPStr)] string lpUsername, int dwFlags);
NETRESOURCEA oNet = new NETRESOURCEA();
oNet.dwType = 1;
oNet.dwScope = 0;
oNet.dwDisplayType = 0;
oNet.dwUsage = 0;
oNet.lpLocalName = null;
oNet.lpComment = null;
oNet.lpProvider = null;
int iFlags = 0;
int iStatus = WNetAddConnection2A(oNet, sPassword, sUsername, iFlags);
unknown user name or bad password.
In the production code the username and password are being read from a
config xml file so I know they are correct.
Interestingly if I set the iFlags variable to 8, which is interactive mode,
then I get prompted for the password and the drive mapping works perfectly.
I'm not sure if it important but the server I am attempting to map to is not
part of our domain, it is a stand alone server which connects to our network
and an external information providers network.
I can map to the resource quite happily via windows explorer it just fails
every time with the api call in non interactive mode.
Any ideas ?
Cheers
Steve Conner
Steve Conner
2006-11-01 13:32:01 UTC
Permalink
OK, I was just being dumb ...

I managed to recreate my error from the command prompt with the following
command:

net use \\server\share [password] /USER:[username]

failed with 1326 - invalid username or password.

So I got to thinking about the username and tried ...

net use \\server\share [password] /USER:[server]\[username]

and everything worked from the command line, so I altered the username in my
config file and everything worked perfectly ... this was a case of not being
able to see the woods for the trees. Hopefully it may help someone else avoid
the problem I was having (and at least I know my code is good!)
Post by Steve Conner
As a follow up note, I have successfully run the code and mapped to an
administrative share on a dev server within our domain, so it would appear to
be an issue with the machine outside the domain. I still find it strange that
I can map to the share using explorer but not programmatically, clearly I am
missing something!
Post by Steve Conner
I am having some problems with the WNetAddConnection2A api call. Here is a
summary of the code that I am using to make the call ...
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
private class NETRESOURCEA
{
public int dwScope;
public int dwType;
public int dwDisplayType;
public int dwUsage;
[MarshalAs(UnmanagedType.LPStr)] public string lpLocalName;
[MarshalAs(UnmanagedType.LPStr)] public string lpRemoteName;
[MarshalAs(UnmanagedType.LPStr)] public string lpComment;
[MarshalAs(UnmanagedType.LPStr)] public string lpProvider;
};
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2A(NETRESOURCEA lpNetResource,
[MarshalAs(UnmanagedType.LPStr)] string lpPassword,
[MarshalAs(UnmanagedType.LPStr)] string lpUsername, int dwFlags);
NETRESOURCEA oNet = new NETRESOURCEA();
oNet.dwType = 1;
oNet.dwScope = 0;
oNet.dwDisplayType = 0;
oNet.dwUsage = 0;
oNet.lpLocalName = null;
oNet.lpComment = null;
oNet.lpProvider = null;
int iFlags = 0;
int iStatus = WNetAddConnection2A(oNet, sPassword, sUsername, iFlags);
unknown user name or bad password.
In the production code the username and password are being read from a
config xml file so I know they are correct.
Interestingly if I set the iFlags variable to 8, which is interactive mode,
then I get prompted for the password and the drive mapping works perfectly.
I'm not sure if it important but the server I am attempting to map to is not
part of our domain, it is a stand alone server which connects to our network
and an external information providers network.
I can map to the resource quite happily via windows explorer it just fails
every time with the api call in non interactive mode.
Any ideas ?
Cheers
Steve Conner
Loading...