Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i create activex using C# [duplicate]

I using Visual Studio 2010 (.NET 4). I need to create a COM object (in C#) and have no idea how to get started (what type of project to use,etc.)

like image 970
Eyal Avatar asked Jul 29 '10 07:07

Eyal


People also ask

What is ActiveX control in C#?

This process will involve the following steps: Create an assembly (class library project) that contains an item of type User Control. Expose an interface for the control. Embed the user control into a web page. Transfer data from a web form to the control and display the data on the control.

Is ActiveX a programming language?

ActiveX is not a programming language, but rather a set of rules for how applications should share information. Programmers can develop ActiveX controls in a variety of languages, including C, C++, Visual Basic, and Java. An ActiveX control is similar to a Java applet.

How to write ActiveX control in C?

The code for the ActiveX control is written here in pure C # under Windows Forms. Of course, WPF and other embedded components are also possible, because you pass the data to the integrating program like Office via interfaces. The ActiveX Control is located in the application in UserControl1.cs The GUID must be new and unique.

How to create ActiveX in Visual Studio?

So Let Us Start... 1 Creating .NET ActiveX. The first step you have to do is to create the ActiveX control:). ... 2 Using ActiveX on the HTML Page. Codebase is the path to the file which should be launched when ActiveX with our GUID is not found. ... 3 Creating .cab Component. ... 4 Invoking JavaScript Methods from ActiveX. ...

How to integrate ActiveX with Windows 10?

Written in c # or Visual Basic .Net in a higher framework like 4, 4.7, so it runs also under Windows 10 And then in the dialog: Insert ActiveX control, select the ActiveX element, which you have defined yourself in Visual Studio. Via the integrated ActiveX one has then from MSAccess as host access to the properties in the ActiveX control. ..

How to create a C++ MFC project in ActiveX?

The first step we need to do is create the C++ MFC project: Give the project a name, and click on "OK", and later on "Finish". The funny thing about ActiveX is that you haven't yet coded anything, but the project is already filled with a lot of code. The ActiveX project uses MFC in a shared DLL. Change it into Static Library.


3 Answers

OK I found the solution and I'll write it here for the common good.

  1. Start VS2010 as administrator.
  2. Open a class library project (exmaple - MyProject).
  3. Add a new interface to the project (see example below).
  4. Add a using System.Runtime.InteropServices; to the file
  5. Add the attributes InterfaceType, Guid to the interface.
  6. You can generate a Guid using Tools->Generate GUID (option 4).
  7. Add a class that implement the interface.
  8. Add the attributes ClassInterface, Guid, ProgId to the interface.
    ProgId convention is {namespace}.{class}
  9. Under the Properties folder in the project in the AssemblyInfo file set ComVisible to true.
  10. In the project properties menu, in the build tab mark "Register for COM interop"
  11. Build the project

now you can use your COM object by using it's ProgID.

example: the C# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Runtime.InteropServices;

namespace Launcher
{

    [InterfaceType(ComInterfaceType.InterfaceIsDual), Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")]
    public interface ILauncher
    {
        void launch();
    }

    [ClassInterface(ClassInterfaceType.None), Guid("YYYYYYYY-YYYY-YYYY-YYYY-YYYYYYYYYYY"), ProgId("Launcher.Launcher")]
    public class Launcher : ILauncher
    {
        private string path = null;

        public void launch()
        {
            Console.WriteLine("I launch scripts for a living.");

        }

    }
}

and VB script using the COM:

set obj = createObject("PSLauncher.PSLauncher") obj.launch()

and the output will be:

I launch scripts for a living

like image 193
Eyal Avatar answered Nov 05 '22 19:11

Eyal


Creation Steps

