sophia liu
sophia liu

Reputation: 91

WNetUseConnection returns ERROR_SESSION_CREDENTIAL_CONFLICT when called with host name

I have the following code which only works when nr.lpRemoteName server part is specified with IP address.

When it's host name ERROR_SESSION_CREDENTIAL_CONFLICT will be returned.

I can access the file using host name from the machine that this code runs in.

Any ideas?

[DllImport("Mpr.dll")]
private static extern int WNetUseConnection(
    IntPtr hwndOwner,
    NETRESOURCE lpNetResource,
    string lpPassword,
    string lpUserID,
    int dwFlags,
    string lpAccessName,
    string lpBufferSize,
    string lpResult
    );

[StructLayout(LayoutKind.Sequential)]
private class NETRESOURCE
{
    public int dwScope = 0;
    public int dwType = 0;
    public int dwDisplayType = 0;
    public int dwUsage = 0;
    public string lpLocalName = "";
    public string lpRemoteName = "";
    public string lpComment = "";
    public string lpProvider = "";
}
    string password = "12345";
    string username = "ole1";
    const int RESOURCETYPE_DISK = 0x00000001;

    NETRESOURCE nr = new NETRESOURCE();
    nr.dwType = RESOURCETYPE_DISK;
    nr.lpRemoteName = @"\\[IP Address]\vids";

    //nr.lpRemoteName = @"[Host Name]\vids";


    int ret = WNetUseConnection(IntPtr.Zero, nr, password, username, 0, null, null, null);

Upvotes: 3

Views: 2639

Answers (1)

Bilbo
Bilbo

Reputation: 380

It means your Windows login session already has a connection to that host, except that connection passed a different value in the [username] parameter.

1219 (0x4C3)

Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed. Disconnect all previous connections to the server or shared resource and try again.

Possible solutions:

  • drop the old connection and make a new one, or
  • pass the old connection's same value in [username], or
  • start a new login session and make its own set of network connections, probably through Impersonation.

Upvotes: 1

Related Questions