惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
The GitHub Blog
The GitHub Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
雷峰网
雷峰网
U
Unit 42
Y
Y Combinator Blog
I
InfoQ
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
量子位
Microsoft Security Blog
Microsoft Security Blog
B
Blog
The Cloudflare Blog
F
Fortinet All Blogs
Google DeepMind News
Google DeepMind News
MyScale Blog
MyScale Blog
C
Check Point Blog
S
SegmentFault 最新的问题
爱范儿
爱范儿
博客园 - 叶小钗
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
罗磊的独立博客
T
Tailwind CSS Blog

博客园 - bluealarm

IIS7中的站点、应用程序和虚拟目录详细介绍 (转) svn提交时强制添加注释 (转) 通过IIS调试ASP.NET项目 当前标识(IIS APPPOOL\DefaultWebSite)没有对“C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files“的写访问权限 - bluealarm (转)WPF控件开源资源 visual 继承当前被禁用,因为基类引用设备特定的组件或包含 p/invoke(转) RDLC导出到EXCEL错误 修改SQLServerExpress的登录模式 通过批处理文件(.bat)执行.sql文件 VS2008找不到导出模板 代码生成相关工具及技术 开源框架项目列表 C# byte数组常用扩展浅析(转) 已处理证书链,但是在不受信任提供程序信任的根证书中终止。 压缩数据库 清理SQL Server数据库日志的两种方法 SQL Server数据库文件恢复技术 WCF客户端链接服务超时--客户端close ASP.net中的几个概念 - bluealarm - 博客园
C#编写客户端AcitveX控件
bluealarm · 2011-07-15 · via 博客园 - bluealarm

  因为项目需要在客户端进行磁卡的读取,通过卡号进行系统的登录。所以需要在客户端通过串口进行读取。

参考的有:1、微软的MSCOMM32控件2、注册的dll控件。http://www.cnblogs.com/yilin/archive/2009/09/15/1567332.html

  下面是我采取的不注册dll的方法。

1、VS下新建一个dll工程。并修改工程的属性。

clip_image004_2.gif (390×379)

2、编写dll部分代码

[ClassInterface(ClassInterfaceType.None), ComSourceInterfaces(typeof(ISerialCtrlCOMEvents))]
public class SerialReadClass : System.Windows.Forms.UserControl, ISerialCtrlCOMIncoming
{

public delegate void DataReceivedHandler();
public event DataReceivedHandler DataRecived;

 private System.IO.Ports.SerialPort serialPort1;

public string Result;

public SerialReadClass()
{
InitializeComponent();

serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived);
OpenPort("Com1", "9600");

}

private void OpenPort(string ComName, string BaudRate)
{
serialPort1.BaudRate = Convert.ToInt32(BaudRate);
serialPort1.PortName = ComName;
try
{
if (!serialPort1.IsOpen)
{
serialPort1.Open();
}
}
catch (Exception ex)
{
//MessageBox.Show(ex.ToString());
}
}

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] inputData = new byte[serialPort1.BytesToRead];
serialPort1.Read(inputData, 0, inputData.Length);
readString = Encoding.Default.GetString(inputData);
this.Invoke(new ControlErrorHandler(ControlError));

}

/// <summary>
/// Source interface for hooking up to COM events so that JScript/VBScript can sink event
/// handlers with us. Disgusting, but it works.
/// </summary>
[Guid("B0026756-20BD-4dfe-9E92-88D69EEB2970")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ISerialCtrlCOMEvents
{
[DispId(0x60020000)]
void DataReceived();
}

/// <summary>
/// Default incoming interface for our control. Required when using COM-style events,
/// otherwise IE will no longer be able to access our public properties and methods.
/// </summary>
public interface ISerialCtrlCOMIncoming
{
string Result{get;}

}

3、web客户端部分

<object id="SerialRead" classid="SerialRead.dll#SerialRead.SerialReadClass" height="0" width="0">
</object>

<script for="SerialRead" event="ControlError">
alert(document.getElementById("SerialRead").Result);
    </script>

4、说明

dll部分要通过接口公开可以订阅的事件和属性,在串口类里实现。客户端可以调用事件和获取属性值。

可是在调用dll的公开方法时,却无法直接调用,总是提示未定义该方法。