Solved

Server path to Mapped Network Drive

Posted on 2004-08-24
4
345 Views
Last Modified: 2013-11-20
Hi,

My OS is WinXP SP1. The following path is mapped to drive Z:

\\med2\transfer where med2 is the server name and transfer is the share folder on the server.

In my MFC dialog program, I get this path

\\med2\transfer\data\log.txt

So my question is how to convert the above path to this Z:\data\log.txt programmatically.

I have tried Volume Mount Point functions, but can't find the solution.

Thanks for your help

-David
0
Comment
Question by:htang_us
  • 2
4 Comments
 

Expert Comment

by:nickblah42
ID: 11884308
Use NetShareEnum to enumerate the current network shares.  SHARE_INFO_2 (for NT, share_info_50 for 9x) has the path for the share with the name.

Parse the UNC path for the \\[server]\[share] part, enum shares on server, compare share name with enum results (case insensitive), find matching share, read out local path, construct final local path.

- Nick
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 50 total points
ID: 11886330
#include <process.h>
#include <string>
#include <fstream>

using namespace std;

char getDriveFromUNC(const string uncPath)
{    
     system("net use >%TEMP%\\mapall.lis");
     int pos = uncPath.find('\\', 2);
     if (pos == string::npos)
          return '\0';
     string server = uncPath.substr(0, pos);
     int pos1 =  uncPath.find('\\', pos+1);
     if (pos1 == string::npos)
          return '\0';
     string share = uncPath.substr(pos, pos1-pos);
   

     char   temp[256];
     int     lent = GetEnvironmentVariable("TEMP", temp, sizeof(temp));
     string tempfile = temp;
     tempfile += "\\mapall.lis";
     ifstream ifs(tempfile.c_str());
     string contents, line;
     while (!ifs.fail() && !ifs.eof())
     {
          getline(ifs, line);
          pos = line.find(server);
          if (pos != string::npos)
          {
              pos1 = line.find(share, pos + server.length());
              if (pos1 != string::npos)
              {
                     pos1 = line.find(':');
                     if (pos1 != string::npos && pos1 < pos)
                         return line[pos1-1];
              }
          }
      }
      return '\0';
}
     
Note, that solution is case-sensitive. You should turn all strings to upper-case if uncPath might have wrong case.

Regards, Alex

     

     
0
 
LVL 2

Author Comment

by:htang_us
ID: 11895056
Hi, Nick,

I tried your solution and NetShareEnum always return ERROR_ACCESS_DENIED, so this solution requires user has priviledge to access the remote host, not only the share folder.

Alex,

Thanks for your code snippet that solves my question.

Thanks again, Nick and Alex

-David
0
 

Expert Comment

by:nickblah42
ID: 11895126
Oh... my bad.  You want NetUseEnum for the local devices.  This does the same thing as the 'net use' shell command.

Glad you got it working. :)

- Nick
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now