How to develop and deploy ActiveX control in C#
Link:https://blogs.msdn.microsoft.com/asiatech/2011/12/05/how-to-develop-and-deploy-activex-control-in-c/
How to develop and deploy ActiveX control in C#
There are lots of documents regarding how to develop and deploy an ActiveX control in native code(VB, C++, Delphi, …), however there’s few documents describe the manual in .NET. With this manual, you will learn how to develop, sign and deploy an ActiveX control with C#.NET. The practice can also be applied to C++.NET or VB.NET.
Create the ActiveX control DLL
In managed world, there’s no OCX control. So, we need to build a DLL control.
- In “Visual Studio 2010”, create a new Library project with “Visual C#”
- Rename the classname, and include “System.Runtime.InteropServices” namespace
- Sign your assembly with a strong name
Select Project-><Project Name> properties…, on Signing tab, check “Sign the assembly” and New a name key file.
- Select “Tools->Create GUID” to create a new GUID
- Add “ProgId”, “Guid”, “ComVisible” to your class, and implement your logic
using System;
using System.Runtime.InteropServices;
namespace DemoCSharpActiveX
{
/// <summary>
/// Demo HelloWorld class
/// </summary>
[ProgId("DemoCSharpActiveX.HelloWorld")]
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("415D09B9-3C9F-43F4-BB5C-C056263EF270")]
[ComVisible(true)]
public class HelloWord
{
[ComVisible(true)]
public String SayHello()
{
return "Hello World!";
}
}
}
- Set ComVisible to true in AssemblyInfo.cs file.
[assembly: ComVisible(true)]
- Write a sample HTML page to test this ActiveX control
<!DOCTYPE>
<html>
<head>
<title>DemoCSharpActiveX webpage</title>
</head>
<body>
<OBJECT id="DemoActiveX" classid="clsid:415D09B9-3C9F-43F4-BB5C-C056263EF270" codebase="DemoCSharpActiveX.cab"></OBJECT>
<script type="text/javascript">
try {
var obj = document.DemoActiveX;
if (obj) {
alert(obj.SayHello());
} else {
alert("Object is not created!");
}
} catch (ex) {
alert("Some error happens, error message is: " + ex.Description);
}
</script>
</body>
</html>
- Build your dll and register it
C:\Windows\Microsoft.NET\Framework\v2.0.50727>regasm /codebase <full path of dll file>
To unregister your dll, just run regasm /u <full path of dll file>
- Now you can open your HTML file and test it. If it succeeds, IE will pop up the below message box.
If you cannot see this message box, you’d better check whether your IE is secured as http://support.real-time.com/browsers/security/ie/activex.html, please just try to change the settings to “Enable” or “Prompt”.
Create the installation package
For ActiveX controls written with native code, IE will register the control automatically when the webpage is visited the first time. However, IE WONT’T register a managed ActiveX control automatically so that we have to build an installation package for the registration.
- Add a new “Setup” project
In the current solution, select File->Add->New Project…, select Other Project Types->Setup and Deployment->Visual Studio Installer, Click Setup Project and specify a project name.
- Add your ActiveX control dll into this setup project.
Right click Application Folder, select Add->File…
Then browse your dll file and add into the project
- Select your ActiveX assembly and change Register to vsdraCOM.
- Now if you build this project, it will create the MSI installation package.
Package to cab
The last step is to package the MSI installation to a *.cab file, together with a *.INF file. There are several tools to make the cab package, here I would like to choose makecab.exe which is shipped in OS.
- Open the setup project which we create in the previous step
- Create the INF file of your ActiveX control, and put it to the root directory of the setup project. The file name should be as same as the control dll’s name, and here’s the content of the file.
[version]
signature="$CHICAGO$"
AdvancedINF=2.0
[Setup Hooks]
install=install
[install]
run=msiexec.exe /package """%EXTRACT_DIR%\DemoCSharpActiveXSetup.msi""" /qn
- Create the Directive File for makecab.exe, and put it to the root directory of the setup project. I name the file as build.ddf, here’s the content
.Set DiskDirectoryTemplate=cab
.Set CabinetNameTemplate=DemoCSharpActiveX.cab
release\DemoCSharpActiveXSetup.msi
DemoCSharpActiveX.inf
The directive file format is documented here.
- Define the Post-Build event of the setup project.
Select the setup project, on the Properties panel, open the PostBuildEvent editor, and input the commands.
cd "$(ProjectDir)"
"%WINDIR%\System32\Makecab.exe" /f "build.ddf"
- Rebuild the setup project, and the cab file will be created in cab folder.
Sign your ActiveX control
Now we also need to sign the ActiveX control as most of the IT professionals block the installation of unsigned ActiveX control in IE. The required tools are contained in Windows SDK, so please download at first.
http://www.microsoft.com/download/en/details.aspx?id=3138
- Make a certificate with the subject key file
makecert -sv myNew.pvk -ss myNewStore myNew.cer
- Run signtool.exe wizard to sign the *.cab file
1) Run command “signtool signwizard”, then click “Next” on the Wizard
2) Select your *.cab file and click “Next”
3) Select “Custom” as the signing type, then click “Next”
4) Click “Select from file…” and select the “*.cer” file as certificate, then click “Next”
5) Select the private key file(*.pvk) and click “Next”
6) Click “Next” several time to finish the wizard, don’t change any settings.
7) After the *.cab file is signed, right click it and check “Properties”->”Digital Signatures”, you will find it is signed with our certificate. But it is not trusted, I will make it trusted in the next steps.
Now everything is ready, please just put the cab and html files to the same web folder.
Test the ActiveX control
Since we only signed the ActiveX control with a test certificate, we need to make the root certificate trusted on the client machine at first. Here’re the steps.
- Double click the *.cer file, go to “Certification Path”, select the root certificate and click “View Certificate”. As you see in the picture, currently it is not trusted.
- On the “Details” tab of the root certificate, click “Copy to File…” to export the root certificate as a *.cer file.
- Double click the exported *.cer file to open it, click “Install Certificate…” -> “Place all certificates in the following store” -> “Trusted Root Certification Authorities”. Then finish the wizard to install the certificate to trusted root authorities.
- Now open IE and browse the web page, the control will be downloaded silently if “Download signed ActiveX controls” is enabled.
References
Create ActiveX in .NET Step by Step
http://www.codeproject.com/KB/cs/CreateActiveXDotNet.aspx
HowTo: Deploy .NET ActiveX Control
http://nikolkos.blogspot.com/2009/08/howto-deploy-net-activex-control.html
Introduction to Code Signing
http://msdn.microsoft.com/en-us/library/ms537361(v=vs.85).aspx
Signing and Checking Code with Authenticode
http://msdn.microsoft.com/en-us/library/ms537364(v=VS.85).aspx
Sign Tool (SignTool.exe)
http://msdn.microsoft.com/en-us/library/8s9b9yaz(v=vs.80).aspx
Regards,
Zhixing Lv from APGC DSI Team
How to develop and deploy ActiveX control in C#的更多相关文章
- ePass1000 Full ActiveX Control Reference Manual Version 2.0
ePass1000 Full ActiveX Control Reference Manual Version 2.0 Error Code Value Return Status Descripti ...
- OCX控件在IE中无法侦测到键盘消息( MFC ActiveX Control in IE Doesn't Detect Keystrokes)
症状描述: Accelerator keys, such as ARROW keys, are first received by the message pump of the ActiveX co ...
- vs2015添加ActiveX Control Test Container工具(转载)
http://blog.csdn.net/lphbtm/article/details/8647565 vs2010 中添加 ActiveX Control Test Container工具(转载) ...
- VB.NET或C#报错:You must hava a license to use this ActiveX control.
VB.NET或者C# winform开发时,如果使用了Microsoft Visual Basic 6.0 ActiveX,并动态创建该控件实例,那么程序移植到没有安装Visual Basic 6.0 ...
- 一个ActiveX control的创建过程
创建 根据这篇文章的介绍:http://www.cnblogs.com/time-is-life/p/6354152.html 来创建,里面包含了创建的基本过程以及属性事件方法的使用. 使用: 参考文 ...
- VS2008 ActiveX(ocx控件)的调试工具ActiveX Control Test Container安装说明
vs2008中的TSTCON( ActiveX Control Test Container )工具非自动安装,而是作为一个例程提供.所以应找到该例程,并编译: 如vs2008安装在默认路径则 1, ...
- vs2010添加TSTCON( ActiveX Control Test Container )工具
vs2010中的TSTCON( ActiveX Control Test Container )工具非自动安装,而是作为一个例程提供.所以应找到该例程,并编译: 如vs2010安装在默认路径则 1, ...
- 解决ActiveX Control异常:"没有注册类(异常来自 HRESULT:0x80040154(REGDB_E_CLASSNOTREG))"
问题背景: 1.我们的程序是用winform调用unity web player 插件来作为播放器在客户端播放动画文件的. 2.播放器是由我们的客户端程序调用的 3.客户端程序默认是以管理员身份启动的 ...
- Visual Studio 2013附加进程调试IE加载的ActiveX Control无效解决方法
默认Attach to选择了Automatically determine the type of code to debug,显示Native Code.但附加进程到iexplore.exe断点无法 ...
随机推荐
- scala编程第17章学习笔记(1)——集合类型
列表 列表的初始化及对其首尾的访问: scala> val colors = List("red", "blue", "green") ...
- html调用servlet(JDBC在Servlet中的使用)(1)
1.页面的数据表单 在使用Servlet处理用户请求之前,先准备一个页面,该页面用来提供数据表单.数据表单就是HTML中的<form>...</form>部分,当用户单击Sub ...
- 数学图形之将曲线(curve)转化成曲面管
在我关于数学图形的博客中,一开始讲曲线的生成算法.然后在最近的章节中介绍了圆环,还介绍了螺旋管以及海螺的生成算法.一类是曲线,一类是环面,为什么不将曲线变成环的图形,毕竟曲线看上去太单薄了,这一节我将 ...
- C#的几种写文件方法
C#写文件处理操作在很多的开发项目中都会涉及,那么具体的实现方法是什么呢?这里向大家介绍三大方法,希望对你在开发应用中有所启发. 首先C#写文件处理操作必须先导入命名空间:using System.I ...
- 默认网关和默认路由 —— Cisco CCNA – Default Gateway & Default Routes
原文:https://www.certificationkits.com/cisco-certification/ccna-articles/cisco-ccna-intro-to-routing-b ...
- 【转】Understanding the Angular Boot Process
原文: https://medium.com/@coderonfleek/understanding-the-angular-boot-process-9a338b06248c ----------- ...
- githug-54-git练习
1-40: http://wiki.jikexueyuan.com/project/git-54-stage-clear/ 41-50: https://blog.csdn.net/maxam0128 ...
- [2014.5.13][Ubuntu] Ubuntu 14.04STL 出现NTFS分区无法訪问的问题
5.12 为了给学生改论文,在UPC上登录了Windows 8.1,晚上正常关机.今日切换登陆Ubuntu 14.04分区,发现原来能够正常訪问的windows下的NTFS分区都被锁死.提演示样例如以 ...
- 详解 Spring 3.0 基于 Annotation 的依赖注入实现
Spring 的依赖配置方式与 Spring 框架的内核自身是松耦合设计的.然而,直到 Spring 3.0 以前,使用 XML 进行依赖配置几乎是唯一的选择.Spring 3.0 的出现改变了这一状 ...
- 用BeanUtilsDate类型值为空报错的解决方法
除BeanUtils外还有一个名为PropertyUtils的工具类,它也提供copyProperties()方法,作用与BeanUtils的同名方法十分相似,主要的区别在于后者提供类型转换功能,即发 ...