Solved

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

Posted on 2006-11-20
2
178 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 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

22 Experts available now in Live!

Get 1:1 Help Now