0

尝试IActiveScriptProfilerControl::StartProfiling从我的 C# 代码中使用,我创建了这个接口定义:

[ComImport]
[Guid(@"784b5ff0-69b0-47d1-a7dc-2518f4230e90")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IActiveScriptProfilerControl
{
    void StartProfiling(
        ref Guid clsidProfilerObject, 
        ProfilerEventMask eventMask, 
        uint dwContext);

    // ...
}

我相信它被正确地翻译成.NET。

原来的第一个参数定义为

[in] REFCLSID clsidProfilerObject

有了这些定义:

typedef GUID CLSID;
typedef CLSID *REFCLSID;

我还能够创建 MicrosoftJQueryScriptEngine对象的实例并查询IActiveScriptProfilerControl.

我目前失败的是如何告诉StartProfiling函数使用我的IActiveScriptProfilerCallback-derived 对象。

我的问题:

如何通过调用将我自己的分析器回调类与活动脚本分析器控制接口连接起来IActiveScriptProfilerControl::StartProfiling

理想情况下,我很乐意在不需要RegAsm的班级的情况下做到这一点。

更新1:

我已将接口定义的第一个参数更改为:

[ComImport]
[Guid(@"784b5ff0-69b0-47d1-a7dc-2518f4230e90")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IActiveScriptProfilerControl
{
    void StartProfiling(
        IActiveScriptProfilerCallback clsidProfilerObject, // <-- changed.
        ProfilerEventMask eventMask, 
        uint dwContext);

    // ...
}

并尝试通过传递我的IActiveScriptProfilerCallback派生类的实例来调用此函数。

不过,我得到了错误:

类未注册(HRESULT 异常:0x80040154 (REGDB_E_CLASSNOTREG))

4

1 回答 1

1

(在搜索类似错误时来自我未来的自我的更新)

我已经设法通过监视注册表来解决这个问题,无论是在对我的程序集执行Regasm.exe调用时,还是在监视分析器在 valling 时请求什么键时StartProfiling

这些密钥是必需的HKEY_CLASSES_ROOT

REGEDIT4

[HKEY_CLASSES_ROOT\ZetaProducer.SuperSlimScriptingEngineProfiler.SlimScriptEngineProfilerCallback] @="ZetaProducer.SuperSlimScriptingEngineProfiler.SlimScriptEngineProfilerCallback"

[HKEY_CLASSES_ROOT\ZetaProducer.SuperSlimScriptingEngineProfiler.SlimScriptEngineProfilerCallback\CLSID] @="{1C4​​06FBA-59EF-4FB2-938C-C1DA182D5914}"

[HKEY_CLASSES_ROOT\CLSID{1C406FBA-59EF-4FB2-938C-C1DA182D5914}] @="ZetaProducer.SuperSlimScriptingEngineProfiler.SlimScriptEngineProfilerCallback"

[HKEY_CLASSES_ROOT\CLSID{1C406FBA-59EF-4FB2-938C-C1DA182D5914}\InprocServer32] @="mscoree.dll" "ThreadingModel"="Both" "Class"="ZetaProducer.SuperSlimScriptingEngineProfiler.SlimScriptEngineProfilerCallback" "Assembly"="ZetaProducer. SuperSlimScriptingEngineProfiler,版本=14.1.0.0,Culture=neutral,PublicKeyToken=null" "RuntimeVersion"="v4.0.30319" "CodeBase"="file:///C:/P/Zeta Producer/13/Zeta Producer Main/Bin /Applications/ZetaProducer.SuperSlimScriptingEngineProfiler.dll"

[HKEY_CLASSES_ROOT\CLSID{1C406FBA-59EF-4FB2-938C-C1DA182D5914}\InprocServer32\14.1.0.0] "Class"="ZetaProducer.SuperSlimScriptingEngineProfiler.SlimScriptEngineProfilerCallback" "Assembly"="ZetaProducer.SuperSlimScriptingEngineProfiler, 版本=14.1.0.0,中立,PublicKeyToken=null" "RuntimeVersion"="v4.0.30319" "CodeBase"="file:///C:/P/Zeta Producer/13/Zeta Producer Main/Bin/Applications/ZetaProducer.SuperSlimScriptingEngineProfiler.dll"

[HKEY_CLASSES_ROOT\CLSID{1C406FBA-59EF-4FB2-938C-C1DA182D5914}\ProgId] @="ZetaProducer.SuperSlimScriptingEngineProfiler.SlimScriptEngineProfilerCallback"

[HKEY_CLASSES_ROOT\CLSID{1C406FBA-59EF-4FB2-938C-C1DA182D5914}\实现的类别{62C8FE65-4EBB-45E7-B440-6E39B2CDBF29}]

JScript 的另一个建议是还添加:

[HKEY_CURRENT_USER\Environment]
"JS_PROFILER"="{1C4​​06FBA-59EF-4FB2-938C-C1DA182D5914}"

因为我希望我的程序在没有管理权限的情况下运行,所以我创建了一个类来创建上述键 unter HKEY_CURRENT_USER,这也可以。

以下是完整的类文件供参考:

namespace ZetaProducer.SuperSlimScriptingEngineProfiler.Helper
{
    using Microsoft.Win32;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Reflection;
    using System.Security.AccessControl;
    using System.Security.Principal;
    using Zeta.VoyagerLibrary.Common.IO;

    public static class ProfilerComRegistration
    {
        public const string ClsidString = @"1C406FBA-59EF-4FB2-938C-C1DA182D5914";
        public static readonly Guid Clsid = new Guid(ClsidString);

        /*
        REGEDIT4

        [HKEY_CLASSES_ROOT\ZetaProducer.SuperSlimScriptingEngineProfiler.SlimScriptEngineProfilerCallback]
        @="ZetaProducer.SuperSlimScriptingEngineProfiler.SlimScriptEngineProfilerCallback"

        [HKEY_CLASSES_ROOT\ZetaProducer.SuperSlimScriptingEngineProfiler.SlimScriptEngineProfilerCallback\CLSID]
        @="{1C406FBA-59EF-4FB2-938C-C1DA182D5914}"

        [HKEY_CLASSES_ROOT\CLSID\{1C406FBA-59EF-4FB2-938C-C1DA182D5914}]
        @="ZetaProducer.SuperSlimScriptingEngineProfiler.SlimScriptEngineProfilerCallback"

        [HKEY_CLASSES_ROOT\CLSID\{1C406FBA-59EF-4FB2-938C-C1DA182D5914}\InprocServer32]
        @="mscoree.dll"
        "ThreadingModel"="Both"
        "Class"="ZetaProducer.SuperSlimScriptingEngineProfiler.SlimScriptEngineProfilerCallback"
        "Assembly"="ZetaProducer.SuperSlimScriptingEngineProfiler, Version=15.0.0.0, Culture=neutral, PublicKeyToken=null"
        "RuntimeVersion"="v4.0.30319"
        "CodeBase"="file:///C:/P/Zeta Producer/13/Zeta Producer Main/Bin/Applications/ZetaProducer.SuperSlimScriptingEngineProfiler.dll"

        [HKEY_CLASSES_ROOT\CLSID\{1C406FBA-59EF-4FB2-938C-C1DA182D5914}\InprocServer32\15.0.0.0]
        "Class"="ZetaProducer.SuperSlimScriptingEngineProfiler.SlimScriptEngineProfilerCallback"
        "Assembly"="ZetaProducer.SuperSlimScriptingEngineProfiler, Version=15.0.0.0, Culture=neutral, PublicKeyToken=null"
        "RuntimeVersion"="v4.0.30319"
        "CodeBase"="file:///C:/P/Zeta Producer/13/Zeta Producer Main/Bin/Applications/ZetaProducer.SuperSlimScriptingEngineProfiler.dll"

        [HKEY_CLASSES_ROOT\CLSID\{1C406FBA-59EF-4FB2-938C-C1DA182D5914}\ProgId]
        @="ZetaProducer.SuperSlimScriptingEngineProfiler.SlimScriptEngineProfilerCallback"

        [HKEY_CLASSES_ROOT\CLSID\{1C406FBA-59EF-4FB2-938C-C1DA182D5914}\Implemented Categories\{62C8FE65-4EBB-45E7-B440-6E39B2CDBF29}]
        */

        /*
        Siehe http://www.hexacorn.com/blog/2014/04/27/beyond-good-ol-run-key-part-11/:

        [HKEY_CURRENT_USER\Environment]
        "JS_PROFILER"="{1C406FBA-59EF-4FB2-938C-C1DA182D5914}"
         */

        public static void Register()
        {
            // Achtung vor dem Wow6432Node-Schlüssel.
            // https://stackoverflow.com/questions/2039186/reading-the-registry-and-wow6432node-key
            var views = new[]
            {
                RegistryView.Registry32,
                RegistryView.Registry64
            };

            foreach (var registryView in views)
            {
                var key = checkCreateKey(
                    registryView,
                    @"Software\Classes\ZetaProducer.SuperSlimScriptingEngineProfiler.SlimScriptEngineProfilerCallback");
                if (key != null)
                {
                    key.SetValue(null,
                        @"ZetaProducer.SuperSlimScriptingEngineProfiler.SlimScriptEngineProfilerCallback",
                        RegistryValueKind.String);
                    key.Close();
                }

                key = checkCreateKey(
                    registryView,
                    @"Software\Classes\ZetaProducer.SuperSlimScriptingEngineProfiler.SlimScriptEngineProfilerCallback\CLSID");
                if (key != null)
                {
                    key.SetValue(null, $@"{{{ClsidString}}}", RegistryValueKind.String);
                    key.Close();
                }

                key = checkCreateKey(
                    registryView,
                    $@"Software\Classes\CLSID\{{{ClsidString}}}");
                if (key != null)
                {
                    key.SetValue(null,
                        @"ZetaProducer.SuperSlimScriptingEngineProfiler.SlimScriptEngineProfilerCallback",
                        RegistryValueKind.String);
                    key.Close();
                }

                key = checkCreateKey(
                    registryView,
                    $@"Software\Classes\CLSID\{{{ClsidString}}}\InprocServer32");
                if (key != null)
                {
                    key.SetValue(null, @"mscoree.dll", RegistryValueKind.String);
                    key.SetValue(@"ThreadingModel", @"Both", RegistryValueKind.String);
                    key.SetValue(@"Class",
                        @"ZetaProducer.SuperSlimScriptingEngineProfiler.SlimScriptEngineProfilerCallback",
                        RegistryValueKind.String);
                    key.SetValue(@"Assembly",
                        @"ZetaProducer.SuperSlimScriptingEngineProfiler, Version=15.0.0.0, Culture=neutral, PublicKeyToken=null",
                        RegistryValueKind.String);
                    key.SetValue(@"RuntimeVersion", @"v4.0.30319", RegistryValueKind.String);
                    key.SetValue(@"CodeBase", getFileUrl(), RegistryValueKind.String);
                    key.Close();
                }

                key = checkCreateKey(
                    registryView,
                    $@"Software\Classes\CLSID\{{{ClsidString}}}\InprocServer32\15.0.0.0");
                if (key != null)
                {
                    key.SetValue(@"Class",
                        @"ZetaProducer.SuperSlimScriptingEngineProfiler.SlimScriptEngineProfilerCallback",
                        RegistryValueKind.String);
                    key.SetValue(@"Assembly",
                        @"ZetaProducer.SuperSlimScriptingEngineProfiler, Version=15.0.0.0, Culture=neutral, PublicKeyToken=null",
                        RegistryValueKind.String);
                    key.SetValue(@"RuntimeVersion", @"v4.0.30319", RegistryValueKind.String);
                    key.SetValue(@"CodeBase", getFileUrl(), RegistryValueKind.String);
                    key.Close();
                }

                key = checkCreateKey(
                    registryView,
                    $@"Software\Classes\CLSID\{{{ClsidString}}}\ProgId");
                if (key != null)
                {
                    key.SetValue(null,
                        @"ZetaProducer.SuperSlimScriptingEngineProfiler.SlimScriptEngineProfilerCallback",
                        RegistryValueKind.String);
                    key.Close();
                }

                // Aus der Dokumentation https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/scripting-articles/cc843609(v=vs.94):
                //
                // The JavaScript language runtime checks the JS_PROFILER environment variable on creation
                // to determine whether profiling should be enabled. If this variable is set to the CLSID
                // of the profiler, the language runtime creates an instance of the profiler COM object,
                // using the value of the variable to determine which profiler to create.

                // Ein Beispiel habe ich hier gefunden:
                // http://www.hexacorn.com/blog/2014/04/27/beyond-good-ol-run-key-part-11/

                key = checkCreateKey(registryView, @"Environment");
                if (key != null)
                {
                    key.SetValue(@"JS_PROFILER", $@"{{{ClsidString}}}", RegistryValueKind.String);
                    key.Close();
                }
            }
        }

        private static string getFileUrl()
        {
            // ReSharper disable once AssignNullToNotNullAttribute
            var filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location),
                @"ZetaProducer.SuperSlimScriptingEngineProfiler.dll");
            return PathHelper.ConvertFilePathToFileUrl(filePath);
        }

        public static void Unregister()
        {
            // Achtung vor dem Wow6432Node-Schlüssel.
            // https://stackoverflow.com/questions/2039186/reading-the-registry-and-wow6432node-key
            var views = new[]
            {
                RegistryView.Registry32,
                RegistryView.Registry64
            };

            foreach (var registryView in views)
            {
                var baseKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, registryView);

                var key = baseKey.OpenSubKey(@"Software\Classes", true);

                if (key != null &&
                    new List<string>(key.GetSubKeyNames()).Contains(
                        @"ZetaProducer.SuperSlimScriptingEngineProfiler.SlimScriptEngineProfilerCallback"))
                {
                    try
                    {
                        key.DeleteSubKeyTree(
                            @"ZetaProducer.SuperSlimScriptingEngineProfiler.SlimScriptEngineProfilerCallback", false);
                    }
                    catch (AccessViolationException)
                    {
                    }

                    key.Close();
                }

                key = baseKey.OpenSubKey(@"Software\Classes\CLSID", true);

                if (key != null &&
                    new List<string>(key.GetSubKeyNames()).Contains($@"{{{ClsidString}}}"))
                {
                    try
                    {
                        key.DeleteSubKeyTree($@"{{{ClsidString}}}", false);
                    }
                    catch (AccessViolationException)
                    {
                    }

                    key.Close();
                }

                key = baseKey.OpenSubKey(@"Environment", true);
                if (key != null)
                {
                    key.DeleteValue(@"JS_PROFILER", false);
                    key.Close();
                }
            }
        }

        private static RegistryKey checkCreateKey(RegistryView registryView, string keyPath)
        {
            // Achtung vor dem Wow6432Node-Schlüssel.
            // https://stackoverflow.com/questions/2039186/reading-the-registry-and-wow6432node-key

            var rs = new RegistrySecurity();

            // Jeder.
            var user = new SecurityIdentifier(WellKnownSidType.WorldSid, null);

            rs.AddAccessRule(
                new RegistryAccessRule(
                    user,
                    RegistryRights.FullControl,
                    InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                    PropagationFlags.None,
                    AccessControlType.Allow));

            var key = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, registryView)
                .CreateSubKey(
                    keyPath,
                    RegistryKeyPermissionCheck.ReadWriteSubTree,
                    rs);

            return key;
        }
    }
}

2018 年 7 月更新

直接从 Visual Studio 2017 中运行时,我的开发机器上再次出现初始错误:

类未注册(HRESULT 异常:0x80040154 (REGDB_E_CLASSNOTREG))

经过一些尝试和错误,我发现这是因为我将 Visual Studio 配置为始终以管理员身份运行

从这种情况下开始时,不知何故似乎出了点问题。我仍然不知道具体是什么。

解决方案是直接从 Windows 文件资源管理器而不是从 Visual Studio 中启动我的可执行文件。

另一种可能的解决方案(我没有尝试过)应该是不要以管理员身份运行 Visual Studio。

于 2017-07-28T11:49:58.750 回答