Solved

Using WNetAddConnection2 and recovering from error 1219

Posted on 2002-07-29
6
7,767 Views
Last Modified: 2012-05-04
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());
     }
}




0
Comment
Question by:wile_e_coyote
6 Comments
 
LVL 4

Author Comment

by:wile_e_coyote
ID: 7201114
Ok, nobody wanted to touch this for 300, how about 400?
0
 

Expert Comment

by:ingritiate2000
ID: 7210375
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.
0
 
LVL 4

Author Comment

by:wile_e_coyote
ID: 7214097
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:st_steve
ID: 7226714
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 :)
0
 

Expert Comment

by:modulo
ID: 7479446

Dear expert(s),

A request has been made to delete this Q in CS:
http://www.experts-exchange.com/Community_Support/Q_20404439.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
0
 
LVL 6

Accepted Solution

by:
Mindphaser earned 0 total points
ID: 7518565
Points refunded and moved to PAQ

** Mindphaser - Community Support Moderator **

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question