Solved

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

Posted on 2006-11-20
2
177 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
Comment Utility
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
Comment Utility
Thanks jkr. The GetFileAttributes() was the answer for what I needed.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MVC app and F5 Load Balancing 3 58
Vb.Net Loading Word 11 95
mixing C++ & C# in Vis Studio 2013 7 125
Problem to packaging 1 73
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.…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

10 Experts available now in Live!

Get 1:1 Help Now