Steve Conner
2006-11-01 11:45:02 UTC
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
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