1. 以下是操作WMI的ActiveX的代码示例,实现的功能是列出浏览者机器上的磁盘驱动器。(当然这个控件以后还要实现浏览机器上所有文件的功能,但是该文中没有提供)
HelloWorld.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Management;
using System.Globalization;
using System.Runtime.InteropServices;
namespace K2ActiveX
{
/// <summary>
/// HelloWorld 的摘要说明。
/// </summary>
public class HelloWorld : System.Windows.Forms.UserControl
{
private System.Windows.Forms.TreeView tvFolders;
private System.Windows.Forms.ListView lvFiles;
private System.Windows.Forms.TextBox txtError;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public HelloWorld()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// Populate TreeView with Drive list
try
{
PopulateDriveList();
}
catch( System.Exception e )
{
txtError.Text = e.ToString();
}
}
Dispose( bool disposing ) // 清理所有正在使用的资源。
组件设计器生成的代码
所有的私有方法
}
}
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Management;
using System.Globalization;
using System.Runtime.InteropServices;
namespace K2ActiveX
{
/// <summary>
/// HelloWorld 的摘要说明。
/// </summary>
public class HelloWorld : System.Windows.Forms.UserControl
{
private System.Windows.Forms.TreeView tvFolders;
private System.Windows.Forms.ListView lvFiles;
private System.Windows.Forms.TextBox txtError;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public HelloWorld()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// Populate TreeView with Drive list
try
{
PopulateDriveList();
}
catch( System.Exception e )
{
txtError.Text = e.ToString();
}
}
Dispose( bool disposing ) // 清理所有正在使用的资源。
组件设计器生成的代码
所有的私有方法
}
}
HelloWorld.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<BODY>
<object id="helloworld" classid="http://localhost/K2ActiveX.dll#K2ActiveX.HelloWorld" Width="800" Height="600" VIEWASTEXT> </object>
</BODY>
</HTML>
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<BODY>
<object id="helloworld" classid="http://localhost/K2ActiveX.dll#K2ActiveX.HelloWorld" Width="800" Height="600" VIEWASTEXT> </object>
</BODY>
</HTML>
浏览HelloWorld.Html,出现如下的错误:
怎么回事哪?
后来参考了
http://www.cnblogs.com/homer/archive/2005/01/04/86473.html
中的方法,为ActiveX控件加上GUID,运行结果就正常了,效果如下:
可是又出现了一个新问题,那就是当我在局域网中的其他机器上访问这个页面的时候,竟然无法显示:
晕,到底是什么原因阿
多亏csdn上兄弟的帮助:
Internet选项-->安全-->受信任的站点-->站点-->http://192.168.0.88
然后:
自定义级别-->对没有标记为安全的ActiveX控件进行初始化和脚本运行(启用)
下载未签名的ActiveX控件(启用)
这个问题算是解决了。在局域网可以正常显示了。
但是又出现了一个新问题:那就是当我把dll文件和html文件都拷贝到虚拟主机上以后,浏览html文件的时候是个红叉,设置了IE安全性也不行,
看起来光拷贝dll文件和html文件不行,虚拟主机上安装了.net框架,我注册dll文件又提示找不到程序的入口点,那么到底还需要做什么才能保证这个ActiveX控件在新的虚拟主机上也能正常工作哪?