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:如果 ...
随机推荐
- linux,Mac下lu 一把
习惯Terminal没有不知道ls命令的(等同于DOS的dir),经常只是需要查看目录的内容大小,但ls -h显示的只是目录的本身大小,而且很多项内容 ls 在这方面的两个诟病出现了: 小诟1. 显示 ...
- 20145227《Java程序设计》课程总结
20145227<Java程序设计>课程总结 每周读书笔记链接汇总 20145227 <Java程序设计>第1周学习总结 20145227 <Java程序设计>第2 ...
- ArrayList集合的实现原理
一. ArrayList概述: ArrayList是基于数组实现的,是一个动态数组,其容量能自动增长,类似于C语言中的动态申请内存,动态增长内存. ArrayList不是线程安全的,只能用在单线程环境 ...
- [算法]判断一个数是不是2的N次方
如果一个数是2^n,说明这个二进制里面只有一个1.除了1. a = (10000)b a-1 = (01111)b a&(a-1) = 0. 如果一个数不是2^n, 说明它的二进制里含有多一 ...
- python 路径
atm---main.py import os ,sys print(__file__) print(os.path.abspath(__file__))#绝对路径 print(os.path.dir ...
- poj-3259-wormholes-spfa-判负环
题意:N个顶点, M条双向边, W条权值为负的单向边.求是否存在负环. 思路:首先你要懂bellman-ford或spfa..这是基础的spfa判断是否存在负环的题,存在负环的节点会重复入队(因为最短 ...
- 【转】Eclipse插件大全介绍及下载地址
转载地址:http://developer.51cto.com/art/200906/127169.htm 尚未一一验证. eclipse插件大全介绍,以及下载地址 Eclipse及其插件下载网址大全 ...
- Channel Allocation
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13231 Accepted: 6774 D ...
- windows下TCP服务器和客户端的实现
服务器 1.初始化 WSAStartup(..) 2.创建Socket s = Socket ( .. ) 3.绑定端口 ret = bind ( ... ) 4.监听 ret = ...
- Oracle数据库的启动与停止
oracle linux下开启与关闭 .启动ORACLE监听 首先要登录用户oracle:su - oracle oracle@localhost bin]$ lsnrctl --启动oracle监听 ...