源代码下载:http://download.youkuaiyun.com/source/816481
参考资料
http://www.cnblogs.com/homer/archive/2008/11/19/97822.html
http://blog.youkuaiyun.com/qghboy/archive/2008/01/02/2010100.aspx
http://www.cnblogs.com/hanfey/archive/2007/05/31/766276.html
http://www.cnblogs.com/szyicol/archive/2007/11/21/967639.html
步骤
1.打开VS2005,新建项目=>选择""class library".取名为ActivexDemo
2.添加一个“Component class"文件,取名为” LoginIn”
3.在解决方案资源管理器中打开 文件夹Properties下面的文件 AssemblyInfo.cs,
将 ComVisible(false) 改为 Comviesible(true) ,
并把[assembly: Guid("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")]注释掉
4在项目属性中勾选在Build中勾选“Register for com Interop”
5.在项目属性页中“Signing”,创建一个程序签名。
6.在“LoginIn.cs”中添加引用
using System.Runtime.InteropServices;
同时插入[Guid("f6e7c5ed-d1fa-4688-8d22-a1cde3ef3243")]
7,创建一个接口IObjectSafety
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace ActivexDemo
{
/// <summary>
/// IObjectSafety 的摘要说明。
/// </summary>
[Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IObjectSafety
{
// methods
void GetInterfacceSafyOptions(
System.Int32 riid,
out System.Int32 pdwSupportedOptions,
out System.Int32 pdwEnabledOptions);
void SetInterfaceSafetyOptions(
System.Int32 riid,
System.Int32 dwOptionsSetMask,
System.Int32 dwEnabledOptions);
}
}
7将LoginIn.cs继承这个接口IObjectSafety
同时添加实现方法
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace ActivexDemo
{
[Guid("f6e7c5ed-d1fa-4688-8d22-a1cde3ef3243")]
public partial class LoginIn : Component, IObjectSafety
{
private string _LocalDomainName;
private string _LocalUserName;
//登录名
public string LocalUserName
{
get
{
return _LocalUserName;
}
set
{
_LocalUserName = value;
}
}
//本地域名
public string LocalDomainName
{
get
{
return _LocalDomainName;
}
set
{
_LocalDomainName = value;
}
}
public LoginIn()
{
InitializeComponent();
LocalUserName = Environment.UserName;
LocalDomainName = Environment.UserDomainName;
MessageBox.Show(LocalUserName + "@" + LocalDomainName);
}
public void HelloWord(string Hi)
{
MessageBox.Show(Hi);
}
public LoginIn(IContainer container)
{
container.Add(this);
InitializeComponent();
}
#region IObjectSafety 成员
public void GetInterfacceSafyOptions(Int32 riid, out Int32 pdwSupportedOptions, out Int32 pdwEnabledOptions)
{
// TODO: 添加WebCamControl.GetInterfacceSafyOptions 实现
pdwSupportedOptions = 1;
pdwEnabledOptions = 2;
}
public void SetInterfaceSafetyOptions(Int32 riid, Int32 dwOptionsSetMask, Int32 dwEnabledOptions)
{
// TODO: 添加WebCamControl.SetInterfaceSafetyOptions 实现
}
#endregion
}
}
8在解决方案中添加一个安装项目,选择“Cab Project”这个模板,并且将项目取名为“AxtivexCab”。
在“AxtivexCab”。项目中添加“ActivexDemo”项目的主输出
9在解决方案中添加一个website取名为“Web”
并且创建一个cab文件夹
10,在AxtivexCab”项目的属性页中将输出路径设置为“../web/cab/AxtivexCab.CAB”
11.在“web”网站的更目录中创建“Default.aspx”,添加如下的脚本
<object id="ActivexDemo" classid="clsid:f6e7c5ed-d1fa-4688-8d22-a1cde3ef3243" codebase="cab/AxtivexCab.CAB" class="ActivexDemo">
</object>
<input id="Button1" type="button" value="button" onclick="alert(ActivexDemo.LocalUserName+'@'+ActivexDemo.LocalDomainName)"/>
<input id="Button2" type="button" value="button" onclick="ActivexDemo.HelloWord(ActivexDemo.LocalUserName+'@'+ActivexDemo.LocalDomainName)"/>
12,浏览Default.aspx,本机测试通过
13,对AxtivexCab.CAB进行签名
去网上去获取一个数字证书,
如果不能获取得到可以利用Vs2005工具生成
具体操作时:
打开Vs2005的命令提示工具
输入
1、创建一个自我签署的X.509证书(.cer)和一个.pvk私钥文件,用到makecert工具,命令如下:
makecert -r -n "CN= cncxz " -b 01/01/2005 -e 01/01/2018 -sv myselfName.pvk myselfName.cer
按提示设置私钥密码(也可以不使用密码)即可在当前目录生成相关文件
2、利用X.509证书(.cer)创建发行者证书 (.spc),用到cert2spc工具,命令如下:
cert2spc myselfName.cer myselfName.spc
3.利用签名工具对AxtivexCab.CAB签名
Signtool signwizard