IE中Active控件实现自动下载注册(基于Tomcat)

4 篇文章 0 订阅
3 篇文章 0 订阅

最近我们老大让我学习Active控件。

一、编写一个Active控件

这里不详细介绍怎么写Active控件,提供一个连接自行查看。
c++ ActiveX基础1:使用VS2010创建MFC ActiveX工程项目
小弟也是一点点看教程学习的,大家要有耐心。

现在附上工具下载链接:ActiveX Control Test Container
http://download.csdn.net/download/xuebing1995/9928202

#注意:实现安全接口

上述项目编译后即可生成ocx文件,该ocx即可嵌入html在IE中运行。但如果该ocx对应页面是放在真实的web服务器上,访问该页面执行ActiveX里对应接口时IE将会提示“无相关属性,需要设置其初始化和脚本运行的安全性”等信息。这是因为ActiveX要在远程IE上执行,需要实现安全接口。有关控件的初始化和脚本安全问题,《再谈IObjectSafety》一文及其引用的Microsoft文章做了较详致描述。

对于ATL写的ActiveX,实现IObjectSafety即可,这里有ATL实现安全接口的详细的描述。

对于MFC写的ActiveX,可以通过修改注册表的方式来实现控件的安全性,微软也提供的详细的文档描述。具体实现步骤如下:

1、首先在项目中添加Cathelp.h和Cathelp.cpp两个文件,其内容如下所示。

Cathelp.h


#include "comcat.h"

// Helper function to create a component category and associated
// description
HRESULT CreateComponentCategory(CATID catid, WCHAR* catDescription);

// Helper function to register a CLSID as belonging to a component
// category
HRESULT RegisterCLSIDInCategory(REFCLSID clsid, CATID catid);

// HRESULT UnRegisterCLSIDInCategory - Remove entries from the registry 
HRESULT UnRegisterCLSIDInCategory(REFCLSID clsid, CATID catid);
Cathelp.cpp
#include "stdafx.h"
#include "comcat.h"
#include "strsafe.h"
#include "objsafe.h"


// HRESULT CreateComponentCategory - Used to register ActiveX control as safe 
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
    size_t len;
    // Make sure the provided description is not too long.
    // Only copy the first 127 characters if it is.
    // The second parameter of StringCchLength is the maximum
    // number of characters that may be read into catDescription.
    // There must be room for a NULL-terminator. The third parameter
    // contains the number of characters excluding the NULL-terminator.
    hr = StringCchLength(catDescription, STRSAFE_MAX_CCH, &len);
    if (SUCCEEDED(hr))
        {
        if (len>127)
          {
            len = 127;
          }
        }   
    else
        {
          // TODO: Write an error handler;
        }
    // The second parameter of StringCchCopy is 128 because you need 
    // room for a NULL-terminator.
    hr = StringCchCopy(catinfo.szDescription, len + 1, catDescription);
    // Make sure the description is null terminated.
    catinfo.szDescription[len + 1] = '\0';
 
    hr = pcr->RegisterCategories(1, &catinfo);
    pcr->Release();
 
    return hr;
}
 
// HRESULT RegisterCLSIDInCategory -
//      Register your component categories information 
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 - Remove entries from the registry 
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;
}

注:Cathelp.cpp中的代码是基于Unicode Character Set的。故项目配置时若改成Multi-Byte Character Set,需对Cathelp.cpp中代码做相应修改,否则编译不过;

2、在MyTestActiveX.cpp文件中,添加CLSID_SafeItem的定义:
这里写图片描述
CLSID_SafeItem的值是根据xxxCtrl.cpp(本例中是MyTestActiveXCtrl.cpp)文件中IMPLEMENT_OLECREATE_EX的定义而来的(实际上就是ActiveX的CLASSID)。本例中MyTestActiveXCtrl.cpp文件中IMPLEMENT_OLECREATE_EX的的值如下:
这里写图片描述
将“0x1345c26b, 0xe979, 0x45a5, 0x99, 0x7d, 0x94, 0x27, 0xfb, 0x81, 0xe7, 0x7”简单的在适当位置添加“{”和“}”括弧即变成了CLSID_SafeItem的值“0x1345c26b, 0xe979, 0x45a5, {0x99, 0x7d, 0x94, 0x27, 0xfb, 0x81, 0xe7, 0x7}”。
另外,MyTestActiveX.cpp文件起始处还需要引入如下两个文件方能正常编译:

这里写图片描述

3、修改MyTestActiveX.cpp中DllRegisterServer和DllUnregisterServer函数,代码如下(照抄即可):

// DllRegisterServer - Adds entries to the system registry

STDAPI DllRegisterServer(void)
{
    HRESULT hr;    // HResult used by Safety Functions
 
    AFX_MANAGE_STATE(_afxModuleAddrThis);
 
    if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))
      return ResultFromScode(SELFREG_E_TYPELIB);
 
    if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))
      return ResultFromScode(SELFREG_E_CLASS);
 
    // Mark the control as safe for initializing.
                                             
    hr = CreateComponentCategory(CATID_SafeForInitializing, 
         L"Controls safely initializable from persistent data!");
    if (FAILED(hr))
      return hr;
 
    hr = RegisterCLSIDInCategory(CLSID_SafeItem, 
         CATID_SafeForInitializing);
    if (FAILED(hr))
        return hr;
 
    // Mark the control as safe for scripting.
 
    hr = CreateComponentCategory(CATID_SafeForScripting, 
                                 L"Controls safely  scriptable!");
    if (FAILED(hr))
        return hr;
 
    hr = RegisterCLSIDInCategory(CLSID_SafeItem, 
                        CATID_SafeForScripting);
    if (FAILED(hr))
        return hr;
 
    return NOERROR;
}



// DllUnregisterServer - Removes entries from the system registry

STDAPI DllUnregisterServer(void)
{
    AFX_MANAGE_STATE(_afxModuleAddrThis);  

    // 删除控件初始化安全入口.   
    HRESULT hr=UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForInitializing);  

    if (FAILED(hr))  
        return hr;  

    // 删除控件脚本安全入口   
    hr=UnRegisterCLSIDInCategory(CLSID_SafeItem, CATID_SafeForScripting);  

    if (FAILED(hr))  
        return hr;  

    if (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor))  
        return ResultFromScode(SELFREG_E_TYPELIB);  

    if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))  
        return ResultFromScode(SELFREG_E_CLASS);  

    return NOERROR;
}

这样第一部分ocx就编写完成了,具体的功能,自己写。不在本文章范围。

二、打包cab包

packageforweb:
工具链接:packageforweb下载

过程我也懒得 写了,这里附上过程链接:发布MFC ActiveX控件并实现自动更新

我先是用这个没实现功能,(在成功之后我试了这个也能用)后来换了一种工具:
链接:cabMake工具下载
这里我也不写过程了:推荐一个链接:OCX控件打包成CAB并实现数字签名过程

三、服务器内容

这里我用了Tomcat里的jsp以及SpringMVC实现Active文件的下载,不废话,直接上代码:

test.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%  
    String path = request.getContextPath();  
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";     
%>  
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	
<OBJECT id ="CustForm" classid="clsid:08CA3397-2391-454C-837E-B0A28166FB90" width="500"height="500" 
codebase="<%=basePath%>jisuanqi.cab#version=1,1,0,0" VIEWASTEXT> 
</OBJECT>
<!--VIEWASTEXT :阻止一些可视化工具,例如Microsoft FrontPage或者Visual InterDev在设计页面时运行  -->

</body>
</html>

ActiveController类

package com.hb.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


/**
***********************************
*
*@类名	ActiveController
*@时间	2017年8月10日上午9:22:56
*@作者	沈雪冰
*@描述	从服务器下载合格签章组件
*
***********************************
*/
@Controller//处理请求 标记此类事宜handler处理器
public class ActiveController {

