question

drjackool-3839 avatar image
0 Votes"
drjackool-3839 asked drjackool-3839 commented

How ignore file creation after calling CreateFile() Win32 API

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-apic++
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You'd have to delete it if it's been created.
Seems an odd situation to be in, what's behind this question?

0 Votes 0 ·

I have code like this:

 hFile = CreateFile()
 if (hFile != invalid_handle...)
 {
      if (SetFilePointer(hFile, ...) == FALSE)
      {
             // here I want cancel the file if SetFilePointer() Fails!!!
      }
    
       if (SetEndOfFile(hFile, ...) == FALSE)
       {
        // also here
       }
    
       CloseHandle(hFile);
 }
0 Votes 0 ·

And by "cancel", you mean "delete"?
I believe you can just call DeleteFile - though there's the (unlikely) situation that you may have permissions to create a file, but not delete it.

0 Votes 0 ·
RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered drjackool-3839 commented

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;
 }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered drjackool-3839 commented

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


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.