pastebin - collaborative debugging tool
nrubsig.kpaste.net RSS


disposefileex1.c - FileDispositionInfoEx test
Posted by Anonymous on Tue 22nd Jul 2025 18:54
raw | new post

  1. /*
  2.  * Compile with:
  3.  * $ gcc -std=gnu17 -Wall disposefileex1.c -lntdll -o disposefileex1.exe #
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <windows.h>
  8. #include <winternl.h>
  9.  
  10. #define EXIT_USAGE 2
  11.  
  12. #if 0
  13. typedef struct _FILE_DISPOSITION_INFO_EX {
  14.     DWORD Flags;
  15. } FILE_DISPOSITION_INFO_EX, *PFILE_DISPOSITION_INFO_EX;
  16. #endif
  17.  
  18.  
  19. #define FILE_DISPOSITION_DELETE                  0x00000001
  20. #define FILE_DISPOSITION_DO_NOT_DELETE           0x00000002
  21. #define FILE_DISPOSITION_POSIX_DISPOSITION       0x00000003
  22. #define FILE_DISPOSITION_FORCE_IMAGE_DEINSTALL   0x00000004
  23. #define FILE_DISPOSITION_ON_CLOSE                0x00000008
  24. #define FILE_DISPOSITION_SPARSE                  0x00000010
  25.  
  26.  
  27. int main(int argc, char *argv[]) {
  28.     int retval;
  29.  
  30.     if (argc != 2) {
  31.         (void)fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
  32.         return EXIT_USAGE;
  33.     }
  34.  
  35.     const char *filename = argv[1];
  36.     HANDLE hFile = INVALID_HANDLE_VALUE;
  37.  
  38.     (void)printf("Attempting to delete file: %s\n", filename);
  39.  
  40.     hFile = CreateFileA(
  41.         filename,
  42.         DELETE,
  43.         FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  44.         NULL,
  45.         OPEN_EXISTING,
  46.         FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_POSIX_SEMANTICS,
  47.         NULL
  48.     );
  49.  
  50.     if (hFile == INVALID_HANDLE_VALUE) {
  51.         (void)fprintf(stderr,
  52.             "Error: Could not open file '%s'. GetLastError: %ld\n",
  53.             filename, (long)GetLastError());
  54.         return 1;
  55.     }
  56.  
  57.     (void)printf("Successfully opened file handle.\n");
  58.  
  59.     FILE_DISPOSITION_INFO_EX fileDispositionInfoEx = {
  60.         .Flags = FILE_DISPOSITION_DELETE | FILE_DISPOSITION_ON_CLOSE
  61.     };
  62.  
  63.     if (SetFileInformationByHandle(hFile,
  64.         FileDispositionInfoEx,
  65.         &fileDispositionInfoEx,
  66.         sizeof(FILE_DISPOSITION_INFO_EX))) {
  67.         (void)printf("FileDispositionInfoEx success\n");
  68.         retval = EXIT_SUCCESS;
  69.     }
  70.     else {
  71.         (void)printf("FileDispositionInfoEx failed!? lasterr=%ld\n",
  72.             (long)GetLastError());
  73.         retval = EXIT_FAILURE;
  74.     }
  75.  
  76.     (void)CloseHandle(hFile);
  77.     return retval;
  78. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at