Solved

Using WNetAddConnection2 and recovering from error 1219

Posted on 2002-07-29
6
7,969 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

623 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