locked
CreateFile2 returns access denied

    Question

  • Hi, I have an existing native dll where I used CreateFile to open a file from Documents library. It workd fine. Now I want to invoke this native dll from a C# Metro application via DllImport. DllImport works fine. I can even step into the native code. But CreateFile always returns an invalid handle, and GetLastError returns 5, which means access denied.

    My Metro application has requested access to Documents library, and in fact the file was created in the same application via C# code. I've verified the file exists, and the content is correct. I've also tried to use CreateFile2 instead of CreateFile. But it returns the same error. Why do I get access denied?


    Sining Oh Blue Star
    Friday, September 23, 2011 7:34 AM

Answers

  • I have the same problem.

    This example (Magazine Sample) reads only from application's deployment folder. I need to open and read a video file. I added video library in the package manifest. Still CreateFile2() returns with "access denied" while opening a file using OpenAsync() works. So CreateFile2() doesn't work outside deployment folder.

    The problem is that I don't want to use OpenAsync(). Parsing a video file (and any complex and big binary file) will be very inefficient and cumbersome. Or I would need to implement all caching myself.

    Thanks,

    Tuesday, December 6, 2011 7:56 PM

All replies

  • Don't forget Metro apps are running inside sandbox, sandbox security model by design does not allow Metro app to access restricted system resource.

    • Proposed as answer by Chris Guzak Wednesday, June 20, 2012 8:30 PM
    Friday, September 23, 2011 5:32 PM
  • Did you demand access to document library in the package manifest?

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Friday, September 23, 2011 5:34 PM
  • Did you demand access to document library in the package manifest?


    Yes.
    Sining Oh Blue Star
    Sunday, September 25, 2011 1:41 PM
  • Actually I can access the same file from the C# code. It's just when I use DllImport to execute the native code, I get access denied...
    Sining Oh Blue Star
    Sunday, September 25, 2011 1:42 PM
  • CreateFile2 should work from what I see you have already tried above. There is an example of the usage of CreateFile2 in the Magazine Sample. If you have a sample project demonstrating the problem, please let me know. My email is:

    d a v i d L a m

    (at)

    m i c r o s o f t

    (dot) c o m

     

    Thanks,

    David

    Monday, October 24, 2011 8:08 PM
    Moderator
  • I have the same problem.

    This example (Magazine Sample) reads only from application's deployment folder. I need to open and read a video file. I added video library in the package manifest. Still CreateFile2() returns with "access denied" while opening a file using OpenAsync() works. So CreateFile2() doesn't work outside deployment folder.

    The problem is that I don't want to use OpenAsync(). Parsing a video file (and any complex and big binary file) will be very inefficient and cumbersome. Or I would need to implement all caching myself.

    Thanks,

    Tuesday, December 6, 2011 7:56 PM
  • That is an accurate assessment. The manifest settings apply to WinRT APIs. The Win32 (new or old) are restricted to the apps Windows::Storage::ApplicationData folders.

     

    Do happen to you have any performance data comparing the two to raise this concern?

     

    I know there is still performance work to be completed, but I'll see if I can find any guidance we can provide at this time for the WinRT storage APIs regarding performance. This may not come until later in the beta cycle however.

     

    Thanks,

    -David



    Wednesday, December 7, 2011 11:06 PM
    Moderator
  • That is an accurate assessment. The manifest settings apply to WinRT APIs. The Win32 (new or old) are restricted to the apps Windows::Storage::ApplicationData folders.

    This is painful.  The main thing that is killing me in most of my porting of existing apps is this.  You can only have access to more folders when using WinRT APIs.  Any chance this is changing in the upcoming consumer preview?
    Sunday, February 19, 2012 4:13 AM
  • this won't be changing. you need to convert your code to use the stream abstraction IRandomAccessStream that you retrieve via StorageFile.OpenAsync(). this enables several scenarios including app2app file access (Flickr, SkyDrive, etc) and access to files not stored in the file system.

    hopefully those scenarios are valuable enough to you to help motivate you to do the work.

    Chris

    Wednesday, March 14, 2012 5:45 AM
  • Yeah, I learned so much since I posted that.  Now fully WinRT.
    Thursday, May 17, 2012 11:57 AM