Solved

Using WNetAddConnection2 and recovering from error 1219

Posted on 2002-07-29
6
7,576 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now