Solved

Detect Network Paths

Posted on 2002-07-23
5
515 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
Comment Utility
For mapped drives, you could use 'GetDriveType()', e.g.

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

 // mapped drive
}
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 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

13 Experts available now in Live!

Get 1:1 Help Now