locked
[UWP]Marshal.GetLastWin32Error() throws Access Denied in UWP C# RRS feed

  • Question

  • I have the below code in my 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 I try to access it from my MainPage.xaml.cs, I got an expcetion of "Access Denied". Switching Visual Studio 2015 Community to Admin mode did not helped either

    public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();

                try
                {
                    DeviceIoControlHelper.ReturnFileHandler();
                }
                catch(Exception ex)
                {

                }
            }
    }
    I am using UWP with C# in Visual Studio 2015 Community


    • Edited by Barry Wang Thursday, April 21, 2016 12:19 AM title tag
    Wednesday, April 20, 2016 2:29 AM

Answers

  • Hi,

    UWP apps work in sandbox this mean that if  you want create file in app Local,Roaming or Temporary Folders you have to use  this method

    using Windows.Storage; 
     StorageFolder localFolder = ApplicationData.Current.LocalFolder//RoamingFolder,TemporaryFolder
      StorageFile file = await localFolder.CreateFileAsync();

    if you want create file in the other directory you have to do that using broker 

    here is description for where you can create file and how 

    File access permissions

    • Proposed as answer by Barry Wang Tuesday, May 3, 2016 3:06 AM
    • Marked as answer by Barry Wang Wednesday, May 4, 2016 3:28 AM
    Wednesday, April 20, 2016 7:02 AM

All replies

  • Hi,

    UWP apps work in sandbox this mean that if  you want create file in app Local,Roaming or Temporary Folders you have to use  this method

    using Windows.Storage; 
     StorageFolder localFolder = ApplicationData.Current.LocalFolder//RoamingFolder,TemporaryFolder
      StorageFile file = await localFolder.CreateFileAsync();

    if you want create file in the other directory you have to do that using broker 

    here is description for where you can create file and how 

    File access permissions

    • Proposed as answer by Barry Wang Tuesday, May 3, 2016 3:06 AM
    • Marked as answer by Barry Wang Wednesday, May 4, 2016 3:28 AM
    Wednesday, April 20, 2016 7:02 AM
  • Is there any way to access I/O device related file from UWP using :
    const string drive = @"\\.\LCD"

    Wednesday, April 20, 2016 8:32 AM
  • Hello six_sic6,

    I'm not so sure things about your device. In UWP, to access devices there are also some device related API:
    https://msdn.microsoft.com/en-us/library/windows/apps/bg182882.aspx

    Please check that whether your device is related to the folllowing:

    If you are using the above devices, you can use device specific API to fetch files.

    If you are not using the above devices, maybe you need to take Azat's advice.

    Best regards,

    Barry


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, April 21, 2016 1:06 AM