M
Mike D Sutton
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.Unicode)]
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: (e-mail address removed)
WWW: Http://EDais.mvps.org/
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.Unicode)]
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: (e-mail address removed)
WWW: Http://EDais.mvps.org/