【问题标题】:Visual Studio Macros on 64 bit fail with COM error64 位上的 Visual Studio 宏因 COM 错误而失败
【发布时间】:2009-12-04 00:26:13
【问题描述】:

我正在做一些 javascript 开发,并找到了一个很酷的宏来对我的代码进行区域划分(“在 Visual Studio 中使用 #region 指令和 JavaScript 文件”)。我在我的 32 位盒子上使用了这个,它第一次工作。 (Visual Studio 2008 SP1, Win7)

为了便于参考,宏是:

Option Strict Off
Option Explicit Off

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
Imports System.Collections

Public Module JsMacros

    Sub OutlineRegions()
        Dim selection As EnvDTE.TextSelection = DTE.ActiveDocument.Selection

        Const REGION_START As String = "//#region"
        Const REGION_END As String = "//#endregion"

        DTE.ExecuteCommand("Edit.StopOutlining")

        selection.SelectAll()
        Dim text As String = selection.Text
        selection.StartOfDocument(True)

        Dim startIndex As Integer
        Dim endIndex As Integer
        Dim lastIndex As Integer = 0
        Dim startRegions As Stack = New Stack()

        Do
            startIndex = text.IndexOf(REGION_START, lastIndex)
            endIndex = text.IndexOf(REGION_END, lastIndex)

            If startIndex = -1 AndAlso endIndex = -1 Then
                Exit Do
            End If

            If startIndex <> -1 AndAlso startIndex < endIndex Then
                startRegions.Push(startIndex)
                lastIndex = startIndex + 1
            Else
                ' Outline region ...
                selection.MoveToLineAndOffset(CalcLineNumber(text, CInt(startRegions.Pop())), text.Length)
                selection.MoveToLineAndOffset(CalcLineNumber(text, endIndex) + 1, 1, True)
                selection.OutlineSection()

                lastIndex = endIndex + 1
            End If
        Loop

        selection.StartOfDocument()
    End Sub

    Private Function CalcLineNumber(ByVal text As String, ByVal index As Integer)
        Dim lineNumber As Integer = 1
        Dim i As Integer = 0

        While i < index
            If text.Chars(i) = vbCr Then
                lineNumber += 1
                i += 1
            End If

            i += 1
        End While

        Return lineNumber
    End Function

End Module

然后我尝试在两台单独的 64 位机器 (Win7 x64) 上使用相同的宏,除了 64 位操作系统版本之外,它都无法工作。使用 Visual Studio 宏 IDE 单步执行它,它在 DTE.ExecuteCommand("Edit.StopOutlining") 行第一次失败并出现 COM 错误(错误 HRESULT E_FAIL 已从调用返回COM 组件)。

如果我尝试第二次运行它,我可以从宏编辑器运行它而没有问题,但不能从 Visual Studio 中使用宏资源管理器“运行宏”命令运行它。

我查看了以下文章,但没有发现任何有用的信息:

我错过了什么愚蠢的东西吗?

【问题讨论】:

  • 还发现代码窗口也必须聚焦。比如说,如果你让 VS 输出窗口处于焦点位置,那么宏将不起作用……这并不奇怪,但它会引发异常。

标签: visual-studio visual-studio-2008 64-bit macros


【解决方案1】:

只需换行

DTE.ExecuteCommand("Edit.StopOutlining")

在 try catch 块中并消除异常 - 我有一个脚本可以做到这一点,但我没有工作 - 在尝试了你的并将上面的行更改为

Try

  DTE.ExecuteCommand("Edit.StopOutlining")

Catch ex As Exception

 End Try

然后公开该方法,然后我使用工具、选项、键盘并将键盘组合 Ctrl M、Ctrl O 分配给宏 MyMacros.OutLineRegions,它就像梦想成真一样工作:)

使用 Windows 7 64 位,Visual Studio 2010..YMMV

【讨论】:

    【解决方案2】:

    我遇到了类似的问题,通过查看 _applicationObject 中的每个命令发现一个命令存在两次(在我的情况下是 Debug.SaveDumpAs) 一个可用,另一个不可用。 “不幸的是”,当我执行命令时,我总是检索到错误的。

    你必须检索你想要的命令的 guid 和 id,当然,你想要一个可用的。 这是 C# 中的示例

            string guid ="";
            int ID = 0;
            string NAMEOFCOMMAND = "Edit.StopOutLining";
    
            try
            {
                foreach (Command command in _applicationObject.Commands)
                {                    
                    if (command.Name == NAMEOFCOMMAND)
                    {
                        if(command.IsAvailable)
                        {
                            guid = command.Guid;
                            ID = command.ID;
                        }
                    }
                }
                //The ExecuteCommand method cannot be used to have the right guid and id. I believe though that there is a format you can use to use it anyway. In doubt: use the Raise command which is most likely the same with additional parameters which you can set to null if you don't need them
                ((DTE)_applicationObject).Commands.Raise(guid, ID, null, null);
    

    干杯! :)

    【讨论】:

      猜你喜欢
      • 2017-07-28
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 2011-05-11
      • 1970-01-01
      • 2011-01-27
      • 2013-01-23
      • 1970-01-01
      相关资源
      最近更新 更多