Solved

What Windows API to be used to test host and mount point for existence?

Posted on 2006-11-20
2
179 Views
Last Modified: 2013-12-03
I do have UNC path like \\host\mountpoint\subdir\subdirB\etc. I want to implement a function that creates all directories along the path if they do not exist. I need to detect whether the host and the mount point exists. What API should be used? (I am using C++, low level is acceptable.)

My previous naive approach did not take UNC paths into account. I did split the path to the disk name and the list of subdirectories. Then I went from the root of the disk and used _stat() function to test whether the item exists and whether it is a subdirectory. Later, I tried to modify the source to accept UNC paths; however, the _stat() fails for \\host and \\host\mountpoint. Now I have to modify my exists() and isdir() functions so that they do not use the _stat() function.

I have already implemented the splitunc() fuction that returns the pair of (unc, rest) paths (the Python language heritage). I need to modify my exists() so that it says whether the \\host exists or not and whether the \\host\mountpoint exists or not.

Is it reasonable to say that \\host is never a file nor the directory, and \\host\mountpoint (if exists) is never a file but can be treated as directory?

Thanks for your time and experience,
  Petr
0
Comment
Question by:pepr
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 17982374
You could

SHARE_INFO_0 si;

      wchar_t *pServer = L"\\\\hostname";      // server to run on
      wchar_t *pShare  = L"mountpoint";       // sharename to query

      int rc = NetShareGetInfo( (LPTSTR) pServer, (LPTSTR) pShare, 0, &si);

        if ( rc == NERR_Success) {

                // server and share are accessible
        }

To create the directories along a path, use

DWORD       ExCreateDir (   char*   pszDir)
{
    char    acDir   [   MAX_PATH];
    char*   pc      =   NULL;
    DWORD   dwAtt   =   0;

    dwAtt   =   GetFileAttributes   (   pszDir);

    if  (   FILE_ATTRIBUTE_DIRECTORY    ==  dwAtt)
            return  (   dwAtt);

    strcpy  (   acDir,  pszDir);

    pc  =   strrchr (   acDir,  '\\');

    if  (   pc)
            *pc =   ( char) 0;
     else
            return  (   0xffffffff);

    dwAtt   =   ExCreateDir (   acDir);

    if  (   FILE_ATTRIBUTE_DIRECTORY    ==  dwAtt)
        {
            if  (   !CreateDirectory    (   pszDir, NULL))
                {
                    return  (   0xffffffff);
                }

            return  (   dwAtt);
        }

    return  (   0);
}
0
 
LVL 28

Author Comment

by:pepr
ID: 17988724
Thanks jkr. The GetFileAttributes() was the answer for what I needed.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

810 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