Solved

Detect Network Paths

Posted on 2002-07-23
5
542 Views
Last Modified: 2013-11-20
I would like to be able to detect network paths.  I considered checking whether the first two letters of the path are both backslashes, but that doesn't catch mapped drives.

What is the best way to do this?
0
Comment
Question by:poohbear_68
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 7171760
For mapped drives, you could use 'GetDriveType()', e.g.

if ( DRIVE_REMOTE == GetDriveType ( "f:\\")) {

 // mapped drive
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 7171764
Oh, and for all other paths, 'PathIsUNC()' might make it a little easier:

PathIsUNC


BOOL PathIsUNC(
    LPCTSTR pszPath
    );

Determines if the string is a valid UNC (universal naming convention) for a server and share path.

Returns TRUE if the string is a valid UNC path, or FALSE otherwise.
pszPath
Address of the path to validate.
Example:

#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"

void main( void )
{
     // String path name 1.
     char buffer_1[] = "\\\\path1\\path2";
     char *lpStr1;
     lpStr1 = buffer_1;

     // String path name 2.
     char buffer_2[] = "\\\\path3";
     char *lpStr2;
     lpStr2 = buffer_2;

     // String path name 3.
     char buffer_3[] = "acme\\path4\\path5";
     char *lpStr3;
     lpStr3 = buffer_3;

     // Variable to get the return
     // from "PathIsUNC".    
    int     retval;    

     // Test path name 1.
    retval = PathIsUNC(lpStr1);
    cout << "The contents of String 1: " << lpStr1
           << "\nThe return value from the function is " << retval << " = TRUE" << endl;
     // Test path name 2.
     retval = PathIsUNC(lpStr2);
    cout << "The contents of String 2: " << lpStr2
           << "\nThe return value from the function is " << retval << " = TRUE" << endl;
     // Test path name 3.
     retval = PathIsUNC(lpStr3);
    cout << "The contents of String 3: " << lpStr3
           << "\nThe return value from the function is " << retval << " = FALSE"<< endl;
}    
OUTPUT:
==============
The contents of String 1: \\path1\path2
The return value from the function is 1 = TRUE
The contents of String 2: \\path3
The return value from the function is 1 = TRUE
The contents of String 3: acme\path4\path5
The return value from the function is 0 = FALSE

0
 
LVL 1

Author Comment

by:poohbear_68
ID: 7173094
I see that the ShareIsUNC requires IE 4 on Windows 95, which is probably a requirement that won't be met on my client's computer.  Here's what I'd like to do: I would like to verify that the client is running the executable locally (not over the network).  Surely the executable path would either be a network drive or start with a double backslash, wouldn't it? Or are there other Windows tricks I'm missing?
0
 
LVL 86

Expert Comment

by:jkr
ID: 7173166
>>Surely the executable path would either be a network
>>drive or start with a double backslash, wouldn't it? Or
>>are there other Windows tricks I'm missing?

No, you are completely right - that's why I posted the solution for mapped drives in the first place. There is just one special exception - when the executable was started from the local computer using an UNC path. In this case you might want to compare the machine name after the leading two backslashes with the one that "GetComputerName()" reports.
0
 
LVL 1

Author Comment

by:poohbear_68
ID: 7173308
Here's my function, both for you and for posterity.  Thanks for your help.


bool IsNetworkPath(CString sPath)
{
   // If the path begins with a double backslash, check whether it's local.
   if(sPath[0] == '\\' && sPath[1] == '\\')
   {
      // Get the name of the local computer
      char szComputerName[MAX_COMPUTERNAME_LENGTH + 1];
      DWORD dwComputerLen = sizeof(szComputerName);
      GetComputerName(szComputerName, &dwComputerLen);

      // Get the name of the computer in the UNC path.
      CString sComputerNameInUNCPath = sPath.Mid(2);
      int iBackslashPos = sComputerNameInUNCPath.Find('\\');
      if(iBackslashPos != -1)
         sComputerNameInUNCPath = sComputerNameInUNCPath.Left(iBackslashPos);

      // This is a network path if it is not the local computer.  (Duh!)
      return sComputerNameInUNCPath.CompareNoCase(szComputerName) != 0;
   }

   // Get the drive letter.
   char szDrive[_MAX_DRIVE + 1/*backslash*/];
   _splitpath(sPath, szDrive, NULL, NULL, NULL);

   // Append a backslash if necessary.
   if(szDrive[2] != '\\') strcat(szDrive, "\\");

   // Check whether this is a remote drive.
   return (DRIVE_REMOTE == GetDriveType(szDrive));
}
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sumDigits challenge 9 170
Prevent this page from creating additional dialogs. 3 611
move a line in eclipse 3 107
isEverywhere  challenge 19 82
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.

840 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