---
title: "VC++开发Activex控件以及签名发布"
author: "唐风汉月"
date: "2010-12-17T16:46:41Z"
tags: ["IE", "JSP", "MFC", "VC++", "脚本"]
categories: ["编程语言"]
slug: "vc-activex"
draft: false
lastmod: "2010-12-17T17:00:27Z"
id: 845775
url: "https://arlon.iteye.com/blog/845775"
---

**前言**  
      本人不懂C++，当前由于要做一个打印控件，使用Activex插件技术，所以在网络上搜索了相关技术文档，今天有空，遂将自己的当前学到的一些关于Activex技术整理之，进而和朋友们分享之。  
**一、 开发环境**  
开发工具：Visual Studio 2008  
开发语言：Visual C++

测试工具：IE 7+  
**二、 创建MFC ActiveX项目**  
1、 打开VS2008新建MFC项目。这里我们取名为“PrintUtil”。

 ![](http://dl.iteye.com/upload/attachment/369665/35951a75-93e5-3ed3-a658-bd779636b11b.jpg)

   
2、 输入项目名称为“PrintUtil”和项目位置。点击“确定”按钮，打开向导对话框。 

![](http://dl.iteye.com/upload/attachment/369663/496ba728-f661-38e7-b48d-2fa743932f7d.png)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

   
3、 选择“控件设置”选项卡，具体设置可参考上图。其它选项卡为默认设置。最后点击“完成”按钮保存设置。

 

三、 添加控件方法  
VC2005会为我们自动创建好MFC ActiveX程序框架，我们只要给该ActiveX控件添加方法即可。现在我们给控件添加一个“AddFun”方法，这个方法是将两个数相加并返回结果。  
1、 点击“视图”，打开“类视图”窗口。 

![](http://dl.iteye.com/upload/attachment/369671/3dafac36-728b-31c7-ab14-f86ec9d9daf4.png)  
   
    
2、 展开“PrintUtilLib”项，选中“\_DPrintUtil”项。点击鼠标右键，选择“添加”下的“添加方法”。 

![](http://dl.iteye.com/upload/attachment/369675/e1e1d42e-49db-3edd-8e3f-93975d3f27c7.png)  
   
   
3、 打开添加方法向导窗口。因为我们是添加一个加法方法，所以我们设置的返回类型为LONG型，方法名设为AddFun，添加两个LONG类型参数Add1，Add2。  
![](http://dl.iteye.com/upload/attachment/369687/823697d8-a1ba-3333-bcab-baa3e91aec67.jpg)  
4、   其它为默认设置，点击“完成”按钮完成添加方法。接下来我们打开“解决方案资源管理器”打开“PrintUtilCtrl.cpp”文件。

![](http://dl.iteye.com/upload/attachment/369689/aa5f25ae-40f2-3ad4-bf73-61b3ff1bb1d7.png)

5、 打开代码视图，我们会发现VC2005已经为我们添加了一个“AddFun”方法，我们在方法内添加“return Add1 + Add2;”语句。

 ![](http://dl.iteye.com/upload/attachment/369693/2215f36c-4d6b-3d6a-a950-80e3babb5c96.png)

四、 MFC Activex 安全问题  
1、在默认环境下，编译的MFC Activex控件，只能在本地代码中运行，即在http://localhost/xxx/xxx.htm中执行，而在http://127.0.0.1/xxx/xxx.htm中提示无相关属性,需要设置其初始化和脚本运行的安全性  
ActiveX在远程IE页面上执行，需要实现安全接口。  
在ATL写的ActiveX中，用IObjectSafety。  
http://support.microsoft.com/kb/168371/en-us  
在MFC写的ActiveX中，直接修改注册表。  
http://support.microsoft.com/kb/161873/en-us  
mfc实现的ocx，要在app实现文件中包括两个文件：  
在PrintUtil.h文件中实现以下方法：

```
// PrintUtil.cpp : CPrintUtilApp 和DLL 注册的实现。 
#include "stdafx.h" 
#include "PrintUtil.h" 
#include <objsafe.h> 

#ifdef _DEBUG 
#define new DEBUG_NEW 
#endif 


CPrintUtilApp theApp; 

const GUID CDECL BASED_CODE _tlid = 
{ 0x3C8F86CA, 0x6470, 0x4B7C, { 0xB2, 0x76, 0x3B, 0xEB, 0xF, 0xB0, 0x1B, 0x4E } }; 
const WORD _wVerMajor = 1; 
const WORD _wVerMinor = 0; 



// CPrintUtilApp::InitInstance - DLL 初始化 

BOOL CPrintUtilApp::InitInstance() 
{ 
BOOL bInit = COleControlModule::InitInstance(); 

if (bInit) 
{ 
// TODO: 在此添加您自己的模块初始化代码。 
} 

return bInit; 
} 



// CPrintUtilApp::ExitInstance - DLL 终止 

int CPrintUtilApp::ExitInstance() 
{ 
// TODO: 在此添加您自己的模块终止代码。 

return COleControlModule::ExitInstance(); 
} 

// 创建组件种类   
HRESULT CreateComponentCategory(CATID catid, WCHAR* catDescription) 
{    
    ICatRegister* pcr = NULL ;    
    HRESULT hr = S_OK ;    
    hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr, NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pcr);    
    if (FAILED(hr)) return hr;    
    // Make sure the HKCR\Component Categories\{..catid...}    
    // key is registered.    
    CATEGORYINFO catinfo;    
    catinfo.catid = catid;    
    catinfo.lcid = 0x0409 ; // english    
    // Make sure the provided description is not too long.    
    // Only copy the first 127 characters if it is.    
    int len = wcslen(catDescription);    
    if (len>127) len = 127;    
    wcsncpy(catinfo.szDescription, catDescription, len);    
    // Make sure the description is null terminated.    
    catinfo.szDescription[len] = '\0';    
    hr = pcr->RegisterCategories(1, &catinfo);    
    pcr->Release();    
    return hr;    
}  

// 注册组件种类   
HRESULT RegisterCLSIDInCategory(REFCLSID clsid, CATID catid) 
{    
    // Register your component categories information.    
    ICatRegister* pcr = NULL ;    
    HRESULT hr = S_OK ;    
    hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr, NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pcr);    
    if (SUCCEEDED(hr)) {    
      // Register this category as being "implemented" by the class.    
      CATID rgcatid[1];    
      rgcatid[0] = catid;    
      hr = pcr->RegisterClassImplCategories(clsid, 1, rgcatid);    
    }    
    if (pcr != NULL) pcr->Release();    
    return hr;    
}   

// 卸载组件种类   
HRESULT UnRegisterCLSIDInCategory(REFCLSID clsid, CATID catid) 
{    
    ICatRegister* pcr = NULL ;    
    HRESULT hr = S_OK ;    
    hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,    
            NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pcr);    
    if (SUCCEEDED(hr)) {    
      // Unregister this category as being "implemented" by the class.    
      CATID rgcatid[1] ;    
      rgcatid[0] = catid;    
      hr = pcr->UnRegisterClassImplCategories(clsid, 1, rgcatid);    
    }    
    if (pcr != NULL) pcr->Release();    
    return hr;    
}    


// DllRegisterServer - 将项添加到系统注册表 
STDAPI DllRegisterServer(void) 
{    
    HRESULT hr;    
    AFX_MANAGE_STATE(_afxModuleAddrThis);    
    if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))    
        return ResultFromScode(SELFREG_E_TYPELIB);    
    if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))    
        return ResultFromScode(SELFREG_E_CLASS);    
    // 标记控件初始化安全.    
    // 创建初始化安全组件种类   
    hr = CreateComponentCategory(CATID_SafeForInitializing, L"Controls safely initializable from persistent data!");    
    if (FAILED(hr)) return hr;    
    // 注册初始化安全   
    hr = RegisterCLSIDInCategory(BASED_CODE _tlid , CATID_SafeForInitializing);    
    if (FAILED(hr)) return hr;    
    // 标记控件脚本安全   
    // 创建脚本安全组件种类   
    hr = CreateComponentCategory(CATID_SafeForScripting, L"Controls safely scriptable!");    
    if (FAILED(hr)) return hr;    
    // 注册脚本安全组件种类   
    hr = RegisterCLSIDInCategory(BASED_CODE _tlid , CATID_SafeForScripting);    
    if (FAILED(hr)) return hr;    
    return NOERROR;    
}    

// DllUnregisterServer - Removes entries from the system registry    
STDAPI DllUnregisterServer(void) 
{    
    HRESULT hr;    
    AFX_MANAGE_STATE(_afxModuleAddrThis);    
    if (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor))    
        return ResultFromScode(SELFREG_E_TYPELIB);    
    if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))    
        return ResultFromScode(SELFREG_E_CLASS);    
    // 删除控件初始化安全入口.    
    hr=UnRegisterCLSIDInCategory(BASED_CODE _tlid , CATID_SafeForInitializing);    
    if (FAILED(hr)) return hr;    
    // 删除控件脚本安全入口   
    hr=UnRegisterCLSIDInCategory(BASED_CODE _tlid , CATID_SafeForScripting);    
    if (FAILED(hr)) return hr;    
    return NOERROR;    
}  
```

  

现在控件就可以在自注册时就注册为安全控件了。  
2、设置项目属性 将配置类型设置成静态库(.lib)  
![](http://dl.iteye.com/upload/attachment/369702/7ae1886d-63f4-3c74-a110-72a83d228b23.jpg)  
3、最后生成项目，ocx控件就产生了。  
五、 ActiveX发布步骤  
在这里简单说明下，打包activeX需要制作证书，具体用到makecert 、cert2spc 、signtool 这三个VS提供的工具，工具在VS文件夹里面，以下制作过程需要在工具所在的文件夹下完成！  
1、单击"开始"--&gt;"运行（R）"--&gt;输入"cmd"--&gt;回车--&gt;进入到操作的控件所在的目录（需要将上面所说的工具，和ocx控件放到一个文件夹下）；

2、创建PVK文件(私人密匙文件)，在命令行中输入" makecert -sk PrintUtil PrintUtil.pvk -n CN=XXXXXXX公司"，然后回车；  
sk-表示主题的密钥容器位置，ss-主题的证书存储名称， n-证书颁发对象，r-证书存储位置；  
![](http://dl.iteye.com/upload/attachment/369706/7d89d724-687b-3661-89c7-5dbf668b02a4.jpg)  
3、创建CER文件(公司证书)，在命令行中输入" makecert -sk PrintUtil.pvk PrintUtil.cer "，然后回车，如图所示，若出现"Successed"提示，则会在C:\\ PrintUtil目录下生成PrintUtil.cer文件；  
sk-表示主题的密钥容器位置，is-颁发者的证书存储名称， n-证书颁发对象，ic-颁发者的证书存储位置，-$-授权范围（用于代码签名）；

 ![](http://dl.iteye.com/upload/attachment/369710/9090c9f2-c28f-3a1f-9374-01498dc1eab1.jpg)  
4、创建SPC测试软件出版商证明书，在命令行中输入" cert2spc PrintUtil.cer PrintUtil.spc "，然后回车；  
![](http://dl.iteye.com/upload/attachment/369712/790576b9-adb3-3bee-bb67-70a6952b9b29.jpg)  
5、创建INF文件，用记录本编辑以下信息：

```
[version] 
signature="$CHINA$" 
AdvancedINF=1.0 

[Add.Code] 
PrintUtil.ocx=PrintUtil.ocx 

[PrintUtil.ocx] 
file=thiscab 
clsid={ 1BABEDC3-3936-4850-B79B-2417E28A5655 } 
FileVersion=1,0,0,0 
RegisterServer=yes 
DestDir=11 
```

 

6、创建CAB文件，在命令行中输入" cabarc -s 6144 n PrintUtil.cab PrintUtil.ocx PrintUtil.inf "，-s 选项表示在压缩文件中保留用于代码签名的空间，n命令指定希望创建 CAB 文件，然后回车；  
![](http://dl.iteye.com/upload/attachment/369716/53b6f25e-e71f-3d5c-b021-f4f3e2eaf736.jpg)  
7、使用Code Signing Wizard签署一个CAB文件，首先双击运行工具集里面的signcode.exe（或在命令行里直接输入“signcode”后回车），系统会弹出如图所示的数字签名向导;  
![](http://dl.iteye.com/upload/attachment/369718/17224505-f16d-3c83-9119-5f133abb3884.png)  
8、单击"下一步（N）"按钮，选择要进行数字签名的且已做成CAB包的文件PrintUtil.cab文件；  
![](http://dl.iteye.com/upload/attachment/369722/bd77db8a-8274-3928-8d42-dad09cb81abf.png)  
9、选择好CAB包后单击"下一步（N）"按钮，在选择想要的签名类型里选择"自定义（C）"并单击"下一步（N）"按钮；  
![](http://dl.iteye.com/upload/attachment/369726/b39d1eb6-03b6-3e9d-8f80-e4071adebfe0.png)  
10、点击“从文件选择”签名证书 ( 公钥文件 )，如： PrintUtil.cer ：  
![](http://dl.iteye.com/upload/attachment/369730/32ac8670-c866-379b-935d-30d2b569c3db.png)  
11、在图20中单击“下一步（N）”按钮来到下图，然后在图里选择“CSP中的私钥（K）”。  
![](http://dl.iteye.com/upload/attachment/369736/5ef0f907-3681-31fa-b499-8a7fe4c3a5fe.png)  
12、在上图中单击“下一步（N）”按钮，然后在下图中的散列算法中选择“shal”，并单击“下一步（N）”按钮。  
![](http://dl.iteye.com/upload/attachment/369740/84900370-7418-3e73-86ea-b1017e512fe1.png)  
13、在“证书路径中的证书”中选择“证书路径中的所有证书，包括根证书（C）”，在“其它证书（可选）”中选择“包括在以下PKCS #7 证书（.p7b）文件中的证书（P）：”，并单击“浏览（R）…”按钮选择PrintUtil.spc文件，选择完后单击“下一步（N）”按钮：  
![](http://dl.iteye.com/upload/attachment/369744/91761283-20da-343c-bb59-ba2d9e83c62f.png)  
14、接下来在弹出的“数据描述”窗口中输入公司的名称和网址并单击“下一步（N）”按：  
![](http://dl.iteye.com/upload/attachment/369750/633ed154-98ce-3521-8ced-c0604fcea9bd.jpg)  
15、现大部份工作都已完成，在接下来的一步当中是可选的操作，其作用只是为CAB加入时间戳，此步骤完全可以不做。

VeriSign:  http://timestamp.verisign.com/scripts/timstamp.dll  
Comodo:  http://timestamp.comodoca.com/authenticode  
GeoTrust/TrustCenter: http://www.trustcenter.de/codesigning/timestamp  
16、完成  
![](http://dl.iteye.com/upload/attachment/369754/86a03ec5-abd5-39fd-92c3-7c4ec9052389.jpg)  
六、 运行  
编写jsp页面，test.jsp 

```
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<% 
String path = request.getContextPath(); 
String basePath = request.getScheme() + "://" 
+ request.getServerName() + ":" + request.getServerPort() 
+ path + "/"; 
%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
<head> 
<base href="<%=basePath%>"> 

<title>My JSP 'index.jsp' starting page</title> 
<meta http-equiv="pragma" content="no-cache"> 
<meta http-equiv="cache-control" content="no-cache"> 
<meta http-equiv="expires" content="0"> 
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
<meta http-equiv="description" content="This is my page"> 
<object id="printUtil" 
classid="clsid:1BABEDC3-3936-4850-B79B-2417E28A5655" 
codebase="<%=basePath%>/PrintUtil.cab#version=1,0,0,0"></object> 
<script type="text/javascript"> 
	function add(arg1,args) { 
		try { 
			var v = printUtil.AddFun(arg1,args); 
			alert(v); 
		} catch (e) { 
			alert(e.message) 
		} 
	} 
	add(1,2); 
</script> 
</head> 
<body> 
</body> 
</html>
```

    
运行结果：

![](http://dl.iteye.com/upload/attachment/369782/0ee52e1f-c440-30d1-a9eb-dc965f45643b.png)  
   
 好了，今天就写到这里了，如果朋友们有什么疑问或者更好的建议和意见，请Email Me。

 

Email：[df.jxnu@gmail.com](mailto:df.jxnu@gmail.com)

 

本文所用到的工具见附件：

 

转载请声明本文链接