473,498 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling WNetAddConnection2 from C#

I'm in the process of porting a bunch of code to C# which for the most part is going well, but just can't seem to get
this one to work properly..
Here's the code I'm currently using:

***
[StructLayout(LayoutKind.Sequential)]
internal struct NETRESOURCE {
public int dwScope;
public int dwType;
public int dwDisplayType;
public int dwUsage;
[MarshalAs(UnmanagedType.LPWStr)]
public string lpLocalName;
[MarshalAs(UnmanagedType.LPWStr)]
public string lpRemoteName;
[MarshalAs(UnmanagedType.LPWStr)]
public string lpComment;
[MarshalAs(UnmanagedType.LPWStr)]
public string lpProvider;
}

[DllImport("mpr.dll", EntryPoint = "WNetAddConnection2W",
CharSet=System.Runtime.InteropServices.CharSet.Uni code)]
private static extern int WNetAddConnection2(
ref NETRESOURCE lpNetResource, string lpPassword, string lpUsername, Int32 dwFlags);

private const int RESOURCETYPE_ANY = 0x0;
private const int CONNECT_INTERACTIVE = 0x00000008;
private const int CONNECT_PROMPT = 0x00000010;

....

NETRESOURCE ConnInf = new NETRESOURCE();

ConnInf.dwScope = 0;
ConnInf.dwType = RESOURCETYPE_ANY;
ConnInf.dwDisplayType = 0;
ConnInf.dwUsage = 0;
ConnInf.lpLocalName = null;
ConnInf.lpRemoteName = inShare + "\0";
ConnInf.lpComment = null;
ConnInf.lpProvider = null;

WNetAddConnection2(ref ConnInf, null, null,
CONNECT_INTERACTIVE | CONNECT_PROMPT);
***

I've also tried WNetAddConnection3() to see if that one played any nicer, but still no joy:

***
[DllImport("mpr.dll", EntryPoint = "WNetAddConnection3W",
CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
private static extern int WNetAddConnection3(IntPtr hWndOwner,
ref NETRESOURCE lpNetResource, string lpPassword,
string lpUserName, int dwFlags);
[DllImport("User32.dll")]
private static extern IntPtr GetDesktopWindow();

....

WNetAddConnection3(GetDesktopWindow(), ref ConnInf, null,
null, CONNECT_INTERACTIVE | CONNECT_PROMPT);
***

I'm getting back error 127 (ERROR_PROC_NOT_FOUND), however if the encoding types on the function and structure declares
are mismatched, I get back error 67 (ERROR_BAD_NET_NAME) as expected since the string data containing the machine name
no longer makes sense.
This code in C++ works just fine:

***
NETRESOURCEW ConnInf;

memset(&ConnInf, 0, sizeof(ConnInf));
ConnInf.dwType = RESOURCETYPE_ANY;
ConnInf.lpRemoteName = (WCHAR*)inShare;
WNetAddConnection2W(&ConnInf, NULL, NULL,
CONNECT_INTERACTIVE | CONNECT_PROMPT);
***

I've also had the call working under VB6 and Delphi so the only thing I can imagine is that it's down to how the
structure is being marshalled. To that end I wrote a simple DLL in VC++ that just takes the pointer it's been passed
and displays a hex dump of that block of memory, including displaying the contents of LPWSTR members on the structure
but everything looked ok there.
The share that I'm connecting to is simply: "\\[MachineName]" Where MachineName is the name or IP of a machine I wish to
initiate a connection on.
Any ideas? I'm rapidly running out of them!
Cheers,

Mike
- Microsoft Visual Basic MVP -
E-Mail: ED***@mvps.org
WWW: Http://EDais.mvps.org/
Jun 8 '06 #1
1 21255
> I'm in the process of porting a bunch of code to C# which for the most part is going well, but just can't seem to get
this one to work properly..


Never mind, got it working now - The posted code works just fine in case anyone is looking for it in the future, the bug
was as usual between chair and keyboard.. ;)
Cheers,

Mike
- Microsoft Visual Basic MVP -
E-Mail: ED***@mvps.org
WWW: Http://EDais.mvps.org/
Jun 8 '06 #2

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

Similar topics

0
1919
by: Chris Powell | last post by:
Does anyone know if there is an equivalant call or method for the WIN32 API call WNetAddConnection2 to map network drives If not I assume a can import the dll and use it directly ok Thanks,...
1
7959
by: Scott Kay | last post by:
I would please like some help solving an annoying problem with the use Win32 API WNetAddConnection2 from VB.Net 2003, a Windows XP client, and Windows 2003 Web Server server. The client is on...
8
2934
by: Muthu | last post by:
I've read calling conventions to be the order(reverse or forward) in which the parameters are being read & understood by compilers. For ex. the following function. int Add(int p1, int p2, int...
7
6577
by: Klaus Friese | last post by:
Hi, i'm currently working on a plugin for Adobe InDesign and i have some problems with that. I'm not really a c++ guru, maybe somebody here has an idea how to solve this. The plugin is...
5
3392
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
1
5021
by: CAIBird | last post by:
Hi, We know that windows API WNetAddConnection2 can make a connection to a network resource. but how to do this job in C#? i'm wondering about it. Thanks -Zhilin
2
2643
by: John M. Gamble | last post by:
I'm getting this message in Visual Studio 2005: PInvokeStackImbalance was detected Message: A call to PInvoke function 'Refresh!Refresh.Main::WNetAddConnection2' has unbalanced the stack. ...
1
4006
by: Chimerique | last post by:
Hello there, here my first post ^^ I'm developping a web application that sometimes acces a shared drive not in the Domain with a local machine logon/password. For that I'm using some code...
7
2663
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7002
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
7165
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
7203
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1417
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
290
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.