为完成网页自动下载并安装控件的功能,需要通过C#创建一个ActiveX控件,然后将该控件置于安装程序中,在打开网页的时候下载、安装并注册该ActiveX控件。本文是采用VS2005创建的,VS2003创建过程与之相似。

    首先,创建一个类库,为其命名为CreateActiveXEmail:

创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)

    删除掉默认生成的类Class1.cs,创建一个接口ActiveXEmailInterface:

 

创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)using System;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
using System.Collections.Generic;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
using System.Text;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
using System.Runtime.InteropServices;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
namespace CreateActiveXEmail
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
...{
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)    [Guid(
"CB5BDC81-93C1-11CF-8F20-00805F2CD064"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)    
public interface ActiveXEmailInterface
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)    
...{
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)        
void GetInterfacceSafyOptions(int riid, out int pdwSupportedOptions, out int pdwEnabledOptions);
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)        
void SetInterfaceSafetyOptions(int riid, int dwOptionsSetMask, int dwEnabledOptions);
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)    }

创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)}

    其中GUID可以通过【工具】-【创建GUID】来产生。

    实现该接口的目的就是提高程序的安全性,以便客户端IE在不更改设置的情况下可以预行该ActiveX控件。

    然后,用你需要实现某些功能的类来继承上面的接口。

 

创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)using System;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
using System.Collections.Generic;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
using System.ComponentModel;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
using System.Drawing;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
using System.Data;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
using System.Text;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
using System.Windows.Forms;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
using System.Runtime.InteropServices;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
namespace CreateActiveXEmail
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
...{
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)    [Guid(
"060d1308-f34e-4c9f-8962-0abafe385d33"), ComVisible(true)]
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)    
public class ActiveXEmail : ActiveXEmailInterface
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)    
...{
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)        
public void GetInterfacceSafyOptions(int riid, out int pdwSupportedOptions, out int pdwEnabledOptions)
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)        
...{
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)            pdwSupportedOptions 
= 1;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)            pdwEnabledOptions 
= 2;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)        }

创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)        
public void SetInterfaceSafetyOptions(int riid, int dwOptionsSetMask, int dwEnabledOptions)
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)        
...{
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)        }

创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)        
public ActiveXEmail()
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)        
...{
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)        }

创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)    }

创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)}

 

    注意,上面代码中的类属性“ComVisible(true)”,是必须添加的。

    在上面的类中,仅仅是现实了接口的两个方法,至于其他需要的方法,自行添加即可。另外,需要对项目属性进行一点修改:

创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)

    注意,类属性中的“ComVisible(true)”和上图中的【为 COM Interop 注册】缺一不可!只有这样才能生成tlb文件。

    这样,一个可用的ActiveX控件就已经生成。在本机你可以随意调用其中的任何方法,但问题是,当客户端机器需要远程调用时,必须在能在客户端机器上注册该ActiveX控件才行。所以,还必须进行下面的步骤:将该ActiveX打包,在安装后在目标机器进行注册。

    创建一个安装包:

创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)

    然后右键单击项目,点击【添加】-【项目输出】,在【主输出】中选择上面创建的工程“CreateActiveXEmail”。然后,打开安装工程的【属性】页面,对项目的【安装URL】项进行设置:

创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)

注意,上图的【安装URL】项中,必须使用绝对路径。另外,上图中的“DllFolder”是一个已发布网站“http://172.16.11.136/TestingAX”下的一个目录,这意味着,当在客户端访问页面时,如果客户端未安装当前ActiveX控件,则从路径http://172.16.11.136/TestingAX/DllFolder”来下载。

    最后,在页面中按如下方法调用即可:

 

创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)<object classid="clsid:060d1308-f34e-4c9f-8962-0abafe385d33"
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)               codebase
="DllFolder/setup.exe#version=1,0,0,0"></object>

 

 

另外,还可以采用其他方法,即上面的类库属性不选择【为 COM Interop 注册】,安装工程属性不为【安装URL】指定路径,类中也不添加“ComVisible(true)”属性,而是创建一个安装类:

 

创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)using System;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
using System.Collections.Generic;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
using System.ComponentModel;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
using System.Configuration.Install;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
using System.Reflection;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
using System.IO;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
using Microsoft.Win32;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
using System.Diagnostics;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
namespace CreateActiveXEmail
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
...{
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)    [RunInstaller(
true)]
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)    
public partial class CustomInstaller : Installer
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)    
...{
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)        
private string regCommandFile = string.Empty;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)        
private string unRegCommandFile = string.Empty;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)        
public CustomInstaller()
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)        
...{
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)            InitializeComponent();
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)        }

创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)        
protected override void OnAfterInstall(System.Collections.IDictionary savedState)
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)        
...{
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)            
base.OnAfterInstall(savedState);
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)            
try
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)            
...{
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                
//get the path of the Regasm.exe
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
                string regasmPath = string.Empty;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                
//get the path of the current executing assembly
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
                string currentAsmDLLFilePath = Assembly.GetExecutingAssembly().Location;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                
string currentAsmPath = currentAsmDLLFilePath.Substring(0, currentAsmDLLFilePath.LastIndexOf('\'+ 1);
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                
string currentRegasmPath = string.Format("{0}{1}", currentAsmPath, "RegAsm.exe");
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                
if (!File.Exists(currentRegasmPath))
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                
...{
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                    
try
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                    
...{
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                        RegistryKey frmReg 
= Registry.LocalMachine.OpenSubKey(@"SOFTWAREMicrosoft.NETFramework");
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                        
if (frmReg == null)
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                        
...{
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                            
//the .net framework do not exist in the local machine
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
                            return;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                        }

创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                        
string frameworkVersion = Environment.Version.ToString();
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                        frameworkVersion 
= frameworkVersion.Substring(0, frameworkVersion.LastIndexOf('.'));
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                        regasmPath 
= string.Format(@"{0}v{1}{2}", frmReg.GetValue("InstallRoot").ToString(), frameworkVersion, "RegAsm.exe");
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                        
if (!File.Exists(regasmPath))
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                        
...{
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                            
//the Regasm.exe do not exist in the local machine
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
                            return;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                        }

创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                    }

创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                    
catch (System.ArgumentException ex)
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                    
...{
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                        
throw new System.ArgumentException(ex.Message);
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                    }

创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                }

创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                
else
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                
...{
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                    regasmPath 
= currentRegasmPath;
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                }

创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                
//create the registration command line
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
                string regCommand = string.Format("{0} "{1}" /{2} /{3}", regasmPath, currentAsmDLLFilePath, "tlb""codebase");                
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                
try
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                
...{
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                    regCommandFile 
= string.Format("{0}{1}", currentAsmPath, "Regasm.bat");
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                    
if (File.Exists(regCommandFile))
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                    
...{
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                        File.Delete(regCommandFile);
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                    }

创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                    
using (StreamWriter swReg = File.CreateText(regCommandFile))
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                    
...{
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                        swReg.Write(regCommand);
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                        swReg.Flush();
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                    }

创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                }

创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)                
catch (UnauthorizedAccessException uaex)
创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)创建可在网页下载安装的ActiveX控件(通过Setup.exe安装)

相关文章:

  • 2021-11-11
  • 2021-11-11
  • 2022-01-02
  • 2022-02-10
  • 2021-11-11
  • 2021-11-11
猜你喜欢
  • 2021-11-11
  • 2021-11-11
  • 2021-11-11
  • 2021-09-07
  • 2021-11-11
相关资源
相似解决方案