SetFileTime returns invalid handle

This is a discussion on SetFileTime returns invalid handle within the Windows Programming forums, part of the Platform Specific Boards category; The following code is intended to restore windows file attributes (including file times). The file names and WIN32_FIND_DATA structures are ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    15

    SetFileTime returns invalid handle

    The following code is intended to restore windows file attributes (including file times). The file names and WIN32_FIND_DATA structures are stored in a file using fwrite (separate program). This program is intended to restore attributes and times for all files that match the input path. I have simplified as much as I can and still let you see what is going on.

    The FindFirstFile appears to succeed (doesn't report an error to GetLastError), but the SetFileTime fails (return 0) and GetLastError shows 6 (invalid handle). What could cause the SetFileTime to fail if the FindFirstFile succeeded?

    Any suggestions will be greatly appreciated.

    Thanks,
    Leon

    Code:
    #include <stdio.h>
    #include <windows.h>
    
    int result;
    
    FILE *attrFilePtr;
    FILETIME ft;
    
    WIN32_FIND_DATA FindFileData;
    
    
    struct file_attrs {
      char filename[500];
      WIN32_FIND_DATA attr_struct;
    }  file_data;
    
    HANDLE hFile = INVALID_HANDLE_VALUE;
    
    
    main(int argc, char *argv[])
    {
    
      if (argc != 3)
      {
        printf("usage is: %s <attrsfilename> <path>\n", argv[0]);
        exit(1);
      }
      attrFilePtr = fopen(argv[1], "r");
      if (attrFilePtr == NULL)
      {
        printf("can't open file: %s\n", argv[1]);
        exit(2);
      }
    
      while ( (fread(&file_data, sizeof(struct file_attrs), 1, attrFilePtr)) != NULL)
      {
        if(strncmp(file_data.filename, argv[2], strlen(argv[2]) ) == 0)
        {
          
          printf("match on: %s\n", file_data.filename);
          SetFileAttributes(file_data.filename, file_data.attr_struct.dwFileAttributes);
          hFile = FindFirstFile(file_data.filename, &FindFileData);
    
          DWORD dw = GetLastError();
          printf("GetLastError on findFirstFile returned: %d\n", dw);
          if (hFile == INVALID_HANDLE_VALUE)
          {
            printf("invalid handle\n");
            exit(3);
          }
    
          SetFileTime(hFile, &file_data.attr_struct.ftCreatsionTime, &file_data.attr_struct.ftLastAccessTime, &file_data.attr_struct.ftLastWriteTime);
          dw = GetLastError();
          printf("GetLastError on setfiletime returned: %d\n", dw);
        }
      }
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Have you read the FindFirstFile description:

    If the function succeeds, the return value is a search handle used in a subsequent call to FindNextFile or FindClose.
    Why do you believe you can use this handle for something else?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    15
    http://msdn2.microsoft.com/en-us/library/aa364418.aspx states:
    The FindFirstFile function opens a search handle and returns information about the first file with a name that matches the specified pattern.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    the information about file is stored in the FindFileData structure, it has nothing to do with the handle
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    As vart said, you have a bogus handle. GetLastError from SetFileTime in the above code should return a 6, indicating an invalid handle. You'll have to first open the file with CreateFile and then call SetFileTime on the opened file.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    15
    Thanks! I'm new enough to windows programming that I didn't understand there are different kinds of handles. I have it working now, so thanks again.

    Leon

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  2. Simple thread object model (my first post)
    By Codeplug in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2004, 11:34 PM
  3. Flood of errors when include .h
    By erik2004 in forum C++ Programming
    Replies: 14
    Last Post: 12-07-2002, 07:37 AM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 10:37 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 10:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21