• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 583
  • Last Modified:

Detect Network Paths

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
poohbear_68
Asked:
poohbear_68
  • 3
  • 2
1 Solution
 
jkrCommented:
For mapped drives, you could use 'GetDriveType()', e.g.

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

 // mapped drive
}
0
 
jkrCommented:
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
 
poohbear_68Author Commented:
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
 
jkrCommented:
>>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
 
poohbear_68Author Commented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now