WNetCancelConnection2 failure

A

AdrianDev

Hi,

I hope this is the right forum.


From a console application running on a Windows 2003 server I have sucessfully added a connection using WNetAddConnection2 to a remote servers $IPC share.

But calling WNetCancelConnection2 using the share name fails with 2250: The network connection could not be found.

Moreover, when I run "net use" from a command prompt I can see ithe IPC share that was created is OKAY ??


Any idea what is going wrong?


Thanks in advance,
 
M

Mattias Sjögren

From a console application running on a Windows 2003 server I have sucessfully added a connection using WNetAddConnection2 to a remote servers $IPC share.
But calling WNetCancelConnection2 using the share name fails with 2250: The network connection could not be found.

Moreover, when I run "net use" from a command prompt I can see ithe IPC share that was created is OKAY ??


Any idea what is going wrong?


Please post your code.


Mattias
 
A

AdrianDev

Heres the code, thanks,

-----------------------------------------------------------

using System;
using Microsoft.Win32;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
struct USER_INFO_1003
{
public string usri1003_password;
}

[StructLayout(LayoutKind.Sequential)]
public struct NETRESOURCEA
{
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;
public override String ToString()
{
String str = "LocalName: " + lpLocalName + " RemoteName: " + lpRemoteName
+ " Comment: " + lpComment + " lpProvider: " + lpProvider;
return(str);
}
}

namespace wnetaddconnection
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
[DllImport("mpr.dll")]
public static extern int WNetAddConnection2(
[MarshalAs(UnmanagedType.LPArray)] NETRESOURCEA[] lpNetResource,
[MarshalAs(UnmanagedType.LPStr)] string lpPassword,
[MarshalAs(UnmanagedType.LPStr)] string UserName,
int dwFlags);

[DllImport("mpr.dll")]
extern static int WNetCancelConnection2(
[MarshalAs(UnmanagedType.LPWStr)] string lpName,
int dwFlags,
int fForce
);

[DllImport("Netapi32.dll")]
extern static int NetUserSetInfo(
[MarshalAs(UnmanagedType.LPWStr)] string servername,
[MarshalAs(UnmanagedType.LPWStr)] string username,
int level,
ref USER_INFO_1003 buf,
int error);

[STAThread]
static void Main(string[] args)
{
string administrator="Administrator";
string password="password1";
string target="192.168.88.100";
string lpName=@"\\" + target + @"\IPC$";
string username="fred";
string newpassword="hellomum";

NETRESOURCEA [] n = new NETRESOURCEA[1];
n[0] = new NETRESOURCEA();
n[0].dwScope = 2;
n[0].dwType = 0;
int dwFlags = 1;
n[0].lpLocalName = null;
n[0].lpRemoteName = lpName;

Console.WriteLine(n[0]);

try
{
// Add an IPC$ connection to the remote host
int res = WNetAddConnection2( n, password, administrator, dwFlags );

Console.WriteLine("WNetAddConnection2 returned : " + res);

USER_INFO_1003 UserInfo1003 = new USER_INFO_1003();
UserInfo1003.usri1003_password=newpassword;

// Set the password of a user
res = NetUserSetInfo(
target,
username,
1003,
ref UserInfo1003,
0);

// Cancel the connection
res = WNetCancelConnection2(lpName, 1, 0);

Console.WriteLine("WNetCancelConnection2 returned : " + res);
}
catch (Exception e)
{
Console.WriteLine(e.Message + " hit return ..");
Console.ReadLine();
}

Console.WriteLine("hit return ..");
Console.ReadLine();
}
}
}
 
H

hpassant

The P/Invoke declaration is wrong, you are passing a Unicode string to
an Ansi function. Remove the MarshalAs attribute for the lpName
argument:

[DllImport("mpr.dll", CharSet=CharSet.Auto)]
extern static int WNetCancelConnection2(
string lpName,
int dwFlags,
int fForce
);

You'll also get into trouble with the declaration for
NetUserSetInfo(), the last argument requires the "ref" keyword.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top