visual c++ - Windows UWP CreateFIle2 cannot read file in ApplicationData.LocalFolder -


i trying build uwp app in c#. app has native library written in c++. whenever app tries read file in applicationdata.localfolder, createfile2 api returning error_not_supported_in_appcontainer. file exists in path specified api.
this sequence of operation in app.

  1. launch app. app creates file & writes data
  2. later on based on user input app tries read data in file


step 1 working fine. app able create file & write data in it. when app tries access later on, error.
i path applicationdata.localfolder using

windows.storage.applicationdata.current.localfolder.path


this actual path see in app:

c:\users\xxxxx\appdata\local\packages\ac7a11e4-c1d6-4d37-b9eb-a4b0dc8f67b8_yyjvd81p022em\localstate\temp.txt


my code below:

createfile2_extended_parameters ms_param = {0};    ms_param.dwsize = sizeof(createfile2_extended_parameters);    ms_param.dwfileattributes = file_attribute_readonly;    ms_param.dwfileflags = file_flag_no_buffering;    ms_param.dwsecurityqosflags = security_delegation;    ms_param.lpsecurityattributes = null;    ms_param.htemplatefile = null;     g_hfile = createfile2(filename, generic_read, file_share_read|file_share_write, open_existing, &ms_param);    if (g_hfile == invalid_handle_value)    {            return getlasterror();    } 


i tried createfile2 both open_existing & open_always option dwcreationdisposition parameter, see same error in either case.

i had similar issue createfile2 earlier. problem app & have fixed issue. time though file available within localfolder, still error.

the problem here related dwsecurityqosflags you've set in createfile2_extended_parameters.

when called windows store app, createfile2 simplified. can open files or directories inside applicationdata.localfolder or package.installedlocation directories. can't open named pipes or mailslots or create encrypted files (file_attribute_encrypted).

the dwsecurityqosflags parameter specifies sqos information. in windows stroe app, can set security_anonymous. using other flag raise error_not_supported_in_appcontainer exception. indicates not supported in uwp app.

following code used test:

storagefolder^ localfolder = applicationdata::current->localfolder; string^ path = localfolder->path; path += l"\\myfile.txt";  createfile2_extended_parameters ms_param = { 0 }; ms_param.dwsize = sizeof(createfile2_extended_parameters); ms_param.dwfileattributes = file_attribute_readonly; ms_param.dwfileflags = file_flag_no_buffering; ms_param.dwsecurityqosflags = security_anonymous; ms_param.lpsecurityattributes = null; ms_param.htemplatefile = null;  handle g_hfile = createfile2(path->data(), generic_read, file_share_read | file_share_write, open_existing, &ms_param);  dword error = getlasterror(); 

if don't have "myfile.txt" under localfolder, error_file_not_found exception, otherwise error_success.


Comments

Popular posts from this blog

ios - Memory not freeing up after popping viewcontroller using ARC -

Java JSoup error fetching URL -

mouseevent - Catch mouse event on tree widget item in QTreeWidget -