Link to home
Start Free TrialLog in
Avatar of wile_e_coyote
wile_e_coyote

asked on

Using WNetAddConnection2 and recovering from error 1219

I am using the code below to connect to a network share via a "proxy" userid (ie the userid is different from that of the currently logged on user).  The code works ok, unless there is already a connection to the same share from the user's PC under a different userid.  In
this case WNetAddConnection2 returns an error 1219.  Is there any way that I can programmatically "break" the existing connection?  Note that my program cannot prompt for the logged-on user's password.


public class Impersonate
{
     //Used in calling WNetAddConnection2
     [StructLayout (LayoutKind.Sequential)]
          public struct NETRESOURCE
     {
          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;
     }
     //WIN32API - WNetAddConnection2
     [DllImport("mpr.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)]
     private static extern int WNetAddConnection2A (
     [MarshalAs (UnmanagedType.LPArray)]
          NETRESOURCE [] lpNetResource
          ,
          [MarshalAs (UnmanagedType.LPStr)]
          string lpPassword
          ,
          [MarshalAs (UnmanagedType.LPStr)]
          string lpUserName
          ,
          int dwFlags
          );
     //WIN32API - WNetCancelConnection2
     [DllImport("mpr.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)]
     private static extern int WNetCancelConnection2A (
          [MarshalAs (UnmanagedType.LPStr)]
          string lpName
          , int dwFlags
          , int fForce
          );

     /// Connects to a share as a different user.  If connect fails, throws an Exception.
     public void ConnectAs (string pShare, string pDomain, string pUser, string pPwd)
     {
          int err = 0;
          string sUser = pDomain + "\\" + pUser;
          int dwFlags = 0;
          NETRESOURCE [] nr = new NETRESOURCE [1];
          nr[0].lpRemoteName = pShare;
          nr[0].lpLocalName = "";  //mLocalName;
          nr[0].dwType = 1;     //disk
          nr[0].dwDisplayType = 0;
          nr[0].dwScope = 0;
          nr[0].dwUsage = 0;
          nr[0].lpComment = "";
          nr[0].lpProvider = "";
          err = WNetAddConnection2A (nr, pPwd, sUser, dwFlags);
          if (err != 0)
               throw new Exception ("Unable to connect to " + pShare
                    + ".  Error=" + err.ToString());
     }
     public void Disconnect (string pShare )
     {
          const int dwFlags = 0;
          const int ForceDisconnect = 1;
          int err = 0;
          err = WNetCancelConnection2A (pShare, dwFlags, ForceDisconnect);
          if ((err != 0) && (err != 2250)) //2250 = connection does not exist
               throw new Exception ("Unable to disconnect from " + pShare
                    + ".  Error=" + err.ToString());
     }
}




Avatar of wile_e_coyote
wile_e_coyote

ASKER

Ok, nobody wanted to touch this for 300, how about 400?
Well, I don't think it's NOBODY who wants to answer, but it's that the question might be a little difficult.

I'm just going to guess a little bit here, I'm not good in C#, so yell out if I'm bugging you with my idiotic answers.

What operating system are you using to run this code on? I'm guessing you're trying this on Windows 9x? I don't think there's an mpr.dll on Win2k? I've tried using mpr.dll to crack the passwords on a Win2k system, tell me there's no mpr.dll. The password cracking program works fine on Win9x by the way.

At times when windows loses a network connection, it tries to re-establish the connection using stored username and password. Eg: For Win2k

You log in initally with username "A"

Then you try to access another computer (the one without a username called "A"). Win2k will give you a prompt to let you enter another username and password to connect to that second computer, let's say username "B".

Once this is done, you can't change your mind and want to log on using username "C". You'll need to end the current session before Win2k prompts you for username and password again.

The "prompting" of logon credentials only happens if the first computer can't find the mirror account name on the second computer. If there is a username "A" on second computer, no prompting happens. First computer gets the same rights and permissions as the rights of "A" on second computer.

It's sort of like a "trust relationship". I don't think you can "break" the connection unless you log off and log on as the username you want to use.

Just my 30-cents worth of information. I know this didn't help your question AT ALL.
The code is running on Win2K and WinXP (it behaves the same on both).  Its compiled using Visual Studio .NET and I think VS.NET ensures that mpr.dll is packaged with the application when it is deployed.

And I'm open to all suggestions, so don't worry about bugging me.
Do you think it might be impossible to log on for the second time using a different ID? You can log in first, then log on using a different ID, that's fine. But one the second connection is made, I think it is not possible to log on with another ID unless you "break" the connection by restarting the computer/logging off completely?

I'm a VB guy, so I'll leave this to the C# experts. Good Luck :)

Dear expert(s),

A request has been made to delete this Q in CS:
https://www.experts-exchange.com/questions/20404439/Please-delete-20329637.html

Without a response in 72 hrs, a moderator will finalize this question by:

 - Saving this Q as a PAQ and refunding the points to the questionner

When you agree or disagree, please add a comment here.

Thank you.

modulo

Community Support Moderator
Experts Exchange
ASKER CERTIFIED SOLUTION
Avatar of Mindphaser
Mindphaser

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial