12月 01

研究一点东西,发现在浏览器中本身无法实现,学习一下ActiveX控件。

 

测试环境为VS2008+.net framework3.5+ie 11。

首先新建一个类库项目WX6.DuanXinHongZhaJi.ActiveXLib

更改“项目属性-应用程序-程序集信息”设置,勾选“使程序集 COM 可见

更改“项目属性-生成”设置,勾选“为 COM 互操作 注册

修改AssemblyInfo.cs文件

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; 

using System.Security;


// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("WX6.DuanXinHongZhaJi.ActiveXLib")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("WX6.DuanXinHongZhaJi.ActiveXLib")]
[assembly: AssemblyCopyright("Copyright ©  2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(true)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("1a347ac4-1afb-4504-a151-b666bfe414fc")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers 
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

添加接口文件(为了解决浏览器安全设置对控件的影响,必须在组件中加入IObjectSafety接口)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace WX6.DuanXinHongZhaJi.ActiveXLib
{
    [ComImport, GuidAttribute("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]
    [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IObjectSafety
    {
        [PreserveSig]
        int GetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] ref int pdwSupportedOptions, [MarshalAs(UnmanagedType.U4)] ref int pdwEnabledOptions);

        [PreserveSig()]
        int SetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] int dwOptionSetMask, [MarshalAs(UnmanagedType.U4)] int dwEnabledOptions);
    }
}

添加用户控件Uc.cs 并且实现接口

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;//Guid引用

namespace WX6.DuanXinHongZhaJi.ActiveXLib
{
    [Guid("0b6ed426-9e67-4cf3-99da-8a346a98e558")]
    public partial class Uc : UserControl
    {



        public string GetStr()
        {
            return "这是从ActiveX传过来的值,from:blog.wx6.org";
        } 

        public Uc()
        {
            InitializeComponent();
        }


        #region IObjectSafety 成员
        private const string _IID_IDispatch = "{00020400-0000-0000-C000-000000000046}";
        private const string _IID_IDispatchEx = "{a6ef9860-c720-11d0-9337-00a0c90dcaa9}";
        private const string _IID_IPersistStorage = "{0000010A-0000-0000-C000-000000000046}";
        private const string _IID_IPersistStream = "{00000109-0000-0000-C000-000000000046}";
        private const string _IID_IPersistPropertyBag = "{37D84F60-42CB-11CE-8135-00AA004BB851}";

        private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001;
        private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002;
        private const int S_OK = 0;
        private const int E_FAIL = unchecked((int)0x80004005);
        private const int E_NOINTERFACE = unchecked((int)0x80004002);

        private bool _fSafeForScripting = true;
        private bool _fSafeForInitializing = true;

        public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions)
        {
            int Rslt = E_FAIL;

            string strGUID = riid.ToString("B");
            pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;
            switch (strGUID)
            {
                case _IID_IDispatch:
                case _IID_IDispatchEx:
                    Rslt = S_OK;
                    pdwEnabledOptions = 0;
                    if (_fSafeForScripting == true)
                        pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;
                    break;
                case _IID_IPersistStorage:
                case _IID_IPersistStream:
                case _IID_IPersistPropertyBag:
                    Rslt = S_OK;
                    pdwEnabledOptions = 0;
                    if (_fSafeForInitializing == true)
                        pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;
                    break;
                default:
                    Rslt = E_NOINTERFACE;
                    break;
            }

            return Rslt;
        }

        public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions)
        {
            int Rslt = E_FAIL;
            string strGUID = riid.ToString("B");
            switch (strGUID)
            {
                case _IID_IDispatch:
                case _IID_IDispatchEx:
                    if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_CALLER) && (_fSafeForScripting == true))
                        Rslt = S_OK;
                    break;
                case _IID_IPersistStorage:
                case _IID_IPersistStream:
                case _IID_IPersistPropertyBag:
                    if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_DATA) && (_fSafeForInitializing == true))
                        Rslt = S_OK;
                    break;
                default:
                    Rslt = E_NOINTERFACE;
                    break;
            }

            return Rslt;
        }

        #endregion
    }


 
}

 

好了,接下来为解决方案添加一个安装项目WX6.DuanXinHongZhaJi.SetupActiveX

为setup项目的removepreviousversions设置为true(MSDN解释:设为true,安装程序检查是否有早期版本;一经发现,便会立即将其卸载。)

在项目上点右键,【添加】->【项目输出】,选择上边的项目

最后得到两个文件 setup.exe  WX6.DuanXinHongZhaJi.SetupActiveX.msi

新建一个web项目 WX6.DuanXinHongZhaJi.Test
将刚才生成的两个文件放在web项目的lib文件夹下

页面测试代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WX6.DuanXinHongZhaJi.Test._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    
     <script type="text/javascript">
       function passValue()
       {  
 document.getElementById("textValue").value=document.getElementById("helloBossma").GetStr();
       }
    </script>
    
</head>
<body>

 <object classid="clsid:0b6ed426-9e67-4cf3-99da-8a346a98e558" codebase="lib/setup.exe"
        width="200" height="40" id="helloBossma">
 </object>
 
 
    <form id="form1" runat="server">
 <div>
        <input id="textValue" type="text" />
        <input id="Button1" type="button" value="传值" onclick="passValue()"/>
    </div>
    </form>
</body>
</html>

注意classid要和上面的对应。

 

然后run一下就发现可以调用了。

最后,如果浏览器中没有提示安装activex控件,请设置浏览器的安全项目。

written by ocean \\ tags: