473,411 Members | 1,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

WNetCancelConnection2 failure

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,
Oct 21 '07 #1
3 10572
>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.
Oct 21 '07 #2
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();
}
}
}


"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:uS**************@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.

Oct 22 '07 #3
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.

Oct 23 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: x2164 | last post by:
hi all, Linux 2.4.28 Glibc 2.2.5 gcc 2.95.3 I'm new to Python. I've compiled Python 2.4 from tar file.
3
by: Damaji Jambhale | last post by:
COMException: Catostrphic failure When I added a "dll" reference in the web project. I was able to instantiate the class OK. But when I tried to set the properties, it failed with...
2
by: JustaCowboy | last post by:
Greetings, I am seeking information related to this subject. BOL suggests backing up the active transaction log immediately after a failure, so that the backup can be used in a recovery scenario...
4
by: J. Marshall Latham | last post by:
I have written an ASP.NET web app in C# that is trying to connect to a database using OleDb. I put code in a dll that uses another dll to create a connection object (and open it if requested) to...
5
by: Ron Louzon | last post by:
I have some C++ code that uses the CSingleLock( CCriticalSection *) constructor. In visual C++ 6.0, this code compiles and runs fine in both Debug and release modes. However, in Visual Studio...
4
by: Mack | last post by:
Have tried varying declares for the api call, i need to close a connection even if resources are open (damn perfcounters) It suggests that you can use a true (dim as a bool in the api declare),...
0
by: Marty Cruise | last post by:
I successfully deploy my application to 20 domain users. Only one new user is giving me a problem, although he can access all domain resources. When he clicks the installation link on the...
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
0
by: AdrianDev | last post by:
Hi, I have sucessfully added a connection using WNetAddConnection2 to a remote servers $IPC share. But calling WNetCancelConnection2 using the share name fails error 2250: "The network...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.