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」として表示されます。

問題のある.NETアセンブリメソッドが呼び出されていることをまだ認識していないJavaScript(以下に再現)を含むサイトにブラウザを起動すると、MSIEメニューの[ツール]、[インターネットオプション]、[プログラム]、[アドオンの管理]で確認できます。ツールバーと拡張機能。「Whatever.CSetup」が他のいくつかのアドオンと一緒に「名前」列に表示され、「ステータス」列に「有効」と表示されます。(ロード時間とナビゲーション時間の列には何も表示されませんが、Bluetoothデバイスまたは他のいくつかに送信することはできません。)

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または未定義です」と表示されます。

  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アセンブリメソッドを呼び出すことができると言われています。それが可能であることを確認する3つのサイトがあります:

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