1

I am converting a C# WinForm app to UWP.

The old app is using pinvoke ShellExecute API in order to open some files. I am trying to copy the same code to the new app, but get an exception.

Here is my code:

     public static readonly int SHELL_EXECUTE_ERROR_CODE_LIMIT = 32;
     [DllImport("shell32.dll")]
        static extern IntPtr ShellExecute(
            IntPtr hwnd,
            string lpOperation,
            string lpFile,
            string lpParameters,
            string lpDirectory,
            /*ShowCommands*/int nShowCmd);

    private void btnPinvoke_Click(object sender, RoutedEventArgs e)
    {
        int ret = 0;
        IntPtr retPtr = ShellExecute(IntPtr.Zero, "open", @"C:\path\to\file\dummy.pdf", "", "", /*ShowCommands.SW_SHOWNOACTIVATE*/4);
        ret = retPtr.ToInt32();
        if (ret <= SHELL_EXECUTE_ERROR_CODE_LIMIT) //Failure
        {
            lblResult.Text = "Error " + ret;
        }
    }

The error code which is returning is 5.

ERROR_ACCESS_DENIED

5 (0x5)

Can you help me understand why am I getting ACCESS_DENIED exception?
It happens also when running Visual Studio as Administrator.

Ðаn
  • 10,934
  • 11
  • 59
  • 95
RK Coder
  • 398
  • 2
  • 14
  • 2
    There are all sort of things you're not allowed to do with UWP. ShellExecute is too powerful. Check this: https://stackoverflow.com/questions/49189353/uwp-how-to-start-an-exe-file-that-is-located-in-specific-directory/49340814 and this to run a file: https://learn.microsoft.com/en-us/windows/uwp/launch-resume/launch-the-default-app-for-a-file – Simon Mourier Jan 21 '20 at 15:29
  • Where does the file path come from? If you have access to the file (eg the user picked it with a File Open dialog or it's in your app folder) then use the `Windows.System.Launcher` class. – Peter Torr - MSFT Jan 21 '20 at 16:57
  • As an aside, ShellExecute doesn't return a Win32 error code. You'd need to use ShellExecuteEx for that. That won't work either of course, but at least you'd get proper error handling. – David Heffernan Jan 21 '20 at 17:56

1 Answers1

-2

Maybe you can try

Diagnostics.Process.Start(@"C:\path\to\file\dummy.pdf");

I used this many times in my applications and it works

Võ Quang Hòa
  • 2,688
  • 3
  • 27
  • 31
  • I assume there is a replacement in UWP for this ShellExecute using PInvok. But in my code there are lots of different PInvoke calls, and I would like to copy-paste my code from the old app to the new one with the fewest changes possible. – RK Coder Jan 21 '20 at 13:10
  • 2
    That you desire something to be possible does not make it possible. If you aren't allowed to use ShellExecute from UWP, then your desire to use existing code as is doesn't count for anything. – David Heffernan Jan 21 '20 at 17:58
  • @DavidHeffernan, Where can I find documentation that "you aren't allowed to use ShellExecute from UWP"? – RK Coder Jan 22 '20 at 06:12
  • 1
    I'm no expert but I'd start with the documentation for ShellExecute which states that it is for desktop apps only. – David Heffernan Jan 22 '20 at 08:14
  • 1
    @RikiCoder - the list is here: https://learn.microsoft.com/en-us/uwp/win32-and-com/win32-and-com-for-uwp-apps for example: `StgOpenStorage` is in that list, and its doc here https://learn.microsoft.com/en-us/windows/win32/api/coml2api/nf-coml2api-stgopenstorage explicitly mentions "[desktop apps | UWP apps]". ShellExecute(Ex) is not in that list and only mentions "[desktop apps]" – Simon Mourier Jan 22 '20 at 13:33
  • Great! This link seems to be very useful for me! – RK Coder Jan 23 '20 at 05:59