我写了一个简单的activeX控件,在ie中如何启动?
下面就是我新建的一个activeX控件,form上有一个按钮和一个textBox,按下按钮执行textBox中指定的程序。编译后生成一个dll文件。但我如何用IE启动这个dll文件???非得把他放到IIS下面吗?我放到本地不可以直接调用吗?using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;namespace WindowsControlLibrary1
{
/// <summary>
/// UserControl1 的摘要说明。
/// </summary>
public class UserControl1 : System.Windows.Forms.UserControl
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public UserControl1()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// TODO: 在 InitComponent 调用后添加任何初始化
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(248, 168);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 20);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 16);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(296, 20);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// UserControl1
//
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "UserControl1";
this.Size = new System.Drawing.Size(336, 208);
this.ResumeLayout(false);
}
#endregion private void button1_Click(object sender, System.EventArgs e)
{
System.Diagnostics.Process.Start(textBox1.Text);
}
}
}
下面就是我新建的一个activeX控件,form上有一个按钮和一个textBox,按下按钮执行textBox中指定的程序。编译后生成一个dll文件。但我如何用IE启动这个dll文件???非得把他放到IIS下面吗?我放到本地不可以直接调用吗?using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;namespace WindowsControlLibrary1
{
/// <summary>
/// UserControl1 的摘要说明。
/// </summary>
public class UserControl1 : System.Windows.Forms.UserControl
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public UserControl1()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// TODO: 在 InitComponent 调用后添加任何初始化
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(248, 168);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 20);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 16);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(296, 20);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// UserControl1
//
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "UserControl1";
this.Size = new System.Drawing.Size(336, 208);
this.ResumeLayout(false);
}
#endregion private void button1_Click(object sender, System.EventArgs e)
{
System.Diagnostics.Process.Start(textBox1.Text);
}
}
}
解决方案 »
- 让更多的人知道如何用C#操作斑马条码打印机
- 关于数据插入或删除后,更新排列序号的问题
- 请教一个数据库报表问题
- 请问对象和函数 ,对象和过程的区别
- 帮我看看错在那里啊!!
- 怎么把别人发到我邮箱里的邮件自动转到系统的数据库里
- 在DataGrid里绑定CheckBox后,点CheckBox不起作用是怎么回事呀?
- 定义了一个指定长度的数组(一维),但在程序运行时要改变数组的长度???该如何实现???
- 有关AxWebBrowser控件的Navigate参数在C#中如何表达
- C# sql ConnectionString没有初始化
- 谁能帮我解释一下这里delegate的作用以及执行过程??最好具体一些!谢谢……
- Winform下的容器的问题
还在生成,chassID才能调到用,
至于如何把exe也嵌入IE,基本上做不到
IE启动Excel,实际上是Excel的一个COM,并不是可执行程序
这样相当于自动发布不过这个不是真正的ActiveX
C#里面要把程序集包装成COM,然后象其他COM一样使用<object classid="some guid" ...到google搜索C# ActiveX
一大堆例子注意两种情况都需要客户机安装了.NET Framework
但是得到客户机部署这个ActiceX
(或者是,IE会问你要不要下载这个ActiceX等)
ActiveX是从OLE技术发展过来的,它的技术轨迹应该是:剪贴板->OLE->ActiveX
ActiveX包括两个部分,ActiveX容器和ActiveX控件还有一种应该被称为ActiveX对象或者文档,其实我也不知道该叫什么。ActiveX控件/对象只能在ActiveX容器中运行。不得不说这是一个非常棒非常好玩的技术。常见的ActiveX容器有Word和IE。并且,当ActiveX对象在ActiveX容器中运行时,可以改变ActiveX容器的环境,也就是可以把整个ActiveX容器的菜单和工具栏都给一股脑的换掉,除了不太稳定,这真的是一个非常激动人心的功能。因为ActiveX改变容器环境的功能复杂而不稳定,所以现在只能在Office和IE中看到这种效果:(。鄙人一直认为ActiveX有着非常广阔的发展前景,而不仅仅局限于在一个小框框里显示Flash动画我知道率人希望实现的效果,就是像在IE中打开Excel或者Word文档一样大家试着把xls文件拖到IE的窗口中看看会发生什么,如果顺利的话,你会发现IE不折不扣的变成了Excel,可能很多人都还没有试过的吧
using System;namespace activex_html
{
public class Class1
{
public string mod(object o) { //此例为方法
return System.Environment.OSVersion.ToString();
}
}
}编译成activex_html.dll,放到WEB 目录(与下面网页同等目录)下写一个网页,内容如下:<object id="myactivex" classid="http:activex_html.dll#activex_html.Class1"></object><script>
function c(obj){
document.all['t'].innerHTML = document.all['myactivex'].mod();
}
</script><body onload="c(this)">
<span id="t">?1</span>
</body>浏览一下就知道了要说明的是,.net 上的activex 是基于.net framework管理的,而不是IE 的安全或者系统的策略来管理,所以默认它有很高的安全限制,比如:return System.Environment.UserName;这就不被允许,而.net 要求你必须对它进行安全和权限配置才允许你执行和访问受限的资源。上面的问题是指外网访问,不知道的朋友注意一下:用机器名和网卡IP 访问是不同的,因为机器名访问是属于本地internet(同时也会自动使用windows 认证) 而ip(不是127.0.0.1 而是网卡或外网IP)访问就等于是外网访问上面这种做法是以.net 写的DLL 在客户端RUN,所以也是一样要有.net framework 而server 端不需要至于传统的就是写activex 或 ctrl dll,然后regsvr32 到注册表生成一个clsid,抄下这个ID 放在HTML 里的object classid="clsid=xxxx" 这就属于传统的做法,会自动下载它指定的CAB 包并安装,安装后就算是本地LIB 基本上不受限也就是说.net framework 将过去的activex 重新进行了定义,上升到了分布式应用程序的等级,所以也只要指定它所能获得的权限就可以允许在任何地方运行。(控制面板-管理工具-.net framework 1.x 配置)
Option Explicit
Private Const NCBASTAT = &H33
Private Const NCBNAMSZ = 16
Private Const HEAP_ZERO_MEMORY = &H8
Private Const HEAP_GENERATE_EXCEPTIONS = &H4
Private Const NCBRESET = &H32
Private Type NCB
ncb_command As Byte 'Integer
ncb_retcode As Byte 'Integer
ncb_lsn As Byte 'Integer
ncb_num As Byte ' Integer
ncb_buffer As Long 'String
ncb_length As Integer
ncb_callname As String * NCBNAMSZ
ncb_name As String * NCBNAMSZ
ncb_rto As Byte 'Integer
ncb_sto As Byte ' Integer
ncb_post As Long
ncb_lana_num As Byte 'Integer
ncb_cmd_cplt As Byte 'Integer
ncb_reserve(9) As Byte ' Reserved, must be 0
ncb_event As Long
End Type
Private Type ADAPTER_STATUS
adapter_address(5) As Byte 'As String * 6
rev_major As Byte 'Integer
reserved0 As Byte 'Integer
adapter_type As Byte 'Integer
rev_minor As Byte 'Integer
duration As Integer
frmr_recv As Integer
frmr_xmit As Integer
iframe_recv_err As Integer
xmit_aborts As Integer
xmit_success As Long
recv_success As Long
iframe_xmit_err As Integer
recv_buff_unavail As Integer
t1_timeouts As Integer
ti_timeouts As Integer
Reserved1 As Long
free_ncbs As Integer
max_cfg_ncbs As Integer
max_ncbs As Integer
xmit_buf_unavail As Integer
max_dgram_size As Integer
pending_sess As Integer
max_cfg_sess As Integer
max_sess As Integer
max_sess_pkt_size As Integer
name_count As Integer
End Type
Private Type NAME_BUFFER
name As String * NCBNAMSZ
name_num As Integer
name_flags As Integer
End Type
Private Type ASTAT
adapt As ADAPTER_STATUS
NameBuff(30) As NAME_BUFFER
End Type
Private Declare Function Netbios Lib "netapi32.dll" (pncb As NCB) As Byte
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
Private Declare Function GetProcessHeap Lib "kernel32" () As Long
Private Declare Function HeapAlloc Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function HeapFree Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, lpMem As Any) As Long
Function getMacPath()
Dim myNcb As NCB
Dim bRet As Byte
myNcb.ncb_command = NCBRESET
bRet = Netbios(myNcb)
myNcb.ncb_command = NCBASTAT
myNcb.ncb_lana_num = 0
myNcb.ncb_callname = "* "
Dim myASTAT As ASTAT, tempASTAT As ASTAT
Dim pASTAT As Long
myNcb.ncb_length = Len(myASTAT)
Debug.Print Err.LastDllError
pASTAT = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS Or HEAP_ZERO_MEMORY, myNcb.ncb_length)
If pASTAT = 0 Then
Debug.Print "memory allcoation failed!"
Exit Function
End If
myNcb.ncb_buffer = pASTAT
bRet = Netbios(myNcb)
Debug.Print Err.LastDllError
CopyMemory myASTAT, myNcb.ncb_buffer, Len(myASTAT)
MsgBox Hex(myASTAT.adapt.adapter_address(0)) & " " & Hex(myASTAT.adapt.adapter_address(1)) _
& " " & Hex(myASTAT.adapt.adapter_address(2)) & " " _
& Hex(myASTAT.adapt.adapter_address(3)) _
& " " & Hex(myASTAT.adapt.adapter_address(4)) & " " _
& Hex(myASTAT.adapt.adapter_address(5))
HeapFree GetProcessHeap(), 0, pASTAT
End Function
这是在.net里设用这个ActiveX控件
<object classid="clsid:1EC95F68-3DFF-4180-B6DC-44A28DBE2840" id="factory" codebase="http://192.168.230.182/webTest/MacPath.cab#Version=1.0.0.0" style="display:none" viewastext></object>
<script language="javascript">
function a1()
{
var fso = new ActiveXObject("Project1.ObjMacPath");//Project1是VB的项目名.ObjMacPath是类名
fso.getMacPath();//类中反回客户端网卡的地址的方法
}
</script>还有生成的MacPath.dll文件必须将它打包成.cab文件放在虚拟目录下