Map a network share with WNetAddConnection3

  • Thread starter Thread starter Thorsten Dittmar
  • Start date Start date
T

Thorsten Dittmar

Hi,

I'm having troubles creating network drives programmatically. I need to
map drive "O:" (which is not in use on my system) to a network share
from a C# application.

I'm using the following code:

[DllImport("mpr.dll")]
public static extern ERRORS WNetAddConnection3(IntPtr hWndOwner, ref
NetResource pnetResource, string psPassword, string psUsername, int
piFlags);

.... SNIP ...

Define RESOURCESCOPE, RESOURCETYPE, etc. as enums here

.... SNIP

MPRApi.NetResource pnetRes = new MPRApi.NetResource();
pnetRes.dwScope = MPRApi.RESOURCESCOPE.GLOBALNET;
pnetRes.dwType = MPRApi.RESOURCETYPE.DISK;
pnetRes.dwDisplayType = MPRApi.RESOURCEDISPLAYTYPE.SHARE;
pnetRes.dwUsage = MPRApi.RESOURCEUSAGE.CONNECTABLE;
pnetRes.lpRemoteName = share;
pnetRes.lpLocalName = "O:";
pnetRes.lpProvider = null;


MPRApi.ERRORS err = MPRApi.WNetAddConnection3(this.Handle, ref pnetRes,
password, user, 0);
if (err != MPRApi.ERRORS.ERROR_SUCCESS)
throw new Win32Exception((int)err);

When I execute this code, I keep getting ERROR_BAD_DEVICE as result code.

Can anybody tell me what I'm doing wrong?

Thanks
Thorsten
 
Thorsten,
[DllImport("mpr.dll")]
public static extern ERRORS WNetAddConnection3(IntPtr hWndOwner, ref
NetResource pnetResource, string psPassword, string psUsername, int
piFlags);


How did you declare NetResource?


Mattias
 
Hi,

I declared NetResource as follows:

[StructLayout(LayoutKind.Sequential)]
public class NetResource
{
public RESOURCESCOPE dwScope;
public RESOURCETYPE dwType;
public RESOURCEDISPLAYTYPE dwDisplayType;
public RESOURCEUSAGE dwUsage;
public string lpLocalName;
public string lpRemoteName;
public string lpComment;
public string lpProvider;

}

The RESOURCEXYZ types are enums declared similar to

enum RESOURCETYPE : int
{
....
}
Thorsten,
[DllImport("mpr.dll")]
public static extern ERRORS WNetAddConnection3(IntPtr hWndOwner, ref
NetResource pnetResource, string psPassword, string psUsername, int
piFlags);


How did you declare NetResource?


Mattias
 
Thorsten Dittmar said:
Hi,

I declared NetResource as follows:

[StructLayout(LayoutKind.Sequential)]
public class NetResource
{
public RESOURCESCOPE dwScope;
public RESOURCETYPE dwType;
public RESOURCEDISPLAYTYPE dwDisplayType;
public RESOURCEUSAGE dwUsage;
public string lpLocalName;
public string lpRemoteName;
public string lpComment;
public string lpProvider;

}

The RESOURCEXYZ types are enums declared similar to

enum RESOURCETYPE : int
{
...
}
Thorsten,
[DllImport("mpr.dll")]
public static extern ERRORS WNetAddConnection3(IntPtr hWndOwner, ref
NetResource pnetResource, string psPassword, string psUsername, int
piFlags);


How did you declare NetResource?


Mattias


Make NetResource a struct, the fields are not laid-out as you specified in
your source code, class fields are laid out starting with the largest fields
first (8 byte types) followed by 32 bit references followed by primitives
like int, short, byte.

Willy.
 
Hi everybody,

I solved the problem myself. I had to turn my NetResource class into a
struct and that solved the problem.

Thanks
Thorsten
 
Thorsten Dittmar said:
Hi everybody,

I solved the problem myself. I had to turn my NetResource class into a
struct and that solved the problem.

Thanks
Thorsten


After I told you to change it into a structure, or didn't you read my reply?

Willy.
 
Hi,

seems Thunderbird downloaded your message after I wrote that reply -
sorry, I did not mean to present your solution as my own idea. I
actually came up with it by playing around a bit with the class/struct
and only read your answer today...

Thorsten
 
Thorsten Dittmar said:
Hi,

seems Thunderbird downloaded your message after I wrote that reply -
sorry, I did not mean to present your solution as my own idea. I
actually came up with it by playing around a bit with the class/struct
and only read your answer today...

No problem, really. I just wanted to make sure that my answer was posted
somehow.

Willy.
 
Back
Top