WNetCancelConnection2 failure

WNetCancelConnection2 failure

am 21.10.2007 20:16:48 von AdrianDev

This is a multi-part message in MIME format.

------=_NextPart_000_0012_01C81416.ED7F2D30
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

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,


------=_NextPart_000_0012_01C81416.ED7F2D30
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable



charset=3Diso-8859-1">




Hi,

 

I hope this is the right =
forum.

 

 

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

 

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

 

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

 

 

Any idea what is going =
wrong?

 

 

Thanks in advance,

 

 


------=_NextPart_000_0012_01C81416.ED7F2D30--

Re: WNetCancelConnection2 failure

am 21.10.2007 23:24:29 von mattias.dont.want.spam

>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Re: WNetCancelConnection2 failure

am 22.10.2007 11:00:09 von 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 description for Class1.
///

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();
}
}
}






"Mattias Sjögren" wrote in message
news:uSkifjCFIHA.4400@TK2MSFTNGP04.phx.gbl...
> >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
>
> --
> Mattias Sjögren [C# MVP] mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.
>

Re: WNetCancelConnection2 failure

am 23.10.2007 13:41:08 von 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.