1
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

除了不知道“N”型导出的含义之外,这很好,但程序集确实在 HKEY_LOCAL_MACHINE\SOFTWARE\Classes\ 中显示为“Whatever.CSetup”

如果我将浏览器启动到包含 JavaScript(如下再现)的站点,该站点尚未识别出有问题调用的 .NET 程序集方法,我可以在 MSIE 菜单工具!Internet 选项!程序!管理加载项中看到!工具栏和扩展,“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>

当我转到该页面时,我得到“无法获取属性 What.CSetup' 的值:对象为空或未定义”

  1. 我知道这一点:如果我通过更改 JavaScript 中的 CLSID 进行测试,则转到该页面根本不会触发任何内容。如果我改回来,我会收到该错误消息。

  2. <OBJECT id="Whatever.CSetup" name=”Whatever.CSetup" classid="clsid:81F44339-C03D-444F-95AD-3E86CF8FA514" VIEWASTEXT codebase="Whatever.CSetup"></OBJECT>除了冒险我在 CLSID 字段中具有正确的值之外,我无法确定我向对方提供了什么。其他的,那些“id”或“name”或“codebase”,我不知道。

  3. 我不知道我是否在 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

感谢您的任何帮助。

4

0 回答 0