简体   繁体   中英

ActiveX control inside PowerPoint slide

I've created a very simple program and exposed it as an ActiveX Control. What I'm trying to do is to embed this control into a PowerPoint slide. The code for the program is as follows:

namespace WindowsFormsApplication1
{
    [ProgId("Tomor.Form1")]
    [ClassInterface(ClassInterfaceType.AutoDispatch)]
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Text = "Clicked";
        }
    }
}

I've cheked the Make the assembly COM-visible and also checked the Register for COM-interop . I've also implemented the RegisterClass and UnregisterClass methods (but haven't shown here, see this , for an exact implementation), and have been able to successfully register it using regasm.exe. I'm also able to access the interface from another project using Tomor1.Form . Now, I am trying to insert this control into a PowerPoint slide as follows:

      PowerPoint.Shape s = Sld.Shapes.AddOLEObject(0, 0, 400, 400, "Tomor.Form1");

However, all I am getting is the following error upon startup:

  • $exception {"Could not read key from registry (Exception from HRESULT: 0x80040150 (REGDB_E_READREGDB))"} System.Exception {System.Runtime.InteropServices.COMException}

The interesting thing is that I can see the "key" in registry by navigating to: Compuer\\HKEY_CLASSES_ROOT\\Tomor1.Form\\CLSID

Even though I'm not quite sure, I suspect that the problem must be with ComRegisterFunction. I've found another implementation somewhere else, and with that implementation, the assembly is registered correctly, and can be accessed from PowerPoint. The implementation for the ComRegisterFunction is as follows:

    [ComRegisterFunction]
    static void ComRegister(Type t)
    {
        string keyName = @"CLSID\" + t.GUID.ToString("B");
        using (RegistryKey key =
            Registry.ClassesRoot.OpenSubKey(keyName, true))
        {
            key.CreateSubKey("Control").Close();
            using (RegistryKey subkey = key.CreateSubKey("MiscStatus"))
            {
                // 131456 decimal == 0x20180.
                long val = (long)
                    (OLEMISC.OLEMISC_INSIDEOUT
                    | OLEMISC.OLEMISC_ACTIVATEWHENVISIBLE
                    | OLEMISC.OLEMISC_SETCLIENTSITEFIRST);
                subkey.SetValue("", val);
            }
            using (RegistryKey subkey = key.CreateSubKey("TypeLib"))
            {
                Guid libid =
                    Marshal.GetTypeLibGuidForAssembly(t.Assembly);
                subkey.SetValue("", libid.ToString("B"));
            }
            using (RegistryKey subkey = key.CreateSubKey("Version"))
            {
                Version ver = t.Assembly.GetName().Version;
                string version =
                  string.Format("{0}.{1}", ver.Major, ver.Minor);
                subkey.SetValue("", version);
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM