UNC paths from local paths

I needed to get an UNC path from a local path. I was searching the MSDN and the Internet to get information about this, but I didn't find anything. I did find posts requesting a procedure to solve this problem, so maybe this code can be useful.

The solution I found is tricky, perhaps too much tricky, but it seems to work properly. It has been tested in Windows 9x and Windows NT 4.0. The idea is to get the name of the shared resources and look for its real path in the registry (the keys are different in Win9x and NT). When we have the path, we check if the local path that we want to translate to UNC is a subtree of the resource path. If not, we continue the process until no more shared resources are found (or until the UNC path is found). This approach could be also used to solve the inverse problem, how to get a local path from an UNC path.

This procedure has been implemented in a class, CUNC, which has just one public method:

bool GetUNCFromLocalPath(LPCTSTR localPath, CString *uncPath);

In order to use this class, just add the files to your project and instantiate a CUNC object when needed:

void Foo() {
  CFileDialog dlg(TRUE);
  if (dlg.DoModal() == IDOK) {
    CString filename;
    CUNC unc;
    if ( unc.GetUNCFromLocalPath(dlg.GetPathName(),
         &filename)) {
      // ...
    }
  }
}

Downloads

Download source - 3Kb

IT Offers

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Go Deeper

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds