microsoft / microsoft/WindowsAppSDK
CoGetObject with COM Elevation Moniker fails in packaged app
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 4.7k
- Forks
- 471
- Avg merge
- 3d 13h
- Merged PRs (30d)
- 28
Description
### 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:
```cs
[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 `` 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_
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the sample in MainWindow.xaml.cs and reproduce it using the packaged app, the allowElevation capability in the appxmanifest, and the listed Windows App SDK version. Compare the packaged and unpackaged CoGetObject behavior and inspect the Windows App SDK handling around packaged COM elevation. Done means the packaged call succeeds after the UAC prompt, as expected.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- desktop
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100