To implement an ActiveX control using C #, three issues need to be resolved:
1. Enable. NET components to be invoked by COM
2. After registering with the client, the ActiveX control can be authenticated by IE security
3. When not registered on the client, the installation package can be authenticated by IE's signature
The development environment for this program is. NET Framework 3.5, a tool that is visual Studio. NET 2008, passing tests on clients that have installed the. NET Framework 3.5.
Here is the implementation step:
(i) Create an assembly that can be accessed from COM
First implement a COM-visible assembly, create a class library project, AssemblyInfo.cs should include:
using System.Runtime.InteropServices;
//使此程序集中的类型对COM组件可见
[assembly: ComVisible(true)]
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("94882155-3B7C-48e3-B357-234D56D8F15E")]
Add the following code to AssemblyInfo.cs to ensure the accessibility of the assembly:
using System.Security;
[assembly: AllowPartiallyTrustedCallers()]
Note the GUID above, if the class inside the assembly is not annotated guid,com the registered GUID is newly generated and the GUID here is not useful.
Create a user control (custom class to be measured) IdentityKey.cs, join:
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace KeyActiveX
{
[Guid("94882155-3B7C-48e3-B357-234D56D8F15E")]
public partial class IdentityKey : UserControl
{
}
}
The GUID here is the same as the AssemblyInfo.cs, which becomes a CLSID in COM registration and is invoked by the HTML as a CLSID.
Class Library project Properties, select Build, tick COM registration, add in HTML file
<object id="controlbyid" classid="clsid:{94882155-3B7C-48e3-B357 -234D56D8F15E}" ></object>
When you enable unsafe controls in IE, view HTML pages, you should be able to access the control, and now an assembly is developed for COM registration at publication time.
You can view the components and CLSID in. NET categories using the Ole/com Object Viewer, which is installed with the VC self.