  1. Start Visual Studio 2013 as administrator
  2. Install Visual Studio extension Microsoft Visual Studio Installer Projects
  3. Create a class library project (WinFormActivex)
  4. Create your example window form (MainWindow)
  5. Create a new component interface(ILauncher)
  6. Create a new security interface (IObjectSafety)
  7. Create the component control (Launcher) that implement interfaces and launch the window.
  8. Check that all GUIDs are generated by you
  9. Check that the project is marked for COM
  10. Create the setup project (LauncherInstaller) with the primary output of WinFormActivex with the property Register = vsdrpCOM
  11. Install LauncherInstaller
  12. Run your test page in explorer (test.html)

MainWindow You can create a normal Form, here is pre-generated.

public partial class MainWindow : Form
{
    public MainWindow()
    {
        InitializeComponent();
    }

    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.textBox2 = new System.Windows.Forms.TextBox();
        this.SuspendLayout();
        //
        // textBox1
        //
        this.textBox1.Location = new System.Drawing.Point(42, 23);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(100, 20);
        this.textBox1.TabIndex = 0;
        //
        // textBox2
        //
        this.textBox2.Location = new System.Drawing.Point(42, 65);
        this.textBox2.Name = "textBox2";
        this.textBox2.Size = new System.Drawing.Size(100, 20);
        this.textBox2.TabIndex = 0;
        //
        // MainWindow
        //
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(284, 261);
        this.Controls.Add(this.textBox2);
        this.Controls.Add(this.textBox1);
        this.Name = "MainWindow";
        this.Text = "MainWindow";
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.TextBox textBox2;
}

ILauncher

using System.Runtime.InteropServices;
namespace WinFormActivex
{
    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    [Guid("94D26775-05E0-4B9C-BC73-C06FE915CF89")]
    public interface ILauncher
    {
        void ShowWindow();
    }
}

IObjectSafety

[ComImport()]
[Guid("51105418-2E5C-4667-BFD6-50C71C5FD15C")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IObjectSafety
{
    [PreserveSig()]
    int GetInterfaceSafetyOptions(ref Guid riid, out int pdwSupportedOptions, out int pdwEnabledOptions);
    [PreserveSig()]
    int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions);
    }

Launcher Please generate your GUID here.

 [ComVisible(true)]
 [ClassInterface(ClassInterfaceType.None)]
 [Guid("D100C392-030A-411C-92B6-4DBE9AC7AA5A")]
 [ProgId("WinFormActivex.Launcher")]
 [ComDefaultInterface(typeof(ILauncher))]
 public class Launcher : UserControl, ILauncher, IObjectSafety
 {
     #region [ ILauncher ]

     public void ShowWindow()
     {
         var f = new MainWindow();
         f.StartPosition = FormStartPosition.Manual;
         f.Location = Screen.AllScreens[0].Bounds.Location;
         f.WindowState = FormWindowState.Normal;
         f.WindowState = FormWindowState.Maximized;
         f.ShowInTaskbar = false;
         f.Show();
     }

     #endregion

     #region [ IObjectSafety ]

     public enum ObjectSafetyOptions
     {
         INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001,
         INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002,
         INTERFACE_USES_DISPEX = 0x00000004,
         INTERFACE_USES_SECURITY_MANAGER = 0x00000008
     };

     public int GetInterfaceSafetyOptions(ref Guid riid, out int pdwSupportedOptions, out int pdwEnabledOptions)
     {
         ObjectSafetyOptions m_options = ObjectSafetyOptions.INTERFACESAFE_FOR_UNTRUSTED_CALLER | ObjectSafetyOptions.INTERFACESAFE_FOR_UNTRUSTED_DATA;
         pdwSupportedOptions = (int)m_options;
         pdwEnabledOptions = (int)m_options;
         return 0;
     }

     public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions)
     {
         return 0;
     }

     #endregion
 }

test.html Please check that your CLSID match (Launcher) GUID.

<html>
    <head>
        <objectname="activexLauncher" style='display:none' id='activexLauncher' classid='CLSID:D100C392-030A-411C-92B6-4DBE9AC7AA5A' codebase='WinFormActivex'></object>
      <script language="javascript">
        <!-- Load the ActiveX object  -->
        var x = new ActiveXObject("WinFormActivex.Launcher");
        alert(x.GetText());
      </script>
    </head>
    <body>
    </body>
</html>

References

  • Stack Overflow question I always use as reference
  • Activex tag you should read
  • Old Microsoft guide
  • Article on creating the acrivex control with security options
  • Article about creating the window
like image 37
Giulio Caccin Avatar answered Nov 05 '22 20:11

Giulio Caccin


You could use a class library project. Declare a type with methods that will be exposed as a COM object.

Make sure that the assembly has been made COM-visible:

alt text

And finally register it using regasm.exe:

regasm.exe /codebase mylib.dll

Now the assembly is exposed as a COM object and the type you declared can be consumed by any client that supports COM.

like image 32
Darin Dimitrov Avatar answered Nov 05 '22 19:11

Darin Dimitrov