UNC paths from local paths
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)) {
// ...
}
}
}

Comments
help please
Posted by greenjade800 on 04/23/2004 03:49pmDo you know how to get UNC when the remote path don't have a local drive letter? For example, server published the software. It is showing on the add/remove program.
ReplyGreat job man !!!
Posted by Legacy on 11/14/2003 12:00amOriginally posted by: Kreelin
Well done Manuel !!!
This is exactly what i need.
Reply
FIX for W9x
Posted by Legacy on 07/30/2002 12:00amOriginally posted by: Pascal
Nice work despite it doesn't work as-is with W9x (nor with the contribution of HJ). I found TWO fixes to make it run fine.
1 - The problem comes when you call GetLocalHostName, because the 'gethostname' function returns the name of Host as it stands in the DNS tab of TCP-IP configuration. With W9x you can put there nothing, or something stupid (e.g. 'asdf'). And finally that can't be used.
Instead of that it's better to call '::GetComputerName' function, which returns the name of the computer as it is seen from the network.
2 - If the path you want to parse is a local root path (C:\hello.txt - that gives you are looking for 'C:'), then in the TryResource function it will NEVER correspond to the resource, as this one will be 'C:\'. So you have to add a couple of lines:
if(localPath.GetLength()==2 && localPath[1]==':')
localPath += "\\";
return localPath.Find(*pathResource) == 0;
And it works in all cases (as far as I can guess).
Pascal
ReplyBug fixed when network mapping
Posted by Legacy on 11/24/2001 12:00amOriginally posted by: Hong Jun
ReplyRE: Existing windows SDK function
Posted by Legacy on 08/04/2001 12:00amOriginally posted by: M. Escribano
Yes, but WNetGetUniversalName() only works if there is a network mapping. If the remote path has not a local drive letter, it doesn't work.
ReplyMAPI also has a ScLocalPathFromUNC()function, but it warns that could not be available in future versions.
Existing windows SDK function
Posted by Legacy on 07/26/2001 12:00amOriginally posted by: Oren Recht
Reply