我已经挣扎了好几天试图得到一个简单的 活动X 尽管我在网上找到了几篇关于该主题的文章,但 DLL 的工作没有成功。

我怀疑我有几处编码错误,因为我对此并不熟悉,而且大多数有关该主题的文章都已过时。我在用 视觉工作室2008 并一直在使用 视窗软件开发工具包 v7.1 用于数字签名。

我想做的是将客户端 machineName 从环境类返回到网页(并最终返回到 Web 服务器)。

这是我的 C# 类:

using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.Win32;

namespace GetClientInfo101
{
    [Guid("121C3E0E-DC6E-45dc-952B-A6617F0FAA32")]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    [ComVisible(true)]
    public interface IGetClient
    {
        [DispId(1)]
        string GetClient();
    }
    [Guid("5085C918-0236-4828-BDFA-063FEE57C69B")]
    [ProgId("getClientInfoAx.CGetClient")]
    [ClassInterface(ClassInterfaceType.None)]
    [ComDefaultInterface(typeof(IGetClient))]
    [System.ComponentModel.ToolboxItem(true)]
    [ComVisible(true)]
    public class CGetClient : IGetClient
    {
        public string GetClient()
        {
           return Environment.MachineName;
        }
    }
}

这是我的装配:

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("GetClientInfo101")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("GetClientInfo101")]
[assembly: AssemblyCopyright("Copyright © N/A 2010")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components.  If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("1bf2a0bc-f9cb-4f68-8990-5caaf3ab525d")]

// Version information for an assembly consists of the following four values:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")

这是我的网站母版页上的对象标签和脚本。**笔记:我收到错误

Microsoft JScript 运行时错误:自动化服务器无法创建对象

关于“var”执行**:

<object id="getClientInfo" name="GetClientInfo101" classid="clsid:5085C918-0236-4828-BDFA-063FEE57C69B"
        codebase="GetClientInfo/GetClientInfoAX.cab#version=1,0,0,0">
</object>

<script type="text/javascript">
    function OpenActiveX()
    {
        var objGetClientInfo = new ActiveXObject("GetClientInfoAx.CGetClient");
        if(objGetClientInfo != null)
         {
             //For testing only
             alert(objGetClientInfo.GetClient());
         }
     }
</script>

然后我手动创建了一个 .inf 文件,如下所示:

[version]
    signature="$CHICAGO$"
    AdvancedINF=2.0
[Add.Code]
    GetClientInfo101.dll=GetClientInfo101.dll
    GetClientInfo101.inf=GetClientInfo101.inf
[GetClientInfo101.dll]
    file-win32-x86=thiscab
    clsid={5085C918-0236-4828-BDFA-063FEE57C69B}
    FileVersion=1,0,0,0
    RegisterServer=yes
[GetClientInfo101.inf]
    file=thiscab

然而,当我构建我的 .cab 文件我也得到了 .OSD 文件输出到 .cab 如下(我担心有一个 .inf 和一个 .OSD 文件在同一个 .cab 文件。这是正确的吗?会引起问题吗?我不知道如何告诉 Visual Studio 不要构建 .OSD 文件。

<?XML version="1.0" ENCODING='UTF-8'?>
<!DOCTYPE SOFTPKG SYSTEM "http://www.microsoft.com/standards/osd/osd.dtd">
<?XML::namespace href="http://www.microsoft.com/standards/osd/msicd.dtd" as="MSICD"?>
<SOFTPKG NAME="GetClientInfoAX" VERSION="1,0,0,0">
    <TITLE> GetClientInfoAX </TITLE>
    <MSICD::NATIVECODE>
        <CODE NAME="GetClientInfo101">
            <IMPLEMENTATION>
                <CODEBASE FILENAME="GetClientInfo101.dll">
                </CODEBASE>
            </IMPLEMENTATION>
        </CODE>
    </MSICD::NATIVECODE>
</SOFTPKG>

不管怎样,试图拼凑出所有细节都是一场噩梦。我将非常感谢解决此问题的任何帮助。

好的, 代码项目 文章 使用 ATL 的完整可编写脚本的 ActiveX Web 控件教程 是我用来作为签署指南的 .cab 文件。请参阅标题为“签署 cab 文件”的部分。

这是我使用的命令。请注意,我使用 Visual Studio 创建 cab 文件而不是 CABARC。另请注意,我仅使用测试证书:

makecert -sv "getclientinfocert.pvk" -n "CN=My Company" getclientinfocert.cer

cert2spc getclientinfocert.cer getclientinfocert.spc

pvk2pfx -pvk getclientinfocert.pvk -pi AOTC20467cert -spc getclientinfocert.spc -pfx getclientinfocert.pfx -po AOTC81396pass –f

signtool sign /f getclientinfocert.pfx /p AOTC81396pass /t http://timestamp.verisign.com/scripts/timstamp.dll /v GetClientInfo101.dll

signtool sign /f getclientinfocert.pfx /p AOTC81396pass /t http://timestamp.verisign.com/scripts/timstamp.dll /v GetClientInfoAX.cab

使用 certmgr 将“根代理”证书导出​​和导入到受信任的根证书:

signtool verify /v /a GetClientInfoAX.cab

请注意,我将 DLL 复制到了 Windows SDK 文件夹,对其进行了签名,然后将其复制回我的 cab 文件,然后我将其复制到我的 SDK 文件中,并签署了 cab 文件。最后,我复制了 cab 文件到我的网站项目。

笔记:请参阅第一个答案末尾的评论。我已经购买了签名证书 科摩多 安装后,即使证书状态为“正常”,我现在也会收到“未知发布者”错误。DLL 和 CAB 文件均已签名。

我相信我现在的问题是控件需要标记为脚本安全。最近发现一篇文章, 使用 C# 在 .NET 中创建 ActiveX 控件, ,这看起来很有帮助。

有帮助吗?

解决方案

ComVisible co 类中与版本无关的 progid 设置为

  [ProgId("getClientInfoAx.CGetClient")] 

而在 JScript 代码中,实例化代码如下

  var objGetClientInfo = new ActiveXObject("GetClientInfoAx.CGetClient");

请首先验证区分大小写并确认行为?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top