C#开发Activex控件(1)
项目结构
创建Activex步骤:
1.选择创建类别(Windows 控件库或类库)
2.设置对应的com属性

AssemblyInfo.cs中须做以下设置:
a.引入命名空间:using System.Security;
b.[assembly: AllowPartiallyTrustedCallers()]

3.生成的证书能正常在IE下安装,需继承IObjectSafety,考虑到所有的生成的证书都需继承自该接口,需写一个基类,代码如下:
a.接口代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices; namespace LamSoonActivex
{
[ComImport, Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
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);
}
}
b.基类代码
using System;
using System.Collections.Generic;
using System.Text; using System.Runtime.InteropServices;
using System.Windows.Forms; namespace LamSoonActivex
{ public class BaseActivex : UserControl, IObjectSafety
{
private Sunisoft.IrisSkin.SkinEngine skinEngine1; #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 = ;
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)
{
//pdwSupportedOptions = 1;
//pdwEnabledOptions = 2;
//throw new Exception("The method or operation is not implemented.");
//以上代码亦能正常运行,SetInterfaceSafetyOptions设置即可 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 = ;
if (_fSafeForScripting == true)
pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;
break;
case _IID_IPersistStorage:
case _IID_IPersistStream:
case _IID_IPersistPropertyBag:
Rslt = S_OK;
pdwEnabledOptions = ;
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)
{
//throw new Exception("The method or operation is not implemented.");
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 private void InitializeComponent()
{
this.skinEngine1 = new Sunisoft.IrisSkin.SkinEngine(((System.ComponentModel.Component)(this)));
this.SuspendLayout();
//
// skinEngine1
//
this.skinEngine1.SerialNumber = "";
this.skinEngine1.SkinFile = "\\Resources\\SportsBlue.ssk";//null;
//
// BaseActivex
//
this.Name = "BaseActivex";
this.Load += new System.EventHandler(this.BaseActivex_Load);
this.ResumeLayout(false); } private void BaseActivex_Load(object sender, EventArgs e)
{
//skinEngine1.SkinFile = LamSoonActivex.Properties.Resources.l.ssk;// @"\Resources\SportsBlue.ssk";
}
}
}
4.证书编码
a.需在证书的[Guid("527A380F-7E26-4fd3-AEA5-010D3E7B73BE")],是在Web页中调用的Id,必须是唯一
b.须引入命名空间 using System.Runtime.InteropServices;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms; using System.Runtime.InteropServices; namespace LamSoonActivex
{
[Guid("527A380F-7E26-4fd3-AEA5-010D3E7B73BE")]
public partial class FrmLamSoon : BaseActivex
{
public FrmLamSoon()
{
InitializeComponent();
} private void btnLogin_Click(object sender, EventArgs e)
{
if (txtStaff_Id.Text.Length == || txtPwd.Text.Length == )
{
MessageBox.Show("请输入有效的系统登陆信息!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
}
}
5.Web UI调用
2种调用方式: <object id="helloworld"
classid="clsid:527A380F-7E26-4fd3-AEA5-010D3E7B73BE"
codebase="LamSoonActivex.exe">
</object> <object id="helloworld"
classid="clsid:527A380F-7E26-4fd3-AEA5-010D3E7B73BE"
codebase="LamSoonActivex.cab#version=1,0,1,0">
</object>
示例:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>TestActivex</title>
</head>
<body>
<form id="form1" runat="server">
<div style=" text-align:center"> <object id="activex_test"
classid="clsid:527A380F-7E26-4fd3-AEA5-010D3E7B73BE"
codebase="LamSoonActivex.cab#version=1,0,1,0">
</object> </div>
</form>
</body>
</html>
C#开发Activex控件(1)的更多相关文章
- 用C#开发ActiveX控件,并使用web调用
入职差不多两个月了,由学生慢慢向职场人做转变,也慢慢的积累知识,不断的更新自己.最近的一个项目里边,涉及到的一些问题,因为SDK提供的只是winform才能使用了,但是有需求咱们必须得完成啊,所以涉及 ...
- ATL开发 ActiveX控件的 inf文件模板
ATL开发 ActiveX控件的 inf文件模板
- 使用C#开发ActiveX控件(新)
前言 ActiveX控件以前也叫做OLE控件,它是微软IE支持的一种软件组件或对象,可以将其插入到Web页面中,实现在浏览器端执行动态程序功能,以增强浏览器端的动态处理能力.通常ActiveX控件都是 ...
- [转]C#开发ActiveX控件,.NET开发OCX控件案例
引自:百度 http://hi.baidu.com/yanzuoguang/blog/item/fe11974edf52873aaec3ab42.html 讲下什么是ActiveX控件,到底有什么 ...
- 使用C#开发ActiveX控件(新) 转 http://www.cnblogs.com/yilin/p/csharp-activex.html
前言 ActiveX控件以前也叫做OLE控件,它是微软IE支持的一种软件组件或对象,可以将其插入到Web页面中,实现在浏览器端执行动态程序功能,以增强浏览器端的动态处理能力.通常ActiveX控件都是 ...
- [转] 使用C#开发ActiveX控件
双魂人生 原文 使用C#开发ActiveX控件 ActiveX 是一个开放的集成平台,为开发人员.用户和 Web生产商提供了一个快速而简便的在 Internet 和 Intranet 创建程序集成和内 ...
- 使用C#开发ActiveX控件 11
C#开发ActiveX控件 ActiveX 是一个开放的集成平台,为开发人员.用户和 Web生产商提供了一个快速而简便的在 Internet 和 Intranet 创建程序集成和内容的方法. 使用 ...
- C#开发ActiveX控件
昨天写了篇博客<Winform 程序嵌入WPF程序 并发送消息>,没有说明为什么要嵌入WPF程序,那么今天就来唠叨唠叨其中的一个使用场景,开发ActiveX控件 首先,新建一个类库工程Hu ...
- 使用C#开发ActiveX控件
使用C#开发ActiveX控件(新) 前言 ActiveX控件以前也叫做OLE控件,它是微软IE支持的一种软件组件或对象,可以将其插入到Web页面中,实现在浏览器端执行动态程序功能,以增强浏览器端的动 ...
- Delphi 开发ActiveX控件(非ActiveForm)
Delphi 开发ActiveX控件(非ActiveForm) Q:为什么不采用ActiveForm工程?通过它可以快速开发带窗体控件,创建过程也非常简单(都不用考虑安全接口问题),很省事! A:如果 ...
随机推荐
- java多线程中的生产者与消费者之等待唤醒机制@Version2.0
二.生产者消费者模式的学生类成员变量生产与消费demo, @Version2.0 在学生类中添加同步方法:synchronized get()消费者,synchronized set()生产者 最终版 ...
- Oracle 临时表
一.临时表的介绍: Oracle的临时表只存在于某个会话或者事物的生命周期里,此时临时表中的数据只对当前这个会话可见. 临时表经常被用于存放一个操作的中间数据(数据处理的中间环节). 临时表由于不产生 ...
- 在Linux下搭建Git服务器的方法是什么样?
第一步 安装git:可以通过命令的方式快速安装,不同的linux的安装方法可能不一样,我的是采用的yum方法.ubuntu可以用apt-get命令.sudo yum install git 第二步 添 ...
- dedecms的title怎么优化?
(1)首页:index.htm 模板:<title>k1,k2,k3 {dede:global.cfg_webname/}</title> 规则:3个关键词+网站名称 示例:& ...
- hdwiki 数据库结构说明
HDWiki数据库结构说明 以下标有“A”的表示该列为自增列,标有“P”的表示该列为主码,标有“I”的表示该列为索引列,标有“U”的表示该列为唯一列,标有“F”的表示全文搜索. ...
- MyBatis 判断条件为等于的问题
在用MyBatis操作数据库的时候相信很多人都用到,当在判断null, 大于,大于等于,小于,小于等于,不等于时估计很多都用到,比较容易实现了,这里就省略了,但唯独判断条件为等于时估计蛮多人遇到坑了, ...
- ACM题目————二叉树的遍历
一.二叉树的后序遍历: 题目描述 给定一颗二叉树,要求输出二叉树的深度以及后序遍历二叉树得到的序列.本题假设二叉树的结点数不超过1000 输入 输 入数据分为多组,第一行是测试数据的组数n,下面的n行 ...
- java 基本类型之间的转换
基本数据类型从低级到高级是:byte short int long float double ,char 类型比int 类型之后的都要低 下面通过一个例子说明: import javax.swing ...
- c#之线程池
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- UVa 10054,欧拉回路
题目链接:https://uva.onlinejudge.org/external/100/10054.pdf 题目链接:http://vjudge.net/contest/132239#proble ...