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.