locked
CreateFile2 returns access denied in Windows 8 RTM

    Question

  • I use CreateFile2() to read a file in my metro app, like this:

                    CREATEFILE2_EXTENDED_PARAMETERS extendedParams = {0};
    extendedParams.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
    extendedParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
    extendedParams.dwFileFlags = FILE_FLAG_SEQUENTIAL_SCAN;
    extendedParams.dwSecurityQosFlags = SECURITY_ANONYMOUS;
    extendedParams.lpSecurityAttributes = nullptr;
    extendedParams.hTemplateFile = nullptr;

    // read the file from hardware
    hFile = ::CreateFile2(path.c_str(), GENERIC_READ, 0, OPEN_EXISTING, &extendedParams);
    if (INVALID_HANDLE_VALUE == hFile)
    {
    //...
    }

    it works well in Windows 8 RP and VS 2012 RC.

    But failed (return access denied) after I upgrade to Windows 8 RTM and the latest VS 2012.

    Why do I get access denied? How to use CreateFile2() in Windows RTM?







    • Edited by Jyosisi Friday, August 17, 2012 11:06 AM
    Friday, August 17, 2012 11:03 AM

Answers

  • Try this: hFile = ::CreateFile2(path.c_str(), GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, &extendedParams);
    • Proposed as answer by zuiopz Friday, August 17, 2012 1:48 PM
    • Marked as answer by Jesse Jiang Tuesday, August 21, 2012 9:04 AM
    Friday, August 17, 2012 1:46 PM

All replies

  • Try this: hFile = ::CreateFile2(path.c_str(), GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, &extendedParams);
    • Proposed as answer by zuiopz Friday, August 17, 2012 1:48 PM
    • Marked as answer by Jesse Jiang Tuesday, August 21, 2012 9:04 AM
    Friday, August 17, 2012 1:46 PM
  • Yes, it works. Thank you!
    Tuesday, August 21, 2012 5:46 AM