	@RequestMapping(method=RequestMethod.GET)  
public String getxxxComponent(HttpServletRequest request,  HttpServletResponse response){  
	    response.setCharacterEncoding("UTF-8");  
		String filePath="E:/新建文件夹/Output/计算器.cab";
		File file=new File(filePath);
	    //String filePath = "../";  
	    String contentType = "application/octet-stream";  
	    try {  
	    	String fileName=new String(file.getName().getBytes("utf-8"),"iso-8859-1"); //解决中文乱码问题
	        download(request,response,filePath,fileName,contentType);  
	       
	    } catch (Exception e) { 
	    	e.printStackTrace();
	    }                 
	    return "test";  
	}  
	  
	/** 
	 * 下载文件 
	 * @param request 
	 * @param response 
	 * @param filePath 文件在服务器的相对路径 
	 * @param fileName 文件下载后的文件名 
	 * @param contentType 文件类型 
	 * @throws Exception 
	 */  
	public void download(HttpServletRequest request,  
	        HttpServletResponse response, String filePath,String fileName,  
	        String contentType) throws Exception {  
	    response.setCharacterEncoding("UTF-8");
	    request.setCharacterEncoding("UTF-8");  
	    BufferedInputStream bis = null;  
	    BufferedOutputStream bos = null;  
	  
	    //String ctxPath = request.getSession().getServletContext().getRealPath("/");    //服务器路径    E:\apache-tomcat-8.0.23\webapps\ActiveDemo
	    String downLoadPath =  filePath ;  
	  
	    long fileLength = new File(downLoadPath).length();  
	  
	    response.setContentType(contentType);  
	    response.setHeader("Content-disposition", "attachment; filename=" + fileName);  
	    response.setHeader("Content-Length", String.valueOf(fileLength));  
	  
	    bis = new BufferedInputStream(new FileInputStream(downLoadPath));  
	    bos = new BufferedOutputStream(response.getOutputStream());  
	    byte[] buff = new byte[1024*8];  
	    int bytesRead;  
	    while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {  
	        bos.write(buff, 0, bytesRead);  
	    }  
	
	    
	    bis.close();  
	    bos.flush();  
	    bos.close();  
	    //FileOutputStream out = new FileOutputStream(fileName,true);  
	   // out.write(new byte[]{(byte)0xEF, (byte)0xBB, (byte)0xBF});//utf-8 bom  
	    //out.write(con.getBytes(charset));  
	    //out.close();  
	}  

}

springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:context="http://www.springframework.org/schema/context"
		xmlns:aop="http://www.springframework.org/schema/aop"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xmlns:mvc="http://www.springframework.org/schema/mvc"
		xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!--	开启扫描	-->
<context:component-scan base-package="com.hb"></context:component-scan>
<!--开启注解支持  -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 视图解析器 -->
<bean id="ViewResolvr" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
<property name="ViewClass" value="org.springframework.web.servlet.view.JstlView"></property>
</bean>
<!-- 文件上传下载设置 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="5000000"></property>
<property name="maxInMemorySize" value="5000000"></property>
<property name="defaultEncoding" value="utf-8"></property>
</bean>		
</beans>

开启Tomcat服务器,到这里就可以实现本地以及局域网访问这个Active控件了。
但是由于制作的数字签名无效,ie会拦截。
这里要把ie的安全定义为自定义级别:
这里写图片描述
以及
这里写图片描述

这样就可以了。
如果大家想获取CA证书,途径:
1.购买收费的CA证书,自行百度
2.申请免费的,www.ca365.com
3.去http://www.globalsign.net 申请DEMO版的证书,只能用一个月,不过是全球的。
4.打开 signcode.exe(这个程序装完PackageForTheWeb在他的安装目录下就有),按照提示,选择到你刚才申请的证书,next,最后一步要选择时间戳服务器,这里有个免费的:http://timestamp.verisign.com/scripts/timstamp.dll 写上去就完了,这样就完成了签名。

本地访问链接:http://127.0.0.1:8080/ActiveDemo/test.jsp

cmd下ipconfig可查看本地ip

局域网访问链接:http://服务器ip地址:8080/ActiveDemo/test.jsp

小弟写的有不足的地方,请大佬们指点。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

离水的鱼儿

一分也是爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值