473,320 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Map a network share with WNetAddConnection3

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
Sep 30 '07 #1
7 8423
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.
Sep 30 '07 #2
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
Oct 3 '07 #3
"Thorsten Dittmar" <th******@dithosoft.dewrote in message
news:uy**************@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.
Oct 3 '07 #4
Hi everybody,

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

Thanks
Thorsten
Oct 3 '07 #5
"Thorsten Dittmar" <th******@dithosoft.dewrote in message
news:%2****************@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.

Oct 3 '07 #6
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" <th******@dithosoft.dewrote in message
news:%2****************@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.
Oct 5 '07 #7
"Thorsten Dittmar" <th******@dithosoft.dewrote in message
news:ew**************@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.

Oct 5 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: JDeats | last post by:
Is it possible to have a .NET Application register a network drive map (e.g. Z: \\server1\myfolder)? If so could someone post a code sample? -Jeremy
1
by: brian.oneil2 | last post by:
Is there a way to install this onto a network file share and allow a team to access it? I would say share a CD from a networked CD drive, but there are multiple CD's that would have to be inserted....
8
by: Lam | last post by:
HI anyone knows how can I open a mapped network file in C#? I try string file = @"T:\file.txt"; it shows me the error: "Could not find a part of the path" but if I copy the file to my C dirve,...
3
by: musosdev | last post by:
Hi guys Okay, I've setup my projects to open and compile fine in VS2005 using FPSE and remote web, but it's *really* slow. So I thought I'd have a go at doing it the normal way, by loading from...
4
by: Jeremy S. | last post by:
We're in the process of writing a new Windows Forms app and the desktop support folks want for it to be run from a network share. I know it's possible (i.e., just have the framework on the clients...
6
by: tendim | last post by:
G'day group. Currently our organization us using VB6 based applications, and I am trying to push forward and migrate some of the smaller things to VB.NET, eventually migrating all applications...
5
by: lmttag | last post by:
ASP.NET 2.0 (C#) application Intranet application (not on the Internet) Using Windows authentication and impersonation Windows Server 2003 (IIS6) Server is a member server on a domain Logged...
3
by: BillGatesFan | last post by:
I want to be able to connect to a file share in C# given a username and password. Any help would be appreciated. Code examples welcomed
2
by: =?Utf-8?B?Z3JlYXRiYXJyaWVyODY=?= | last post by:
Hi, Is there an API call for mapping a network drive? i think i am using the right expression but i am referring to something in the same realm as InitiateSystemShutdown. That's an API call...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.