Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 466
  • Last Modified:

SHPathPrepareForWrite API not working correctly

I am having a piece of code(on windows) to detect whether file/folder/drive given in the path physically exists or not. for the same i m using SHPathPrepareForWrite API, but this is failing, returning wrong result, if given path is of a solaris system. here is the code ...
 
bool Exists(const uchar16 *path) const
{
 HRESULT result;
 
 // Check if the folder already exists
 result = SHPathPrepareForWrite(NULL, NULL, path, SHPPFW_NONE);
 if (result == S_OK) {
  return = TRUE;
 }
 return FALSE;
}
 
if my path is say
\\diehard\tiwana\abc.txt or \\10.91.123.231\tiwana\abc.txt
diehard (IP of diehard) is a solaris system and folder tiwana exist there but file doesn't exist there. the API produces S_OK result which is wrong. Same is working fine if Win or MAC system's path is provided.
 
Do anyone any idea regarding this?? Am I missing something?? Please suggest a workaround if possible.
0
jastiwana
Asked:
jastiwana
  • 2
  • 2
1 Solution
 
AlexFMCommented:
Try GetFileAttributes instead this function.
0
 
jkrCommented:
A little more specific:

bool Exists(const uchar16 *path) const
{

DWORD dwAtt = GetFileAttributes(path);

if ( -1 == dwAtt) return false;

return true;
}

You can even check if it is a folder using

if ( FILE_ATTRIBUTE_DIRECTORY & dwAtt) {
}
0
 
jastiwanaAuthor Commented:
Hi Alex
Thanx for the idea, we've used GetFileAttributesEx() and it has served my purpose,  although what I exactly wanted was to just check whether a file exists or not.
So its kinda workaround but it serves my purpose for the time being. But plz see to it
if you know anything more specific.

This i s my code:

bool8 Exists(const uchar16 *path) const
{
    WIN32_FILE_ATTRIBUTE_DATA data;

    if (GetFileAttributesExW(path, GetFileExInfoStandard, &data) == 0) {
        DWORD error = GetLastError();
        if (error == ERROR_FILE_NOT_FOUND || error == ERROR_PATH_NOT_FOUND ) {
            return FALSE;
        }
    }
    return TRUE;
}

*GetFileAttrinbuteExW() is unicode wrapper over GetFileAtrributeEx().
0
 
AlexFMCommented:
Looks OK, you can add FILE_ATTRIBUTE_DIRECTORY test as jkr suggests, if you want only file or only directory. I have my own FileExists function which works with GetFileAttributes, I don't see anything wrong in this way.
0
 
jkrCommented:
Thanks for appreciating my help.
0

Featured Post

Independent Software Vendors: 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!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now