Click here to Skip to main content
13,554,844 members
Click here to Skip to main content
Add your own
alternative version


25 bookmarked
Posted 20 May 2007

Creating network share programmatically

, 20 May 2007
Rate this:
Please Sign up or sign in to vote.
I have used window api to create the share to the network resource.


This article shows the use of the window api to create the share on the network resource. The code snippet firstly check for all the system drives, if it it found the mapped network drive then program simply returns. otherwise it create the network share using the command line arguments supplied to the executable. This is just to demonstrate for creating a mapped drive programmatically.

Using the code

Download & compile the code using the vs2003, now run the executable by supplying the command line parameters to it. i have used the command line parameters to make the program more that the user of the program simply provide the parameter as he/she required. Please check the command line arguments use as follows

map_drive [username, password, local drive, remote drive]

where username, password is the login credential for the remote system

local drive can be any drive letter other than the drive letter used at local system for example Q:, P: etc.

remote drive is basically the share path which will be mapped to local drive, it should be supplied as follows.

\\ <remote system name> \ <remote directory or drive.>

so lets say the remote system name is admin & the shared directory is songs. so the remote drive parameter could be having the following format.


so to sum up all the things...the following command could be used to make network share programmatically

map_drive sysadmin pwd123 X: \\admin\songs.

The following code shows the use of the window api which is responsible creating the above shared network resource.

 DWORD res;
 TCHAR szUserName[MAXLEN];
 TCHAR  szPassword[MAXLEN];
 TCHAR  szLocalName[MAXLEN];
 TCHAR  szRemoteName[MAXLEN];
// Assign values to the NETRESOURCE structure.

 nr.lpLocalName = szLocalName;
 nr.lpRemoteName = szRemoteName;
 nr.lpProvider = NULL;
// Call the WNetAddConnection2 function to assign
//   a drive letter to the share.

 res = WNetAddConnection2(&nr, szPassword, szUserName, FALSE);

Points of Interest

I have used conversion funtion which convert char * to TCHAR *, this basically runs the loop on char* & then typecast each char to TCHAR. currently this is working fine..but there could be another efficient way to do that...i dont know :) if any one know about the other way to do char * to TCHAR * please write to me in feedback.


This is just a first release.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Raja Iqbal Singh
Software Developer (Senior)
India India
I love programming & developing things from the scratch...

You may also be interested in...


Comments and Discussions

QuestionUnusual behavior with WNetAddConnection2 & Windows Explorer Pin
fred shaw19-Jul-07 5:27
memberfred shaw19-Jul-07 5:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180515.1 | Last Updated 21 May 2007
Article Copyright 2007 by Raja Iqbal Singh
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid