win universal app - Marshal.GetLastWin32Error() throws Access Denied in UWP C# -


i have below code in uwp app

public static class deviceiocontrolhelper     { [dllimport("kernel32.dll", setlasterror = true, charset = charset.ansi)]         private static extern safefilehandle createfile(            string lpfilename,            [marshalas(unmanagedtype.u4)] fileaccess dwdesiredaccess,            [marshalas(unmanagedtype.u4)] fileshare dwsharemode,            intptr lpsecurityattributes,            [marshalas(unmanagedtype.u4)] filemode dwcreationdisposition,            [marshalas(unmanagedtype.u4)] fileattributes dwflagsandattributes,            intptr htemplatefile);  public static safefilehandle returnfilehandler()         {             const string drive = @"\\.\lcd";              safefilehandle hddhandle = createfile(drive, fileaccess.read, fileshare.none, intptr.zero, filemode.open, fileattributes.normal, intptr.zero);              if (hddhandle.isinvalid)             {                 int lasterror = marshal.getlastwin32error();                 string errormessage = string.format(@"!! invalid {0}; error ({1}): {2}", drive, lasterror, new win32exception(lasterror).message);                 throw new win32exception(errormessage);             }              return hddhandle;         } } 

but, when try access mainpage.xaml.cs, got expcetion of "access denied". switching visual studio 2015 community admin mode did not helped either

public sealed partial class mainpage : page     {         public mainpage()         {             this.initializecomponent();              try             {                 deviceiocontrolhelper.returnfilehandler();             }             catch(exception ex)             {              }         } } 

i using uwp c# in visual studio 2015 community

you need use createfile2 open files universal windows application. won't allow open devices, though. quote msdn: when called windows store app, createfile2 simplified. can open files or directories inside applicationdata.localfolder or package.installedlocation directories.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -