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
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
You'd have to delete it if it's been created.
Seems an odd situation to be in, what's behind this question?
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);
}
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.
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;
}
If you always want to delete this file, then consider CreateFile with FILE_FLAG_DELETE_ON_CLOSE flag.
12 people are following this question.