Welcome Guest Search | Active Topics | Members | Log In | Register

Map a network share with WNetAddConnection3 Options · View
Thorsten Dittmar
Posted: Sunday, September 30, 2007 9:12:52 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 30/09/2007 21:12:52
Date: Sun, 30 Sep 2007 19:12:52 +0200

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
Mattias Sjögren
Posted: Monday, October 01, 2007 12:19:13 AM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 01/10/2007 00:19:13
Date: Sun, 30 Sep 2007 22:19:13 +0200

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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Thorsten Dittmar
Posted: Wednesday, October 03, 2007 12:53:24 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 03/10/2007 12:53:24
Date: Wed, 03 Oct 2007 10:53:24 +0200

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
{
....
}

Mattias Sjögren schrieb:
> 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
>
Willy Denoyette [MVP]
Posted: Wednesday, October 03, 2007 2:35:00 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 03/10/2007 14:35:00
Date: Wed, 3 Oct 2007 12:35:00 +0200

"Thorsten Dittmar" <thorsten@dithosoft.de> wrote in message
news:uyweXrZBIHA.4956@TK2MSFTNGP06.phx.gbl...
> 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
> {
> ...
> }
>
> Mattias Sjögren schrieb:
>> 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.


Thorsten Dittmar
Posted: Wednesday, October 03, 2007 3:09:30 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 03/10/2007 15:09:30
Date: Wed, 03 Oct 2007 13:09:30 +0200

Hi everybody,

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

Thanks
Thorsten
Willy Denoyette [MVP]
Posted: Wednesday, October 03, 2007 7:00:56 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 03/10/2007 19:00:56
Date: Wed, 3 Oct 2007 17:00:56 +0200

"Thorsten Dittmar" <thorsten@dithosoft.de> wrote in message
news:%23fzab3aBIHA.1208@TK2MSFTNGP05.phx.gbl...
> 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.

Thorsten Dittmar
Posted: Friday, October 05, 2007 12:14:09 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 05/10/2007 12:14:09
Date: Fri, 05 Oct 2007 10:14:09 +0200

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

Willy Denoyette [MVP] schrieb:
> "Thorsten Dittmar" <thorsten@dithosoft.de> wrote in message
> news:%23fzab3aBIHA.1208@TK2MSFTNGP05.phx.gbl...
>> 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.
>
Willy Denoyette [MVP]
Posted: Friday, October 05, 2007 8:12:49 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 05/10/2007 20:12:49
Date: Fri, 5 Oct 2007 18:12:49 +0200

"Thorsten Dittmar" <thorsten@dithosoft.de> wrote in message
news:ewUv2eyBIHA.5196@TK2MSFTNGP02.phx.gbl...
> 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.

Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFPro Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.1 (NET v2.0) - 9/10/2007
Copyright © 2003-2006 Yet Another Forum.net. All rights reserved.
This page was generated in 0.198 seconds.