Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

A Quick Drive Manager

1.57/5 (5 votes)
5 Jun 2009CPOL 26.5K   675  
Connecting and disconnecting network drives

Introduction 

The project demonstrates the use of some useful network-API of Windows by connecting and disconnecting network shares.

Background 

This is very useful if anyone wants to handle network shares and assign and delete drive letters for it.

Using the Code 

The main focus lies on the Drives class which handles the networking tasks and its API. Useful is the ErrorText class by providing some error texts.

The Drives class encapsulates the API and works this way:

Connecting to a share:

C++
DWORD dw = m_drives.ConnectionDialog1( m_csPath, &t );

//for deleting a drive letter (not its content)
DWORD dw = m_drives.DisConnect( m_csDrive );	

It hides some structs and its initializing with proper values: 

C++
memset( &m_NetResource, 0, sizeof(m_NetResource) );
m_NetResource.dwScope = RESOURCE_REMEMBERED;
m_NetResource.dwType = RESOURCETYPE_DISK;
m_NetResource.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE;
m_NetResource.dwUsage = RESOURCEUSAGE_CONNECTABLE;
m_NetResource.lpLocalName = szDrive;//TEXT("Test");//
m_NetResource.lpRemoteName = (LPTSTR) pszPath;

CONNECTDLGSTRUCT dlg = {0};
dlg.cbStructure = sizeof(dlg);
dlg.hwndOwner = GetDesktopWindow();
dlg.lpConnRes = &m_NetResource;
dlg.dwFlags = CONNDLG_PERSIST|CONNDLG_USE_MRU;

Demonstration 

For real world use, I had conceptualised the class. I have provided a tiny MFC GUI.

I want to give the audience a look for what class is useable. With the open drive button, you can access the new drive. 

QDM_Main.jpg

Points of Interest  

Networking is a real huge area; this is only a little demonstration of two interesting tasks. There is a lot I could do to extend it. It would be interesting to enumerate findable shares and make them selectable. The desired drive letters didn't work on Vista.

History 

  • 2009/06/05: Initial release v0.9

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)