LaunchAdvancedAssociationUI in C# -> Element not found on Windows 8

3

I'm trying to set up a way to manage file associations for my program in C#. I already set the correct values in the registry with WiX, and found a wrapper for ApplicationAssociationRegistrationUI which should allow me to open the GUI to set file associations. But it doesn't work. I get the following exception: Element not found. (Exception from HRESULT: 0x80070490)

The wrapper:

namespace FileAssociation
{
    [ClassInterface(ClassInterfaceType.None)]
    [ComImport]
    [Guid("1968106d-f3b5-44cf-890e-116fcb9ecef1")]
    [TypeLibType(TypeLibTypeFlags.FCanCreate)]
    public sealed class ApplicationAssociationRegistrationUI : IApplicationAssociationRegistrationUI
    {
        [MethodImpl(MethodImplOptions.InternalCall)]
        public extern void LaunchAdvancedAssociationUI(string appRegistryName);
    }

    [CoClass(typeof(ApplicationAssociationRegistrationUI))]
    [ComImport]
    [Guid("1f76a169-f994-40ac-8fc8-0959e8874710")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [TypeLibImportClass(typeof(ApplicationAssociationRegistrationUI))]
    public interface IApplicationAssociationRegistrationUI
    {
        void LaunchAdvancedAssociationUI([MarshalAs(UnmanagedType.LPWStr)] string appRegistryName);
    }
}

Usage:

var assocUi = new ApplicationAssociationRegistrationUI();
try
{
     assocUi.LaunchAdvancedAssociationUI(InstanceManager.ProgId);
}
catch
{
     MessageBox.Show("Could not display the file association manager. Please repair the installation and try again.", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
}
finally
{
     Marshal.ReleaseComObject(assocUi);
}

Again, all the correct keys exist in the registry. This is not the first time COM Interop fails miserably for me so I am beginning to think that I must be missing something important. I tried checking "Register for COM Interop" in the project properties, and I tried making it COM-Visible.

I am aware that this only works on Vista or newer, which is fine since my program doesn't support XP anyway. I'm testing it on Windows 8.1, both as Admin and as normal user.

EDIT: It works on Windows 7! On MSDN it does not say that this API was deprecated in Win8...

What have I done wrong? Is there an easier way to do this which I don't know about?

c#
windows-8
com
com-interop
asked on Stack Overflow Feb 8, 2015 by OronDF343 • edited Jun 4, 2015 by OronDF343

1 Answer

1

Finally found the problem!!! Starting from Windows 8, the program needs to have company information (I think this is a bug, since it isn't mentioned on Microsoft's site.) So make sure to fill out this attribute in AssemblyInfo.cs:

[assembly: AssemblyCompany("YourCompany")]

If it's an empty string it won't work!

answered on Stack Overflow Jun 4, 2015 by OronDF343

User contributions licensed under CC BY-SA 3.0