How ignore file creation after calling CreateFile() Win32 API

drjackool 956 Reputation points
2021-09-07T16:18:19.387+00:00

Hi
How cancel file creating after calling CreateFile() and before CloseHandle? is there a way or have to use CloseHandle() then DeleteFile()?
Can I use CancelIo() ?
Thanks

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,428 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,542 questions
{count} votes

Accepted answer
  1. RLWA32 40,756 Reputation points
    2021-09-07T17:27:28.087+00:00

    Try this -

    #define WIN32_LEAN_AND_MEAN
    #include <Windows.h>
    
    int main()
    {
        DWORD dwDesiredAccess = GENERIC_ALL; // or ask for DELETE with other desired permissions
    
        HANDLE hFile = CreateFile(TEXT("Badfile.txt"), dwDesiredAccess, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
        if (hFile != INVALID_HANDLE_VALUE)
        {
            // Oops! Something bad happened
    
            FILE_DISPOSITION_INFO fdi = { static_cast<BOOLEAN>(TRUE) };
            SetFileInformationByHandle(hFile, FileDispositionInfo, &fdi, sizeof fdi);
            CloseHandle(hFile);
        }
        return 0;
    }
    

1 additional answer

Sort by: Most helpful
  1. Viorel 112.5K Reputation points
    2021-09-07T16:57:13.303+00:00

    If you always want to delete this file, then consider CreateFile with FILE_FLAG_DELETE_ON_CLOSE flag.

    0 comments No comments