UNC paths from local paths | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Jul 25, 2001
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

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

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.