Solved

Detect Network Paths

Posted on 2002-07-23
5
547 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Define unique primary key 9 95
bigDiff challenge 17 134
My project did see openJDK that I installed. What could be the problem 7 160
Increment column based of a FK 8 48
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 …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.

735 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