Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Server path to Mapped Network Drive

Posted on 2004-08-24
4
Medium Priority
?
376 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 200 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Screencast - Getting to Know the Pipeline
Suggested Courses

927 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