Mounting Network Drives | CodeGuru

Mounting Network Drives

Environment: Windows 95, Visual C++ 6 We had a need for users to not tie up file sharing licenses all day when not needed. Since they only needed the network drive mounted for one app, I embedded routines in the app to automatically check for the drive non-existence, prompt for password, and mount the drive. […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 16, 2000
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: Windows 95, Visual C++ 6

We had a need for users to not tie up file sharing licenses all day when not needed.

Since they only needed the network drive mounted for one app, I embedded routines in the app to automatically check for the drive non-existence, prompt for password, and mount the drive.

I created the password screen exactly the same as the one NT shows for mounting drives. Then I created the wrapped class for it, called CDriveMountDlg.

In the application Doc.cpp, I check for a folder on the network drive. If it exists, fine, do nothing. Otherwise, call the CDriveMountDlg, and get the password.

I begin a loop of drive letters, attempting to mount the network drive for each one until successful, or I run out of letters. I save the letter for unmounting later.

#include <io.h&gt

BOOL CMounterDoc::OnNewDocument()
{
 if (!CDocument::OnNewDocument())
  return FALSE;
 // Check if \\APPS1\data is mounted
 // -1 means failed
 if( (_access( “\\\\apps1\\data\\parcel”, 0 )) == -1 )
 {
  CDriveMountDlg  dlg;
  int response = dlg.DoModal();
  if (response == IDCANCEL)
   return FALSE;
  NETRESOURCE resource;
  resource.dwType = RESOURCETYPE_ANY;
  resource.lpRemoteName = “\\\\apps1\\data”;
  resource.lpProvider = NULL;
  int nDrive = 4;
  CString sDrive;
  sDrive.Format(“%c:”, nDrive + ‘A’);
  resource.lpLocalName = sDrive.GetBuffer(2);
  DWORD result = WNetAddConnection2(&resource,
   (LPCSTR)dlg.m_sPassword, NULL, 0);
  while ((result != 0) && (nDrive < 25))
  {
   nDrive++;
   sDrive.Format("%c:", nDrive + 'A');
   result = WNetAddConnection2(&resource,
    (LPCSTR)dlg.m_sPassword, NULL, 0);
  }

  if (result != 0)
  {
   AfxMessageBox("Unable to find a free drive. Aborting.");
   return FALSE;
  }

  m_sDrive = sDrive;
  m_bMountFlag = TRUE;
 }
}

When the application exits, it calls OnCloseDocument

void CMounterDoc::OnCloseDocument()
{
      if (m_bMountFlag)
      {
         DWORD result = WNetCancelConnection2(m_sDrive,
		  0, TRUE);

         if (result != NO_ERROR)
         {
         // Error handling here
         }
      }
   CDocument::OnCloseDocument();
}

Finally, add the file mpr.lib to the project.

WNetAddConnection2 is also supposed to handle empty drive letter strings, creating an invisible connection. This works, although I was unable to disconnect later. Any solutions to this problem would be appreciated.

Downloads

Download demo project – 18 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.