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.)
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.
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.
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.
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. ...
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. ..
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.
OK I found the solution and I'll write it here for the common good.
using System.Runtime.InteropServices;
to the file
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
Creation Steps
Register = vsdrpCOM
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
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With