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

推荐订阅源

aimingoo的专栏
aimingoo的专栏
S
Securelist
博客园 - Franky
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
IT之家
IT之家
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
The Cloudflare Blog
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
AI
AI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Schneier on Security
Schneier on Security
Attack and Defense Labs
Attack and Defense Labs
Vercel News
Vercel News
腾讯CDC
Google DeepMind News
Google DeepMind News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
M
MIT News - Artificial intelligence
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
Netflix TechBlog - Medium
量子位
S
Schneier on Security
Hacker News: Ask HN
Hacker News: Ask HN
Cyberwarzone
Cyberwarzone
S
Security Affairs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
N
News and Events Feed by Topic
T
Tenable Blog
PCI Perspectives
PCI Perspectives
MyScale Blog
MyScale Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cyber Attacks, Cyber Crime and Cyber Security
W
WeLiveSecurity
N
News | PayPal Newsroom
P
Proofpoint News Feed
O
OpenAI News
C
CERT Recently Published Vulnerability Notes
B
Blog
Cisco Talos Blog
Cisco Talos Blog
Microsoft Security Blog
Microsoft Security Blog
V
Visual Studio Blog
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Y
Y Combinator Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Spread Privacy
Spread Privacy

博客园 - 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的公开方法时,却无法直接调用,总是提示未定义该方法。