Click here to Skip to main content
Email Password   helpLost your password?

Introduction

This is a simple wrapper DLL to allow easier access to the WNet API from within the .NET environment.

The WNet API can be used to add/delete network connections (drive and printer mappings) as well as enumerate all devices and shares on the network (see MSDN for the full documentation).

Out of a total of 23 functions and seven structures, 17 functions and all structures are included in this version of the DLL.

The download contains source code for the DLL, which has XML documentation included. There is also a (very) simple sample application in order to demonstrate how to call the functions.

At the moment, the following functions still have not been wrapped.

Any feedback would be appreciated.

Email (make obvious replacements): nlinnett *at* integranetworks *dot* com.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generalhow to test these classes
baya3
9:46 28 May '08  
i tried to imprt the solution to v.studio , but couldnt run it or debyg it.
any ideas
thx
GeneralThank you
sbsrazor
16:53 23 May '08  
Thank you for this solution. I haven't done much coding with the Windows API, and a new project required me to resolve network paths (using drive letters) to UNC paths. Most of the solutions I found on the web are in VB and not easily converted to VS2005 C++. The only other solution I found used a ManagementObjectSearcher and a SQL'ish statement to resolve the UNC path. However, it took nearly 5-10 seconds per function call. Yours works much better.

Thanks
GeneralNice article
Vikas Salvi
22:23 24 May '06  
Nice article,but from where u find the enum DEFINITION?

Vikas Salvi

Programmer Analyst
Generalnice, but need WNetGetNetworkInformation
Princesse
6:49 18 Jan '05  
Really nice, it's exaclty what I was searching for.

The only thing missing to fully feed my need is the WNetGetNetworkInformation function.
Here is what I want to do :

I have a network connection (ex. Z: pointing on \\server\Dir) that I want to redirect temporary on a local directory (ex. Z: pointing on \\Computer_Name\C$\Windows\Temp) and after some job redirect it again to its previous location (\\server\Dir).

When I unmap a connection without making it persistent, the API won't let me redirect it to another location. Consequently, I have to make the change persistent and if so, I cannot use the RestoreConnection function. Cry

I want to be able to know where a drive connection where accessing before I change it, I suppose that the WNetGetNetworkInformation is the right API to use, but When I tried to add it to your class I had some problem with the Interfacing thing (kind of thing I'm not used to yet).

Is anybody can give me a quick method to get a path drive connection?
Or then, can you help me find my problem with my essaie?

Thanks in advance about everyting. Big Grin


GeneralRe: nice, but need WNetGetNetworkInformation
Princesse
7:02 18 Jan '05  

Sorry... D'Oh!

I meant the WNetGetConnection rather than WNetGetNetworkInformation...
GeneralRe: nice, but need WNetGetNetworkInformation
Princesse
5:40 20 Jan '05  

With a little time and willing and the help of Calishar (thanks for giving me the www.pinvoke.net link), I sucess to get the network name of a local drive Big Grin

Here is the code for that...

(In the API section)

[DllImport("mpr.dll", CharSet=CharSet.Auto, SetLastError=true)] private static extern int WNetGetConnection( string localName, StringBuilder remoteName, ref int length);
private const int NO_ERROR = 0x00000000;

(In the core functions sections)

/// /// Get the full network path related with a connection drive letter.
///
/// The local drive letter as a "Z:" or "Z:\" template. /// The network path of the local drive as a "\\server_name\path" template. private String zGetConnection(String szDriveLetter)
{
//Start length, if it isn't enough, will enlarge it later.
int length = 100;
StringBuilder remoteName = new StringBuilder(length);
int res = WNetGetConnection(szDriveLetter, remoteName, ref length);

/* If the call fails because the StringBuilder is not large enough, the call
* returns a value of 234(ERROR_MORE_DATA), and the length parameter contains
* the required size. */
if(res==ERROR_MORE_DATA)
{
remoteName = new String Builder(length);
res = WNetGetConnection(szDriveLetter, remoteName, ref length);
}

if(res==NO_ERROR)
return remoteNAme.ToString();
else throw new System.ComponentModel.Win32Exception(res);
}

That's it!

You can now create a mapping function call "GetDriveFullPath" or equivalent that calls zGetConnection and you're in business.

Hope this will help!

Ciao!

GeneralNice Job
Eric Rinn
7:06 21 Dec '03  
Nice job on the downloadable stuff, the article wasn't long, but the code was what I was looking for.

Thanks!
GeneralHow do you respond to the Author of these code submissions
SilverKitty
5:52 21 Aug '03  
If there is no email listed?


Generalsuch a hi rating
Normski
21:19 19 Aug '03  
for such a skimpy article?
GeneralRe: such a hi rating
Nishant S
22:34 19 Aug '03  
1.8 is high?

Nish Confused


Extending MFC Applications with the .NET Framework [NW] (My book with Tom)
Summer Love and Some more Cricket [NW] (My first novel)
Shog's review of SLASMC [NW] Come with me if you want to live


Last Updated 19 Aug 2003 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010