WindowsAppSDK icon indicating copy to clipboard operation
WindowsAppSDK copied to clipboard

CoGetObject with COM Elevation Moniker fails in packaged app

Open gave92 opened this issue 2 years ago • 3 comments

Describe the bug

Trying to use CoGetObject function to create an elevated IFileOperation COM object fails with "Interface Not Registered" error in a packaged app. When calling the function an UAC prompt correctly appears but, when granted, CoGetObject returns an error. The same code works correctly if the app is not packaged.

Sample code:

[StructLayout(LayoutKind.Sequential)]
private struct BIND_OPTS3
{
  public uint cbStruct;
  public uint grfFlags;
  public uint grfMode;
  public uint dwTickCountDeadline;
  public uint dwTrackFlags;
  public uint dwClassContext;
  public uint locale;
  object pServerInfo; // will be passing null, so type doesn't matter
  public IntPtr hwnd;
}

[DllImport("ole32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, PreserveSig = true)]
private static extern int CoGetObject(
   string pszName,
   [In] ref BIND_OPTS3 pBindOptions,
   [In, MarshalAs(UnmanagedType.LPStruct)] Guid riid,
   [MarshalAs(UnmanagedType.Interface)] out object ppv);

private void myButton_Click(object sender, RoutedEventArgs e)
{
  var bop = new BIND_OPTS3();
  bop.cbStruct = (uint)Marshal.SizeOf(bop);
  bop.dwClassContext = (uint)4; // CLSCTX_LOCAL_SERVER
  bop.hwnd = IntPtr.Zero;

  // Try create elevated IFileOperation COM object
  var hr = CoGetObject("Elevation:Administrator!new:{3ad05575-8857-4850-9277-11b85bdb8e09}", ref bop, new Guid("947aab5f-0a5c-4c13-b4d6-4bf7836fc9f8"), out var ppv);
  // HR == INTERFACE NOT REGISTERED
}

Steps to reproduce the bug

  1. Create a single package winui 3 app and use windows apps sdk 1.1.5 or later
  2. Add <rescap:Capability Name="allowElevation"/> to the project appxmanifest
  3. Add the sample code above e.g. to MainWindow.xaml.cs
  4. Observe that when invoked an UAC prompt appears but, when granted, CoGetObject returns an error

Expected behavior

CoGetObject should succeed (the app has allowElevation capability)

Screenshots

No response

NuGet package version

1.2.220930.4-preview2

Packaging type

Packaged (MSIX)

Windows version

Windows 10 version 21H1 (19043, May 2021 Update)

IDE

Visual Studio 2022

Additional context

No response

gave92 avatar Oct 31 '22 17:10 gave92