?
Solved

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

Posted on 2006-11-20
2
Medium Priority
?
183 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
[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
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 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 29

Author Comment

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

770 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