Solved

Detect Network Paths

Posted on 2002-07-23
5
535 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expand data scrubbing tool 13 32
EvenOdd challenge 10 108
Execute multiple curl cmds with sleep and send output to file 10 97
Not needed 13 113
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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…

803 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