Create The ActiveX Control DLL
Create The ActiveX Control DLL
Create The ActiveX Control DLL
In managed world, there’s no OCX control. So, we need to build a DLL control.
1. In “Visual Studio 2010”, create a new Library project with “Visual C#”
Select Project-><Project Name> properties…, on Signing tab, check “Sign the assembly” and New a name key file.
5. 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!";
}
}
}
[assembly: ComVisible(true)]
<!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>
To unregister your dll, just run regasm /u <full path of dll file>
9. 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.
Then browse your dll file and add into the project
3. Select your ActiveX assembly and change Register to vsdraCOM.
4. 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.
cd "$(ProjectDir)"
"%WINDIR%\System32\Makecab.exe" /f "build.ddf"
5. 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
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.
1. 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.
2. On the “Details” tab of the root certificate, click “Copy to File…” to export the root certificate as a *.cer file.
3. 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.
4. Now open IE and browse the web page, the control will be downloaded silently if “Download signed ActiveX controls” is enabled.