Map a network share with WNetAddConnection3

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
 
M

Mattias Sjögren

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
 
T

Thorsten Dittmar

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
 
W

Willy Denoyette [MVP]

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.
 
T

Thorsten Dittmar

Hi everybody,

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

Thanks
Thorsten
 
W

Willy Denoyette [MVP]

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.
 
T

Thorsten Dittmar

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
 
W

Willy Denoyette [MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top