RE: WNetAddConnection2

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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!)

"Steve Conner" wrote:

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!

"Steve Conner" wrote:

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
.



Relevant Pages

  • Re: RasEnumEntries returns only 1 dial up entry (PocketPC)
    ... public int reserved1 = 0; ... public string szPhoneNumber; ... public extern static uint RasHangUp( ... // buffer to hold error string ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: how to declare GetOpenFileNameEx in C#
    ... public IntPtr hwndOwner; ... public string lpstrCustomFilter; ... public int nMaxCustFilter; ... public IntPtr lpstrFile; ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Which generic?
    ... public string description; ... static void Main ... foreach(Column col in myCols) ... public int IndexOf{ ...
    (microsoft.public.dotnet.languages.csharp)
  • Namespace confusion
    ... fulfilmentRequestID = dataAccessObject.GenerateFulfilmentRequest(orderID, ... public string status; ... public int customerID; ...
    (microsoft.public.dotnet.framework.aspnet.webservices)
  • Re: How do I use WinHttpGetProxyForUrl from WinHttp.dll
    ... WINHTTP_AUTO_DETECT_TYPE_DNS_A in autodetect flags. ... > public struct WINHTTP_AUTOPROXY_OPTIONS ... > public int dwAutoDetectFlags; ... > public string lpszAutoConfigUrl; ...
    (microsoft.public.dotnet.framework.interop)