'Strange results when Deleting directory

Working with an old (really old) application. No access to the source so unable to determine what it may be doing but it seems to be the source of unusual behavior.

Lets call this application the "Black Box". Here is what is happening.

Scenario 1: If I create a directory via the command prompt and run my program (see code below) to delete the directory, the directory gets deleted as expected.

Scenario 2: If the Black Box creates a directory and my program is run to delete that same directory, it returns "Successful", but when I check via the command prompt, the directory still exists.

Scenario 3: If the Black Box creates a directory then turns around and deletes the same directory, the directory is still visible via the command prompt, but when I delete the same directory with my code it returns "Invalid Handle".

#include <windows.h>
#include <tchar.h> 
#include <stdio.h>
#include <strsafe.h>
#pragma comment(lib, "User32.lib")

//void DisplayErrorBox(LPTSTR lpszFunction);

int _tmain(int argc, TCHAR* argv[])
{
    WIN32_FIND_DATA ffd;
    LARGE_INTEGER filesize;
    TCHAR szDir[MAX_PATH];
    TCHAR szDir_original[MAX_PATH];
    size_t length_of_arg;
    HANDLE hFind = INVALID_HANDLE_VALUE;
    DWORD dwError = 0;

    // If the directory is not specified as a command-line argument,
    // print usage.

    if (argc != 2)
    {
        _tprintf(TEXT("\nUsage: %s <directory name>\n"), argv[0]);
        return (-1);
    }

    // Check that the input path plus 3 is not longer than MAX_PATH.
    // Three characters are for the "\*" plus NULL appended below.

    StringCchLength(argv[1], MAX_PATH, &length_of_arg);

    if (length_of_arg > (MAX_PATH - 3))
    {
        _tprintf(TEXT("\nDirectory path is too long.\n"));
        return (-1);
    }

    _tprintf(TEXT("\nTarget directory is %s\n\n"), argv[1]);

    // Prepare string for use with FindFile functions.  First, copy the
    // string to a buffer, then append '\*' to the directory name.

    StringCchCopy(szDir, MAX_PATH, argv[1]);
    StringCchCopy(szDir_original, MAX_PATH, argv[1]);
    StringCchCat(szDir, MAX_PATH, TEXT("\\*"));

    // Find the first file in the directory.
    hFind = FindFirstFile(szDir, &ffd);


    if (INVALID_HANDLE_VALUE == hFind)
    {
        _tprintf(TEXT("Invalid Handle  %s   <DIR>\n"), szDir);
        //DisplayErrorBox(TEXT("FindFirstFile"));
        return dwError;
    }

    // List all the files in the directory with some info about them.

    

    do
    {
        if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
            _tprintf(TEXT("  %s   <DIR>\n"), ffd.cFileName);
        }
        else
        {
            filesize.LowPart = ffd.nFileSizeLow;
            filesize.HighPart = ffd.nFileSizeHigh;
            _tprintf(TEXT("  %s   %ld bytes\n"), ffd.cFileName, filesize.QuadPart);
        }
    } while (FindNextFile(hFind, &ffd) != 0);

    dwError = GetLastError();
    if (dwError != ERROR_NO_MORE_FILES)
    {
        //DisplayErrorBox(TEXT("FindFirstFile"));
        _tprintf(TEXT("Error\n"));
    }

    
    _tprintf(TEXT("Done. This is what we will delete : %s\n"), szDir_original);

    BOOL fSuccess = FALSE;
    fSuccess = RemoveDirectory((szDir_original));

    if (!fSuccess)
    {
        // Handle the error.
        printf("DeleteFile() failed, error % d\n", GetLastError());
        return (6);
    }
    else
        printf("DeleteFile() Successful!!!!");



    FindClose(hFind);
    return dwError;
}

Questions

1 - what could this "black box" be doing or what special command could it be placing on the directory which causes my application to return Success yet doesn't really delete it??

2 - what could this "black box" be doing or what special command could it be placing on the directory so when it deletes it, it still remains and causes my application to return "Invalid Handle" yet it still exists and I can access it via the command line.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source