|
|

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

 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.comPlease reply only to the newsgroup.
|
|

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

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

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

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

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

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