Solved

Server path to Mapped Network Drive

Posted on 2004-08-24
4
347 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
notReplace  challenge 53 125
post4 challenge 28 100
firstChar challenge 13 115
Problem to App source 6 36
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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