using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Security;
using System.Security.Permissions;
using System.Collections;
using System.Windows.Forms;
using System.Reflection;
using System.Runtime.CompilerServices;
[assembly: AllowPartiallyTrustedCallers]
namespace Whatever
{
[
Guid("AD3EE0B2-4C9F-441B-8E6F-0D1223354EBD"),
InterfaceType(ComInterfaceType.InterfaceIsDual),
ComVisible(true)
]
public interface ISetup
{
[DispId(1)]
string Hello();
[DispId(2)]
int ShowDialog(string msg);
};
[
Guid("81F44339-C03D-444F-95AD-3E86CF8FA514"),
ProgId("Whatever.CSetup"),
ClassInterface(ClassInterfaceType.None),
ComDefaultInterface(typeof(ISetup)),
ComVisible(true)
]
public class CSetup : ISetup
{
#region [ISetup implementation]
[ZoneIdentityPermission(SecurityAction.Demand, Zone = SecurityZone.Intranet)]
public string Hello()
{
MessageBox.Show("Hello world.");
return "Hello from CSetup object";
}
public int ShowDialog(string msg)
{
System.Windows.Forms.MessageBox.Show(msg, "");
return 0;
}
#endregion
};
}
当我使用regasm /codebase /tlb /verbose Whatever.dll
时,我得到了这样的反馈:
Microsoft (R) .NET Framework Assembly Registration Utility 4.0.30319.1
Copyright (C) Microsoft Corporation 1998-2004. All rights reserved.
Types registered successfully
Type 'N' exported.
Type 'N' exported.
Assembly exported to 'C:\Users\Whoever\Whatever.tlb', and the type lib
rary was registered successfully
除了不知道Type'N'导出的含义之外,哪个很好,但程序集确实在HKEY_LOCAL_MACHINE \ SOFTWARE \ Classes \中显示为“Whatever.CSetup”
如果我启动浏览器到包含JavaScript(下面转载)的网站,该网站尚未识别出有问题调用的.NET程序集方法,我可以看到,在MSIE菜单工具!Internet选项!程序!管理添加-ons!工具栏和扩展,“Whatever.CSetup”与“名称”列中的其他几个加载项一起显示,“状态”列将其报告为“已启用”。 (虽然它的“加载时间”和“导航时间”列没有显示任何内容 - 但是“发送到蓝牙设备”或其他一些设备都没有。)
JavaScript:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body onload="OpenActiveX()">
<!-- Our activeX object -->
<OBJECT id="Whatever.CSetup" name=”Whatever.CSetup" classid="clsid:81F44339-C03D-444F-95AD-3E86CF8FA514" VIEWASTEXT codebase="Whatever.CSetup"></OBJECT>
<!-- Attaching to an ActiveX event-->
<script language="javascript">
function OurActiveX::OnClose(redirectionUrl)
{
alert(redirectionUrl); <!-- http://otherwebsite.com should be returned-->
//window.location = redirectionUrl;
}
</script>
<script language="javascript">
//Passing parameters to ActiveX object and starting application
function OpenActiveX()
{
try
{
// document.OurActiveX.MyParam = "Hi I am here." //Passing parameter to the ActiveX
document.Whatever.CSetup.Open(); //Running method from activeX
}
catch(Err)
{
alert(Err.description);
}
}
</script>
</body>
</html>
当我进入页面时,我得到“无法获取属性Whatever.CSetup'的值:对象为null或未定义”
我知道这一点:如果我通过更改JavaScript中的CLSID进行测试,那么转到该页面根本不会触发任何内容。如果我将其更改回来,我会收到该错误消息。
我无法确定我在<OBJECT id="Whatever.CSetup" name=”Whatever.CSetup" classid="clsid:81F44339-C03D-444F-95AD-3E86CF8FA514" VIEWASTEXT codebase="Whatever.CSetup"></OBJECT>
中提供的内容,除了冒险我在CLSID字段中拥有正确的值。还有其他的,“id”或“name”或“codebase”,我不知道。
我不知道我是否正确地在js底部附近调用汇编类方法。
类似于此的JavaScript被认为能够调用.NET汇编方法。有三个网站确认这是可能的:
http://www.dreamincode.net/forums/topic/38890-activex-with-c# http://www.codeproject.com/Articles/24089/Create-ActiveX-in-NET-Step-by-Step http://www.codeproject.com/Articles/1265/COM-IDs-Registry-keys-in-a-nutshell
感谢您的帮助。