作为IActiveScriptProfilerControl.StartProfiling的第一个参数传递什么?

时间:2015-12-07 12:14:58

标签: c# .net com-interop iactivescript

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

[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;

我还可以创建一个微软JQueryScriptEngine对象的实例并查询IActiveScriptProfilerControl

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

我的问题:

如何通过调用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))

1 个答案:

答案 0 :(得分:1)

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

我已经设法通过monitoring the registry解决了这个问题,无论是对我的程序集进行Regasm.exe调用,还是监视分析器在调用StartProfiling时请求的键。< / p>

这些密钥是HKEY_CLASSES_ROOT

所必需的
  

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”=“两个”   “类”= “ZetaProducer.SuperSlimScriptingEngineProfiler.SlimScriptEngineProfilerCallback”   “装配”=“ZetaProducer.SuperSlimScriptingEngineProfiler,   Version = 14.1.0.0,Culture = neutral,PublicKeyToken = null“   “RuntimeVersion”=“v4.0.30319”“CodeBase”=“file:/// C:/ P / Zeta   制片人/ 13 / Zeta制片人   主/滨/应用/ ZetaProducer.SuperSlimScriptingEngineProfiler.dll“

     

[HKEY_CLASSES_ROOT \ CLSID {1C406FBA-59EF-4FB2-938C-C1DA182D5914} \ InprocServer32的\ 14.1.0.0]   “类”= “ZetaProducer.SuperSlimScriptingEngineProfiler.SlimScriptEngineProfilerCallback”   “装配”=“ZetaProducer.SuperSlimScriptingEngineProfiler,   Version = 14.1.0.0,Culture = neutral,PublicKeyToken = null“   “RuntimeVersion”=“v4.0.30319”“CodeBase”=“file:/// C:/ P / Zeta   制片人/ 13 / Zeta制片人   主/滨/应用/ ZetaProducer.SuperSlimScriptingEngineProfiler.dll“

     

[HKEY_CLASSES_ROOT \ CLSID {1C406FBA-59EF-4FB2-938C-C1DA182D5914} \程序id]   @ = “ZetaProducer.SuperSlimScriptingEngineProfiler.SlimScriptEngineProfilerCallback”

     

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

对于JScript,

Another suggestion也要添加:

  

[HKEY_CURRENT_USER \环境]
  “JS_PROFILER”= “{1C406FBA-59EF-4FB2-938C-C1DA182D5914}”

由于我希望我的程序在没有管理权限的情况下运行,我创建了一个类来创建上面的密钥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))

经过一番尝试和错误,我发现这是因为我configured my Visual Studio to always run as an administrator

当从这个上下文开始时,某种程度似乎出错了。我还是不知道到底是什么。

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

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